From 54fe47fbca4023061a657bd54425c17b8667c5d2 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 24 Jul 2025 13:24:44 +0800 Subject: [PATCH] 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 --- go.mod | 2 +- go.sum | 4 +- models/migrations/migrations.go | 4 ++ models/migrations/v1_25/main_test.go | 14 ++++++ models/migrations/v1_25/v321.go | 52 +++++++++++++++++++++ models/migrations/v1_25/v321_test.go | 70 ++++++++++++++++++++++++++++ models/packages/package_property.go | 2 +- models/system/notice.go | 2 +- 8 files changed, 145 insertions(+), 5 deletions(-) create mode 100644 models/migrations/v1_25/main_test.go create mode 100644 models/migrations/v1_25/v321.go create mode 100644 models/migrations/v1_25/v321_test.go diff --git a/go.mod b/go.mod index 8b8d61dc65..eab0d417cf 100644 --- a/go.mod +++ b/go.mod @@ -131,7 +131,7 @@ require ( mvdan.cc/xurls/v2 v2.6.0 strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251 xorm.io/builder v0.3.13 - xorm.io/xorm v1.3.9 + xorm.io/xorm v1.3.10 ) require ( diff --git a/go.sum b/go.sum index 2e7c51f747..2ccdc64d77 100644 --- a/go.sum +++ b/go.sum @@ -955,5 +955,5 @@ strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251 h1:mUcz5b3 strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251/go.mod h1:FJGmPh3vz9jSos1L/F91iAgnC/aejc0wIIrF2ZwJxdY= xorm.io/builder v0.3.13 h1:a3jmiVVL19psGeXx8GIurTp7p0IIgqeDmwhcR6BAOAo= xorm.io/builder v0.3.13/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE= -xorm.io/xorm v1.3.9 h1:TUovzS0ko+IQ1XnNLfs5dqK1cJl1H5uHpWbWqAQ04nU= -xorm.io/xorm v1.3.9/go.mod h1:LsCCffeeYp63ssk0pKumP6l96WZcHix7ChpurcLNuMw= +xorm.io/xorm v1.3.10 h1:yR83hTT4mKIPyA/lvWFTzS35xjLwkiYnwdw0Qupeh0o= +xorm.io/xorm v1.3.10/go.mod h1:Lo7hmsFF0F0GbDE7ubX5ZKa+eCf0eCuiJAUG3oI5cxQ= diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 176372486e..4f899453b5 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -24,6 +24,7 @@ import ( "code.gitea.io/gitea/models/migrations/v1_22" "code.gitea.io/gitea/models/migrations/v1_23" "code.gitea.io/gitea/models/migrations/v1_24" + "code.gitea.io/gitea/models/migrations/v1_25" "code.gitea.io/gitea/models/migrations/v1_6" "code.gitea.io/gitea/models/migrations/v1_7" "code.gitea.io/gitea/models/migrations/v1_8" @@ -382,6 +383,9 @@ func prepareMigrationTasks() []*migration { newMigration(318, "Add anonymous_access_mode for repo_unit", v1_24.AddRepoUnitAnonymousAccessMode), newMigration(319, "Add ExclusiveOrder to Label table", v1_24.AddExclusiveOrderColumnToLabelTable), newMigration(320, "Migrate two_factor_policy to login_source table", v1_24.MigrateSkipTwoFactor), + + // Gitea 1.24.0 ends at database version 321 + newMigration(321, "Use LONGTEXT for some columns and fix review_state.updated_files column", v1_25.UseLongTextInSomeColumnsAndFixBugs), } return preparedMigrations } diff --git a/models/migrations/v1_25/main_test.go b/models/migrations/v1_25/main_test.go new file mode 100644 index 0000000000..d2c4a4105d --- /dev/null +++ b/models/migrations/v1_25/main_test.go @@ -0,0 +1,14 @@ +// Copyright 2025 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_25 + +import ( + "testing" + + "code.gitea.io/gitea/models/migrations/base" +) + +func TestMain(m *testing.M) { + base.MainTest(m) +} diff --git a/models/migrations/v1_25/v321.go b/models/migrations/v1_25/v321.go new file mode 100644 index 0000000000..73ef180f48 --- /dev/null +++ b/models/migrations/v1_25/v321.go @@ -0,0 +1,52 @@ +// Copyright 2025 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_25 + +import ( + "code.gitea.io/gitea/models/migrations/base" + "code.gitea.io/gitea/modules/setting" + + "xorm.io/xorm" + "xorm.io/xorm/schemas" +) + +func UseLongTextInSomeColumnsAndFixBugs(x *xorm.Engine) 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, + }) +} diff --git a/models/migrations/v1_25/v321_test.go b/models/migrations/v1_25/v321_test.go new file mode 100644 index 0000000000..4897783fd3 --- /dev/null +++ b/models/migrations/v1_25/v321_test.go @@ -0,0 +1,70 @@ +// Copyright 2025 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_25 + +import ( + "testing" + + "code.gitea.io/gitea/models/migrations/base" + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/timeutil" + + "github.com/stretchr/testify/assert" +) + +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:"LONGTEXT"` + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` + } + + // Prepare and load the testing database + x, deferable := base.PrepareTestEnv(t, 0, new(ReviewState), new(PackageProperty), new(Notice)) + defer deferable() + + assert.NoError(t, UseLongTextInSomeColumnsAndFixBugs(x)) + + tables, err := x.DBMetas() + assert.NoError(t, err) + + for _, table := range tables { + switch table.Name { + case "review_state": + column := table.GetColumn("updated_files") + assert.NotNil(t, column) + assert.Equal(t, "LONGTEXT", column.SQLType.Name) + case "package_property": + column := table.GetColumn("value") + assert.NotNil(t, column) + assert.Equal(t, "LONGTEXT", column.SQLType.Name) + case "notice": + column := table.GetColumn("description") + assert.NotNil(t, column) + assert.Equal(t, "LONGTEXT", column.SQLType.Name) + } + } +} diff --git a/models/packages/package_property.go b/models/packages/package_property.go index 7ddbfd97e9..acc05d8d5a 100644 --- a/models/packages/package_property.go +++ b/models/packages/package_property.go @@ -32,7 +32,7 @@ type PackageProperty struct { RefType PropertyType `xorm:"INDEX NOT NULL"` RefID int64 `xorm:"INDEX NOT NULL"` Name string `xorm:"INDEX NOT NULL"` - Value string `xorm:"TEXT NOT NULL"` + Value string `xorm:"LONGTEXT NOT NULL"` } // InsertProperty creates a property diff --git a/models/system/notice.go b/models/system/notice.go index e7ec6a9693..91bf4be0f6 100644 --- a/models/system/notice.go +++ b/models/system/notice.go @@ -29,7 +29,7 @@ const ( type Notice struct { ID int64 `xorm:"pk autoincr"` Type NoticeType - Description string `xorm:"TEXT"` + Description string `xorm:"LONGTEXT"` CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` }