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

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

View file

@ -26,12 +26,12 @@ import (
// APPEND NEW MIGRATIONS
// they are executed in order and if one fail woodpecker try to rollback and quit
var migrationTasks = []task{
legacy2Xorm,
alterTableReposDropFallback,
alterTableReposDropAllowDeploysAllowTags,
fixPRSecretEventName,
alterTableReposDropCounter,
var migrationTasks = []*task{
&legacy2Xorm,
&alterTableReposDropFallback,
&alterTableReposDropAllowDeploysAllowTags,
&fixPRSecretEventName,
&alterTableReposDropCounter,
}
var allBeans = []interface{}{
@ -56,8 +56,9 @@ type migrations struct {
}
type task struct {
name string
fn func(sess *xorm.Session) error
name string
required bool
fn func(sess *xorm.Session) error
}
// initNew create tables for new instance
@ -111,7 +112,7 @@ func Migrate(e *xorm.Engine) error {
return syncAll(e)
}
func runTasks(sess *xorm.Session, tasks []task) error {
func runTasks(sess *xorm.Session, tasks []*task) error {
// cache migrations in db
migCache := make(map[string]bool)
var migList []*migrations
@ -132,7 +133,11 @@ func runTasks(sess *xorm.Session, tasks []task) error {
if task.fn != nil {
if err := task.fn(sess); err != nil {
return err
if task.required {
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)
} else {

View file

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