2020-07-13 09:20:14 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-07-16 09:28:27 +02:00
|
|
|
"forge.cadoles.com/Cadoles/daddy/internal/model"
|
|
|
|
"forge.cadoles.com/Cadoles/daddy/internal/orm"
|
|
|
|
"github.com/jinzhu/gorm"
|
2020-07-13 09:20:14 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
"gitlab.com/wpetit/goweb/service"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
migrateUp = "up"
|
|
|
|
migrateLatest = "latest"
|
|
|
|
migrateDown = "down"
|
|
|
|
)
|
|
|
|
|
|
|
|
func applyMigration(ctx context.Context, ctn *service.Container) error {
|
2020-07-16 09:28:27 +02:00
|
|
|
orm, err := orm.From(ctn)
|
2020-07-13 09:20:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-16 09:28:27 +02:00
|
|
|
migr := orm.Migration()
|
|
|
|
|
2020-07-13 09:20:14 +02:00
|
|
|
// Register available migrations
|
|
|
|
migr.Register(
|
|
|
|
m000initialSchema(),
|
|
|
|
)
|
|
|
|
|
|
|
|
currentVersion, err := migr.CurrentVersion(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not retrieve current data schema version")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch migrate {
|
|
|
|
case migrateUp:
|
|
|
|
if err := migr.Up(ctx); err != nil {
|
|
|
|
return errors.Wrap(err, "could not apply up migration")
|
|
|
|
}
|
|
|
|
|
|
|
|
case migrateLatest:
|
|
|
|
latestVersion, err := migr.LatestVersion()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not retrieve latest data schema version")
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info(
|
|
|
|
ctx,
|
|
|
|
"migrating data schema to latest version",
|
|
|
|
logger.F("currentVersion", currentVersion),
|
|
|
|
logger.F("latestVersion", latestVersion),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Execute migration to latest available version
|
|
|
|
if err := migr.Latest(ctx); err != nil {
|
|
|
|
return errors.Wrap(err, "could not migrate to latest data schema")
|
|
|
|
}
|
|
|
|
|
|
|
|
case migrateDown:
|
|
|
|
if err := migr.Down(ctx); err != nil {
|
|
|
|
return errors.Wrap(err, "could not apply down migration")
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return errors.Errorf("unknown migration command: '%s'", migrate)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info(
|
|
|
|
ctx,
|
|
|
|
"migration completed",
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 09:28:27 +02:00
|
|
|
// nolint: gochecknoglobals
|
|
|
|
var initialModels = []interface{}{
|
|
|
|
&model.User{},
|
2020-07-21 18:10:03 +02:00
|
|
|
&model.Workgroup{},
|
2020-07-16 09:28:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func m000initialSchema() orm.Migration {
|
|
|
|
return orm.NewDBMigration(
|
2020-07-13 09:20:14 +02:00
|
|
|
"00_initial_schema",
|
2020-07-16 09:28:27 +02:00
|
|
|
func(ctx context.Context, tx *gorm.DB) error {
|
|
|
|
for _, m := range initialModels {
|
|
|
|
if err := tx.AutoMigrate(m).Error; err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-07-13 09:20:14 +02:00
|
|
|
},
|
2020-07-16 09:28:27 +02:00
|
|
|
func(ctx context.Context, tx *gorm.DB) error {
|
|
|
|
for _, m := range initialModels {
|
|
|
|
if err := tx.DropTableIfExists(m).Error; err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-07-13 09:20:14 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|