From 771c7acf5faa94bbc7e294260ff77b04b2a537b9 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Mon, 15 Apr 2019 13:48:19 -0400 Subject: [PATCH] Run migrations on db initialization This makes it so we can keep all schema changes in the `migrations` module, instead of adding them there *and* in the schema files. It fixes #92 and should prevent these kinds of issues in the future. --- app.go | 8 +++++++- migrations/migrations.go | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app.go b/app.go index 0b42ec5..540b496 100644 --- a/app.go +++ b/app.go @@ -637,12 +637,18 @@ func adminInitDatabase(app *app) error { } // Set up migrations table - log.Info("Updating appmigrations table...") + log.Info("Initializing appmigrations table...") err = migrations.SetInitialMigrations(migrations.NewDatastore(app.db.DB, app.db.driverName)) if err != nil { return fmt.Errorf("Unable to set initial migrations: %v", err) } + log.Info("Running migrations...") + err = migrations.Migrate(migrations.NewDatastore(app.db.DB, app.db.driverName)) + if err != nil { + return fmt.Errorf("migrate: %s", err) + } + log.Info("Done.") return nil } diff --git a/migrations/migrations.go b/migrations/migrations.go index 9d73f86..70e4b7b 100644 --- a/migrations/migrations.go +++ b/migrations/migrations.go @@ -65,7 +65,8 @@ func CurrentVer() int { } func SetInitialMigrations(db *datastore) error { - _, err := db.Exec("INSERT INTO appmigrations (version, migrated, result) VALUES (?, "+db.now()+", ?)", CurrentVer(), "") + // Included schema files represent changes up to V1, so note that in the database + _, err := db.Exec("INSERT INTO appmigrations (version, migrated, result) VALUES (?, "+db.now()+", ?)", 1, "") if err != nil { return err }