diff options
Diffstat (limited to 'model/database.go')
-rw-r--r-- | model/database.go | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/model/database.go b/model/database.go index 7021181..3d8a0c4 100644 --- a/model/database.go +++ b/model/database.go @@ -52,12 +52,23 @@ func InitDatabase() { func InjectTestRecipes() { recipes := RecipeTestData() + tx, err := NewTx() + if err != nil { + log.Fatalf("Failed to inject test recipes: %v\n", err) + } + for _, recipe := range recipes { - err := recipe.Create() + err = recipe.Create(tx) if err != nil { + Rollback(tx) log.Fatalf("Failed to inject test recipe: %v\n", err) } } + + err = tx.Commit() + if err != nil { + log.Fatalf("Failed to inject test recipe: %v\n", err) + } } func CloseDatabase() { @@ -69,7 +80,11 @@ func CloseDatabase() { } } -func rollback(tx *sql.Tx) { +func NewTx() (*sql.Tx, error) { + return db.Begin() +} + +func Rollback(tx *sql.Tx) { err := tx.Rollback() if err != nil { log.Printf("Failed to rollback transaction: %v\n", err) |