Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let non required migration tasks fail and continue #729

Merged
merged 6 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions server/store/datastore/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package datastore
import (
"os"
"testing"
"time"

"xorm.io/xorm"
"xorm.io/xorm/schemas"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -67,5 +69,10 @@ func newTestStore(t *testing.T, tables ...interface{}) (*storage, func()) {
t.Error(err)
t.FailNow()
}

if engine.Dialect().URI().DBType == schemas.MYSQL {
// wait for mysql to sync ...
time.Sleep(10 * time.Millisecond)
}
}
}
3 changes: 2 additions & 1 deletion server/store/datastore/migration/000_legacy_to_xorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
25 changes: 15 additions & 10 deletions server/store/datastore/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions server/store/datastore/migration/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,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))
Expand Down