1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-11-24 08:02:18 +02:00

store migrations: cache migrations and add note (#522)

* Cache migrations instead of query each time
* Add a note of migration execute order 

Co-authored-by: Anbraten <anton@ju60.de>
This commit is contained in:
6543 2021-11-23 05:06:46 +01:00 committed by GitHub
parent 34ff30010e
commit f454371e56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,6 +25,7 @@ import (
)
// APPEND NEW MIGRATIONS
// they are executed in order and if one fail woodpecker try to rollback and quit
var migrationTasks = []task{
legacy2Xorm,
alterTableReposDropFallback,
@ -92,17 +93,24 @@ func Migrate(e *xorm.Engine) error {
}
func runTasks(sess *xorm.Session, tasks []task) error {
for _, task := range tasks {
log.Trace().Msgf("start migration task '%s'", task.name)
exist, err := sess.Exist(&migrations{task.name})
if err != nil {
// cache migrations in db
migCache := make(map[string]bool)
var migList []*migrations
if err := sess.Find(&migList); err != nil {
return err
}
if exist {
log.Trace().Msgf("migration task '%s' exist", task.name)
for i := range migList {
migCache[migList[i].Name] = true
}
for _, task := range tasks {
if migCache[task.name] {
log.Trace().Msgf("migration task '%s' already applied", task.name)
continue
}
log.Trace().Msgf("start migration task '%s'", task.name)
if task.fn != nil {
if err := task.fn(sess); err != nil {
return err
@ -115,6 +123,7 @@ func runTasks(sess *xorm.Session, tasks []task) error {
if _, err := sess.Insert(&migrations{task.name}); err != nil {
return err
}
migCache[task.name] = true
}
return nil
}