From 4fa3a8e578dc44464a02fed36024cdc2891061fb Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 6 Nov 2024 13:51:12 +0100 Subject: [PATCH] Add migration to autofix corrupted users.org_id entrys in db (#4307) --- .../migration/018_fix-orgs-users-match.go | 59 +++++++++++++++++++ server/store/datastore/migration/migration.go | 1 + 2 files changed, 60 insertions(+) create mode 100644 server/store/datastore/migration/018_fix-orgs-users-match.go diff --git a/server/store/datastore/migration/018_fix-orgs-users-match.go b/server/store/datastore/migration/018_fix-orgs-users-match.go new file mode 100644 index 000000000..f99e2af15 --- /dev/null +++ b/server/store/datastore/migration/018_fix-orgs-users-match.go @@ -0,0 +1,59 @@ +// Copyright 2024 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" + + "src.techknowlogick.com/xormigrate" + "xorm.io/xorm" + "xorm.io/xorm/schemas" +) + +var correctPotentialCorruptOrgsUsersRelation = xormigrate.Migration{ + ID: "correct-potential-corrupt-orgs-users-relation", + MigrateSession: func(sess *xorm.Session) error { + type users struct { + ID int64 `xorm:"pk autoincr 'id'"` + ForgeID int64 `xorm:"forge_id"` + Login string `xorm:"UNIQUE 'login'"` + OrgID int64 `xorm:"org_id"` + } + + type orgs struct { + ID int64 `xorm:"pk autoincr 'id'"` + ForgeID int64 `xorm:"forge_id"` + Name string `xorm:"UNIQUE 'name'"` + } + + if err := sess.Sync(new(users), new(orgs)); err != nil { + return fmt.Errorf("sync new models failed: %w", err) + } + + dialect := sess.Engine().Dialect().URI().DBType + var err error + switch dialect { + case schemas.MYSQL: + _, err = sess.Exec(`UPDATE users u JOIN orgs o ON o.name = u.login AND o.forge_id = u.forge_id SET u.org_id = o.id;`) + case schemas.POSTGRES: + _, err = sess.Exec(`UPDATE users u SET org_id = o.id FROM orgs o WHERE o.name = u.login AND o.forge_id = u.forge_id;`) + case schemas.SQLITE: + _, err = sess.Exec(`UPDATE users SET org_id = ( SELECT orgs.id FROM orgs WHERE orgs.name = users.login AND orgs.forge_id = users.forge_id ) WHERE users.login IN (SELECT orgs.name FROM orgs);`) + default: + err = fmt.Errorf("dialect '%s' not supported", dialect) + } + return err + }, +} diff --git a/server/store/datastore/migration/migration.go b/server/store/datastore/migration/migration.go index d12e21628..b4a1b8a79 100644 --- a/server/store/datastore/migration/migration.go +++ b/server/store/datastore/migration/migration.go @@ -46,6 +46,7 @@ var migrationTasks = []*xormigrate.Migration{ &addOrgAgents, &addCustomLabelsToAgent, &splitTrusted, + &correctPotentialCorruptOrgsUsersRelation, } var allBeans = []any{