refactor: clean up git repo and model migration packages (#38564)

enable the golangci depguard lint rule: deny "models" and its sub
packages in "modelmigration" package.
This commit is contained in:
wxiaoguang
2026-07-23 09:22:26 +08:00
committed by GitHub
parent fa64b4627a
commit 1acf93d854
43 changed files with 243 additions and 167 deletions
+6 -7
View File
@@ -11,19 +11,14 @@ import (
"regexp"
"strings"
"gitea.dev/models/db"
"gitea.dev/models/db" //nolint:depguard // allow to access db in migration
"gitea.dev/modules/log"
"gitea.dev/modules/setting"
"xorm.io/builder"
"xorm.io/xorm/schemas"
)
// 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".
// However, many the existing migrations already abuses "modules/structs" (search "gitea.dev/models/" in the migrations).
// TODO: need to fully decouple the migration package and models & structs package
type (
EngineMigration = db.EngineMigration
Session = db.Session
@@ -519,3 +514,7 @@ func ModifyColumn(x EngineMigration, tableName string, col *schemas.Column) erro
}
return nil
}
func Iterate[Bean any](ctx context.Context, cond builder.Cond, f func(ctx context.Context, bean *Bean) error) error {
return db.Iterate(ctx, cond, f)
}
-13
View File
@@ -1,13 +0,0 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package base
import (
repo_model "gitea.dev/models/repo"
"gitea.dev/modules/git/gitcmd"
)
func LocalCodeGitRepo(ownerName, repoName string) gitcmd.RepositoryFacade {
return repo_model.CodeRepoByName(ownerName, repoName)
}
+25
View File
@@ -0,0 +1,25 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package base
import (
"gitea.dev/models/db" //nolint:depguard // allow to access db in migration
"gitea.dev/modules/git/gitrepo"
)
// HINT: MIGRATION-STRUCT-FROZEN: the structs used in migrations should not be affected by the changes happen in the future,
// because the details 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 existing migration will break because it will use the new (incorrect) User model,
// it should only use the old User model.
//
// Related: "models", "modules/structs", git repo directory layout on the disk, etc.
//
// If changes happen, the old migrations need to use a snapshot struct/function (copy the code and freeze)
type ResourceIndex = db.ResourceIndex
func LocalCodeGitRepo(ownerName, repoName string) gitrepo.RepositoryFacade {
return gitrepo.CodeRepoByName(ownerName, repoName)
}