Skip to content

Commit 54fe47f

Browse files
authored
Change some columns from text to longtext and fix column wrong type caused by xorm (#35141)
This PR upgrade xorm to v1.3.10 which fixed a bug when both `longtext json` tags in the struct field. The `longtext` will be ignored and `json` will be considered as `text`. A migration has been introduced to modify the column directly to longtext. And another two columns should also be migrated from text to longtext. All these changes only affect mysql database because for other databases Gitea supported, text is the same as longtext. Fix #27244 Fix #34764 Fix #35042
1 parent c72174a commit 54fe47f

File tree

8 files changed

+145
-5
lines changed

8 files changed

+145
-5
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ require (
131131
mvdan.cc/xurls/v2 v2.6.0
132132
strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251
133133
xorm.io/builder v0.3.13
134-
xorm.io/xorm v1.3.9
134+
xorm.io/xorm v1.3.10
135135
)
136136

137137
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -955,5 +955,5 @@ strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251 h1:mUcz5b3
955955
strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251/go.mod h1:FJGmPh3vz9jSos1L/F91iAgnC/aejc0wIIrF2ZwJxdY=
956956
xorm.io/builder v0.3.13 h1:a3jmiVVL19psGeXx8GIurTp7p0IIgqeDmwhcR6BAOAo=
957957
xorm.io/builder v0.3.13/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE=
958-
xorm.io/xorm v1.3.9 h1:TUovzS0ko+IQ1XnNLfs5dqK1cJl1H5uHpWbWqAQ04nU=
959-
xorm.io/xorm v1.3.9/go.mod h1:LsCCffeeYp63ssk0pKumP6l96WZcHix7ChpurcLNuMw=
958+
xorm.io/xorm v1.3.10 h1:yR83hTT4mKIPyA/lvWFTzS35xjLwkiYnwdw0Qupeh0o=
959+
xorm.io/xorm v1.3.10/go.mod h1:Lo7hmsFF0F0GbDE7ubX5ZKa+eCf0eCuiJAUG3oI5cxQ=

models/migrations/migrations.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"code.gitea.io/gitea/models/migrations/v1_22"
2525
"code.gitea.io/gitea/models/migrations/v1_23"
2626
"code.gitea.io/gitea/models/migrations/v1_24"
27+
"code.gitea.io/gitea/models/migrations/v1_25"
2728
"code.gitea.io/gitea/models/migrations/v1_6"
2829
"code.gitea.io/gitea/models/migrations/v1_7"
2930
"code.gitea.io/gitea/models/migrations/v1_8"
@@ -382,6 +383,9 @@ func prepareMigrationTasks() []*migration {
382383
newMigration(318, "Add anonymous_access_mode for repo_unit", v1_24.AddRepoUnitAnonymousAccessMode),
383384
newMigration(319, "Add ExclusiveOrder to Label table", v1_24.AddExclusiveOrderColumnToLabelTable),
384385
newMigration(320, "Migrate two_factor_policy to login_source table", v1_24.MigrateSkipTwoFactor),
386+
387+
// Gitea 1.24.0 ends at database version 321
388+
newMigration(321, "Use LONGTEXT for some columns and fix review_state.updated_files column", v1_25.UseLongTextInSomeColumnsAndFixBugs),
385389
}
386390
return preparedMigrations
387391
}

models/migrations/v1_25/main_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_25
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/migrations/base"
10+
)
11+
12+
func TestMain(m *testing.M) {
13+
base.MainTest(m)
14+
}

