remigration and rollback all
continuous-integration/drone the build failed Details

This commit is contained in:
Geno 2021-07-14 12:29:58 +02:00
parent d888e27790
commit 6fb3b904df
8 changed files with 81 additions and 45 deletions

View File

@ -20,6 +20,24 @@ type Database struct {
// Run database config - connect and migrate // Run database config - connect and migrate
func (config *Database) Run() error { func (config *Database) Run() error {
if err := config.run(); err != nil {
return err
}
return config.migrate(config.Testdata)
}
// ReRun database config - connect and re migration
func (config *Database) ReRun() error {
if err := config.run(); err != nil {
return err
}
if err := config.Rollback(); err != nil {
return err
}
return config.migrate(config.Testdata)
}
func (config *Database) run() error {
db, err := gorm.Open(postgres.Open(config.Connection), &gorm.Config{ db, err := gorm.Open(postgres.Open(config.Connection), &gorm.Config{
Logger: logger.Default.LogMode(config.LogLevel), Logger: logger.Default.LogMode(config.LogLevel),
}) })
@ -32,9 +50,6 @@ func (config *Database) Run() error {
} }
config.DB = db config.DB = db
if err = config.migrate(config.Testdata); err != nil {
return err
}
return nil return nil
} }

View File

@ -3,7 +3,6 @@ package database
import ( import (
"sort" "sort"
"github.com/bdlm/log"
gormigrate "github.com/go-gormigrate/gormigrate/v2" gormigrate "github.com/go-gormigrate/gormigrate/v2"
) )
@ -23,6 +22,24 @@ func (config *Database) sortedMigration(testdata bool) []*gormigrate.Migration {
return migrations return migrations
} }
func (config *Database) setupMigrator(testdata bool) (*gormigrate.Gormigrate, error) {
migrations := config.sortedMigration(testdata)
if len(migrations) == 0 {
return nil, ErrNothingToMigrate
}
return gormigrate.New(config.DB, gormigrate.DefaultOptions, migrations), nil
}
func (config *Database) migrate(testdata bool) error {
m, err := config.setupMigrator(testdata)
if err != nil {
return err
}
return m.Migrate()
}
// Migrate run migration // Migrate run migration
func (config *Database) Migrate() error { func (config *Database) Migrate() error {
return config.migrate(false) return config.migrate(false)
@ -32,16 +49,6 @@ func (config *Database) Migrate() error {
func (config *Database) MigrateTestdata() error { func (config *Database) MigrateTestdata() error {
return config.migrate(true) return config.migrate(true)
} }
func (config *Database) migrate(testdata bool) error {
migrations := config.sortedMigration(testdata)
if len(migrations) == 0 {
return ErrNothingToMigrate
}
m := gormigrate.New(config.DB, gormigrate.DefaultOptions, migrations)
return m.Migrate()
}
// AddMigration add to database config migration step // AddMigration add to database config migration step
func (config *Database) AddMigration(m ...*gormigrate.Migration) { func (config *Database) AddMigration(m ...*gormigrate.Migration) {
@ -69,9 +76,32 @@ func (config *Database) addMigrate(testdata bool, m ...*gormigrate.Migration) {
} }
} }
// ReRun Rollback und run every migration step again till id // Rollback all migrations steps
func (config *Database) ReRun(id string) { func (config *Database) Rollback() error {
m, err := config.setupMigrator(true)
if err != nil {
return err
}
for {
err = m.RollbackLast()
if err != nil {
if err == gormigrate.ErrNoRunMigration {
return nil
}
return err
}
}
}
// ReMigrate Rollback und run every migration step again till id
func (config *Database) ReMigrate(id string) error {
migrations := config.sortedMigration(true) migrations := config.sortedMigration(true)
m, err := config.setupMigrator(true)
if err != nil {
return err
}
x := 0 x := 0
for _, m := range migrations { for _, m := range migrations {
if m.ID == id { if m.ID == id {
@ -79,20 +109,13 @@ func (config *Database) ReRun(id string) {
} }
x = x + 1 x = x + 1
} }
// TODO not found
for i := len(migrations) - 1; i >= x; i = i - 1 { for i := len(migrations) - 1; i >= x; i = i - 1 {
m := migrations[i] mStep := migrations[i]
log.Warnf("rollback %s", m.ID) if err := m.RollbackTo(mStep.ID); err != nil {
err := m.Rollback(config.DB) return err
if err != nil {
log.Errorf("rollback %s", err)
}
}
for _, m := range migrations {
log.Warnf("run %s", m.ID)
err := m.Migrate(config.DB)
if err != nil {
log.Errorf("run %s", err)
} }
} }
return m.Migrate()
} }

View File

@ -12,12 +12,10 @@ import (
func TestAPILogin(t *testing.T) { func TestAPILogin(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
s, err := webtest.New() s, err := webtest.NewWithDBSetup(SetupMigration)
assert.NoError(err) assert.NoError(err)
defer s.Close() defer s.Close()
assert.NotNil(s) assert.NotNil(s)
SetupMigration(s.DB)
s.DB.MigrateTestdata()
hErr := web.HTTPError{} hErr := web.HTTPError{}
// invalid // invalid

View File

@ -12,12 +12,10 @@ import (
func TestAPIDeleteMyProfil(t *testing.T) { func TestAPIDeleteMyProfil(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
s, err := webtest.New() s, err := webtest.NewWithDBSetup(SetupMigration)
assert.NoError(err) assert.NoError(err)
defer s.Close() defer s.Close()
assert.NotNil(s) assert.NotNil(s)
SetupMigration(s.DB)
s.DB.MigrateTestdata()
hErr := web.HTTPError{} hErr := web.HTTPError{}
// invalid // invalid
@ -37,5 +35,5 @@ func TestAPIDeleteMyProfil(t *testing.T) {
assert.NoError(err) assert.NoError(err)
assert.True(true) assert.True(true)
s.DB.ReRun("10-data-0008-01-user") s.DB.ReMigrate("10-data-0008-01-user")
} }

View File

@ -12,12 +12,10 @@ import (
func TestAPIPassword(t *testing.T) { func TestAPIPassword(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
s, err := webtest.New() s, err := webtest.NewWithDBSetup(SetupMigration)
assert.NoError(err) assert.NoError(err)
defer s.Close() defer s.Close()
assert.NotNil(s) assert.NotNil(s)
SetupMigration(s.DB)
s.DB.MigrateTestdata()
passwordCurrent := "CHANGEME" passwordCurrent := "CHANGEME"
passwordNew := "test" passwordNew := "test"

View File

@ -12,12 +12,10 @@ import (
func TestAPIStatus(t *testing.T) { func TestAPIStatus(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
s, err := webtest.New() s, err := webtest.NewWithDBSetup(SetupMigration)
assert.NoError(err) assert.NoError(err)
defer s.Close() defer s.Close()
assert.NotNil(s) assert.NotNil(s)
SetupMigration(s.DB)
s.DB.MigrateTestdata()
hErr := web.HTTPError{} hErr := web.HTTPError{}
// invalid // invalid

View File

@ -13,12 +13,10 @@ import (
func TestAPIPasswordCode(t *testing.T) { func TestAPIPasswordCode(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
s, err := webtest.New() s, err := webtest.NewWithDBSetup(SetupMigration)
assert.NoError(err) assert.NoError(err)
defer s.Close() defer s.Close()
assert.NotNil(s) assert.NotNil(s)
SetupMigration(s.DB)
s.DB.MigrateTestdata()
forgetCode := uuid.New() forgetCode := uuid.New()
passwordCurrent := "CHANGEME" passwordCurrent := "CHANGEME"

View File

@ -38,6 +38,11 @@ type Login struct {
// New starts WebService for testing // New starts WebService for testing
func New() (*testServer, error) { func New() (*testServer, error) {
return NewWithDBSetup(nil)
}
// NewWithDBSetup allows to reconfigure before ReRun the database - e.g. for adding Migration-Steps
func NewWithDBSetup(dbCall func(db *database.Database)) (*testServer, error) {
// db setup // db setup
dbConfig := database.Database{ dbConfig := database.Database{
Connection: DBConnection, Connection: DBConnection,
@ -45,7 +50,10 @@ func New() (*testServer, error) {
Debug: false, Debug: false,
LogLevel: 0, LogLevel: 0,
} }
err := dbConfig.Run() if dbCall != nil {
dbCall(&dbConfig)
}
err := dbConfig.ReRun()
if err != nil && err != database.ErrNothingToMigrate { if err != nil && err != database.ErrNothingToMigrate {
return nil, err return nil, err
} }