2021-11-13 19:18:06 +00:00
|
|
|
// Copyright 2021 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package migration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"xorm.io/xorm"
|
|
|
|
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
// APPEND NEW MIGRATIONS
|
2021-11-23 04:06:46 +00:00
|
|
|
// they are executed in order and if one fail woodpecker try to rollback and quit
|
2022-01-31 14:50:10 +00:00
|
|
|
var migrationTasks = []*task{
|
|
|
|
&legacy2Xorm,
|
|
|
|
&alterTableReposDropFallback,
|
|
|
|
&alterTableReposDropAllowDeploysAllowTags,
|
|
|
|
&fixPRSecretEventName,
|
|
|
|
&alterTableReposDropCounter,
|
2022-05-17 19:27:44 +00:00
|
|
|
&dropSenders,
|
2022-07-29 12:57:18 +00:00
|
|
|
&alterTableLogUpdateColumnLogDataType,
|
2022-08-14 11:48:53 +00:00
|
|
|
&alterTableSecretsAddUserCol,
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 19:21:22 +00:00
|
|
|
var allBeans = []interface{}{
|
|
|
|
new(model.Agent),
|
|
|
|
new(model.Build),
|
|
|
|
new(model.BuildConfig),
|
|
|
|
new(model.Config),
|
|
|
|
new(model.File),
|
|
|
|
new(model.Logs),
|
|
|
|
new(model.Perm),
|
|
|
|
new(model.Proc),
|
|
|
|
new(model.Registry),
|
|
|
|
new(model.Repo),
|
|
|
|
new(model.Secret),
|
|
|
|
new(model.Task),
|
|
|
|
new(model.User),
|
2022-06-01 18:06:27 +00:00
|
|
|
new(model.ServerConfig),
|
2022-01-08 19:21:22 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
type migrations struct {
|
|
|
|
Name string `xorm:"UNIQUE"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type task struct {
|
2022-01-31 14:50:10 +00:00
|
|
|
name string
|
|
|
|
required bool
|
|
|
|
fn func(sess *xorm.Session) error
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initNew create tables for new instance
|
|
|
|
func initNew(sess *xorm.Session) error {
|
|
|
|
if err := syncAll(sess); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// dummy run migrations
|
|
|
|
for _, task := range migrationTasks {
|
|
|
|
if _, err := sess.Insert(&migrations{task.name}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Migrate(e *xorm.Engine) error {
|
|
|
|
if err := e.Sync2(new(migrations)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sess := e.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we have a fresh installation or need to check for migrations
|
|
|
|
c, err := sess.Count(new(migrations))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == 0 {
|
|
|
|
if err := initNew(sess); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := runTasks(sess, migrationTasks); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:02:52 +00:00
|
|
|
if err := sess.Commit(); err != nil {
|
2021-11-13 19:18:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:02:52 +00:00
|
|
|
return syncAll(e)
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:50:10 +00:00
|
|
|
func runTasks(sess *xorm.Session, tasks []*task) error {
|
2021-11-23 04:06:46 +00:00
|
|
|
// cache migrations in db
|
|
|
|
migCache := make(map[string]bool)
|
|
|
|
var migList []*migrations
|
|
|
|
if err := sess.Find(&migList); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range migList {
|
|
|
|
migCache[migList[i].Name] = true
|
|
|
|
}
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
for _, task := range tasks {
|
2021-11-23 04:06:46 +00:00
|
|
|
if migCache[task.name] {
|
|
|
|
log.Trace().Msgf("migration task '%s' already applied", task.name)
|
2021-11-13 19:18:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-23 04:06:46 +00:00
|
|
|
log.Trace().Msgf("start migration task '%s'", task.name)
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
if task.fn != nil {
|
|
|
|
if err := task.fn(sess); err != nil {
|
2022-01-31 14:50:10 +00:00
|
|
|
if task.required {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Error().Err(err).Msgf("migration task '%s' failed but is not required", task.name)
|
|
|
|
continue
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
log.Info().Msgf("migration task '%s' done", task.name)
|
|
|
|
} else {
|
|
|
|
log.Trace().Msgf("skip migration task '%s'", task.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := sess.Insert(&migrations{task.name}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-23 04:06:46 +00:00
|
|
|
migCache[task.name] = true
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:02:52 +00:00
|
|
|
type syncEngine interface {
|
|
|
|
Sync2(beans ...interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func syncAll(sess syncEngine) error {
|
2022-01-08 19:21:22 +00:00
|
|
|
for _, bean := range allBeans {
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := sess.Sync2(bean); err != nil {
|
|
|
|
return fmt.Errorf("sync2 error '%s': %v", reflect.TypeOf(bean), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|