diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 81a618a207..be7b333a2e 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -415,6 +415,7 @@ func prepareMigrationTasks() []*migration { newMigration(335, "Add reusable workflow fields and action_run_attempt_job_id_index table for ActionRunJob", v1_27.AddReusableWorkflowFieldsToActionRunJob), newMigration(336, "Add ActionRunJobSummary table", v1_27.AddActionRunJobSummaryTable), newMigration(337, "Add visibility to team", v1_27.AddVisibilityToTeam), + newMigration(338, "Expand legacy MSSQL issue/comment long-text columns", v1_27.ExpandIssueAndCommentLongTextFieldsForMSSQL), } return preparedMigrations } diff --git a/models/migrations/v1_27/v338.go b/models/migrations/v1_27/v338.go new file mode 100644 index 0000000000..a495907174 --- /dev/null +++ b/models/migrations/v1_27/v338.go @@ -0,0 +1,73 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_27 + +import ( + "fmt" + "strings" + + "gitea.dev/models/db" + "gitea.dev/models/migrations/base" + + "xorm.io/xorm/schemas" +) + +type issueWithLongTextContent struct { + Content string `xorm:"LONGTEXT"` +} + +func (issueWithLongTextContent) TableName() string { + return "issue" +} + +type commentWithLongTextFields struct { + Content string `xorm:"LONGTEXT"` + PatchQuoted string `xorm:"LONGTEXT patch"` +} + +func (commentWithLongTextFields) TableName() string { + return "comment" +} + +func isMSSQLMaxTextColumn(column *schemas.Column) bool { + if column.Length != -1 { + return false + } + return strings.EqualFold(column.SQLType.Name, schemas.Varchar) || strings.EqualFold(column.SQLType.Name, schemas.NVarchar) +} + +func modifyLongTextColumnsForMSSQL(x db.EngineMigration, bean any, columnNames ...string) error { + table, err := x.TableInfo(bean) + if err != nil { + return err + } + + for _, columnName := range columnNames { + column := table.GetColumn(columnName) + if column == nil { + return fmt.Errorf("column %s does not exist in table %s", columnName, table.Name) + } + if isMSSQLMaxTextColumn(column) { + continue + } + if err := base.ModifyColumn(x, table.Name, column); err != nil { + return fmt.Errorf("modify %s.%s: %w", table.Name, columnName, err) + } + } + + return nil +} + +// ExpandIssueAndCommentLongTextFieldsForMSSQL expands legacy MSSQL nvarchar(4000) +// columns to nvarchar(max) so PR push comments and long issue content are not truncated. +func ExpandIssueAndCommentLongTextFieldsForMSSQL(x db.EngineMigration) error { + if x.Dialect().URI().DBType != schemas.MSSQL { + return nil + } + + if err := modifyLongTextColumnsForMSSQL(x, new(issueWithLongTextContent), "content"); err != nil { + return err + } + return modifyLongTextColumnsForMSSQL(x, new(commentWithLongTextFields), "content", "patch") +} diff --git a/models/migrations/v1_27/v338_test.go b/models/migrations/v1_27/v338_test.go new file mode 100644 index 0000000000..4e47253779 --- /dev/null +++ b/models/migrations/v1_27/v338_test.go @@ -0,0 +1,52 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_27 + +import ( + "strings" + "testing" + + "gitea.dev/models/migrations/migrationtest" + "gitea.dev/modules/setting" + + "github.com/stretchr/testify/require" +) + +type issueBeforeLongTextMSSQLMigration struct { + ID int64 `xorm:"pk autoincr"` + Content string `xorm:"VARCHAR(4000)"` +} + +func (issueBeforeLongTextMSSQLMigration) TableName() string { + return "issue" +} + +type commentBeforeLongTextMSSQLMigration struct { + ID int64 `xorm:"pk autoincr"` + Content string `xorm:"VARCHAR(4000)"` + Patch string `xorm:"VARCHAR(4000) patch"` +} + +func (commentBeforeLongTextMSSQLMigration) TableName() string { + return "comment" +} + +func Test_ExpandIssueAndCommentLongTextFieldsForMSSQL(t *testing.T) { + if !setting.Database.Type.IsMSSQL() { + t.Skip("Only MSSQL needs to expand legacy nvarchar(4000) long-text columns") + } + + x, deferrable := migrationtest.PrepareTestEnv(t, 0, new(issueBeforeLongTextMSSQLMigration), new(commentBeforeLongTextMSSQLMigration)) + defer deferrable() + + require.NoError(t, ExpandIssueAndCommentLongTextFieldsForMSSQL(x)) + require.NoError(t, ExpandIssueAndCommentLongTextFieldsForMSSQL(x)) + + longText := strings.Repeat("x", 5000) + _, err := x.Insert(&issueBeforeLongTextMSSQLMigration{Content: longText}) + require.NoError(t, err) + + _, err = x.Insert(&commentBeforeLongTextMSSQLMigration{Content: longText, Patch: longText}) + require.NoError(t, err) +}