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 (
|
2024-07-13 23:46:01 +00:00
|
|
|
"context"
|
2021-11-13 19:18:06 +00:00
|
|
|
"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,
|
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,
|
2024-08-06 17:22:28 +00:00
|
|
|
&cronWithoutSec,
|
2024-08-07 08:49:03 +00:00
|
|
|
&renameStartEndTime,
|
2024-09-14 11:41:14 +00:00
|
|
|
&fixV31Registries,
|
2024-09-16 19:09:42 +00:00
|
|
|
&removeOldMigrationsOfV1,
|
2024-09-30 11:33:16 +00:00
|
|
|
&addOrgAgents,
|
2024-10-06 15:13:41 +00:00
|
|
|
&addCustomLabelsToAgent,
|
2024-11-01 20:37:31 +00:00
|
|
|
&splitTrusted,
|
2024-11-06 12:51:12 +00:00
|
|
|
&correctPotentialCorruptOrgsUsersRelation,
|
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
|
|
|
}
|
|
|
|
|
2024-07-13 23:46:01 +00:00
|
|
|
// TODO: make xormigrate context aware
|
|
|
|
func Migrate(_ context.Context, 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
|
|
|
|
}
|