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 03:42:02 +00:00
committed by GitHub
co-authored by delvh
parent 775e3bdb34
commit fff32e9469
378 changed files with 708 additions and 721 deletions
+14
View File
@@ -0,0 +1,14 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package v1_25
import (
"testing"
"gitea.dev/modelmigration/migrationtest"
)
func TestMain(m *testing.M) {
migrationtest.MainTest(m)
}
+51
View File
@@ -0,0 +1,51 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package v1_25
import (
"gitea.dev/modelmigration/base"
"gitea.dev/modules/setting"
"xorm.io/xorm/schemas"
)
func UseLongTextInSomeColumnsAndFixBugs(x base.EngineMigration) error {
if !setting.Database.Type.IsMySQL() {
return nil // Only mysql need to change from text to long text, for other databases, they are the same
}
if err := base.ModifyColumn(x, "review_state", &schemas.Column{
Name: "updated_files",
SQLType: schemas.SQLType{
Name: "LONGTEXT",
},
Length: 0,
Nullable: false,
DefaultIsEmpty: true,
}); err != nil {
return err
}
if err := base.ModifyColumn(x, "package_property", &schemas.Column{
Name: "value",
SQLType: schemas.SQLType{
Name: "LONGTEXT",
},
Length: 0,
Nullable: false,
DefaultIsEmpty: true,
}); err != nil {
return err
}
return base.ModifyColumn(x, "notice", &schemas.Column{
Name: "description",
SQLType: schemas.SQLType{
Name: "LONGTEXT",
},
Length: 0,
Nullable: false,
DefaultIsEmpty: true,
})
}
+64
View File
@@ -0,0 +1,64 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package v1_25
import (
"testing"
"gitea.dev/modelmigration/migrationtest"
"gitea.dev/modules/setting"
"gitea.dev/modules/timeutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_UseLongTextInSomeColumnsAndFixBugs(t *testing.T) {
if !setting.Database.Type.IsMySQL() {
t.Skip("Only MySQL needs to change from TEXT to LONGTEXT")
}
type ReviewState struct {
ID int64 `xorm:"pk autoincr"`
UserID int64 `xorm:"NOT NULL UNIQUE(pull_commit_user)"`
PullID int64 `xorm:"NOT NULL INDEX UNIQUE(pull_commit_user) DEFAULT 0"` // Which PR was the review on?
CommitSHA string `xorm:"NOT NULL VARCHAR(64) UNIQUE(pull_commit_user)"` // Which commit was the head commit for the review?
UpdatedFiles map[string]int `xorm:"NOT NULL TEXT JSON"` // Stores for each of the changed files of a PR whether they have been viewed, changed since last viewed, or not viewed
UpdatedUnix timeutil.TimeStamp `xorm:"updated"` // Is an accurate indicator of the order of commits as we do not expect it to be possible to make reviews on previous commits
}
type PackageProperty struct {
ID int64 `xorm:"pk autoincr"`
RefType int `xorm:"INDEX NOT NULL"`
RefID int64 `xorm:"INDEX NOT NULL"`
Name string `xorm:"INDEX NOT NULL"`
Value string `xorm:"TEXT NOT NULL"`
}
type Notice struct {
ID int64 `xorm:"pk autoincr"`
Type int
Description string `xorm:"TEXT"`
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
}
// Prepare and load the testing database
x, deferrable := migrationtest.PrepareTestEnv(t, 0, new(ReviewState), new(PackageProperty), new(Notice))
defer deferrable()
require.NoError(t, UseLongTextInSomeColumnsAndFixBugs(x))
tables := migrationtest.LoadTableSchemasMap(t, x)
table := tables["review_state"]
column := table.GetColumn("updated_files")
assert.Equal(t, "LONGTEXT", column.SQLType.Name)
table = tables["package_property"]
column = table.GetColumn("value")
assert.Equal(t, "LONGTEXT", column.SQLType.Name)
table = tables["notice"]
column = table.GetColumn("description")
assert.Equal(t, "LONGTEXT", column.SQLType.Name)
}
+27
View File
@@ -0,0 +1,27 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package v1_25
import (
"gitea.dev/modelmigration/base"
"xorm.io/xorm/schemas"
)
func ExtendCommentTreePathLength(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
}
return base.ModifyColumn(x, "comment", &schemas.Column{
Name: "tree_path",
SQLType: schemas.SQLType{
Name: "VARCHAR",
},
Length: 4000,
Nullable: true, // To keep compatible as nullable
DefaultIsEmpty: true,
})
}
+34
View File
@@ -0,0 +1,34 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package v1_25
import (
"testing"
"gitea.dev/modelmigration/migrationtest"
"gitea.dev/modules/setting"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_ExtendCommentTreePathLength(t *testing.T) {
if setting.Database.Type.IsSQLite3() {
t.Skip("For SQLITE, varchar or char will always be represented as TEXT")
}
type Comment struct {
ID int64 `xorm:"pk autoincr"`
TreePath string `xorm:"VARCHAR(255)"`
}
x, deferrable := migrationtest.PrepareTestEnv(t, 0, new(Comment))
defer deferrable()
require.NoError(t, ExtendCommentTreePathLength(x))
table := migrationtest.LoadTableSchemasMap(t, x)["comment"]
column := table.GetColumn("tree_path")
assert.Contains(t, []string{"NVARCHAR", "VARCHAR"}, column.SQLType.Name)
assert.EqualValues(t, 4000, column.Length)
}