Browse Source

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.
Matt Baer 5 months ago
parent
commit
771c7acf5f
2 changed files with 9 additions and 2 deletions
  1. 7
    1
      app.go
  2. 2
    1
      migrations/migrations.go

+ 7
- 1
app.go View File

@@ -637,12 +637,18 @@ func adminInitDatabase(app *app) error {
637 637
 	}
638 638
 
639 639
 	// Set up migrations table
640
-	log.Info("Updating appmigrations table...")
640
+	log.Info("Initializing appmigrations table...")
641 641
 	err = migrations.SetInitialMigrations(migrations.NewDatastore(app.db.DB, app.db.driverName))
642 642
 	if err != nil {
643 643
 		return fmt.Errorf("Unable to set initial migrations: %v", err)
644 644
 	}
645 645
 
646
+	log.Info("Running migrations...")
647
+	err = migrations.Migrate(migrations.NewDatastore(app.db.DB, app.db.driverName))
648
+	if err != nil {
649
+		return fmt.Errorf("migrate: %s", err)
650
+	}
651
+
646 652
 	log.Info("Done.")
647 653
 	return nil
648 654
 }

+ 2
- 1
migrations/migrations.go View File

@@ -65,7 +65,8 @@ func CurrentVer() int {
65 65
 }
66 66
 
67 67
 func SetInitialMigrations(db *datastore) error {
68
-	_, err := db.Exec("INSERT INTO appmigrations (version, migrated, result) VALUES (?, "+db.now()+", ?)", CurrentVer(), "")
68
+	// Included schema files represent changes up to V1, so note that in the database
69
+	_, err := db.Exec("INSERT INTO appmigrations (version, migrated, result) VALUES (?, "+db.now()+", ?)", 1, "")
69 70
 	if err != nil {
70 71
 		return err
71 72
 	}