Let non required migration tasks fail and continue (#729)

This commit is contained in:
6543 2022-01-31 15:50:10 +01:00 committed by GitHub
parent 2a5159f7fe
commit 0d463ca467
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 11 deletions

View file

@ -24,6 +24,7 @@ import (
var legacy2Xorm = task{ var legacy2Xorm = task{
name: "xorm", name: "xorm",
required: true,
fn: func(sess *xorm.Session) error { fn: func(sess *xorm.Session) error {
// make sure we have required migrations - else fail and point to last major version // make sure we have required migrations - else fail and point to last major version
for _, mig := range []string{ for _, mig := range []string{

View file

@ -26,12 +26,12 @@ import (
// APPEND NEW MIGRATIONS // APPEND NEW MIGRATIONS
// they are executed in order and if one fail woodpecker try to rollback and quit // they are executed in order and if one fail woodpecker try to rollback and quit
var migrationTasks = []task{ var migrationTasks = []*task{
legacy2Xorm, &legacy2Xorm,
alterTableReposDropFallback, &alterTableReposDropFallback,
alterTableReposDropAllowDeploysAllowTags, &alterTableReposDropAllowDeploysAllowTags,
fixPRSecretEventName, &fixPRSecretEventName,
alterTableReposDropCounter, &alterTableReposDropCounter,
} }
var allBeans = []interface{}{ var allBeans = []interface{}{
@ -57,6 +57,7 @@ type migrations struct {
type task struct { type task struct {
name string name string
required bool
fn func(sess *xorm.Session) error fn func(sess *xorm.Session) error
} }
@ -111,7 +112,7 @@ func Migrate(e *xorm.Engine) error {
return syncAll(e) return syncAll(e)
} }
func runTasks(sess *xorm.Session, tasks []task) error { func runTasks(sess *xorm.Session, tasks []*task) error {
// cache migrations in db // cache migrations in db
migCache := make(map[string]bool) migCache := make(map[string]bool)
var migList []*migrations var migList []*migrations
@ -132,8 +133,12 @@ func runTasks(sess *xorm.Session, tasks []task) error {
if task.fn != nil { if task.fn != nil {
if err := task.fn(sess); err != nil { if err := task.fn(sess); err != nil {
if task.required {
return err return err
} }
log.Error().Err(err).Msgf("migration task '%s' failed but is not required", task.name)
continue
}
log.Info().Msgf("migration task '%s' done", task.name) log.Info().Msgf("migration task '%s' done", task.name)
} else { } else {
log.Trace().Msgf("skip migration task '%s'", task.name) log.Trace().Msgf("skip migration task '%s'", task.name)

View file

@ -81,6 +81,11 @@ func testDB(t *testing.T, new bool) (engine *xorm.Engine, close func()) {
} }
func TestMigrate(t *testing.T) { func TestMigrate(t *testing.T) {
// make all tasks required for tests
for _, task := range migrationTasks {
task.required = true
}
// init new db // init new db
engine, close := testDB(t, true) engine, close := testDB(t, true)
assert.NoError(t, Migrate(engine)) assert.NoError(t, Migrate(engine))