summaryrefslogtreecommitdiff
path: root/model/database.go
diff options
context:
space:
mode:
Diffstat (limited to 'model/database.go')
-rw-r--r--model/database.go38
1 files changed, 20 insertions, 18 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)