chore(db): introduce db.Session and db.EngineMigration interfaces (#37746)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: wxiaoguang <2114189+wxiaoguang@users.noreply.github.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Copilot
2026-05-18 03:56:39 +08:00
committed by GitHub
parent d9149d8a0a
commit 94e3482d1a
300 changed files with 745 additions and 864 deletions

View File

@@ -21,7 +21,6 @@ import (
"code.gitea.io/gitea/services/doctor"
"github.com/urfave/cli/v3"
"xorm.io/xorm"
)
func newDoctorCommand() *cli.Command {
@@ -132,7 +131,7 @@ func runRecreateTable(ctx context.Context, cmd *cli.Command) error {
}
recreateTables := migrate_base.RecreateTables(beans...)
return db.InitEngineWithMigration(context.Background(), func(ctx context.Context, x *xorm.Engine) error {
return db.InitEngineWithMigration(context.Background(), func(ctx context.Context, x db.EngineMigration) error {
if err := migrations.EnsureUpToDate(ctx, x); err != nil {
return err
}

View File

@@ -242,7 +242,7 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, int64, err
if opts.Page < 10 { // TODO: why it's 10 but other values? It's an experience value.
sess := db.GetEngine(ctx).Where(cond)
sess = db.SetSessionPagination(sess, &opts)
db.SetSessionPagination(sess, &opts)
if opts.DontCount {
err = sess.Desc("`action`.created_unix").Find(&actions)
@@ -255,7 +255,7 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, int64, err
} else {
// First, only query which IDs are necessary, and only then query all actions to speed up the overall query
sess := db.GetEngine(ctx).Where(cond).Select("`action`.id")
sess = db.SetSessionPagination(sess, &opts)
db.SetSessionPagination(sess, &opts)
actionIDs := make([]int64, 0, opts.PageSize)
if err := sess.Table("action").Desc("`action`.created_unix").Find(&actionIDs); err != nil {

View File

@@ -17,7 +17,6 @@ import (
"code.gitea.io/gitea/modules/gitrepo"
"xorm.io/builder"
"xorm.io/xorm"
)
// ActivityAuthorData represents statistical git commit count data
@@ -248,7 +247,7 @@ func (stats *ActivityStats) FillPullRequests(ctx context.Context, repoID int64,
return nil
}
func pullRequestsForActivityStatement(ctx context.Context, repoID int64, fromTime time.Time, merged bool) *xorm.Session {
func pullRequestsForActivityStatement(ctx context.Context, repoID int64, fromTime time.Time, merged bool) db.Session {
sess := db.GetEngine(ctx).Where("pull_request.base_repo_id=?", repoID).
Join("INNER", "issue", "pull_request.issue_id = issue.id")
@@ -324,7 +323,7 @@ func (stats *ActivityStats) FillUnresolvedIssues(ctx context.Context, repoID int
return sess.Find(&stats.UnresolvedIssues)
}
func newlyCreatedIssues(ctx context.Context, repoID int64, fromTime time.Time) *xorm.Session {
func newlyCreatedIssues(ctx context.Context, repoID int64, fromTime time.Time) db.Session {
sess := db.GetEngine(ctx).Where("issue.repo_id = ?", repoID).
And("issue.is_pull = ?", false). // Retain the is_pull check to exclude pull requests
And("issue.created_unix >= ?", fromTime.Unix()) // Include all issues created after fromTime
@@ -332,7 +331,7 @@ func newlyCreatedIssues(ctx context.Context, repoID int64, fromTime time.Time) *
return sess
}
func activeIssues(ctx context.Context, repoID int64, fromTime time.Time) *xorm.Session {
func activeIssues(ctx context.Context, repoID int64, fromTime time.Time) db.Session {
sess := db.GetEngine(ctx).Where("issue.repo_id = ?", repoID).
And("issue.is_pull = ?", false).
And(builder.Or(
@@ -343,7 +342,7 @@ func activeIssues(ctx context.Context, repoID int64, fromTime time.Time) *xorm.S
return sess
}
func issuesForActivityStatement(ctx context.Context, repoID int64, fromTime time.Time, closed, unresolved bool) *xorm.Session {
func issuesForActivityStatement(ctx context.Context, repoID int64, fromTime time.Time, closed, unresolved bool) db.Session {
sess := db.GetEngine(ctx).Where("issue.repo_id = ?", repoID).
And("issue.is_closed = ?", closed)
@@ -385,7 +384,7 @@ func (stats *ActivityStats) FillReleases(ctx context.Context, repoID int64, from
return nil
}
func releasesForActivityStatement(ctx context.Context, repoID int64, fromTime time.Time) *xorm.Session {
func releasesForActivityStatement(ctx context.Context, repoID int64, fromTime time.Time) db.Session {
return db.GetEngine(ctx).Where("`release`.repo_id = ?", repoID).
And("`release`.is_draft = ?", false).
And("`release`.created_unix >= ?", fromTime.Unix())

View File

@@ -12,7 +12,6 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
"xorm.io/xorm/schemas"
)
@@ -27,7 +26,7 @@ type CheckCollationsResult struct {
InconsistentCollationColumns []string
}
func findAvailableCollationsMySQL(x *xorm.Engine) (ret container.Set[string], err error) {
func findAvailableCollationsMySQL(x EngineMigration) (ret container.Set[string], err error) {
var res []struct {
Collation string
}
@@ -41,7 +40,7 @@ func findAvailableCollationsMySQL(x *xorm.Engine) (ret container.Set[string], er
return ret, nil
}
func findAvailableCollationsMSSQL(x *xorm.Engine) (ret container.Set[string], err error) {
func findAvailableCollationsMSSQL(x EngineMigration) (ret container.Set[string], err error) {
var res []struct {
Name string
}
@@ -55,7 +54,7 @@ func findAvailableCollationsMSSQL(x *xorm.Engine) (ret container.Set[string], er
return ret, nil
}
func CheckCollations(x *xorm.Engine) (*CheckCollationsResult, error) {
func CheckCollations(x EngineMigration) (*CheckCollationsResult, error) {
dbTables, err := x.DBMetas()
if err != nil {
return nil, err
@@ -143,7 +142,7 @@ func CheckCollationsDefaultEngine() (*CheckCollationsResult, error) {
return CheckCollations(xormEngine)
}
func alterDatabaseCollation(x *xorm.Engine, collation string) error {
func alterDatabaseCollation(x EngineMigration, collation string) error {
if x.Dialect().URI().DBType == schemas.MYSQL {
_, err := x.Exec("ALTER DATABASE CHARACTER SET utf8mb4 COLLATE " + collation)
return err
@@ -156,7 +155,7 @@ func alterDatabaseCollation(x *xorm.Engine, collation string) error {
}
// preprocessDatabaseCollation checks database & table column collation, and alter the database collation if needed
func preprocessDatabaseCollation(x *xorm.Engine) {
func preprocessDatabaseCollation(x EngineMigration) {
r, err := CheckCollations(x)
if err != nil {
log.Error("Failed to check database collation: %v", err)

View File

@@ -16,6 +16,10 @@ import (
_ "github.com/microsoft/go-mssqldb" // Needed for the MSSQL driver
"xorm.io/xorm"
"xorm.io/xorm/core"
"xorm.io/xorm/dialects"
"xorm.io/xorm/names"
"xorm.io/xorm/schemas"
)
var (
@@ -33,6 +37,7 @@ type Engine interface {
Truncate(...any) (int64, error)
Exec(...any) (sql.Result, error)
Find(any, ...any) error
FindAndCount(any, ...any) (int64, error)
Get(beans ...any) (bool, error)
ID(any) *xorm.Session
In(string, ...any) *xorm.Session
@@ -61,9 +66,41 @@ type Engine interface {
IsTableExist(tableNameOrBean any) (bool, error)
}
// Session represents a xorm session interface, used as an abstraction over *xorm.Session.
type Session interface {
Engine
And(query any, args ...any) *xorm.Session
Begin() error
Close() error
Commit() error
IsInTx() bool
Rollback() error
Engine() *xorm.Engine
}
// EngineMigration is a xorm engine interface used for migrations.
// It extends Engine with additional methods that are only available on the engine (not on the session)
// and are needed by the migration packages.
type EngineMigration interface {
Engine
Close() error
DB() *core.DB
DBMetas() ([]*schemas.Table, error)
Dialect() dialects.Dialect
DropTables(beans ...any) error
NewSession() *xorm.Session
QueryInterface(sqlOrArgs ...any) ([]map[string]any, error)
SetMapper(mapper names.Mapper)
SyncWithOptions(opts xorm.SyncOptions, beans ...any) (*xorm.SyncResult, error)
TableInfo(bean any) (*schemas.Table, error)
TableName(bean any, includeSchema ...bool) string
}
var (
_ Engine = (*xorm.Engine)(nil)
_ Engine = (*xorm.Session)(nil)
_ Engine = (*xorm.Engine)(nil)
_ Engine = (*xorm.Session)(nil)
_ Session = (*xorm.Session)(nil)
_ EngineMigration = (*xorm.Engine)(nil)
)
// RegisterModel registers model, if initFuncs provided, it will be invoked after data model sync

View File

@@ -92,7 +92,7 @@ func UnsetDefaultEngine() {
// When called from the "doctor" command, the migration function is a version check
// that prevents the doctor from fixing anything in the database if the migration level
// is different from the expected value.
func InitEngineWithMigration(ctx context.Context, migrateFunc func(context.Context, *xorm.Engine) error) (err error) {
func InitEngineWithMigration(ctx context.Context, migrateFunc func(context.Context, EngineMigration) error) (err error) {
if err = InitEngine(ctx); err != nil {
return err
}

View File

@@ -9,7 +9,6 @@ import (
"code.gitea.io/gitea/modules/setting"
"xorm.io/builder"
"xorm.io/xorm"
)
const (
@@ -25,7 +24,7 @@ type Paginator interface {
}
// SetSessionPagination sets pagination for a database session
func SetSessionPagination(sess Engine, p Paginator) *xorm.Session {
func SetSessionPagination(sess Engine, p Paginator) Session {
skip, take := p.GetSkipTake()
return sess.Limit(take, skip)

View File

@@ -101,7 +101,7 @@ func (opts FindBranchOptions) ToOrders() string {
func FindBranchNames(ctx context.Context, opts FindBranchOptions) ([]string, error) {
sess := db.GetEngine(ctx).Select("name").Where(opts.ToConds())
if opts.PageSize > 0 && !opts.IsListAll() {
sess = db.SetSessionPagination(sess, &opts.ListOptions)
db.SetSessionPagination(sess, &opts.ListOptions)
}
var branches []string

View File

@@ -25,7 +25,6 @@ import (
"code.gitea.io/gitea/modules/translation"
"xorm.io/builder"
"xorm.io/xorm"
)
// CommitStatus holds a single Status of a single Commit
@@ -329,7 +328,7 @@ type CommitStatusIndex struct {
MaxIndex int64 `xorm:"index"`
}
func makeRepoCommitQuery(ctx context.Context, repoID int64, sha string) *xorm.Session {
func makeRepoCommitQuery(ctx context.Context, repoID int64, sha string) db.Session {
return db.GetEngine(ctx).Table(&CommitStatus{}).
Where("repo_id = ?", repoID).And("sha = ?", sha)
}
@@ -337,12 +336,10 @@ func makeRepoCommitQuery(ctx context.Context, repoID int64, sha string) *xorm.Se
// GetLatestCommitStatus returns all statuses with a unique context for a given commit.
func GetLatestCommitStatus(ctx context.Context, repoID int64, sha string, listOptions db.ListOptions) ([]*CommitStatus, error) {
indices := make([]int64, 0, 10)
sess := makeRepoCommitQuery(ctx, repoID, sha).
Select("max( `index` ) as `index`").
GroupBy("context_hash").
OrderBy("max( `index` ) desc")
sess := makeRepoCommitQuery(ctx, repoID, sha)
sess.Select("max( `index` ) as `index`").GroupBy("context_hash").OrderBy("max( `index` ) desc")
if !listOptions.IsListAll() {
sess = db.SetSessionPagination(sess, &listOptions)
db.SetSessionPagination(sess, &listOptions)
}
if err := sess.Find(&indices); err != nil {
return nil, err
@@ -372,7 +369,7 @@ func GetLatestCommitStatusForPairs(ctx context.Context, repoSHAs []RepoSHA) (map
results := make([]result, 0, len(repoSHAs))
getBase := func() *xorm.Session {
getBase := func() db.Session {
return db.GetEngine(ctx).Table(&CommitStatus{})
}
@@ -425,7 +422,7 @@ func GetLatestCommitStatusForRepoCommitIDs(ctx context.Context, repoID int64, co
SHA string
}
getBase := func() *xorm.Session {
getBase := func() db.Session {
return db.GetEngine(ctx).Table(&CommitStatus{}).Where("repo_id = ?", repoID)
}
results := make([]result, 0, len(commitIDs))

View File

@@ -1124,7 +1124,7 @@ func FindComments(ctx context.Context, opts *FindCommentsOptions) (CommentList,
}
if opts.Page > 0 {
sess = db.SetSessionPagination(sess, opts)
db.SetSessionPagination(sess, opts)
}
// WARNING: If you change this order you will need to fix createCodeComment

View File

@@ -682,7 +682,7 @@ func (issue *Issue) BlockedByDependencies(ctx context.Context, opts db.ListOptio
// sort by repo id then created date, with the issues of the same repo at the beginning of the list
OrderBy("CASE WHEN issue.repo_id = ? THEN 0 ELSE issue.repo_id END, issue.created_unix DESC", issue.RepoID)
if opts.Page > 0 {
sess = db.SetSessionPagination(sess, &opts)
db.SetSessionPagination(sess, &opts)
}
total, err = sess.FindAndCount(&issueDeps)

View File

@@ -19,7 +19,6 @@ import (
"code.gitea.io/gitea/modules/util"
"xorm.io/builder"
"xorm.io/xorm"
)
const ScopeSortPrefix = "scope-"
@@ -71,7 +70,7 @@ func (o *IssuesOptions) Copy(edit ...func(options *IssuesOptions)) *IssuesOption
// applySorts sort an issues-related session based on the provided
// sortType string
func applySorts(sess *xorm.Session, sortType string, priorityRepoID int64) {
func applySorts(sess db.Session, sortType string, priorityRepoID int64) {
// Since this sortType is dynamically created, it has to be treated specially.
if after, ok := strings.CutPrefix(sortType, ScopeSortPrefix); ok {
scope := after
@@ -129,7 +128,7 @@ func applySorts(sess *xorm.Session, sortType string, priorityRepoID int64) {
}
}
func applyLimit(sess *xorm.Session, opts *IssuesOptions) {
func applyLimit(sess db.Session, opts *IssuesOptions) {
if opts.Paginator == nil || opts.Paginator.IsListAll() {
return
}
@@ -141,7 +140,7 @@ func applyLimit(sess *xorm.Session, opts *IssuesOptions) {
sess.Limit(opts.Paginator.PageSize, start)
}
func applyLabelsCondition(sess *xorm.Session, opts *IssuesOptions) {
func applyLabelsCondition(sess db.Session, opts *IssuesOptions) {
if len(opts.LabelIDs) > 0 {
if opts.LabelIDs[0] == 0 {
sess.Where("issue.id NOT IN (SELECT issue_id FROM issue_label)")
@@ -182,7 +181,7 @@ func applyLabelsCondition(sess *xorm.Session, opts *IssuesOptions) {
}
}
func applyMilestoneCondition(sess *xorm.Session, opts *IssuesOptions) {
func applyMilestoneCondition(sess db.Session, opts *IssuesOptions) {
if len(opts.MilestoneIDs) == 1 && opts.MilestoneIDs[0] == db.NoConditionID {
sess.And("issue.milestone_id = 0")
} else if len(opts.MilestoneIDs) > 0 {
@@ -197,7 +196,7 @@ func applyMilestoneCondition(sess *xorm.Session, opts *IssuesOptions) {
}
}
func applyProjectCondition(sess *xorm.Session, opts *IssuesOptions) {
func applyProjectCondition(sess db.Session, opts *IssuesOptions) {
projectIDs := util.SliceRemoveAll(opts.ProjectIDs, 0)
if len(projectIDs) == 1 && projectIDs[0] == db.NoConditionID { // show those that are in no project
sess.And(builder.NotIn("issue.id", builder.Select("issue_id").From("project_issue")))
@@ -211,7 +210,7 @@ func applyProjectCondition(sess *xorm.Session, opts *IssuesOptions) {
// do not need to apply any condition
}
func applyRepoConditions(sess *xorm.Session, opts *IssuesOptions) {
func applyRepoConditions(sess db.Session, opts *IssuesOptions) {
if len(opts.RepoIDs) == 1 {
opts.RepoCond = builder.Eq{"issue.repo_id": opts.RepoIDs[0]}
} else if len(opts.RepoIDs) > 1 {
@@ -228,7 +227,7 @@ func applyRepoConditions(sess *xorm.Session, opts *IssuesOptions) {
}
}
func applyConditions(sess *xorm.Session, opts *IssuesOptions) {
func applyConditions(sess db.Session, opts *IssuesOptions) {
if len(opts.IssueIDs) > 0 {
sess.In("issue.id", opts.IssueIDs)
}
@@ -362,7 +361,7 @@ func issuePullAccessibleRepoCond(repoIDstr string, userID int64, owner *user_mod
return cond
}
func applyAssigneeCondition(sess *xorm.Session, assigneeID string) {
func applyAssigneeCondition(sess db.Session, assigneeID string) {
// old logic: 0 is also treated as "not filtering assignee", because the "assignee" was read as FormInt64
if assigneeID == "(none)" {
sess.Where("issue.id NOT IN (SELECT issue_id FROM issue_assignees)")
@@ -374,7 +373,7 @@ func applyAssigneeCondition(sess *xorm.Session, assigneeID string) {
}
}
func applyPosterCondition(sess *xorm.Session, posterID string) {
func applyPosterCondition(sess db.Session, posterID string) {
// Actually every issue has a poster.
// The "(none)" is for internal usage only: when doer tries to search non-existing user as poster, use "(none)" to return empty result.
if posterID == "(none)" {
@@ -384,13 +383,13 @@ func applyPosterCondition(sess *xorm.Session, posterID string) {
}
}
func applyMentionedCondition(sess *xorm.Session, mentionedID int64) {
func applyMentionedCondition(sess db.Session, mentionedID int64) {
sess.Join("INNER", "issue_user", "issue.id = issue_user.issue_id").
And("issue_user.is_mentioned = ?", true).
And("issue_user.uid = ?", mentionedID)
}
func applyReviewRequestedCondition(sess *xorm.Session, reviewRequestedID int64) {
func applyReviewRequestedCondition(sess db.Session, reviewRequestedID int64) {
existInTeamQuery := builder.Select("team_user.team_id").
From("team_user").
Where(builder.Eq{"team_user.uid": reviewRequestedID})
@@ -415,7 +414,7 @@ func applyReviewRequestedCondition(sess *xorm.Session, reviewRequestedID int64)
And(builder.In("issue.id", subQuery))
}
func applyReviewedCondition(sess *xorm.Session, reviewedID int64) {
func applyReviewedCondition(sess db.Session, reviewedID int64) {
// Query for pull requests where you are a reviewer or commenter, excluding
// any pull requests already returned by the review requested filter.
notPoster := builder.Neq{"issue.poster_id": reviewedID}
@@ -445,7 +444,7 @@ func applyReviewedCondition(sess *xorm.Session, reviewedID int64) {
sess.And(notPoster, builder.Or(reviewed, commented))
}
func applySubscribedCondition(sess *xorm.Session, subscriberID int64) {
func applySubscribedCondition(sess db.Session, subscriberID int64) {
sess.And(
builder.
NotIn("issue.id",

View File

@@ -10,7 +10,6 @@ import (
"code.gitea.io/gitea/models/db"
"xorm.io/builder"
"xorm.io/xorm"
)
// IssueStats represents issue statistic information.
@@ -129,7 +128,7 @@ func getIssueStatsChunk(ctx context.Context, opts *IssuesOptions, issueIDs []int
return stats, err
}
func applyIssuesOptions(sess *xorm.Session, opts *IssuesOptions, issueIDs []int64) *xorm.Session {
func applyIssuesOptions(sess db.Session, opts *IssuesOptions, issueIDs []int64) db.Session {
if len(opts.RepoIDs) > 1 {
sess.In("issue.repo_id", opts.RepoIDs)
} else if len(opts.RepoIDs) == 1 {

View File

@@ -106,7 +106,7 @@ func GetIssueWatchers(ctx context.Context, issueID int64, listOptions db.ListOpt
Join("INNER", "`user`", "`user`.id = `issue_watch`.user_id")
if listOptions.Page > 0 {
sess = db.SetSessionPagination(sess, &listOptions)
db.SetSessionPagination(sess, &listOptions)
watches := make([]*IssueWatch, 0, listOptions.PageSize)
return watches, sess.Find(&watches)
}

View File

@@ -396,7 +396,7 @@ func GetLabelsByRepoID(ctx context.Context, repoID int64, sortType string, listO
}
if listOptions.Page > 0 {
sess = db.SetSessionPagination(sess, &listOptions)
db.SetSessionPagination(sess, &listOptions)
}
return labels, sess.Find(&labels)
@@ -471,7 +471,7 @@ func GetLabelsByOrgID(ctx context.Context, orgID int64, sortType string, listOpt
}
if listOptions.Page > 0 {
sess = db.SetSessionPagination(sess, &listOptions)
db.SetSessionPagination(sess, &listOptions)
}
return labels, sess.Find(&labels)

View File

@@ -18,7 +18,6 @@ import (
"code.gitea.io/gitea/modules/util"
"xorm.io/builder"
"xorm.io/xorm"
)
// PullRequestsOptions holds the options for PRs
@@ -32,7 +31,7 @@ type PullRequestsOptions struct {
BaseBranch string
}
func listPullRequestStatement(ctx context.Context, baseRepoID int64, opts *PullRequestsOptions) *xorm.Session {
func listPullRequestStatement(ctx context.Context, baseRepoID int64, opts *PullRequestsOptions) db.Session {
sess := db.GetEngine(ctx).Where("pull_request.base_repo_id=?", baseRepoID)
if opts.BaseBranch != "" {

View File

@@ -165,7 +165,7 @@ func FindReactions(ctx context.Context, opts FindReactionsOptions) (ReactionList
In("reaction.`type`", setting.UI.Reactions).
Asc("reaction.issue_id", "reaction.comment_id", "reaction.created_unix", "reaction.id")
if opts.Page > 0 {
sess = db.SetSessionPagination(sess, &opts)
db.SetSessionPagination(sess, &opts)
reactions := make([]*Reaction, 0, opts.PageSize)
count, err := sess.FindAndCount(&reactions)

View File

@@ -121,7 +121,7 @@ func FindReviews(ctx context.Context, opts FindReviewOptions) (ReviewList, error
reviews := make([]*Review, 0, 10)
sess := db.GetEngine(ctx).Where(opts.toCond())
if opts.Page > 0 && !opts.IsListAll() {
sess = db.SetSessionPagination(sess, &opts)
db.SetSessionPagination(sess, &opts)
}
return reviews, sess.
Asc("created_unix").
@@ -135,7 +135,7 @@ func FindLatestReviews(ctx context.Context, opts FindReviewOptions) (ReviewList,
cond := opts.toCond()
sess := db.GetEngine(ctx).Where(cond)
if opts.Page > 0 {
sess = db.SetSessionPagination(sess, &opts)
db.SetSessionPagination(sess, &opts)
}
sess.In("id", builder.

View File

@@ -77,7 +77,7 @@ func GetUserStopwatches(ctx context.Context, userID int64, listOptions db.ListOp
sws := make([]*Stopwatch, 0, 8)
sess := db.GetEngine(ctx).Where("stopwatch.user_id = ?", userID)
if listOptions.Page > 0 {
sess = db.SetSessionPagination(sess, &listOptions)
db.SetSessionPagination(sess, &listOptions)
}
err := sess.Find(&sws)

View File

@@ -16,7 +16,6 @@ import (
"code.gitea.io/gitea/modules/util"
"xorm.io/builder"
"xorm.io/xorm"
)
// TrackedTime represents a time that was spent for a specific issue.
@@ -140,7 +139,7 @@ func (opts *FindTrackedTimesOptions) toSession(e db.Engine) db.Engine {
sess = sess.Where(opts.ToConds())
if opts.Page > 0 {
sess = db.SetSessionPagination(sess, opts)
db.SetSessionPagination(sess, opts)
}
return sess
@@ -344,7 +343,7 @@ func GetIssueTotalTrackedTime(ctx context.Context, opts *IssuesOptions, isClosed
}
func getIssueTotalTrackedTimeChunk(ctx context.Context, opts *IssuesOptions, isClosed optional.Option[bool], issueIDs []int64) (int64, error) {
sumSession := func(opts *IssuesOptions, issueIDs []int64) *xorm.Session {
sumSession := func(opts *IssuesOptions, issueIDs []int64) db.Session {
sess := db.GetEngine(ctx).
Table("tracked_time").
Where("tracked_time.deleted = ?", false).
@@ -359,7 +358,7 @@ func getIssueTotalTrackedTimeChunk(ctx context.Context, opts *IssuesOptions, isC
session := sumSession(opts, issueIDs)
if isClosed.Has() {
session = session.And("issue.is_closed = ?", isClosed.Value())
session.And("issue.is_closed = ?", isClosed.Value())
}
return session.SumInt(new(trackedTime), "tracked_time.time")
}

View File

@@ -11,17 +11,17 @@ import (
"regexp"
"strings"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
"xorm.io/xorm/schemas"
)
// RecreateTables will recreate the tables for the provided beans using the newly provided bean definition and move all data to that new table
// WARNING: YOU MUST PROVIDE THE FULL BEAN DEFINITION
func RecreateTables(beans ...any) func(*xorm.Engine) error {
return func(x *xorm.Engine) error {
func RecreateTables(beans ...any) func(db.EngineMigration) error {
return func(x db.EngineMigration) error {
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
@@ -41,7 +41,7 @@ func RecreateTables(beans ...any) func(*xorm.Engine) error {
// RecreateTable will recreate the table using the newly provided bean definition and move all data to that new table
// WARNING: YOU MUST PROVIDE THE FULL BEAN DEFINITION
// WARNING: YOU MUST COMMIT THE SESSION AT THE END
func RecreateTable(sess *xorm.Session, bean any) error {
func RecreateTable(sess db.Session, bean any) error {
// TODO: This will not work if there are foreign keys
tableName := sess.Engine().TableName(bean)
@@ -304,7 +304,7 @@ func RecreateTable(sess *xorm.Session, bean any) error {
}
// WARNING: YOU MUST COMMIT THE SESSION AT THE END
func DropTableColumns(sess *xorm.Session, tableName string, columnNames ...string) (err error) {
func DropTableColumns(sess db.Session, tableName string, columnNames ...string) (err error) {
if tableName == "" || len(columnNames) == 0 {
return nil
}
@@ -474,7 +474,7 @@ func DropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
}
// ModifyColumn will modify column's type or other property. SQLITE is not supported
func ModifyColumn(x *xorm.Engine, tableName string, col *schemas.Column) error {
func ModifyColumn(x db.EngineMigration, tableName string, col *schemas.Column) error {
var indexes map[string]*schemas.Index
var err error
// MSSQL have to remove index at first, otherwise alter column will fail

View File

@@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/migrations/v1_10"
"code.gitea.io/gitea/models/migrations/v1_11"
"code.gitea.io/gitea/models/migrations/v1_12"
@@ -35,7 +36,6 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
"xorm.io/xorm/names"
)
@@ -44,23 +44,23 @@ const minDBVersion = 70 // Gitea 1.5.3
type migration struct {
idNumber int64 // DB version is "the last migration's idNumber" + 1
description string
migrate func(context.Context, *xorm.Engine) error
migrate func(context.Context, db.EngineMigration) error
}
// newMigration creates a new migration
func newMigration[T func(*xorm.Engine) error | func(context.Context, *xorm.Engine) error](idNumber int64, desc string, fn T) *migration {
func newMigration[T func(db.EngineMigration) error | func(context.Context, db.EngineMigration) error](idNumber int64, desc string, fn T) *migration {
m := &migration{idNumber: idNumber, description: desc}
var ok bool
if m.migrate, ok = any(fn).(func(context.Context, *xorm.Engine) error); !ok {
m.migrate = func(ctx context.Context, x *xorm.Engine) error {
return any(fn).(func(*xorm.Engine) error)(x)
if m.migrate, ok = any(fn).(func(context.Context, db.EngineMigration) error); !ok {
m.migrate = func(ctx context.Context, x db.EngineMigration) error {
return any(fn).(func(db.EngineMigration) error)(x)
}
}
return m
}
// Migrate executes the migration
func (m *migration) Migrate(ctx context.Context, x *xorm.Engine) error {
func (m *migration) Migrate(ctx context.Context, x db.EngineMigration) error {
return m.migrate(ctx, x)
}
@@ -71,7 +71,7 @@ type Version struct {
}
// Use noopMigration when there is a migration that has been no-oped
var noopMigration = func(_ *xorm.Engine) error { return nil }
var noopMigration = func(_ db.EngineMigration) error { return nil }
var preparedMigrations []*migration
@@ -417,7 +417,7 @@ func prepareMigrationTasks() []*migration {
}
// GetCurrentDBVersion returns the current db version
func GetCurrentDBVersion(x *xorm.Engine) (int64, error) {
func GetCurrentDBVersion(x db.EngineMigration) (int64, error) {
if err := x.Sync(new(Version)); err != nil {
return -1, fmt.Errorf("sync: %w", err)
}
@@ -450,7 +450,7 @@ func ExpectedDBVersion() int64 {
}
// EnsureUpToDate will check if the db is at the correct version
func EnsureUpToDate(ctx context.Context, x *xorm.Engine) error {
func EnsureUpToDate(ctx context.Context, x db.EngineMigration) error {
currentDB, err := GetCurrentDBVersion(x)
if err != nil {
return err
@@ -482,7 +482,7 @@ func migrationIDNumberToDBVersion(idNumber int64) int64 {
}
// Migrate database to current version
func Migrate(ctx context.Context, x *xorm.Engine) error {
func Migrate(ctx context.Context, x db.EngineMigration) error {
migrations := prepareMigrationTasks()
maxDBVer := calcDBVersion(migrations)
@@ -501,7 +501,7 @@ func Migrate(ctx context.Context, x *xorm.Engine) error {
// XORM model framework will create all tables when initializing.
currentVersion.ID = 0
currentVersion.Version = maxDBVer
if _, err = x.InsertOne(currentVersion); err != nil {
if _, err = x.Insert(currentVersion); err != nil {
return fmt.Errorf("insert: %w", err)
}
}

View File

@@ -16,7 +16,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"xorm.io/xorm"
"xorm.io/xorm/schemas"
)
@@ -24,7 +23,7 @@ import (
// Provide models to be sync'd with the database - in particular any models you expect fixtures to be loaded from.
//
// fixtures in `models/migrations/fixtures/<TestName>` will be loaded automatically
func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (*xorm.Engine, func()) {
func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (db.EngineMigration, func()) {
t.Helper()
ourSkip := 2
ourSkip += skip
@@ -89,7 +88,7 @@ func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (*xorm.Engine, fu
return x, deferFn
}
func LoadTableSchemasMap(t *testing.T, x *xorm.Engine) map[string]*schemas.Table {
func LoadTableSchemasMap(t *testing.T, x db.EngineMigration) map[string]*schemas.Table {
tables, err := x.DBMetas()
require.NoError(t, err)
tableMap := make(map[string]*schemas.Table)

View File

@@ -8,10 +8,10 @@ import (
"strings"
"time"
"xorm.io/xorm"
"code.gitea.io/gitea/models/db"
)
func UpdateMigrationServiceTypes(x *xorm.Engine) error {
func UpdateMigrationServiceTypes(x db.EngineMigration) error {
type Repository struct {
ID int64
OriginalServiceType int `xorm:"index default(0)"`

View File

@@ -3,11 +3,9 @@
package v1_10
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func ChangeSomeColumnsLengthOfExternalLoginUser(x *xorm.Engine) error {
func ChangeSomeColumnsLengthOfExternalLoginUser(x db.EngineMigration) error {
type ExternalLoginUser struct {
AccessToken string `xorm:"TEXT"`
AccessTokenSecret string `xorm:"TEXT"`

View File

@@ -7,14 +7,14 @@ import (
"crypto/sha1"
"fmt"
"xorm.io/xorm"
"code.gitea.io/gitea/models/db"
)
func hashContext(context string) string {
return fmt.Sprintf("%x", sha1.Sum([]byte(context)))
}
func AddCommitStatusContext(x *xorm.Engine) error {
func AddCommitStatusContext(x db.EngineMigration) error {
type CommitStatus struct {
ID int64 `xorm:"pk autoincr"`
ContextHash string `xorm:"char(40) index"`

View File

@@ -3,9 +3,9 @@
package v1_10
import "xorm.io/xorm"
import "code.gitea.io/gitea/models/db"
func AddOriginalMigrationInfo(x *xorm.Engine) error {
func AddOriginalMigrationInfo(x db.EngineMigration) error {
// Issue see models/issue.go
type Issue struct {
OriginalAuthor string

View File

@@ -3,9 +3,9 @@
package v1_10
import "xorm.io/xorm"
import "code.gitea.io/gitea/models/db"
func ChangeSomeColumnsLengthOfRepo(x *xorm.Engine) error {
func ChangeSomeColumnsLengthOfRepo(x db.EngineMigration) error {
type Repository struct {
ID int64 `xorm:"pk autoincr"`
Description string `xorm:"TEXT"`

View File

@@ -3,9 +3,9 @@
package v1_10
import "xorm.io/xorm"
import "code.gitea.io/gitea/models/db"
func AddIndexOnRepositoryAndComment(x *xorm.Engine) error {
func AddIndexOnRepositoryAndComment(x db.EngineMigration) error {
type Repository struct {
ID int64 `xorm:"pk autoincr"`
OwnerID int64 `xorm:"index"`

View File

@@ -4,11 +4,12 @@
package v1_10
import (
"code.gitea.io/gitea/models/db"
"xorm.io/builder"
"xorm.io/xorm"
)
func RemoveLingeringIndexStatus(x *xorm.Engine) error {
func RemoveLingeringIndexStatus(x db.EngineMigration) error {
_, err := x.Exec(builder.Delete(builder.NotIn("`repo_id`", builder.Select("`id`").From("`repository`"))).From("`repo_indexer_status`"))
return err
}

View File

@@ -3,9 +3,9 @@
package v1_10
import "xorm.io/xorm"
import "code.gitea.io/gitea/models/db"
func AddEmailNotificationEnabledToUser(x *xorm.Engine) error {
func AddEmailNotificationEnabledToUser(x db.EngineMigration) error {
// User see models/user.go
type User struct {
EmailNotificationsPreference string `xorm:"VARCHAR(20) NOT NULL DEFAULT 'enabled'"`

View File

@@ -3,9 +3,9 @@
package v1_10
import "xorm.io/xorm"
import "code.gitea.io/gitea/models/db"
func AddStatusCheckColumnsForProtectedBranches(x *xorm.Engine) error {
func AddStatusCheckColumnsForProtectedBranches(x db.EngineMigration) error {
type ProtectedBranch struct {
EnableStatusCheck bool `xorm:"NOT NULL DEFAULT false"`
StatusCheckContexts []string `xorm:"JSON TEXT"`

View File

@@ -3,9 +3,9 @@
package v1_10
import "xorm.io/xorm"
import "code.gitea.io/gitea/models/db"
func AddCrossReferenceColumns(x *xorm.Engine) error {
func AddCrossReferenceColumns(x db.EngineMigration) error {
// Comment see models/comment.go
type Comment struct {
RefRepoID int64 `xorm:"index"`

View File

@@ -6,13 +6,12 @@ package v1_10
import (
"path/filepath"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"xorm.io/xorm"
)
func DeleteOrphanedAttachments(x *xorm.Engine) error {
func DeleteOrphanedAttachments(x db.EngineMigration) error {
type Attachment struct {
ID int64 `xorm:"pk autoincr"`
UUID string `xorm:"uuid UNIQUE"`

View File

@@ -3,9 +3,9 @@
package v1_10
import "xorm.io/xorm"
import "code.gitea.io/gitea/models/db"
func AddRepoAdminChangeTeamAccessColumnForUser(x *xorm.Engine) error {
func AddRepoAdminChangeTeamAccessColumnForUser(x db.EngineMigration) error {
type User struct {
RepoAdminChangeTeamAccess bool `xorm:"NOT NULL DEFAULT false"`
}

View File

@@ -3,9 +3,9 @@
package v1_10
import "xorm.io/xorm"
import "code.gitea.io/gitea/models/db"
func AddOriginalAuthorOnMigratedReleases(x *xorm.Engine) error {
func AddOriginalAuthorOnMigratedReleases(x db.EngineMigration) error {
type Release struct {
ID int64
OriginalAuthor string

View File

@@ -4,12 +4,11 @@
package v1_10
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil"
"xorm.io/xorm"
)
func AddTaskTable(x *xorm.Engine) error {
func AddTaskTable(x db.EngineMigration) error {
// TaskType defines task type
type TaskType int

View File

@@ -4,12 +4,11 @@
package v1_11
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/migrations/base"
"xorm.io/xorm"
)
func DropColumnHeadUserNameOnPullRequest(x *xorm.Engine) error {
func DropColumnHeadUserNameOnPullRequest(x db.EngineMigration) error {
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {

View File

@@ -3,11 +3,9 @@
package v1_11
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func AddWhitelistDeployKeysToBranches(x *xorm.Engine) error {
func AddWhitelistDeployKeysToBranches(x db.EngineMigration) error {
type ProtectedBranch struct {
ID int64
WhitelistDeployKeys bool `xorm:"NOT NULL DEFAULT false"`

View File

@@ -4,12 +4,11 @@
package v1_11
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/migrations/base"
"xorm.io/xorm"
)
func RemoveLabelUneededCols(x *xorm.Engine) error {
func RemoveLabelUneededCols(x db.EngineMigration) error {
// Make sure the columns exist before dropping them
type Label struct {
QueryString string

View File

@@ -3,11 +3,9 @@
package v1_11
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func AddTeamIncludesAllRepositories(x *xorm.Engine) error {
func AddTeamIncludesAllRepositories(x db.EngineMigration) error {
type Team struct {
ID int64 `xorm:"pk autoincr"`
IncludesAllRepositories bool `xorm:"NOT NULL DEFAULT false"`

View File

@@ -3,9 +3,7 @@
package v1_11
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
// RepoWatchMode specifies what kind of watch the user has on a repository
type RepoWatchMode int8
@@ -16,7 +14,7 @@ type Watch struct {
Mode RepoWatchMode `xorm:"SMALLINT NOT NULL DEFAULT 1"`
}
func AddModeColumnToWatch(x *xorm.Engine) error {
func AddModeColumnToWatch(x db.EngineMigration) error {
if err := x.Sync(new(Watch)); err != nil {
return err
}

View File

@@ -3,11 +3,9 @@
package v1_11
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func AddTemplateToRepo(x *xorm.Engine) error {
func AddTemplateToRepo(x db.EngineMigration) error {
type Repository struct {
IsTemplate bool `xorm:"INDEX NOT NULL DEFAULT false"`
TemplateID int64 `xorm:"INDEX"`

View File

@@ -3,11 +3,9 @@
package v1_11
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func AddCommentIDOnNotification(x *xorm.Engine) error {
func AddCommentIDOnNotification(x db.EngineMigration) error {
type Notification struct {
ID int64 `xorm:"pk autoincr"`
CommentID int64

View File

@@ -3,11 +3,9 @@
package v1_11
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func AddCanCreateOrgRepoColumnForTeam(x *xorm.Engine) error {
func AddCanCreateOrgRepoColumnForTeam(x db.EngineMigration) error {
type Team struct {
CanCreateOrgRepo bool `xorm:"NOT NULL DEFAULT false"`
}

View File

@@ -4,11 +4,12 @@
package v1_11
import (
"xorm.io/xorm"
"code.gitea.io/gitea/models/db"
"xorm.io/xorm/schemas"
)
func ChangeReviewContentToText(x *xorm.Engine) error {
func ChangeReviewContentToText(x db.EngineMigration) error {
switch x.Dialect().URI().DBType {
case schemas.MYSQL:
_, err := x.Exec("ALTER TABLE review MODIFY COLUMN content TEXT")

View File

@@ -7,10 +7,10 @@ import (
"fmt"
"slices"
"xorm.io/xorm"
"code.gitea.io/gitea/models/db"
)
func AddBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error {
func AddBranchProtectionCanPushAndEnableWhitelist(x db.EngineMigration) error {
type ProtectedBranch struct {
CanPush bool `xorm:"NOT NULL DEFAULT false"`
EnableApprovalsWhitelist bool `xorm:"NOT NULL DEFAULT false"`
@@ -132,7 +132,7 @@ func AddBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error {
}
// getUserRepoPermission static function based on issues_model.IsOfficialReviewer at 5d78792385
getUserRepoPermission := func(sess *xorm.Session, repo *Repository, user *User) (Permission, error) {
getUserRepoPermission := func(sess db.Session, repo *Repository, user *User) (Permission, error) {
var perm Permission
repoOwner := new(User)
@@ -305,7 +305,7 @@ func AddBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error {
}
// isOfficialReviewer static function based on 5d78792385
isOfficialReviewer := func(sess *xorm.Session, issueID int64, reviewer *User) (bool, error) {
isOfficialReviewer := func(sess db.Session, issueID int64, reviewer *User) (bool, error) {
pr := new(PullRequest)
has, err := sess.ID(issueID).Get(pr)
if err != nil {

View File

@@ -6,15 +6,15 @@ package v1_11
import (
"path/filepath"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"xorm.io/builder"
"xorm.io/xorm"
)
func RemoveAttachmentMissedRepo(x *xorm.Engine) error {
func RemoveAttachmentMissedRepo(x db.EngineMigration) error {
type Attachment struct {
UUID string `xorm:"uuid"`
}

View File

@@ -6,10 +6,10 @@ package v1_11
import (
"fmt"
"xorm.io/xorm"
"code.gitea.io/gitea/models/db"
)
func FeatureChangeTargetBranch(x *xorm.Engine) error {
func FeatureChangeTargetBranch(x db.EngineMigration) error {
type Comment struct {
OldRef string
NewRef string

View File

@@ -6,10 +6,10 @@ package v1_11
import (
"net/url"
"xorm.io/xorm"
"code.gitea.io/gitea/models/db"
)
func SanitizeOriginalURL(x *xorm.Engine) error {
func SanitizeOriginalURL(x db.EngineMigration) error {
type Repository struct {
ID int64
OriginalURL string `xorm:"VARCHAR(2048)"`

View File

@@ -12,15 +12,14 @@ import (
"path/filepath"
"time"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"xorm.io/xorm"
)
func RenameExistingUserAvatarName(x *xorm.Engine) error {
func RenameExistingUserAvatarName(x db.EngineMigration) error {
sess := x.NewSession()
defer sess.Close()

View File

@@ -3,11 +3,9 @@
package v1_11
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func ExtendTrackedTimes(x *xorm.Engine) error {
func ExtendTrackedTimes(x db.EngineMigration) error {
type TrackedTime struct {
Time int64 `xorm:"NOT NULL"`
Deleted bool `xorm:"NOT NULL DEFAULT false"`

View File

@@ -3,11 +3,9 @@
package v1_12
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func AddBlockOnRejectedReviews(x *xorm.Engine) error {
func AddBlockOnRejectedReviews(x db.EngineMigration) error {
type ProtectedBranch struct {
BlockOnRejectedReviews bool `xorm:"NOT NULL DEFAULT false"`
}

View File

@@ -3,11 +3,9 @@
package v1_12
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func AddReviewCommitAndStale(x *xorm.Engine) error {
func AddReviewCommitAndStale(x db.EngineMigration) error {
type Review struct {
CommitID string `xorm:"VARCHAR(40)"`
Stale bool `xorm:"NOT NULL DEFAULT false"`

View File

@@ -3,11 +3,9 @@
package v1_12
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func FixMigratedRepositoryServiceType(x *xorm.Engine) error {
func FixMigratedRepositoryServiceType(x db.EngineMigration) error {
// structs.GithubService:
// GithubService = 2
_, err := x.Exec("UPDATE repository SET original_service_type = ? WHERE original_url LIKE 'https://github.com/%'", 2)

View File

@@ -3,11 +3,9 @@
package v1_12
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func AddOwnerNameOnRepository(x *xorm.Engine) error {
func AddOwnerNameOnRepository(x db.EngineMigration) error {
type Repository struct {
OwnerName string
}

View File

@@ -3,9 +3,9 @@
package v1_12
import "xorm.io/xorm"
import "code.gitea.io/gitea/models/db"
func AddIsRestricted(x *xorm.Engine) error {
func AddIsRestricted(x db.EngineMigration) error {
// User see models/user.go
type User struct {
ID int64 `xorm:"pk autoincr"`

View File

@@ -3,11 +3,9 @@
package v1_12
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func AddRequireSignedCommits(x *xorm.Engine) error {
func AddRequireSignedCommits(x db.EngineMigration) error {
type ProtectedBranch struct {
RequireSignedCommits bool `xorm:"NOT NULL DEFAULT false"`
}

View File

@@ -3,11 +3,9 @@
package v1_12
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func AddReactionOriginals(x *xorm.Engine) error {
func AddReactionOriginals(x db.EngineMigration) error {
type Reaction struct {
OriginalAuthorID int64 `xorm:"INDEX NOT NULL DEFAULT(0)"`
OriginalAuthor string

View File

@@ -3,11 +3,9 @@
package v1_12
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func AddUserRepoMissingColumns(x *xorm.Engine) error {
func AddUserRepoMissingColumns(x db.EngineMigration) error {
type VisibleType int
type User struct {
PasswdHashAlgo string `xorm:"NOT NULL DEFAULT 'pbkdf2'"`

View File

@@ -6,10 +6,10 @@ package v1_12
import (
"fmt"
"xorm.io/xorm"
"code.gitea.io/gitea/models/db"
)
func AddReviewMigrateInfo(x *xorm.Engine) error {
func AddReviewMigrateInfo(x db.EngineMigration) error {
type Review struct {
OriginalAuthor string
OriginalAuthorID int64

View File

@@ -4,11 +4,12 @@
package v1_12
import (
"code.gitea.io/gitea/models/db"
"xorm.io/builder"
"xorm.io/xorm"
)
func FixTopicRepositoryCount(x *xorm.Engine) error {
func FixTopicRepositoryCount(x db.EngineMigration) error {
_, err := x.Exec(builder.Delete(builder.NotIn("`repo_id`", builder.Select("`id`").From("`repository`"))).From("`repo_topic`"))
if err != nil {
return err

View File

@@ -6,12 +6,11 @@ package v1_12
import (
"fmt"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil"
"xorm.io/xorm"
)
func AddLanguageStats(x *xorm.Engine) error {
func AddLanguageStats(x db.EngineMigration) error {
// LanguageStat see models/repo_language_stats.go
type LanguageStat struct {
ID int64 `xorm:"pk autoincr"`

View File

@@ -11,15 +11,14 @@ import (
"strings"
"time"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/git/gitcmd"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
)
func FixMergeBase(ctx context.Context, x *xorm.Engine) error {
func FixMergeBase(ctx context.Context, x db.EngineMigration) error {
type Repository struct {
ID int64 `xorm:"pk autoincr"`
OwnerID int64 `xorm:"UNIQUE(s) index"`

View File

@@ -3,11 +3,9 @@
package v1_12
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func PurgeUnusedDependencies(x *xorm.Engine) error {
func PurgeUnusedDependencies(x db.EngineMigration) error {
if _, err := x.Exec("DELETE FROM issue_dependency WHERE issue_id NOT IN (SELECT id FROM issue)"); err != nil {
return err
}

View File

@@ -4,13 +4,12 @@
package v1_12
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
)
func ExpandWebhooks(x *xorm.Engine) error {
func ExpandWebhooks(x db.EngineMigration) error {
type HookEvents struct {
Create bool `json:"create"`
Delete bool `json:"delete"`

View File

@@ -6,10 +6,10 @@ package v1_12
import (
"fmt"
"xorm.io/xorm"
"code.gitea.io/gitea/models/db"
)
func AddSystemWebhookColumn(x *xorm.Engine) error {
func AddSystemWebhookColumn(x db.EngineMigration) error {
type Webhook struct {
IsSystemWebhook bool `xorm:"NOT NULL DEFAULT false"`
}

View File

@@ -6,10 +6,10 @@ package v1_12
import (
"fmt"
"xorm.io/xorm"
"code.gitea.io/gitea/models/db"
)
func AddBranchProtectionProtectedFilesColumn(x *xorm.Engine) error {
func AddBranchProtectionProtectedFilesColumn(x db.EngineMigration) error {
type ProtectedBranch struct {
ProtectedFilePatterns string `xorm:"TEXT"`
}

View File

@@ -3,9 +3,9 @@
package v1_12
import "xorm.io/xorm"
import "code.gitea.io/gitea/models/db"
func AddEmailHashTable(x *xorm.Engine) error {
func AddEmailHashTable(x db.EngineMigration) error {
// EmailHash represents a pre-generated hash map
type EmailHash struct {
Hash string `xorm:"pk varchar(32)"`

View File

@@ -11,14 +11,13 @@ import (
"strings"
"time"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/git/gitcmd"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
)
func RefixMergeBase(ctx context.Context, x *xorm.Engine) error {
func RefixMergeBase(ctx context.Context, x db.EngineMigration) error {
type Repository struct {
ID int64 `xorm:"pk autoincr"`
OwnerID int64 `xorm:"UNIQUE(s) index"`

View File

@@ -6,10 +6,10 @@ package v1_12
import (
"fmt"
"xorm.io/xorm"
"code.gitea.io/gitea/models/db"
)
func AddOrgIDLabelColumn(x *xorm.Engine) error {
func AddOrgIDLabelColumn(x db.EngineMigration) error {
type Label struct {
OrgID int64 `xorm:"INDEX"`
}

View File

@@ -8,16 +8,15 @@ import (
"math"
"time"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
)
func AddCommitDivergenceToPulls(x *xorm.Engine) error {
func AddCommitDivergenceToPulls(x db.EngineMigration) error {
type Repository struct {
ID int64 `xorm:"pk autoincr"`
OwnerID int64 `xorm:"UNIQUE(s) index"`

View File

@@ -3,11 +3,9 @@
package v1_12
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func AddBlockOnOutdatedBranch(x *xorm.Engine) error {
func AddBlockOnOutdatedBranch(x db.EngineMigration) error {
type ProtectedBranch struct {
BlockOnOutdatedBranch bool `xorm:"NOT NULL DEFAULT false"`
}

View File

@@ -6,10 +6,10 @@ package v1_12
import (
"fmt"
"xorm.io/xorm"
"code.gitea.io/gitea/models/db"
)
func AddResolveDoerIDCommentColumn(x *xorm.Engine) error {
func AddResolveDoerIDCommentColumn(x db.EngineMigration) error {
type Comment struct {
ResolveDoerID int64
}

View File

@@ -4,12 +4,11 @@
package v1_12
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
)
func PrependRefsHeadsToIssueRefs(x *xorm.Engine) error {
func PrependRefsHeadsToIssueRefs(x db.EngineMigration) error {
var query string
switch {

View File

@@ -6,13 +6,12 @@ package v1_13
import (
"fmt"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/migrations/base"
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
)
func FixLanguageStatsToSaveSize(x *xorm.Engine) error {
func FixLanguageStatsToSaveSize(x db.EngineMigration) error {
// LanguageStat see models/repo_language_stats.go
type LanguageStat struct {
Size int64 `xorm:"NOT NULL DEFAULT 0"`

View File

@@ -6,10 +6,10 @@ package v1_13
import (
"fmt"
"xorm.io/xorm"
"code.gitea.io/gitea/models/db"
)
func AddKeepActivityPrivateUserColumn(x *xorm.Engine) error {
func AddKeepActivityPrivateUserColumn(x db.EngineMigration) error {
type User struct {
KeepActivityPrivate bool `xorm:"NOT NULL DEFAULT false"`
}

View File

@@ -4,13 +4,13 @@
package v1_13
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"xorm.io/builder"
"xorm.io/xorm"
)
func SetIsArchivedToFalse(x *xorm.Engine) error {
func SetIsArchivedToFalse(x db.EngineMigration) error {
type Repository struct {
IsArchived bool `xorm:"INDEX"`
}

View File

@@ -4,12 +4,11 @@
package v1_13
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"xorm.io/xorm"
)
func RecalculateStars(x *xorm.Engine) (err error) {
func RecalculateStars(x db.EngineMigration) (err error) {
// because of issue https://github.com/go-gitea/gitea/issues/11949,
// recalculate Stars number for all users to fully fix it.

View File

@@ -4,13 +4,13 @@
package v1_13
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"xorm.io/builder"
"xorm.io/xorm"
)
func UpdateMatrixWebhookHTTPMethod(x *xorm.Engine) error {
func UpdateMatrixWebhookHTTPMethod(x db.EngineMigration) error {
matrixHookTaskType := 9 // value comes from the models package
type Webhook struct {
HTTPMethod string

View File

@@ -6,12 +6,11 @@ package v1_13
import (
"fmt"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
)
func IncreaseLanguageField(x *xorm.Engine) error {
func IncreaseLanguageField(x db.EngineMigration) error {
type LanguageStat struct {
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
Language string `xorm:"VARCHAR(50) UNIQUE(s) INDEX NOT NULL"`

View File

@@ -4,12 +4,11 @@
package v1_13
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil"
"xorm.io/xorm"
)
func AddProjectsInfo(x *xorm.Engine) error {
func AddProjectsInfo(x db.EngineMigration) error {
// Create new tables
type (
ProjectType uint8

View File

@@ -4,12 +4,11 @@
package v1_13
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil"
"xorm.io/xorm"
)
func CreateReviewsForCodeComments(x *xorm.Engine) error {
func CreateReviewsForCodeComments(x db.EngineMigration) error {
// Review
type Review struct {
ID int64 `xorm:"pk autoincr"`

View File

@@ -3,11 +3,9 @@
package v1_13
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func PurgeInvalidDependenciesComments(x *xorm.Engine) error {
func PurgeInvalidDependenciesComments(x db.EngineMigration) error {
_, err := x.Exec("DELETE FROM comment WHERE dependent_issue_id != 0 AND dependent_issue_id NOT IN (SELECT id FROM issue)")
return err
}

View File

@@ -6,12 +6,11 @@ package v1_13
import (
"fmt"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil"
"xorm.io/xorm"
)
func AddCreatedAndUpdatedToMilestones(x *xorm.Engine) error {
func AddCreatedAndUpdatedToMilestones(x db.EngineMigration) error {
type Milestone struct {
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`

View File

@@ -4,13 +4,12 @@
package v1_13
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/migrations/base"
"code.gitea.io/gitea/modules/timeutil"
"xorm.io/xorm"
)
func AddPrimaryKeyToRepoTopic(x *xorm.Engine) error {
func AddPrimaryKeyToRepoTopic(x db.EngineMigration) error {
// Topic represents a topic of repositories
type Topic struct {
ID int64 `xorm:"pk autoincr"`

View File

@@ -9,14 +9,14 @@ import (
"fmt"
"strings"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
"xorm.io/xorm/schemas"
)
func SetDefaultPasswordToArgon2(x *xorm.Engine) error {
func SetDefaultPasswordToArgon2(x db.EngineMigration) error {
switch {
case setting.Database.Type.IsMySQL():
_, err := x.Exec("ALTER TABLE `user` ALTER passwd_hash_algo SET DEFAULT 'argon2';")

View File

@@ -3,9 +3,9 @@
package v1_13
import "xorm.io/xorm"
import "code.gitea.io/gitea/models/db"
func AddTrustModelToRepository(x *xorm.Engine) error {
func AddTrustModelToRepository(x db.EngineMigration) error {
type Repository struct {
TrustModel int
}

View File

@@ -3,11 +3,9 @@
package v1_13
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func AddTeamReviewRequestSupport(x *xorm.Engine) error {
func AddTeamReviewRequestSupport(x db.EngineMigration) error {
type Review struct {
ReviewerTeamID int64 `xorm:"NOT NULL DEFAULT 0"`
}

View File

@@ -4,12 +4,11 @@
package v1_13
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil"
"xorm.io/xorm"
)
func AddTimeStamps(x *xorm.Engine) error {
func AddTimeStamps(x db.EngineMigration) error {
// this will add timestamps where it is useful to have
// Star represents a starred repo by an user.

View File

@@ -6,10 +6,10 @@ package v1_14
import (
"fmt"
"xorm.io/xorm"
"code.gitea.io/gitea/models/db"
)
func AddChangedProtectedFilesPullRequestColumn(x *xorm.Engine) error {
func AddChangedProtectedFilesPullRequestColumn(x db.EngineMigration) error {
type PullRequest struct {
ChangedProtectedFiles []string `xorm:"TEXT JSON"`
}

View File

@@ -9,11 +9,10 @@ import (
"path/filepath"
"strings"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
)
// Copy paste from models/repo.go because we cannot import models package
@@ -25,7 +24,7 @@ func userPath(userName string) string {
return filepath.Join(setting.RepoRootPath, strings.ToLower(userName))
}
func FixPublisherIDforTagReleases(ctx context.Context, x *xorm.Engine) error {
func FixPublisherIDforTagReleases(ctx context.Context, x db.EngineMigration) error {
type Release struct {
ID int64
RepoID int64

View File

@@ -3,11 +3,9 @@
package v1_14
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func FixRepoTopics(x *xorm.Engine) error {
func FixRepoTopics(x db.EngineMigration) error {
type Repository struct {
ID int64 `xorm:"pk autoincr"`
Topics []string `xorm:"TEXT JSON"`

View File

@@ -7,13 +7,12 @@ import (
"errors"
"strconv"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
)
func UpdateCodeCommentReplies(x *xorm.Engine) error {
func UpdateCodeCommentReplies(x db.EngineMigration) error {
type Comment struct {
ID int64 `xorm:"pk autoincr"`
CommitSHA string `xorm:"VARCHAR(40)"`

View File

@@ -4,13 +4,12 @@
package v1_14
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/migrations/base"
"code.gitea.io/gitea/modules/timeutil"
"xorm.io/xorm"
)
func UpdateReactionConstraint(x *xorm.Engine) error {
func UpdateReactionConstraint(x db.EngineMigration) error {
// Reaction represents a reactions on issues and comments.
type Reaction struct {
ID int64 `xorm:"pk autoincr"`

View File

@@ -3,11 +3,9 @@
package v1_14
import (
"xorm.io/xorm"
)
import "code.gitea.io/gitea/models/db"
func AddBlockOnOfficialReviewRequests(x *xorm.Engine) error {
func AddBlockOnOfficialReviewRequests(x db.EngineMigration) error {
type ProtectedBranch struct {
BlockOnOfficialReviewRequests bool `xorm:"NOT NULL DEFAULT false"`
}

View File

@@ -6,12 +6,11 @@ package v1_14
import (
"context"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/migrations/base"
"xorm.io/xorm"
)
func ConvertTaskTypeToString(x *xorm.Engine) error {
func ConvertTaskTypeToString(x db.EngineMigration) error {
const (
GOGS int = iota + 1
SLACK

View File

@@ -4,12 +4,11 @@
package v1_14
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/migrations/base"
"xorm.io/xorm"
)
func ConvertWebhookTaskTypeToString(x *xorm.Engine) error {
func ConvertWebhookTaskTypeToString(x db.EngineMigration) error {
const (
GOGS int = iota + 1
SLACK

View File

@@ -4,12 +4,11 @@
package v1_14
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/migrations/base"
"xorm.io/xorm"
)
func ConvertTopicNameFrom25To50(x *xorm.Engine) error {
func ConvertTopicNameFrom25To50(x db.EngineMigration) error {
type Topic struct {
ID int64 `xorm:"pk autoincr"`
Name string `xorm:"UNIQUE VARCHAR(50)"`

View File

@@ -6,7 +6,7 @@ package v1_14
import (
"fmt"
"xorm.io/xorm"
"code.gitea.io/gitea/models/db"
)
// OAuth2Grant here is a snapshot of models.OAuth2Grant for this version
@@ -29,7 +29,7 @@ func (grant *OAuth2Grant) TableName() string {
return "oauth2_grant"
}
func AddScopeAndNonceColumnsToOAuth2Grant(x *xorm.Engine) error {
func AddScopeAndNonceColumnsToOAuth2Grant(x db.EngineMigration) error {
if err := x.Sync(new(OAuth2Grant)); err != nil {
return fmt.Errorf("Sync: %w", err)
}

Some files were not shown because too many files have changed in this diff Show More