golang-lib/database/migration.go

110 lines
2.6 KiB
Go
Raw Permalink Normal View History

2021-06-01 10:51:35 +02:00
package database
import (
"sort"
gormigrate "github.com/genofire/gormigrate/v2"
2021-06-01 10:51:35 +02:00
)
func (config *Database) sortedMigration(testdata bool) []*gormigrate.Migration {
var migrations []*gormigrate.Migration
for _, v := range config.migrations {
migrations = append(migrations, v)
}
if testdata {
for _, v := range config.migrationTestdata {
migrations = append(migrations, v)
}
}
sort.SliceStable(migrations, func(i, j int) bool {
return migrations[i].ID < migrations[j].ID
})
return migrations
}
2021-07-14 12:29:58 +02:00
func (config *Database) setupMigrator(testdata bool) (*gormigrate.Gormigrate, error) {
migrations := config.sortedMigration(testdata)
if len(migrations) == 0 {
return nil, ErrNothingToMigrate
}
2022-06-06 01:56:24 +02:00
return gormigrate.New(config.DB, &gormigrate.Options{
TableName: "migrations",
IDColumnName: "id",
IDColumnSize: 255,
UseTransaction: true,
ValidateUnknownMigrations: false,
}, migrations), nil
2021-07-14 12:29:58 +02:00
}
func (config *Database) migrate(testdata bool) error {
m, err := config.setupMigrator(testdata)
if err != nil {
return err
}
return m.Migrate()
}
2021-06-01 17:41:05 +02:00
// Migrate run migration
2021-06-01 10:51:35 +02:00
func (config *Database) Migrate() error {
return config.migrate(false)
}
2021-06-01 17:41:05 +02:00
// MigrateTestdata run migration and testdata migration
2021-06-01 10:51:35 +02:00
func (config *Database) MigrateTestdata() error {
return config.migrate(true)
}
2021-06-01 17:41:05 +02:00
// AddMigration add to database config migration step
2021-06-01 10:51:35 +02:00
func (config *Database) AddMigration(m ...*gormigrate.Migration) {
config.addMigrate(false, m...)
}
2021-06-01 17:41:05 +02:00
// AddMigrationTestdata add to database config migration step of testdata
2021-06-01 10:51:35 +02:00
func (config *Database) AddMigrationTestdata(m ...*gormigrate.Migration) {
config.addMigrate(true, m...)
}
func (config *Database) addMigrate(testdata bool, m ...*gormigrate.Migration) {
if config.migrations == nil {
config.migrations = make(map[string]*gormigrate.Migration)
}
if config.migrationTestdata == nil {
config.migrationTestdata = make(map[string]*gormigrate.Migration)
}
for _, i := range m {
if testdata {
config.migrationTestdata[i.ID] = i
} else {
config.migrations[i.ID] = i
2021-06-01 10:51:35 +02:00
}
}
}
2021-07-14 12:29:58 +02:00
// ReMigrate Rollback und run every migration step again till id
func (config *Database) ReMigrate(id string) error {
2021-06-01 10:51:35 +02:00
migrations := config.sortedMigration(true)
2021-07-14 12:29:58 +02:00
m, err := config.setupMigrator(true)
if err != nil {
return err
}
2021-06-01 10:51:35 +02:00
x := 0
for _, m := range migrations {
if m.ID == id {
break
}
x = x + 1
}
2021-07-14 12:29:58 +02:00
// TODO not found
2021-06-01 10:51:35 +02:00
for i := len(migrations) - 1; i >= x; i = i - 1 {
2021-07-14 12:29:58 +02:00
mStep := migrations[i]
if err := m.RollbackTo(mStep.ID); err != nil {
return err
2021-06-01 10:51:35 +02:00
}
}
2021-07-14 12:29:58 +02:00
return m.Migrate()
2021-06-01 10:51:35 +02:00
}