mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-01-21 16:55:06 -05:00
376a2e19ea
Using SELECT `%s` FROM `%s` WHERE 0 = 1 to assert the existence of a column is simple but noisy: it shows errors in the migrations that are confusing for Forgejo admins because they are not actual errors. Use introspection instead, which is more complicated but leads to the same result. Add a test that ensures it works as expected, for all database types. Although the migration is run for all database types, it does not account for various scenarios and is never tested in the case a column does not exist. Refs: https://codeberg.org/forgejo/forgejo/issues/6583 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6591 Reviewed-by: Otto <otto@codeberg.org> Co-authored-by: Earl Warren <contact@earl-warren.org> Co-committed-by: Earl Warren <contact@earl-warren.org>
60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
// Copyright 2025 The Forgejo Authors.
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package v1_23 //nolint
|
|
|
|
import (
|
|
"code.gitea.io/gitea/models/migrations/base"
|
|
|
|
"xorm.io/xorm"
|
|
"xorm.io/xorm/schemas"
|
|
)
|
|
|
|
func GiteaLastDrop(x *xorm.Engine) error {
|
|
tables, err := x.DBMetas()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
sess := x.NewSession()
|
|
defer sess.Close()
|
|
|
|
for _, drop := range []struct {
|
|
table string
|
|
column string
|
|
}{
|
|
{"badge", "slug"},
|
|
{"oauth2_application", "skip_secondary_authorization"},
|
|
{"repository", "default_wiki_branch"},
|
|
{"repo_unit", "everyone_access_mode"},
|
|
{"protected_branch", "can_force_push"},
|
|
{"protected_branch", "enable_force_push_allowlist"},
|
|
{"protected_branch", "force_push_allowlist_user_i_ds"},
|
|
{"protected_branch", "force_push_allowlist_team_i_ds"},
|
|
{"protected_branch", "force_push_allowlist_deploy_keys"},
|
|
} {
|
|
var table *schemas.Table
|
|
found := false
|
|
|
|
for _, table = range tables {
|
|
if table.Name == drop.table {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
continue
|
|
}
|
|
|
|
if table.GetColumn(drop.column) == nil {
|
|
continue
|
|
}
|
|
|
|
if err := base.DropTableColumns(sess, drop.table, drop.column); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return sess.Commit()
|
|
}
|