summaryrefslogtreecommitdiff
path: root/model
diff options
context:
space:
mode:
Diffstat (limited to 'model')
-rw-r--r--model/database.go38
-rw-r--r--model/recipe_test.go5
-rw-r--r--model/recipes_test.go5
3 files changed, 26 insertions, 22 deletions
diff --git a/model/database.go b/model/database.go
index 44792fe..8b66bb5 100644
--- a/model/database.go
+++ b/model/database.go
@@ -23,7 +23,7 @@ func GetSql(code string) (string, error) {
return string(query), nil
}
-func InitDatabase(path string) {
+func ConnectDatabase(path string) {
var err error
db, err = sql.Open("sqlite3", path)
@@ -35,22 +35,6 @@ func InitDatabase(path string) {
if err != nil {
log.Fatal(err)
}
-
- query, err := GetSql(`migrate`)
- if err != nil {
- log.Fatal(err)
- }
-
- err = Transaction(func(tx *sql.Tx) error {
- _, err := tx.Exec(query)
- if err != nil {
- return err
- }
- return nil
- })
- if err != nil {
- log.Fatalf("Migration failed: %v\n", err)
- }
}
func Transaction(f func(*sql.Tx) error) error {
@@ -70,6 +54,24 @@ func Transaction(f func(*sql.Tx) error) error {
return tx.Commit()
}
+func MigrateDatabase() {
+ query, err := GetSql(`migrate`)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ err = Transaction(func(tx *sql.Tx) error {
+ _, err := tx.Exec(query)
+ if err != nil {
+ return err
+ }
+ return nil
+ })
+ if err != nil {
+ log.Fatalf("Migration failed: %v\n", err)
+ }
+}
+
func InjectTestRecipes() {
Transaction(func(tx *sql.Tx) error {
recipes := RecipeTestData()
@@ -85,7 +87,7 @@ func InjectTestRecipes() {
})
}
-func CloseDatabase() {
+func DisconnectDatabase() {
var err error = db.Close()
if err != nil {
log.Printf("Failed to close database: %v\n", err)
diff --git a/model/recipe_test.go b/model/recipe_test.go
index 74ccfa2..37b43e0 100644
--- a/model/recipe_test.go
+++ b/model/recipe_test.go
@@ -23,8 +23,9 @@ func TestRecipeCrud(t *testing.T) {
storage.Create()
}
- InitDatabase(filepath.Join(storage.Path, "ceres.sqlite3"))
- defer CloseDatabase()
+ ConnectDatabase(filepath.Join(storage.Path, "ceres.sqlite3"))
+ defer DisconnectDatabase()
+ MigrateDatabase()
Transaction(func(tx *sql.Tx) error {
var original, readback, update, updated, deleted Recipe
diff --git a/model/recipes_test.go b/model/recipes_test.go
index 15b90b6..d17ed92 100644
--- a/model/recipes_test.go
+++ b/model/recipes_test.go
@@ -21,8 +21,9 @@ func TestRecipesRead(t *testing.T) {
storage.Create()
}
- InitDatabase(filepath.Join(storage.Path, "ceres.sqlite3"))
- defer CloseDatabase()
+ ConnectDatabase(filepath.Join(storage.Path, "ceres.sqlite3"))
+ defer DisconnectDatabase()
+ MigrateDatabase()
InjectTestRecipes()