Skip to content

Commit ef90bef

Browse files
mithileshgupta12Mithilesh Guptawxiaoguang
authored
Add test for ExtendCommentTreePathLength migration and fix bugs (#35791)
Co-authored-by: Mithilesh Gupta <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent c3472dd commit ef90bef

File tree

6 files changed

+82
-26
lines changed

6 files changed

+82
-26
lines changed

models/migrations/base/tests.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/stretchr/testify/require"
2121
"xorm.io/xorm"
22+
"xorm.io/xorm/schemas"
2223
)
2324

2425
// FIXME: this file shouldn't be in a normal package, it should only be compiled for tests
@@ -88,6 +89,16 @@ func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (*xorm.Engine, fu
8889
return x, deferFn
8990
}
9091

92+
func LoadTableSchemasMap(t *testing.T, x *xorm.Engine) map[string]*schemas.Table {
93+
tables, err := x.DBMetas()
94+
require.NoError(t, err)
95+
tableMap := make(map[string]*schemas.Table)
96+
for _, table := range tables {
97+
tableMap[table.Name] = table
98+
}
99+
return tableMap
100+
}
101+
91102
func MainTest(m *testing.M) {
92103
testlogger.Init()
93104

models/migrations/migrations.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"code.gitea.io/gitea/models/migrations/v1_23"
2626
"code.gitea.io/gitea/models/migrations/v1_24"
2727
"code.gitea.io/gitea/models/migrations/v1_25"
28+
"code.gitea.io/gitea/models/migrations/v1_26"
2829
"code.gitea.io/gitea/models/migrations/v1_6"
2930
"code.gitea.io/gitea/models/migrations/v1_7"
3031
"code.gitea.io/gitea/models/migrations/v1_8"
@@ -379,8 +380,8 @@ func prepareMigrationTasks() []*migration {
379380
newMigration(309, "Improve Notification table indices", v1_23.ImproveNotificationTableIndices),
380381
newMigration(310, "Add Priority to ProtectedBranch", v1_23.AddPriorityToProtectedBranch),
381382
newMigration(311, "Add TimeEstimate to Issue table", v1_23.AddTimeEstimateColumnToIssueTable),
382-
383383
// Gitea 1.23.0-rc0 ends at migration ID number 311 (database version 312)
384+
384385
newMigration(312, "Add DeleteBranchAfterMerge to AutoMerge", v1_24.AddDeleteBranchAfterMergeForAutoMerge),
385386
newMigration(313, "Move PinOrder from issue table to a new table issue_pin", v1_24.MovePinOrderToTableIssuePin),
386387
newMigration(314, "Update OwnerID as zero for repository level action tables", v1_24.UpdateOwnerIDOfRepoLevelActionsTables),
@@ -390,11 +391,13 @@ func prepareMigrationTasks() []*migration {
390391
newMigration(318, "Add anonymous_access_mode for repo_unit", v1_24.AddRepoUnitAnonymousAccessMode),
391392
newMigration(319, "Add ExclusiveOrder to Label table", v1_24.AddExclusiveOrderColumnToLabelTable),
392393
newMigration(320, "Migrate two_factor_policy to login_source table", v1_24.MigrateSkipTwoFactor),
394+
// Gitea 1.24.0 ends at migration ID number 320 (database version 321)
393395

394-
// Gitea 1.24.0 ends at database version 321
395396
newMigration(321, "Use LONGTEXT for some columns and fix review_state.updated_files column", v1_25.UseLongTextInSomeColumnsAndFixBugs),
396397
newMigration(322, "Extend comment tree_path length limit", v1_25.ExtendCommentTreePathLength),
397-
newMigration(323, "Add support for actions concurrency", v1_25.AddActionsConcurrency),
398+
// Gitea 1.25.0 ends at migration ID number 322 (database version 323)
399+
400+
newMigration(323, "Add support for actions concurrency", v1_26.AddActionsConcurrency),
398401
}
399402
return preparedMigrations
400403
}

models/migrations/v1_25/v321_test.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"code.gitea.io/gitea/modules/timeutil"
1212

1313
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
1415
)
1516

1617
func Test_UseLongTextInSomeColumnsAndFixBugs(t *testing.T) {
@@ -38,33 +39,26 @@ func Test_UseLongTextInSomeColumnsAndFixBugs(t *testing.T) {
3839
type Notice struct {
3940
ID int64 `xorm:"pk autoincr"`
4041
Type int
41-
Description string `xorm:"LONGTEXT"`
42+
Description string `xorm:"TEXT"`
4243
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
4344
}
4445

4546
// Prepare and load the testing database
46-
x, deferable := base.PrepareTestEnv(t, 0, new(ReviewState), new(PackageProperty), new(Notice))
47-
defer deferable()
47+
x, deferrable := base.PrepareTestEnv(t, 0, new(ReviewState), new(PackageProperty), new(Notice))
48+
defer deferrable()
4849

49-
assert.NoError(t, UseLongTextInSomeColumnsAndFixBugs(x))
50+
require.NoError(t, UseLongTextInSomeColumnsAndFixBugs(x))
5051

51-
tables, err := x.DBMetas()
52-
assert.NoError(t, err)
52+
tables := base.LoadTableSchemasMap(t, x)
53+
table := tables["review_state"]
54+
column := table.GetColumn("updated_files")
55+
assert.Equal(t, "LONGTEXT", column.SQLType.Name)
5356

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-
}
57+
table = tables["package_property"]
58+
column = table.GetColumn("value")
59+
assert.Equal(t, "LONGTEXT", column.SQLType.Name)
60+
61+
table = tables["notice"]
62+
column = table.GetColumn("description")
63+
assert.Equal(t, "LONGTEXT", column.SQLType.Name)
7064
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func Test_ExtendCommentTreePathLength(t *testing.T) {
17+
if setting.Database.Type.IsSQLite3() {
18+
t.Skip("For SQLITE, varchar or char will always be represented as TEXT")
19+
}
20+
21+
type Comment struct {
22+
ID int64 `xorm:"pk autoincr"`
23+
TreePath string `xorm:"VARCHAR(255)"`
24+
}
25+
26+
x, deferrable := base.PrepareTestEnv(t, 0, new(Comment))
27+
defer deferrable()
28+
29+
require.NoError(t, ExtendCommentTreePathLength(x))
30+
table := base.LoadTableSchemasMap(t, x)["comment"]
31+
column := table.GetColumn("tree_path")
32+
assert.Contains(t, []string{"NVARCHAR", "VARCHAR"}, column.SQLType.Name)
33+
assert.EqualValues(t, 4000, column.Length)
34+
}
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_26
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/v323.go renamed to models/migrations/v1_26/v323.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2025 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

4-
package v1_25
4+
package v1_26
55

66
import (
77
"xorm.io/xorm"

0 commit comments

Comments
 (0)