From a2038b0ea35d1466c84e6e04a2e4597fc038815a Mon Sep 17 00:00:00 2001 From: xengineering Date: Thu, 9 May 2024 10:02:00 +0200 Subject: Restructure database-related functions --- main.go | 6 +++--- model/database.go | 38 ++++++++++++++++++++------------------ model/recipe_test.go | 5 +++-- model/recipes_test.go | 5 +++-- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/main.go b/main.go index 223a7df..6c123da 100644 --- a/main.go +++ b/main.go @@ -33,9 +33,9 @@ func main() { } log.Printf("Storage directory: %s\n", storage.Path) - model.InitDatabase(filepath.Join(storage.Path, "ceres.sqlite3")) - defer model.CloseDatabase() - + model.ConnectDatabase(filepath.Join(storage.Path, "ceres.sqlite3")) + defer model.DisconnectDatabase() + model.MigrateDatabase() model.InjectTestRecipes() server := NewServer(config.HttpAddress) 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() -- cgit v1.2.3-70-g09d2