refactor: prepare to decouple the "model migration" package and "models" package (#38533)

Migrations should never use model structs directly, because the model
structs can be different in different releases. e.g. if one migration uses
"User" model, it works in the early releases, then one day, when the
User model changes, the migration breaks because it will use the
new (incorrect) User model, it should only use the old User model.

The same to "modules/structs".

---------

Signed-off-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: delvh <dev.lh@web.de>
This commit is contained in:
wxiaoguang
2026-07-20 11:42:02 +08:00
committed by GitHub
parent 775e3bdb34
commit fff32e9469
378 changed files with 708 additions and 721 deletions
+16
View File
@@ -0,0 +1,16 @@
// Copyright 2018 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package v1_7
import "gitea.dev/modelmigration/base"
func AddMustChangePassword(x base.EngineMigration) error {
// User see models/user.go
type User struct {
ID int64 `xorm:"pk autoincr"`
MustChangePassword bool `xorm:"NOT NULL DEFAULT false"`
}
return x.Sync(new(User))
}
+15
View File
@@ -0,0 +1,15 @@
// Copyright 2018 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package v1_7
import "gitea.dev/modelmigration/base"
func AddApprovalWhitelistsToProtectedBranches(x base.EngineMigration) error {
type ProtectedBranch struct {
ApprovalsWhitelistUserIDs []int64 `xorm:"JSON TEXT"`
ApprovalsWhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
RequiredApprovals int64 `xorm:"NOT NULL DEFAULT 0"`
}
return x.Sync(new(ProtectedBranch))
}
+33
View File
@@ -0,0 +1,33 @@
// Copyright 2018 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package v1_7
import (
"gitea.dev/modelmigration/base"
"xorm.io/builder"
)
func ClearNonusedData(x base.EngineMigration) error {
condDelete := func(colName string) builder.Cond {
return builder.NotIn(colName, builder.Select("id").From("`user`"))
}
if _, err := x.Exec(builder.Delete(condDelete("uid")).From("team_user")); err != nil {
return err
}
if _, err := x.Exec(builder.Delete(condDelete("user_id")).From("collaboration")); err != nil {
return err
}
if _, err := x.Exec(builder.Delete(condDelete("user_id")).From("stopwatch")); err != nil {
return err
}
if _, err := x.Exec(builder.Delete(condDelete("owner_id")).From("gpg_key")); err != nil {
return err
}
return nil
}