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"
|
|
|
|
|
2023-11-28 09:31:54 +00:00
|
|
|
"src.techknowlogick.com/xormigrate"
|
2021-11-13 19:18:06 +00:00
|
|
|
"xorm.io/xorm"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2021-11-13 19:18:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// APPEND NEW MIGRATIONS
|
2024-05-13 20:58:21 +00:00
|
|
|
// They are executed in order and if one fails Xormigrate will try to rollback that specific one and quits.
|
2023-11-28 09:31:54 +00:00
|
|
|
var migrationTasks = []*xormigrate.Migration{
|
|
|
|
&legacyToXormigrate,
|
2022-01-31 14:50:10 +00:00
|
|
|
&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,
|
2023-10-24 18:38:47 +00:00
|
|
|
&removePluginOnlyOptionFromSecretsTable,
|
2023-11-03 10:44:03 +00:00
|
|
|
&convertToNewPipelineErrorFormat,
|
2023-11-14 16:12:12 +00:00
|
|
|
&renameLinkToURL,
|
2024-01-22 06:56:18 +00:00
|
|
|
&cleanRegistryPipeline,
|
2024-04-16 06:04:55 +00:00
|
|
|
&setForgeID,
|
2024-06-27 07:32:06 +00:00
|
|
|
&unifyColumnsTables,
|
2024-07-03 13:33:11 +00:00
|
|
|
&alterTableRegistriesFixRequiredFields,
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-12 17:23:48 +00:00
|
|
|
var allBeans = []any{
|
2022-01-08 19:21:22 +00:00
|
|
|
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),
|
2024-04-16 06:04:55 +00:00
|
|
|
new(model.Forge),
|
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
|
|
|
}
|
|
|
|
|
2023-11-28 09:31:54 +00:00
|
|
|
func Migrate(e *xorm.Engine, allowLong bool) error {
|
2023-06-06 01:27:17 +00:00
|
|
|
e.SetDisableGlobalCache(true)
|
|
|
|
|
2023-11-28 09:31:54 +00:00
|
|
|
m := xormigrate.New(e, migrationTasks)
|
|
|
|
m.AllowLong(allowLong)
|
|
|
|
oldCount, err := e.Table("migrations").Count()
|
|
|
|
if oldCount < 1 || err != nil {
|
|
|
|
// allow new schema initialization if old migrations table is empty or it does not exist (err != nil)
|
|
|
|
// schema initialization will always run if we call `InitSchema`
|
2024-02-08 21:49:07 +00:00
|
|
|
m.InitSchema(func(_ *xorm.Engine) error {
|
2023-11-28 09:31:54 +00:00
|
|
|
// do nothing on schema init, models are synced in any case below
|
|
|
|
return nil
|
|
|
|
})
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 09:31:54 +00:00
|
|
|
m.SetLogger(&xormigrateLogger{})
|
2021-11-13 19:18:06 +00:00
|
|
|
|
2023-11-28 09:31:54 +00:00
|
|
|
if err := m.Migrate(); err != nil {
|
|
|
|
return 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-11-28 09:31:54 +00:00
|
|
|
func syncAll(sess *xorm.Engine) 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 {
|
2024-01-10 21:56:42 +00:00
|
|
|
return fmt.Errorf("sync error '%s': %w", reflect.TypeOf(bean), err)
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|