diff options
-rw-r--r-- | main.go | 2 | ||||
-rw-r--r-- | model/database.go | 21 |
2 files changed, 21 insertions, 2 deletions
@@ -35,7 +35,7 @@ func main() { model.ConnectDatabase(filepath.Join(storage.Path, "ceres.sqlite3")) defer model.DisconnectDatabase() - model.MigrateDatabase() + model.MigrateDatabase(version) model.InjectTestRecipes() server := NewServer(config.HttpAddress) diff --git a/model/database.go b/model/database.go index 16a6cea..cdded4a 100644 --- a/model/database.go +++ b/model/database.go @@ -78,10 +78,29 @@ func isDatabaseEmpty(tx *sql.Tx) bool { return number == 0 } -func MigrateDatabase() { +func setupMinimalDatabase(tx *sql.Tx, version string) error { + cmd := ` +CREATE TABLE metadata ( + key TEXT PRIMARY KEY, + value TEXT +); +INSERT INTO metadata + (key, value) +VALUES + ('version', ?); +` + _, err := tx.Exec(cmd, version) + return err +} + +func MigrateDatabase(version string) { err := Transaction(func(tx *sql.Tx) error { if isDatabaseEmpty(tx) { log.Println("Starting with empty database") + err := setupMinimalDatabase(tx, version) + if err != nil { + log.Fatalf("Failed to setup minimal database schema: %v", err) + } } query, err := GetSql(`migrate`) |