mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-03 21:08:25 +00:00 
			
		
		
		
	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
This commit is contained in:
		
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							@@ -131,7 +131,7 @@ require (
 | 
				
			|||||||
	mvdan.cc/xurls/v2 v2.6.0
 | 
						mvdan.cc/xurls/v2 v2.6.0
 | 
				
			||||||
	strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251
 | 
						strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251
 | 
				
			||||||
	xorm.io/builder v0.3.13
 | 
						xorm.io/builder v0.3.13
 | 
				
			||||||
	xorm.io/xorm v1.3.9
 | 
						xorm.io/xorm v1.3.10
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										4
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								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=
 | 
					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 h1:a3jmiVVL19psGeXx8GIurTp7p0IIgqeDmwhcR6BAOAo=
 | 
				
			||||||
xorm.io/builder v0.3.13/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE=
 | 
					xorm.io/builder v0.3.13/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE=
 | 
				
			||||||
xorm.io/xorm v1.3.9 h1:TUovzS0ko+IQ1XnNLfs5dqK1cJl1H5uHpWbWqAQ04nU=
 | 
					xorm.io/xorm v1.3.10 h1:yR83hTT4mKIPyA/lvWFTzS35xjLwkiYnwdw0Qupeh0o=
 | 
				
			||||||
xorm.io/xorm v1.3.9/go.mod h1:LsCCffeeYp63ssk0pKumP6l96WZcHix7ChpurcLNuMw=
 | 
					xorm.io/xorm v1.3.10/go.mod h1:Lo7hmsFF0F0GbDE7ubX5ZKa+eCf0eCuiJAUG3oI5cxQ=
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -24,6 +24,7 @@ import (
 | 
				
			|||||||
	"code.gitea.io/gitea/models/migrations/v1_22"
 | 
						"code.gitea.io/gitea/models/migrations/v1_22"
 | 
				
			||||||
	"code.gitea.io/gitea/models/migrations/v1_23"
 | 
						"code.gitea.io/gitea/models/migrations/v1_23"
 | 
				
			||||||
	"code.gitea.io/gitea/models/migrations/v1_24"
 | 
						"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_6"
 | 
				
			||||||
	"code.gitea.io/gitea/models/migrations/v1_7"
 | 
						"code.gitea.io/gitea/models/migrations/v1_7"
 | 
				
			||||||
	"code.gitea.io/gitea/models/migrations/v1_8"
 | 
						"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(318, "Add anonymous_access_mode for repo_unit", v1_24.AddRepoUnitAnonymousAccessMode),
 | 
				
			||||||
		newMigration(319, "Add ExclusiveOrder to Label table", v1_24.AddExclusiveOrderColumnToLabelTable),
 | 
							newMigration(319, "Add ExclusiveOrder to Label table", v1_24.AddExclusiveOrderColumnToLabelTable),
 | 
				
			||||||
		newMigration(320, "Migrate two_factor_policy to login_source table", v1_24.MigrateSkipTwoFactor),
 | 
							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
 | 
						return preparedMigrations
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										14
									
								
								models/migrations/v1_25/main_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								models/migrations/v1_25/main_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -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)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										52
									
								
								models/migrations/v1_25/v321.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								models/migrations/v1_25/v321.go
									
									
									
									
									
										Normal file
									
								
							@@ -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,
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										70
									
								
								models/migrations/v1_25/v321_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								models/migrations/v1_25/v321_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -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)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -32,7 +32,7 @@ type PackageProperty struct {
 | 
				
			|||||||
	RefType PropertyType `xorm:"INDEX NOT NULL"`
 | 
						RefType PropertyType `xorm:"INDEX NOT NULL"`
 | 
				
			||||||
	RefID   int64        `xorm:"INDEX NOT NULL"`
 | 
						RefID   int64        `xorm:"INDEX NOT NULL"`
 | 
				
			||||||
	Name    string       `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
 | 
					// InsertProperty creates a property
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -29,7 +29,7 @@ const (
 | 
				
			|||||||
type Notice struct {
 | 
					type Notice struct {
 | 
				
			||||||
	ID          int64 `xorm:"pk autoincr"`
 | 
						ID          int64 `xorm:"pk autoincr"`
 | 
				
			||||||
	Type        NoticeType
 | 
						Type        NoticeType
 | 
				
			||||||
	Description string             `xorm:"TEXT"`
 | 
						Description string             `xorm:"LONGTEXT"`
 | 
				
			||||||
	CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
 | 
						CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user