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 -2
View File
@@ -90,7 +90,7 @@ func (actionRun) TableName() string {
}
// AddActionRunAttemptModel adds the ActionRunAttempt table and the supporting ActionRun/ActionRunJob fields.
func AddActionRunAttemptModel(x base.EngineMigration) error {
func AddActionRunAttemptModel(ctx context.Context, x base.EngineMigration) error {
// add "action_run_attempt"
if _, err := x.SyncWithOptions(xorm.SyncOptions{
IgnoreDropIndices: true,
@@ -136,7 +136,7 @@ func AddActionRunAttemptModel(x base.EngineMigration) error {
}
concurrencyColumns := make([]string, 0, 2)
for _, col := range []string{"concurrency_group", "concurrency_cancel"} {
exist, err := x.Dialect().IsColumnExist(x.DB(), context.Background(), "action_run", col)
exist, err := x.Dialect().IsColumnExist(x.DB(), ctx, "action_run", col)
if err != nil {
return err
}
+5 -6
View File
@@ -4,7 +4,6 @@
package v1_27
import (
"context"
"slices"
"testing"
@@ -67,7 +66,7 @@ func Test_AddActionRunAttemptModel(t *testing.T) {
})
require.NoError(t, err)
require.NoError(t, AddActionRunAttemptModel(x))
require.NoError(t, AddActionRunAttemptModel(t.Context(), x))
tableMap := migrationtest.LoadTableSchemasMap(t, x)
@@ -88,22 +87,22 @@ func Test_AddActionRunAttemptModel(t *testing.T) {
require.Contains(t, jobTable.ColumnsSeq(), "attempt_job_id")
require.Contains(t, jobTable.ColumnsSeq(), "source_task_id")
attemptIndexes, err := x.Dialect().GetIndexes(x.DB(), context.Background(), "action_run_attempt")
attemptIndexes, err := x.Dialect().GetIndexes(x.DB(), t.Context(), "action_run_attempt")
require.NoError(t, err)
assert.True(t, hasIndexWithColumns(attemptIndexes, []string{"run_id", "attempt"}, true))
assert.True(t, hasIndexWithColumns(attemptIndexes, []string{"repo_id", "concurrency_group", "status"}, false))
runIndexes, err := x.Dialect().GetIndexes(x.DB(), context.Background(), "action_run")
runIndexes, err := x.Dialect().GetIndexes(x.DB(), t.Context(), "action_run")
require.NoError(t, err)
assert.True(t, hasIndexWithColumns(runIndexes, []string{"latest_attempt_id"}, false))
assert.False(t, hasIndexWithColumns(runIndexes, []string{"repo_id", "concurrency_group"}, false))
jobIndexes, err := x.Dialect().GetIndexes(x.DB(), context.Background(), "action_run_job")
jobIndexes, err := x.Dialect().GetIndexes(x.DB(), t.Context(), "action_run_job")
require.NoError(t, err)
assert.True(t, hasIndexWithColumns(jobIndexes, []string{"run_attempt_id"}, false))
assert.True(t, hasIndexWithColumns(jobIndexes, []string{"attempt_job_id"}, false))
indexes, err := x.Dialect().GetIndexes(x.DB(), context.Background(), "action_artifact")
indexes, err := x.Dialect().GetIndexes(x.DB(), t.Context(), "action_artifact")
require.NoError(t, err)
assert.False(t, hasIndexWithColumns(indexes, []string{"run_id", "artifact_path", "artifact_name"}, true))
assert.True(t, hasIndexWithColumns(indexes, []string{"run_id", "run_attempt_id", "artifact_path", "artifact_name"}, true))
+3 -1
View File
@@ -4,6 +4,8 @@
package v1_27
import (
"context"
"gitea.dev/modelmigration/base"
"xorm.io/xorm"
@@ -17,7 +19,7 @@ func (mirrorWithLastSyncUnix) TableName() string {
return "mirror"
}
func AddLastSyncUnixToMirror(x base.EngineMigration) error {
func AddLastSyncUnixToMirror(_ context.Context, x base.EngineMigration) error {
_, err := x.SyncWithOptions(xorm.SyncOptions{
IgnoreDropIndices: true,
}, new(mirrorWithLastSyncUnix))
+3 -1
View File
@@ -4,12 +4,14 @@
package v1_27
import (
"context"
"gitea.dev/modelmigration/base"
"xorm.io/xorm"
)
func AddBranchProtectionBypassAllowlist(x base.EngineMigration) error {
func AddBranchProtectionBypassAllowlist(_ context.Context, x base.EngineMigration) error {
type ProtectedBranch struct {
EnableBypassAllowlist bool `xorm:"NOT NULL DEFAULT false"`
BypassAllowlistUserIDs []int64 `xorm:"JSON TEXT"`
+1 -1
View File
@@ -38,7 +38,7 @@ func Test_AddBranchProtectionBypassAllowlist(t *testing.T) {
})
require.NoError(t, err)
require.NoError(t, AddBranchProtectionBypassAllowlist(x))
require.NoError(t, AddBranchProtectionBypassAllowlist(t.Context(), x))
// Verify the default values record
var pb ProtectedBranch
+3 -1
View File
@@ -4,12 +4,14 @@
package v1_27
import (
"context"
"gitea.dev/modelmigration/base"
"xorm.io/xorm"
)
func AddCancellingSupportToActionRunner(x base.EngineMigration) error {
func AddCancellingSupportToActionRunner(_ context.Context, x base.EngineMigration) error {
type ActionRunner struct {
HasCancellingSupport bool `xorm:"has_cancelling_support NOT NULL DEFAULT false"`
}
+1 -1
View File
@@ -26,7 +26,7 @@ func TestAddCancellingSupportToActionRunner(t *testing.T) {
_, err := x.Insert(&ActionRunner{Name: "runner"})
require.NoError(t, err)
require.NoError(t, AddCancellingSupportToActionRunner(x))
require.NoError(t, AddCancellingSupportToActionRunner(t.Context(), x))
var hasCancellingSupport bool
has, err := x.SQL("SELECT has_cancelling_support FROM action_runner WHERE id = ?", 1).Get(&hasCancellingSupport)
+3 -1
View File
@@ -4,6 +4,8 @@
package v1_27
import (
"context"
"gitea.dev/modelmigration/base"
"xorm.io/xorm"
@@ -11,7 +13,7 @@ import (
// AddReusableWorkflowFieldsToActionRunJob adds the ActionRunJob columns that describe the reusable workflow caller hierarchy,
// and the ActionRunAttemptJobIDIndex table backing run-wide AttemptJobID allocation.
func AddReusableWorkflowFieldsToActionRunJob(x base.EngineMigration) error {
func AddReusableWorkflowFieldsToActionRunJob(_ context.Context, x base.EngineMigration) error {
type ActionRunJob struct {
WorkflowSourceRepoID int64 `xorm:"NOT NULL DEFAULT 0"`
WorkflowSourceCommitSHA string `xorm:"VARCHAR(64) NOT NULL DEFAULT ''"`
+3 -1
View File
@@ -4,11 +4,13 @@
package v1_27
import (
"context"
"gitea.dev/modelmigration/base"
"gitea.dev/modules/timeutil"
)
func AddActionRunJobSummaryTable(x base.EngineMigration) error {
func AddActionRunJobSummaryTable(_ context.Context, x base.EngineMigration) error {
type ActionRunJobSummary struct {
ID int64 `xorm:"pk autoincr"`
+3 -1
View File
@@ -4,6 +4,8 @@
package v1_27
import (
"context"
"gitea.dev/modelmigration/base"
"xorm.io/xorm"
@@ -19,7 +21,7 @@ func (teamWithVisibility) TableName() string {
return "team"
}
func AddVisibilityToTeam(x base.EngineMigration) error {
func AddVisibilityToTeam(_ context.Context, x base.EngineMigration) error {
if _, err := x.SyncWithOptions(xorm.SyncOptions{
IgnoreDropIndices: true,
IgnoreConstrains: true,
+6 -5
View File
@@ -4,6 +4,7 @@
package v1_27
import (
"context"
"fmt"
"strings"
@@ -36,7 +37,7 @@ func isMSSQLMaxTextColumn(column *schemas.Column) bool {
return strings.EqualFold(column.SQLType.Name, schemas.Varchar) || strings.EqualFold(column.SQLType.Name, schemas.NVarchar)
}
func modifyLongTextColumnsForMSSQL(x base.EngineMigration, bean any, columnNames ...string) error {
func modifyLongTextColumnsForMSSQL(ctx context.Context, x base.EngineMigration, bean any, columnNames ...string) error {
table, err := x.TableInfo(bean)
if err != nil {
return err
@@ -50,7 +51,7 @@ func modifyLongTextColumnsForMSSQL(x base.EngineMigration, bean any, columnNames
if isMSSQLMaxTextColumn(column) {
continue
}
if err := base.ModifyColumn(x, table.Name, column); err != nil {
if err := base.ModifyColumn(ctx, x, table.Name, column); err != nil {
return fmt.Errorf("modify %s.%s: %w", table.Name, columnName, err)
}
}
@@ -60,13 +61,13 @@ func modifyLongTextColumnsForMSSQL(x base.EngineMigration, bean any, columnNames
// ExpandIssueAndCommentLongTextFieldsForMSSQL expands legacy MSSQL nvarchar(4000)
// columns to nvarchar(max) so PR push comments and long issue content are not truncated.
func ExpandIssueAndCommentLongTextFieldsForMSSQL(x base.EngineMigration) error {
func ExpandIssueAndCommentLongTextFieldsForMSSQL(ctx context.Context, x base.EngineMigration) error {
if x.Dialect().URI().DBType != schemas.MSSQL {
return nil
}
if err := modifyLongTextColumnsForMSSQL(x, new(issueWithLongTextContent), "content"); err != nil {
if err := modifyLongTextColumnsForMSSQL(ctx, x, new(issueWithLongTextContent), "content"); err != nil {
return err
}
return modifyLongTextColumnsForMSSQL(x, new(commentWithLongTextFields), "content", "patch")
return modifyLongTextColumnsForMSSQL(ctx, x, new(commentWithLongTextFields), "content", "patch")
}
+2 -2
View File
@@ -40,8 +40,8 @@ func Test_ExpandIssueAndCommentLongTextFieldsForMSSQL(t *testing.T) {
x, deferrable := migrationtest.PrepareTestEnv(t, 0, new(issueBeforeLongTextMSSQLMigration), new(commentBeforeLongTextMSSQLMigration))
defer deferrable()
require.NoError(t, ExpandIssueAndCommentLongTextFieldsForMSSQL(x))
require.NoError(t, ExpandIssueAndCommentLongTextFieldsForMSSQL(x))
require.NoError(t, ExpandIssueAndCommentLongTextFieldsForMSSQL(t.Context(), x))
require.NoError(t, ExpandIssueAndCommentLongTextFieldsForMSSQL(t.Context(), x))
longText := strings.Repeat("x", 5000)
_, err := x.Insert(&issueBeforeLongTextMSSQLMigration{Content: longText})
+2 -2
View File
@@ -14,11 +14,11 @@ import (
// AddCreatedUnixToActionUserIsDeletedIndex extends the c_u composite index on
// the action table to include created_unix, enabling efficient ORDER BY on the
// dashboard feed query without a full sort of all matching rows.
func AddCreatedUnixToActionUserIsDeletedIndex(x base.EngineMigration) error {
func AddCreatedUnixToActionUserIsDeletedIndex(ctx context.Context, x base.EngineMigration) error {
// xorm Sync cannot reliably update an index when another index already
// covers the same columns in a different order (Equal() is order-insensitive).
// Drop the old c_u index explicitly, then recreate it with the new column set.
indexes, err := x.Dialect().GetIndexes(x.DB(), context.Background(), "action")
indexes, err := x.Dialect().GetIndexes(x.DB(), ctx, "action")
if err != nil {
return err
}
+3 -4
View File
@@ -4,7 +4,6 @@
package v1_27
import (
"context"
"testing"
"gitea.dev/modelmigration/migrationtest"
@@ -58,14 +57,14 @@ func Test_AddCreatedUnixToActionUserIsDeletedIndex(t *testing.T) {
return
}
indexes, err := x.Dialect().GetIndexes(x.DB(), context.Background(), "action")
indexes, err := x.Dialect().GetIndexes(x.DB(), t.Context(), "action")
require.NoError(t, err)
assert.True(t, hasIndexWithColumns(indexes, []string{"user_id", "is_deleted"}, false), "old c_u index should exist before migration")
assert.False(t, hasIndexWithColumns(indexes, []string{"user_id", "is_deleted", "created_unix"}, false), "new c_u index should not exist before migration")
require.NoError(t, AddCreatedUnixToActionUserIsDeletedIndex(x))
require.NoError(t, AddCreatedUnixToActionUserIsDeletedIndex(t.Context(), x))
indexes, err = x.Dialect().GetIndexes(x.DB(), context.Background(), "action")
indexes, err = x.Dialect().GetIndexes(x.DB(), t.Context(), "action")
require.NoError(t, err)
assert.False(t, hasIndexWithColumns(indexes, []string{"user_id", "is_deleted"}, false), "old 2-column c_u index should be gone after migration")
assert.True(t, hasIndexWithColumns(indexes, []string{"user_id", "is_deleted", "created_unix"}, false), "new 3-column c_u index must exist after migration")
+3 -1
View File
@@ -4,6 +4,8 @@
package v1_27
import (
"context"
"gitea.dev/modelmigration/base"
"xorm.io/xorm"
@@ -11,7 +13,7 @@ import (
// AddContinueOnErrorToActionRunJob adds the ContinueOnError column to ActionRunJob,
// storing the job-level continue-on-error value from the workflow YAML.
func AddContinueOnErrorToActionRunJob(x base.EngineMigration) error {
func AddContinueOnErrorToActionRunJob(_ context.Context, x base.EngineMigration) error {
type ActionRunJob struct {
ContinueOnError bool `xorm:"NOT NULL DEFAULT FALSE"`
}
+3 -2
View File
@@ -4,6 +4,7 @@
package v1_27
import (
"context"
"fmt"
"strings"
"time"
@@ -46,7 +47,7 @@ func (lfsLockWithCreated) TableName() string {
// when the MSSQL session language is not English, breaking external account
// linking and LFS lock creation. New installs already use DATETIME2, so only
// legacy MSSQL columns need converting.
func FixLegacyMSSQLDateTimeColumns(x base.EngineMigration) error {
func FixLegacyMSSQLDateTimeColumns(ctx context.Context, x base.EngineMigration) error {
if x.Dialect().URI().DBType != schemas.MSSQL {
return nil
}
@@ -70,7 +71,7 @@ func FixLegacyMSSQLDateTimeColumns(x base.EngineMigration) error {
if column == nil {
return fmt.Errorf("column %s does not exist in table %s", c.column, table.Name)
}
if err := base.ModifyColumn(x, table.Name, column); err != nil {
if err := base.ModifyColumn(ctx, x, table.Name, column); err != nil {
return fmt.Errorf("modify %s.%s: %w", table.Name, c.column, err)
}
}
+2 -2
View File
@@ -52,8 +52,8 @@ func Test_FixLegacyMSSQLDateTimeColumns(t *testing.T) {
require.Equal(t, "datetime", mssqlColumnType(t, x, "external_login_user", "expires_at"))
require.Equal(t, "datetime", mssqlColumnType(t, x, "lfs_lock", "created"))
require.NoError(t, FixLegacyMSSQLDateTimeColumns(x))
require.NoError(t, FixLegacyMSSQLDateTimeColumns(x)) // idempotent
require.NoError(t, FixLegacyMSSQLDateTimeColumns(t.Context(), x))
require.NoError(t, FixLegacyMSSQLDateTimeColumns(t.Context(), x)) // idempotent
require.Equal(t, "datetime2", mssqlColumnType(t, x, "external_login_user", "expires_at"))
require.Equal(t, "datetime2", mssqlColumnType(t, x, "lfs_lock", "created"))
+3 -1
View File
@@ -4,13 +4,15 @@
package v1_27
import (
"context"
"gitea.dev/modelmigration/base"
"gitea.dev/modules/timeutil"
"xorm.io/xorm"
)
func AddScopedWorkflowsSchema(x base.EngineMigration) error {
func AddScopedWorkflowsSchema(_ context.Context, x base.EngineMigration) error {
// Create the action_scoped_workflow_source table
type ScopedWorkflowConfig struct {
Required bool `json:"required"`