models/migrations/v1_25/v321.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_25
5+
6+
import (
7+
"code.gitea.io/gitea/models/migrations/base"
8+
"code.gitea.io/gitea/modules/setting"
9+
10+
"xorm.io/xorm"
11+
"xorm.io/xorm/schemas"
12+
)
13+
14+
func UseLongTextInSomeColumnsAndFixBugs(x *xorm.Engine) error {
15+
if !setting.Database.Type.IsMySQL() {
16+
return nil // Only mysql need to change from text to long text, for other databases, they are the same
17+
}
18+
19+
if err := base.ModifyColumn(x, "review_state", &schemas.Column{
20+
Name: "updated_files",
21+
SQLType: schemas.SQLType{
22+
Name: "LONGTEXT",
23+
},
24+
Length: 0,
25+
Nullable: false,
26+
DefaultIsEmpty: true,
27+
}); err != nil {
28+
return err
29+
}
30+
31+
if err := base.ModifyColumn(x, "package_property", &schemas.Column{
32+
Name: "value",
33+
SQLType: schemas.SQLType{
34+
Name: "LONGTEXT",
35+
},
36+
Length: 0,
37+
Nullable: false,
38+
DefaultIsEmpty: true,
39+
}); err != nil {
40+
return err
41+
}
42+
43+
return base.ModifyColumn(x, "notice", &schemas.Column{
44+
Name: "description",
45+
SQLType: schemas.SQLType{
46+
Name: "LONGTEXT",
47+
},
48+
Length: 0,
49+
Nullable: false,
50+
DefaultIsEmpty: true,
51+
})
52+
}

models/migrations/v1_25/v321_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_25
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/migrations/base"
10+
"code.gitea.io/gitea/modules/setting"
11+
"code.gitea.io/gitea/modules/timeutil"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func Test_UseLongTextInSomeColumnsAndFixBugs(t *testing.T) {
17+
if !setting.Database.Type.IsMySQL() {
18+
t.Skip("Only MySQL needs to change from TEXT to LONGTEXT")
19+
}
20+
21+
type ReviewState struct {
22+
ID int64 `xorm:"pk autoincr"`
23+
UserID int64 `xorm:"NOT NULL UNIQUE(pull_commit_user)"`
24+
PullID int64 `xorm:"NOT NULL INDEX UNIQUE(pull_commit_user) DEFAULT 0"` // Which PR was the review on?
25+
CommitSHA string `xorm:"NOT NULL VARCHAR(64) UNIQUE(pull_commit_user)"` // Which commit was the head commit for the review?
26+
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
27+
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
28+
}
29+
30+
type PackageProperty struct {
31+
ID int64 `xorm:"pk autoincr"`
32+
RefType int `xorm:"INDEX NOT NULL"`
33+
RefID int64 `xorm:"INDEX NOT NULL"`
34+
Name string `xorm:"INDEX NOT NULL"`
35+
Value string `xorm:"TEXT NOT NULL"`
36+
}
37+
38+
type Notice struct {
39+
ID int64 `xorm:"pk autoincr"`
40+
Type int
41+
Description string `xorm:"LONGTEXT"`
42+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
43+
}
44+
45+
// Prepare and load the testing database
46+
x, deferable := base.PrepareTestEnv(t, 0, new(ReviewState), new(PackageProperty), new(Notice))
47+
defer deferable()
48+
49+
assert.NoError(t, UseLongTextInSomeColumnsAndFixBugs(x))
50+
51+
tables, err := x.DBMetas()
52+
assert.NoError(t, err)
53+
54+
for _, table := range tables {
55+
switch table.Name {
56+
case "review_state":
57+
column := table.GetColumn("updated_files")
58+
assert.NotNil(t, column)
59+
assert.Equal(t, "LONGTEXT", column.SQLType.Name)
60+
case "package_property":
61+
column := table.GetColumn("value")
62+
assert.NotNil(t, column)
63+
assert.Equal(t, "LONGTEXT", column.SQLType.Name)
64+
case "notice":
65+
column := table.GetColumn("description")
66+
assert.NotNil(t, column)
67+
assert.Equal(t, "LONGTEXT", column.SQLType.Name)
68+
}
69+
}
70+
}

models/packages/package_property.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type PackageProperty struct {
3232
RefType PropertyType `xorm:"INDEX NOT NULL"`
3333
RefID int64 `xorm:"INDEX NOT NULL"`
3434
Name string `xorm:"INDEX NOT NULL"`
35-
Value string `xorm:"TEXT NOT NULL"`
35+
Value string `xorm:"LONGTEXT NOT NULL"`
3636
}
3737

3838
// InsertProperty creates a property

models/system/notice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
type Notice struct {
3030
ID int64 `xorm:"pk autoincr"`
3131
Type NoticeType
32-
Description string `xorm:"TEXT"`
32+
Description string `xorm:"LONGTEXT"`
3333
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
3434
}
3535

0 commit comments

Comments
 (0)