Просмотр исходного кода

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.
tags/v0.9.1
Matt Baer 5 лет назад
Родитель
Сommit
771c7acf5f
2 измененных файлов: 9 добавлений и 2 удалений
  1. +7
    -1
      app.go
  2. +2
    -1
      migrations/migrations.go

+ 7
- 1
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
}

+ 2
- 1
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
}


Загрузка…
Отмена
Сохранить