summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-05-09 10:02:00 +0200
committerxengineering <me@xengineering.eu>2024-05-09 10:02:00 +0200
commita2038b0ea35d1466c84e6e04a2e4597fc038815a (patch)
treefe94c7992dc5f7efb2b8266a5974be2430980cbf
parent0ba1a7661a81200db98e40149eef1e39fd22f407 (diff)
downloadceres-a2038b0ea35d1466c84e6e04a2e4597fc038815a.tar
ceres-a2038b0ea35d1466c84e6e04a2e4597fc038815a.tar.zst
ceres-a2038b0ea35d1466c84e6e04a2e4597fc038815a.zip
Restructure database-related functions
-rw-r--r--main.go6
-rw-r--r--model/database.go38
-rw-r--r--model/recipe_test.go5
-rw-r--r--model/recipes_test.go5
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()