Migrations: Support independent migration log for entity api (#68511)

This commit is contained in:
Ryan McKinley
2023-05-22 11:31:07 -07:00
committed by GitHub
parent 2c75a51285
commit 26658d172f
6 changed files with 127 additions and 64 deletions
+40 -11
View File
@@ -28,6 +28,7 @@ type Migrator struct {
Cfg *setting.Cfg
isLocked atomic.Bool
logMap map[string]MigrationLog
tableName string
}
type MigrationLog struct {
@@ -40,16 +41,44 @@ type MigrationLog struct {
}
func NewMigrator(engine *xorm.Engine, cfg *setting.Cfg) *Migrator {
mg := &Migrator{}
mg.DBEngine = engine
mg.Logger = log.New("migrator")
mg.migrations = make([]Migration, 0)
mg.migrationIds = make(map[string]struct{})
mg.Dialect = NewDialect(mg.DBEngine)
mg.Cfg = cfg
return NewScopedMigrator(engine, cfg, "")
}
// NewScopedMigrator should only be used for the transition to a new storage engine
func NewScopedMigrator(engine *xorm.Engine, cfg *setting.Cfg, scope string) *Migrator {
mg := &Migrator{
Cfg: cfg,
DBEngine: engine,
migrations: make([]Migration, 0),
migrationIds: make(map[string]struct{}),
Dialect: NewDialect(engine),
}
if scope == "" {
mg.tableName = "migration_log"
mg.Logger = log.New("migrator")
} else {
mg.tableName = scope + "_migration_log"
mg.Logger = log.New(scope + " migrator")
}
return mg
}
// AddCreateMigration adds the initial migration log table -- this should likely be
// automatic and first, but enough tests exists that do not expect that we can keep it explicit
func (mg *Migrator) AddCreateMigration() {
mg.AddMigration("create "+mg.tableName+" table", NewAddTableMigration(Table{
Name: mg.tableName,
Columns: []*Column{
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
{Name: "migration_id", Type: DB_NVarchar, Length: 255},
{Name: "sql", Type: DB_Text},
{Name: "success", Type: DB_Bool},
{Name: "error", Type: DB_Text},
{Name: "timestamp", Type: DB_DateTime},
},
}))
}
func (mg *Migrator) MigrationsCount() int {
return len(mg.migrations)
}
@@ -79,7 +108,7 @@ func (mg *Migrator) GetMigrationLog() (map[string]MigrationLog, error) {
logMap := make(map[string]MigrationLog)
logItems := make([]MigrationLog, 0)
exists, err := mg.DBEngine.IsTableExist(new(MigrationLog))
exists, err := mg.DBEngine.IsTableExist(mg.tableName)
if err != nil {
return nil, fmt.Errorf("%v: %w", "failed to check table existence", err)
}
@@ -87,7 +116,7 @@ func (mg *Migrator) GetMigrationLog() (map[string]MigrationLog, error) {
return logMap, nil
}
if err = mg.DBEngine.Find(&logItems); err != nil {
if err = mg.DBEngine.Table(mg.tableName).Find(&logItems); err != nil {
return nil, err
}
@@ -167,7 +196,7 @@ func (mg *Migrator) run() (err error) {
mg.Logger.Error("Exec failed", "error", err, "sql", sql)
record.Error = err.Error()
if !m.SkipMigrationLog() {
if _, err := sess.Insert(&record); err != nil {
if _, err := sess.Table(mg.tableName).Insert(&record); err != nil {
return err
}
}
@@ -175,7 +204,7 @@ func (mg *Migrator) run() (err error) {
}
record.Success = true
if !m.SkipMigrationLog() {
_, err = sess.Insert(&record)
_, err = sess.Table(mg.tableName).Insert(&record)
}
if err == nil {
migrationsPerformed++