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 (
|
2023-06-10 09:59:15 +00:00
|
|
|
"context"
|
2023-06-06 01:27:17 +00:00
|
|
|
"errors"
|
2021-11-13 19:18:06 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2023-06-10 09:59:15 +00:00
|
|
|
"time"
|
2021-11-13 19:18:06 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"xorm.io/xorm"
|
|
|
|
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
// APPEND NEW MIGRATIONS
|
2023-06-06 01:27:17 +00:00
|
|
|
// they are executed in order and if one fails woodpecker will try to rollback that specific one and quits
|
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,
|
2023-01-28 13:13:04 +00:00
|
|
|
&recreateAgentsTable,
|
2022-10-16 12:58:13 +00:00
|
|
|
&lowercaseSecretNames,
|
2022-10-18 01:24:12 +00:00
|
|
|
&renameBuildsToPipeline,
|
2022-10-22 13:54:43 +00:00
|
|
|
&renameColumnsBuildsToPipeline,
|
2022-10-28 15:38:53 +00:00
|
|
|
&renameTableProcsToSteps,
|
2022-11-04 23:35:06 +00:00
|
|
|
&renameRemoteToForge,
|
2022-11-15 14:01:23 +00:00
|
|
|
&renameForgeIDToForgeRemoteID,
|
2023-03-18 20:21:20 +00:00
|
|
|
&removeActiveFromUsers,
|
2023-03-21 22:01:59 +00:00
|
|
|
&removeInactiveRepos,
|
2023-05-31 16:03:03 +00:00
|
|
|
&dropFiles,
|
2023-06-02 12:39:29 +00:00
|
|
|
&removeMachineCol,
|
2023-06-07 17:22:44 +00:00
|
|
|
&dropOldCols,
|
2023-06-12 20:43:14 +00:00
|
|
|
&initLogsEntriesTable,
|
|
|
|
&migrateLogs2LogEntries,
|
2023-06-27 16:01:18 +00:00
|
|
|
&parentStepsToWorkflows,
|
2023-07-21 17:45:32 +00:00
|
|
|
&addOrgs,
|
2023-08-21 13:04:12 +00:00
|
|
|
&addOrgID,
|
2023-09-10 11:33:48 +00:00
|
|
|
&alterTableTasksUpdateColumnTaskDataType,
|
2023-09-10 20:24:51 +00:00
|
|
|
&alterTableConfigUpdateColumnConfigDataType,
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 19:21:22 +00:00
|
|
|
var allBeans = []interface{}{
|
|
|
|
new(model.Agent),
|
2022-10-18 01:24:12 +00:00
|
|
|
new(model.Pipeline),
|
|
|
|
new(model.PipelineConfig),
|
2022-01-08 19:21:22 +00:00
|
|
|
new(model.Config),
|
2023-06-06 07:52:08 +00:00
|
|
|
new(model.LogEntry),
|
2022-01-08 19:21:22 +00:00
|
|
|
new(model.Perm),
|
2022-10-28 15:38:53 +00:00
|
|
|
new(model.Step),
|
2022-01-08 19:21:22 +00:00
|
|
|
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-08-31 22:36:32 +00:00
|
|
|
new(model.Cron),
|
2022-09-05 15:08:51 +00:00
|
|
|
new(model.Redirection),
|
2023-06-27 16:01:18 +00:00
|
|
|
new(model.Workflow),
|
2023-07-21 17:45:32 +00:00
|
|
|
new(model.Org),
|
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
|
2023-06-12 20:43:14 +00:00
|
|
|
// engineFn does manage session on it's own. only use it if you really need to
|
|
|
|
engineFn func(e *xorm.Engine) 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 {
|
2023-06-06 01:27:17 +00:00
|
|
|
e.SetDisableGlobalCache(true)
|
|
|
|
|
2023-05-31 19:27:57 +00:00
|
|
|
if err := e.Sync(new(migrations)); err != nil {
|
2023-07-21 17:45:32 +00:00
|
|
|
return fmt.Errorf("error to create migrations table: %w", err)
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sess := e.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
2023-07-21 17:45:32 +00:00
|
|
|
return fmt.Errorf("could not create initial migration session: %w", err)
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if we have a fresh installation or need to check for migrations
|
|
|
|
c, err := sess.Count(new(migrations))
|
|
|
|
if err != nil {
|
2023-07-21 17:45:32 +00:00
|
|
|
return fmt.Errorf("could not count migrations: %w", err)
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c == 0 {
|
|
|
|
if err := initNew(sess); err != nil {
|
2023-07-21 17:45:32 +00:00
|
|
|
return fmt.Errorf("could not init a new database: %w", err)
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
2023-07-21 17:45:32 +00:00
|
|
|
if err := sess.Commit(); err != nil {
|
|
|
|
return fmt.Errorf("could not commit initial migration session: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 11:02:52 +00:00
|
|
|
if err := sess.Commit(); err != nil {
|
2023-07-21 17:45:32 +00:00
|
|
|
return fmt.Errorf("could not commit initial migration session: %w", err)
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2023-06-06 01:27:17 +00:00
|
|
|
if err := runTasks(e, migrationTasks); err != nil {
|
2023-07-21 17:45:32 +00:00
|
|
|
return fmt.Errorf("run tasks failed: %w", err)
|
2022-10-22 13:54:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-06 01:27:17 +00:00
|
|
|
e.SetDisableGlobalCache(false)
|
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
if err := syncAll(e); err != nil {
|
|
|
|
return fmt.Errorf("msg: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2023-06-06 01:27:17 +00:00
|
|
|
func runTasks(e *xorm.Engine, tasks []*task) error {
|
2021-11-23 04:06:46 +00:00
|
|
|
// cache migrations in db
|
|
|
|
migCache := make(map[string]bool)
|
|
|
|
var migList []*migrations
|
2023-06-06 01:27:17 +00:00
|
|
|
if err := e.Find(&migList); err != nil {
|
2021-11-23 04:06:46 +00:00
|
|
|
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)
|
2023-06-12 20:43:14 +00:00
|
|
|
aliveMsgCancel := showBeAliveSign(task.name)
|
|
|
|
defer aliveMsgCancel(nil)
|
|
|
|
var taskErr error
|
2021-11-13 19:18:06 +00:00
|
|
|
if task.fn != nil {
|
2023-06-12 20:43:14 +00:00
|
|
|
sess := e.NewSession().NoCache()
|
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
2023-07-21 17:45:32 +00:00
|
|
|
return fmt.Errorf("could not begin session for '%s': %w", task.name, err)
|
2023-06-12 20:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if taskErr = task.fn(sess); taskErr != nil {
|
2023-06-10 09:59:15 +00:00
|
|
|
aliveMsgCancel(nil)
|
2023-07-21 17:45:32 +00:00
|
|
|
if err := sess.Rollback(); err != nil {
|
|
|
|
taskErr = errors.Join(taskErr, err)
|
2022-01-31 14:50:10 +00:00
|
|
|
}
|
2023-07-21 17:45:32 +00:00
|
|
|
} else if err := sess.Commit(); err != nil {
|
|
|
|
return fmt.Errorf("could not commit session for '%s': %w", task.name, err)
|
2023-06-12 20:43:14 +00:00
|
|
|
}
|
|
|
|
} else if task.engineFn != nil {
|
|
|
|
taskErr = task.engineFn(e)
|
2021-11-13 19:18:06 +00:00
|
|
|
} else {
|
|
|
|
log.Trace().Msgf("skip migration task '%s'", task.name)
|
2023-06-12 20:43:14 +00:00
|
|
|
aliveMsgCancel(nil)
|
|
|
|
continue
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2023-06-12 20:43:14 +00:00
|
|
|
aliveMsgCancel(nil)
|
|
|
|
if taskErr != nil {
|
|
|
|
if task.required {
|
2023-07-21 17:45:32 +00:00
|
|
|
return fmt.Errorf("migration task '%s' failed: %w", task.name, taskErr)
|
2023-06-12 20:43:14 +00:00
|
|
|
}
|
|
|
|
log.Error().Err(taskErr).Msgf("migration task '%s' failed but is not required", task.name)
|
|
|
|
continue
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
2023-06-12 20:43:14 +00:00
|
|
|
log.Debug().Msgf("migration task '%s' done", task.name)
|
|
|
|
|
|
|
|
if _, err := e.Insert(&migrations{task.name}); err != nil {
|
2023-07-21 17:45:32 +00:00
|
|
|
return fmt.Errorf("migration task '%s' could not be marked as finished: %w", task.name, err)
|
2023-06-06 01:27:17 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-05-31 19:27:57 +00:00
|
|
|
Sync(beans ...interface{}) error
|
2021-11-27 11:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func syncAll(sess syncEngine) error {
|
2022-01-08 19:21:22 +00:00
|
|
|
for _, bean := range allBeans {
|
2023-05-31 19:27:57 +00:00
|
|
|
if err := sess.Sync(bean); err != nil {
|
|
|
|
return fmt.Errorf("Sync error '%s': %w", reflect.TypeOf(bean), err)
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-10 09:59:15 +00:00
|
|
|
|
|
|
|
var showBeAliveSignDelay = time.Second * 20
|
|
|
|
|
|
|
|
func showBeAliveSign(taskName string) context.CancelCauseFunc {
|
|
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-time.After(showBeAliveSignDelay):
|
|
|
|
log.Info().Msgf("Migration '%s' is still running, please be patient", taskName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return cancel
|
|
|
|
}
|