refactor(modelmigration): thread context through migration functions (#38758)

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Copilot
2026-08-03 08:13:15 +00:00
committed by GitHub
co-authored by wxiaoguang
parent e23fe79e5e
commit ac49dbe1a2
304 changed files with 1070 additions and 449 deletions
+2 -1
View File
@@ -4,12 +4,13 @@
package v1_14
import (
"context"
"fmt"
"gitea.dev/modelmigration/base"
)
func AddChangedProtectedFilesPullRequestColumn(x base.EngineMigration) error {
func AddChangedProtectedFilesPullRequestColumn(_ context.Context, x base.EngineMigration) error {
type PullRequest struct {
ChangedProtectedFiles []string `xorm:"TEXT JSON"`
}
+6 -2
View File
@@ -3,9 +3,13 @@
package v1_14
import "gitea.dev/modelmigration/base"
import (
"context"
func FixRepoTopics(x base.EngineMigration) error {
"gitea.dev/modelmigration/base"
)
func FixRepoTopics(_ context.Context, x base.EngineMigration) error {
type Repository struct {
ID int64 `xorm:"pk autoincr"`
Topics []string `xorm:"TEXT JSON"`
+2 -1
View File
@@ -4,6 +4,7 @@
package v1_14
import (
"context"
"errors"
"strconv"
@@ -12,7 +13,7 @@ import (
"gitea.dev/modules/setting"
)
func UpdateCodeCommentReplies(x base.EngineMigration) error {
func UpdateCodeCommentReplies(_ context.Context, x base.EngineMigration) error {
type Comment struct {
ID int64 `xorm:"pk autoincr"`
CommitSHA string `xorm:"VARCHAR(40)"`
+3 -1
View File
@@ -4,11 +4,13 @@
package v1_14
import (
"context"
"gitea.dev/modelmigration/base"
"gitea.dev/modules/timeutil"
)
func UpdateReactionConstraint(x base.EngineMigration) error {
func UpdateReactionConstraint(_ context.Context, x base.EngineMigration) error {
// Reaction represents a reactions on issues and comments.
type Reaction struct {
ID int64 `xorm:"pk autoincr"`
+6 -2
View File
@@ -3,9 +3,13 @@
package v1_14
import "gitea.dev/modelmigration/base"
import (
"context"
func AddBlockOnOfficialReviewRequests(x base.EngineMigration) error {
"gitea.dev/modelmigration/base"
)
func AddBlockOnOfficialReviewRequests(_ context.Context, x base.EngineMigration) error {
type ProtectedBranch struct {
BlockOnOfficialReviewRequests bool `xorm:"NOT NULL DEFAULT false"`
}
+2 -2
View File
@@ -9,7 +9,7 @@ import (
"gitea.dev/modelmigration/base"
)
func ConvertTaskTypeToString(x base.EngineMigration) error {
func ConvertTaskTypeToString(ctx context.Context, x base.EngineMigration) error {
const (
GOGS int = iota + 1
SLACK
@@ -44,7 +44,7 @@ func ConvertTaskTypeToString(x base.EngineMigration) error {
}
// to keep the migration could be rerun
exist, err := x.Dialect().IsColumnExist(x.DB(), context.Background(), "hook_task", "type")
exist, err := x.Dialect().IsColumnExist(x.DB(), ctx, "hook_task", "type")
if err != nil {
return err
}
+3 -1
View File
@@ -4,10 +4,12 @@
package v1_14
import (
"context"
"gitea.dev/modelmigration/base"
)
func ConvertWebhookTaskTypeToString(x base.EngineMigration) error {
func ConvertWebhookTaskTypeToString(_ context.Context, x base.EngineMigration) error {
const (
GOGS int = iota + 1
SLACK
+3 -1
View File
@@ -4,10 +4,12 @@
package v1_14
import (
"context"
"gitea.dev/modelmigration/base"
)
func ConvertTopicNameFrom25To50(x base.EngineMigration) error {
func ConvertTopicNameFrom25To50(_ context.Context, x base.EngineMigration) error {
type Topic struct {
ID int64 `xorm:"pk autoincr"`
Name string `xorm:"UNIQUE VARCHAR(50)"`
+2 -1
View File
@@ -4,6 +4,7 @@
package v1_14
import (
"context"
"fmt"
"gitea.dev/modelmigration/base"
@@ -29,7 +30,7 @@ func (grant *OAuth2Grant) TableName() string {
return "oauth2_grant"
}
func AddScopeAndNonceColumnsToOAuth2Grant(x base.EngineMigration) error {
func AddScopeAndNonceColumnsToOAuth2Grant(_ context.Context, x base.EngineMigration) error {
if err := x.Sync(new(OAuth2Grant)); err != nil {
return fmt.Errorf("Sync: %w", err)
}
+5 -3
View File
@@ -4,19 +4,21 @@
package v1_14
import (
"context"
"gitea.dev/modelmigration/base"
"xorm.io/xorm/schemas"
)
func ConvertHookTaskTypeToVarcharAndTrim(x base.EngineMigration) error {
func ConvertHookTaskTypeToVarcharAndTrim(ctx context.Context, x base.EngineMigration) error {
dbType := x.Dialect().URI().DBType
if dbType == schemas.SQLITE { // For SQLITE, varchar or char will always be represented as TEXT
return nil
}
// HookTask: Typ string `xorm:"VARCHAR(16) index"`
if err := base.ModifyColumn(x, "hook_task", &schemas.Column{
if err := base.ModifyColumn(ctx, x, "hook_task", &schemas.Column{
Name: "typ",
SQLType: schemas.SQLType{
Name: "VARCHAR",
@@ -39,7 +41,7 @@ func ConvertHookTaskTypeToVarcharAndTrim(x base.EngineMigration) error {
}
// Webhook: Type string `xorm:"VARCHAR(16) index"`
if err := base.ModifyColumn(x, "webhook", &schemas.Column{
if err := base.ModifyColumn(ctx, x, "webhook", &schemas.Column{
Name: "type",
SQLType: schemas.SQLType{
Name: "VARCHAR",
+2 -1
View File
@@ -4,6 +4,7 @@
package v1_14
import (
"context"
"crypto/sha256"
"encoding/hex"
@@ -16,7 +17,7 @@ import (
"xorm.io/builder"
)
func RecalculateUserEmptyPWD(x base.EngineMigration) (err error) {
func RecalculateUserEmptyPWD(_ context.Context, x base.EngineMigration) (err error) {
const (
algoBcrypt = "bcrypt"
algoScrypt = "scrypt"
+2 -1
View File
@@ -4,12 +4,13 @@
package v1_14
import (
"context"
"fmt"
"gitea.dev/modelmigration/base"
)
func AddUserRedirect(x base.EngineMigration) (err error) {
func AddUserRedirect(_ context.Context, x base.EngineMigration) (err error) {
type UserRedirect struct {
ID int64 `xorm:"pk autoincr"`
LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
+6 -2
View File
@@ -3,8 +3,12 @@
package v1_14
import "gitea.dev/modelmigration/base"
import (
"context"
func RecreateUserTableToFixDefaultValues(_ base.EngineMigration) error {
"gitea.dev/modelmigration/base"
)
func RecreateUserTableToFixDefaultValues(_ context.Context, _ base.EngineMigration) error {
return nil
}
+6 -2
View File
@@ -3,9 +3,13 @@
package v1_14
import "gitea.dev/modelmigration/base"
import (
"context"
func CommentTypeDeleteBranchUseOldRef(x base.EngineMigration) error {
"gitea.dev/modelmigration/base"
)
func CommentTypeDeleteBranchUseOldRef(_ context.Context, x base.EngineMigration) error {
_, err := x.Exec("UPDATE comment SET old_ref = commit_sha, commit_sha = '' WHERE type = 11")
return err
}
+2 -1
View File
@@ -4,12 +4,13 @@
package v1_14
import (
"context"
"fmt"
"gitea.dev/modelmigration/base"
)
func AddDismissedReviewColumn(x base.EngineMigration) error {
func AddDismissedReviewColumn(_ context.Context, x base.EngineMigration) error {
type Review struct {
Dismissed bool `xorm:"NOT NULL DEFAULT false"`
}
+2 -1
View File
@@ -4,12 +4,13 @@
package v1_14
import (
"context"
"fmt"
"gitea.dev/modelmigration/base"
)
func AddSortingColToProjectBoard(x base.EngineMigration) error {
func AddSortingColToProjectBoard(_ context.Context, x base.EngineMigration) error {
type ProjectBoard struct {
Sorting int8 `xorm:"NOT NULL DEFAULT 0"`
}
+3 -1
View File
@@ -4,11 +4,13 @@
package v1_14
import (
"context"
"gitea.dev/modelmigration/base"
"gitea.dev/modules/timeutil"
)
func AddSessionTable(x base.EngineMigration) error {
func AddSessionTable(_ context.Context, x base.EngineMigration) error {
type Session struct {
Key string `xorm:"pk CHAR(16)"`
Data []byte `xorm:"BLOB"`
+2 -1
View File
@@ -4,12 +4,13 @@
package v1_14
import (
"context"
"fmt"
"gitea.dev/modelmigration/base"
)
func AddTimeIDCommentColumn(x base.EngineMigration) error {
func AddTimeIDCommentColumn(_ context.Context, x base.EngineMigration) error {
type Comment struct {
TimeID int64
}
+2 -1
View File
@@ -4,12 +4,13 @@
package v1_14
import (
"context"
"fmt"
"gitea.dev/modelmigration/base"
)
func AddRepoTransfer(x base.EngineMigration) error {
func AddRepoTransfer(_ context.Context, x base.EngineMigration) error {
type RepoTransfer struct {
ID int64 `xorm:"pk autoincr"`
DoerID int64
+2 -1
View File
@@ -4,6 +4,7 @@
package v1_14
import (
"context"
"fmt"
"regexp"
@@ -12,7 +13,7 @@ import (
"gitea.dev/modules/setting"
)
func FixPostgresIDSequences(x base.EngineMigration) error {
func FixPostgresIDSequences(_ context.Context, x base.EngineMigration) error {
if !setting.Database.Type.IsPostgreSQL() {
return nil
}
+6 -2
View File
@@ -3,12 +3,16 @@
package v1_14
import "gitea.dev/modelmigration/base"
import (
"context"
"gitea.dev/modelmigration/base"
)
// RemoveInvalidLabels looks through the database to look for comments and issue_labels
// that refer to labels do not belong to the repository or organization that repository
// that the issue is in
func RemoveInvalidLabels(x base.EngineMigration) error {
func RemoveInvalidLabels(_ context.Context, x base.EngineMigration) error {
type Comment struct {
ID int64 `xorm:"pk autoincr"`
Type int `xorm:"INDEX"`
+1 -1
View File
@@ -79,7 +79,7 @@ func Test_RemoveInvalidLabels(t *testing.T) {
}
// Run the migration
if err := RemoveInvalidLabels(x); err != nil {
if err := RemoveInvalidLabels(t.Context(), x); err != nil {
t.Errorf("unable to RemoveInvalidLabels: %v", err)
}
+2 -1
View File
@@ -4,13 +4,14 @@
package v1_14
import (
"context"
"fmt"
"gitea.dev/modelmigration/base"
)
// DeleteOrphanedIssueLabels looks through the database for issue_labels where the label no longer exists and deletes them.
func DeleteOrphanedIssueLabels(x base.EngineMigration) error {
func DeleteOrphanedIssueLabels(_ context.Context, x base.EngineMigration) error {
type IssueLabel struct {
ID int64 `xorm:"pk autoincr"`
IssueID int64 `xorm:"UNIQUE(s)"`
+1 -1
View File
@@ -55,7 +55,7 @@ func Test_DeleteOrphanedIssueLabels(t *testing.T) {
}
// Run the migration
if err := DeleteOrphanedIssueLabels(x); err != nil {
if err := DeleteOrphanedIssueLabels(t.Context(), x); err != nil {
assert.NoError(t, err)
return
}