From abd3ca8bb144b2d64cf5e1e9aec266ae7b7de83f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 25 Dec 2020 20:21:36 +0800 Subject: [PATCH 01/12] Fix wrong type on hooktask to convert typ from char(16) to varchar(16) --- models/migrations/migrations.go | 2 ++ models/migrations/v165.go | 36 +++++++++++++++++++++++++++++++++ models/webhook.go | 6 ++---- services/webhook/deliver.go | 2 +- services/webhook/webhook.go | 6 +++--- 5 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 models/migrations/v165.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index bb3adccc2572b..bc2f9fd07df80 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -273,6 +273,8 @@ var migrations = []Migration{ NewMigration("Convert topic name from 25 to 50", convertTopicNameFrom25To50), // v164 -> v165 NewMigration("Add scope and nonce columns to oauth2_grant table", addScopeAndNonceColumnsToOAuth2Grant), + // v165 -> v166 + NewMigration("Convert hook task type from char(16) to varchar(16) and trim the column", convertHookTaskTypeToVarcharAndTrim), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v165.go b/models/migrations/v165.go new file mode 100644 index 0000000000000..0e3d3c722f11a --- /dev/null +++ b/models/migrations/v165.go @@ -0,0 +1,36 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package migrations + +import ( + "xorm.io/xorm" + "xorm.io/xorm/schemas" +) + +func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { + type HookTask struct { + Typ string `xorm:"VARCHAR(16) index notnull"` + } + + alterSQL := x.Dialect().ModifyColumnSQL("hook_task", &schemas.Column{ + Name: "typ", + TableName: "hook_task", + SQLType: schemas.SQLType{ + Name: "VARCHAR", + DefaultLength: 16, + }, + Nullable: false, + }) + if _, err := x.Exec(alterSQL); err != nil { + return err + } + + if _, err := x.Exec("UPDATE hook_task SET typ = TRIM(typ)"); err != nil { + return err + } + + _, err := x.Exec("UPDATE web_hook SET type = TRIM(type)") + return err +} diff --git a/models/webhook.go b/models/webhook.go index 9f1b6131afc54..c11a4993dd3a2 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -8,7 +8,6 @@ package models import ( "encoding/json" "fmt" - "strings" "time" "code.gitea.io/gitea/modules/log" @@ -113,7 +112,7 @@ type Webhook struct { *HookEvent `xorm:"-"` IsSSL bool `xorm:"is_ssl"` IsActive bool `xorm:"INDEX"` - Type HookTaskType `xorm:"char(16) 'type'"` + Type HookTaskType `xorm:"VARCHAR(16) 'type'"` Meta string `xorm:"TEXT"` // store hook-specific attributes LastStatus HookStatus // Last delivery status @@ -311,7 +310,6 @@ func CreateWebhook(w *Webhook) error { } func createWebhook(e Engine, w *Webhook) error { - w.Type = strings.TrimSpace(w.Type) _, err := e.Insert(w) return err } @@ -641,7 +639,7 @@ type HookTask struct { RepoID int64 `xorm:"INDEX"` HookID int64 UUID string - Typ HookTaskType + Typ string `xorm:"VARCHAR(16)"` URL string `xorm:"TEXT"` Signature string `xorm:"TEXT"` api.Payloader `xorm:"-"` diff --git a/services/webhook/deliver.go b/services/webhook/deliver.go index 44c1a18b6c8af..76fb4ff19b3c5 100644 --- a/services/webhook/deliver.go +++ b/services/webhook/deliver.go @@ -78,7 +78,7 @@ func Deliver(t *models.HookTask) error { return err } case http.MethodPut: - switch t.Typ { + switch strings.TrimSpace(t.Typ) { case models.MATRIX: req, err = getMatrixHookRequest(t) if err != nil { diff --git a/services/webhook/webhook.go b/services/webhook/webhook.go index b779b38466ade..7b6bc555f73ea 100644 --- a/services/webhook/webhook.go +++ b/services/webhook/webhook.go @@ -60,7 +60,7 @@ var ( // RegisterWebhook registers a webhook func RegisterWebhook(name string, webhook *webhook) { - webhooks[models.HookTaskType(strings.TrimSpace(name))] = webhook + webhooks[models.HookTaskType(name)] = webhook } // IsValidHookTaskType returns true if a webhook registered @@ -68,7 +68,7 @@ func IsValidHookTaskType(name string) bool { if name == models.GITEA || name == models.GOGS { return true } - _, ok := webhooks[models.HookTaskType(strings.TrimSpace(name))] + _, ok := webhooks[models.HookTaskType(name)] return ok } @@ -147,7 +147,7 @@ func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.Hoo var payloader api.Payloader var err error - webhook, ok := webhooks[strings.TrimSpace(w.Type)] // NOTICE: w.Type maynot be trimmed before store into database + webhook, ok := webhooks[w.Type] if ok { payloader, err = webhook.payloadCreator(p, event, w.Meta) if err != nil { From 5cb1ec261a2f3c52cef1bfe7f49453a060524161 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 25 Dec 2020 20:30:00 +0800 Subject: [PATCH 02/12] Fix bugs --- models/migrations/v161.go | 2 +- models/migrations/v163.go | 8 ++++++++ models/webhook.go | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/models/migrations/v161.go b/models/migrations/v161.go index 127dca150074f..3eff7df8a3bb4 100644 --- a/models/migrations/v161.go +++ b/models/migrations/v161.go @@ -34,7 +34,7 @@ func convertTaskTypeToString(x *xorm.Engine) error { } type HookTask struct { - Typ string `xorm:"char(16) index"` + Typ string `xorm:"VARCHAR(16) index"` } if err := x.Sync2(new(HookTask)); err != nil { return err diff --git a/models/migrations/v163.go b/models/migrations/v163.go index 150cc34f02e4f..5dd953da523a1 100644 --- a/models/migrations/v163.go +++ b/models/migrations/v163.go @@ -21,6 +21,10 @@ func convertTopicNameFrom25To50(x *xorm.Engine) error { return err } + type HookTask struct { + Typ string `xorm:"VARCHAR(16) index"` + } + sess := x.NewSession() defer sess.Close() if err := sess.Begin(); err != nil { @@ -30,5 +34,9 @@ func convertTopicNameFrom25To50(x *xorm.Engine) error { return err } + if err := recreateTable(sess, new(HookTask)); err != nil { + return err + } + return sess.Commit() } diff --git a/models/webhook.go b/models/webhook.go index c11a4993dd3a2..6abf9521f668b 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -639,7 +639,7 @@ type HookTask struct { RepoID int64 `xorm:"INDEX"` HookID int64 UUID string - Typ string `xorm:"VARCHAR(16)"` + Typ string `xorm:"VARCHAR(16) index"` URL string `xorm:"TEXT"` Signature string `xorm:"TEXT"` api.Payloader `xorm:"-"` From 19d2a6bd78ccca0edd76e90f63e26290f100dcb5 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 25 Dec 2020 20:32:39 +0800 Subject: [PATCH 03/12] Improve code --- models/webhook.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/models/webhook.go b/models/webhook.go index 6abf9521f668b..38af3d698d68a 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -639,9 +639,9 @@ type HookTask struct { RepoID int64 `xorm:"INDEX"` HookID int64 UUID string - Typ string `xorm:"VARCHAR(16) index"` - URL string `xorm:"TEXT"` - Signature string `xorm:"TEXT"` + Typ HookTaskType `xorm:"VARCHAR(16) index"` + URL string `xorm:"TEXT"` + Signature string `xorm:"TEXT"` api.Payloader `xorm:"-"` PayloadContent string `xorm:"TEXT"` HTTPMethod string `xorm:"http_method"` From 6bb67aa3d40f58946a163fa1d3c74883ffc05202 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 27 Dec 2020 11:01:31 +0800 Subject: [PATCH 04/12] Use different trim function for MSSQL --- models/migrations/v165.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/models/migrations/v165.go b/models/migrations/v165.go index 0e3d3c722f11a..cc4f18c1974c2 100644 --- a/models/migrations/v165.go +++ b/models/migrations/v165.go @@ -27,10 +27,22 @@ func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { return err } - if _, err := x.Exec("UPDATE hook_task SET typ = TRIM(typ)"); err != nil { + var hookTaskTrimSQL string + if x.Dialect().URI().DBType == schemas.MSSQL { + hookTaskTrimSQL = "UPDATE hook_task SET typ = RTRIM(LTRIM(typ))" + } else { + hookTaskTrimSQL = "UPDATE hook_task SET typ = TRIM(typ)" + } + if _, err := x.Exec(hookTaskTrimSQL); err != nil { return err } - _, err := x.Exec("UPDATE web_hook SET type = TRIM(type)") + var webhookTrimSQL string + if x.Dialect().URI().DBType == schemas.MSSQL { + webhookTrimSQL = "UPDATE webhook SET type = RTRIM(LTRIM(type))" + } else { + webhookTrimSQL = "UPDATE webhook SET type = TRIM(type)" + } + _, err := x.Exec(webhookTrimSQL) return err } From a895e03890ac201106f2a573d9b43a7ea63d76a1 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 27 Dec 2020 11:19:50 +0800 Subject: [PATCH 05/12] Fix bug --- models/migrations/v165.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/models/migrations/v165.go b/models/migrations/v165.go index cc4f18c1974c2..1c34ed8ac3ed6 100644 --- a/models/migrations/v165.go +++ b/models/migrations/v165.go @@ -11,17 +11,16 @@ import ( func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { type HookTask struct { - Typ string `xorm:"VARCHAR(16) index notnull"` + Typ string `xorm:"VARCHAR(16) index"` } alterSQL := x.Dialect().ModifyColumnSQL("hook_task", &schemas.Column{ Name: "typ", TableName: "hook_task", SQLType: schemas.SQLType{ - Name: "VARCHAR", - DefaultLength: 16, + Name: "VARCHAR", }, - Nullable: false, + Length: 16, }) if _, err := x.Exec(alterSQL); err != nil { return err @@ -37,6 +36,22 @@ func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { return err } + type Webhook struct { + Type string `xorm:"VARCHAR(16) index"` + } + + alterSQL = x.Dialect().ModifyColumnSQL("webhook", &schemas.Column{ + Name: "type", + TableName: "webhook", + SQLType: schemas.SQLType{ + Name: "VARCHAR", + }, + Length: 16, + }) + if _, err := x.Exec(alterSQL); err != nil { + return err + } + var webhookTrimSQL string if x.Dialect().URI().DBType == schemas.MSSQL { webhookTrimSQL = "UPDATE webhook SET type = RTRIM(LTRIM(type))" From ef2fb779a61bc3e9b6d11c3eebef6c9d229ed27e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 27 Dec 2020 11:27:01 +0800 Subject: [PATCH 06/12] Removed wrong changed line --- models/migrations/v163.go | 4 ---- models/webhook.go | 2 ++ services/webhook/deliver.go | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/models/migrations/v163.go b/models/migrations/v163.go index 5dd953da523a1..b6f96873420b7 100644 --- a/models/migrations/v163.go +++ b/models/migrations/v163.go @@ -34,9 +34,5 @@ func convertTopicNameFrom25To50(x *xorm.Engine) error { return err } - if err := recreateTable(sess, new(HookTask)); err != nil { - return err - } - return sess.Commit() } diff --git a/models/webhook.go b/models/webhook.go index 38af3d698d68a..e0a75843db899 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -8,6 +8,7 @@ package models import ( "encoding/json" "fmt" + "strings" "time" "code.gitea.io/gitea/modules/log" @@ -310,6 +311,7 @@ func CreateWebhook(w *Webhook) error { } func createWebhook(e Engine, w *Webhook) error { + w.Type = strings.TrimSpace(w.Type) _, err := e.Insert(w) return err } diff --git a/services/webhook/deliver.go b/services/webhook/deliver.go index 76fb4ff19b3c5..44c1a18b6c8af 100644 --- a/services/webhook/deliver.go +++ b/services/webhook/deliver.go @@ -78,7 +78,7 @@ func Deliver(t *models.HookTask) error { return err } case http.MethodPut: - switch strings.TrimSpace(t.Typ) { + switch t.Typ { case models.MATRIX: req, err = getMatrixHookRequest(t) if err != nil { From 4516d700a1078f620560ddc176d20de85467987e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 27 Dec 2020 11:28:13 +0800 Subject: [PATCH 07/12] Removed wrong changed line --- models/migrations/v163.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/models/migrations/v163.go b/models/migrations/v163.go index b6f96873420b7..150cc34f02e4f 100644 --- a/models/migrations/v163.go +++ b/models/migrations/v163.go @@ -21,10 +21,6 @@ func convertTopicNameFrom25To50(x *xorm.Engine) error { return err } - type HookTask struct { - Typ string `xorm:"VARCHAR(16) index"` - } - sess := x.NewSession() defer sess.Close() if err := sess.Begin(); err != nil { From afebd2fc94c4a9555d62fa68db3e6644df78503f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 28 Dec 2020 00:14:43 +0800 Subject: [PATCH 08/12] Fix nullable --- models/migrations/v165.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/models/migrations/v165.go b/models/migrations/v165.go index 1c34ed8ac3ed6..838a29009df85 100644 --- a/models/migrations/v165.go +++ b/models/migrations/v165.go @@ -20,7 +20,8 @@ func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { SQLType: schemas.SQLType{ Name: "VARCHAR", }, - Length: 16, + Length: 16, + Nullable: true, // To keep compitable as nullable }) if _, err := x.Exec(alterSQL); err != nil { return err @@ -46,7 +47,8 @@ func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { SQLType: schemas.SQLType{ Name: "VARCHAR", }, - Length: 16, + Length: 16, + Nullable: true, // To keep compitable as nullable }) if _, err := x.Exec(alterSQL); err != nil { return err From c30dbd818ff06bafa94227f58fee170ab98b837c Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 28 Dec 2020 12:37:51 +0800 Subject: [PATCH 09/12] Fix lint --- models/migrations/v165.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/migrations/v165.go b/models/migrations/v165.go index 838a29009df85..d85701e2baa5b 100644 --- a/models/migrations/v165.go +++ b/models/migrations/v165.go @@ -21,7 +21,7 @@ func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { Name: "VARCHAR", }, Length: 16, - Nullable: true, // To keep compitable as nullable + Nullable: true, // To keep compatible as nullable }) if _, err := x.Exec(alterSQL); err != nil { return err @@ -48,7 +48,7 @@ func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { Name: "VARCHAR", }, Length: 16, - Nullable: true, // To keep compitable as nullable + Nullable: true, // To keep compatible as nullable }) if _, err := x.Exec(alterSQL); err != nil { return err From b400816acb4be27e4b1fd71985724ccbee07a623 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 1 Jan 2021 23:39:55 +0800 Subject: [PATCH 10/12] Ignore sqlite on migration --- models/migrations/v165.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/models/migrations/v165.go b/models/migrations/v165.go index d85701e2baa5b..ff2759ee18f5b 100644 --- a/models/migrations/v165.go +++ b/models/migrations/v165.go @@ -10,10 +10,14 @@ import ( ) func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { + dbType := x.Dialect().URI().DBType + if dbType == schemas.SQLITE { // For SQLITE, varchar or char will always be represented as TEXT + return nil + } + type HookTask struct { Typ string `xorm:"VARCHAR(16) index"` } - alterSQL := x.Dialect().ModifyColumnSQL("hook_task", &schemas.Column{ Name: "typ", TableName: "hook_task", @@ -28,7 +32,7 @@ func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { } var hookTaskTrimSQL string - if x.Dialect().URI().DBType == schemas.MSSQL { + if dbType == schemas.MSSQL { hookTaskTrimSQL = "UPDATE hook_task SET typ = RTRIM(LTRIM(typ))" } else { hookTaskTrimSQL = "UPDATE hook_task SET typ = TRIM(typ)" @@ -55,7 +59,7 @@ func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { } var webhookTrimSQL string - if x.Dialect().URI().DBType == schemas.MSSQL { + if dbType == schemas.MSSQL { webhookTrimSQL = "UPDATE webhook SET type = RTRIM(LTRIM(type))" } else { webhookTrimSQL = "UPDATE webhook SET type = TRIM(type)" From 134306744a323ed6f623ccf87fd54b5904923dc5 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 5 Jan 2021 17:31:57 +0800 Subject: [PATCH 11/12] Fix mssql modify column failure --- models/migrations/v165.go | 55 ++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/models/migrations/v165.go b/models/migrations/v165.go index ff2759ee18f5b..5c083f238d594 100644 --- a/models/migrations/v165.go +++ b/models/migrations/v165.go @@ -5,10 +5,48 @@ package migrations import ( + "context" + + "code.gitea.io/gitea/modules/log" "xorm.io/xorm" "xorm.io/xorm/schemas" ) +func modifyColumn(x *xorm.Engine, tableName string, col *schemas.Column) error { + var indexes map[string]*schemas.Index + var err error + // MSSQL have to remove index at first, otherwise alter column will fail + // ref. https://sqlzealots.com/2018/05/09/error-message-the-index-is-dependent-on-column-alter-table-alter-column-failed-because-one-or-more-objects-access-this-column/ + if x.Dialect().URI().DBType == schemas.MSSQL { + indexes, err = x.Dialect().GetIndexes(x.DB(), context.Background(), tableName) + if err != nil { + return err + } + + for _, index := range indexes { + _, err = x.Exec(x.Dialect().DropIndexSQL(tableName, index)) + if err != nil { + return err + } + } + } + + defer func() { + for _, index := range indexes { + _, err = x.Exec(x.Dialect().CreateIndexSQL(tableName, index)) + if err != nil { + log.Error("Create index %s on table %s failed: %v", index.Name, tableName, err) + } + } + }() + + alterSQL := x.Dialect().ModifyColumnSQL(tableName, col) + if _, err := x.Exec(alterSQL); err != nil { + return err + } + return nil +} + func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { dbType := x.Dialect().URI().DBType if dbType == schemas.SQLITE { // For SQLITE, varchar or char will always be represented as TEXT @@ -18,16 +56,15 @@ func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { type HookTask struct { Typ string `xorm:"VARCHAR(16) index"` } - alterSQL := x.Dialect().ModifyColumnSQL("hook_task", &schemas.Column{ - Name: "typ", - TableName: "hook_task", + + if err := modifyColumn(x, "hook_task", &schemas.Column{ + Name: "typ", SQLType: schemas.SQLType{ Name: "VARCHAR", }, Length: 16, Nullable: true, // To keep compatible as nullable - }) - if _, err := x.Exec(alterSQL); err != nil { + }); err != nil { return err } @@ -45,16 +82,14 @@ func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { Type string `xorm:"VARCHAR(16) index"` } - alterSQL = x.Dialect().ModifyColumnSQL("webhook", &schemas.Column{ - Name: "type", - TableName: "webhook", + if err := modifyColumn(x, "webhook", &schemas.Column{ + Name: "type", SQLType: schemas.SQLType{ Name: "VARCHAR", }, Length: 16, Nullable: true, // To keep compatible as nullable - }) - if _, err := x.Exec(alterSQL); err != nil { + }); err != nil { return err } From 7fd394dbad74a6cd47a5af5a5cbf1fef0b2c0623 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 5 Jan 2021 20:28:49 +0800 Subject: [PATCH 12/12] Move modifyColumn to migrations.go so that other migrate function could use it --- models/migrations/migrations.go | 38 +++++++++++++++++++++++++++++++++ models/migrations/v165.go | 38 --------------------------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index bc2f9fd07df80..4f3d823b9afa1 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -6,6 +6,7 @@ package migrations import ( + "context" "fmt" "os" "reflect" @@ -16,6 +17,7 @@ import ( "code.gitea.io/gitea/modules/setting" "xorm.io/xorm" + "xorm.io/xorm/schemas" ) const minDBVersion = 70 // Gitea 1.5.3 @@ -739,3 +741,39 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin return nil } + +// modifyColumn will modify column's type or other propertity. SQLITE is not supported +func modifyColumn(x *xorm.Engine, tableName string, col *schemas.Column) error { + var indexes map[string]*schemas.Index + var err error + // MSSQL have to remove index at first, otherwise alter column will fail + // ref. https://sqlzealots.com/2018/05/09/error-message-the-index-is-dependent-on-column-alter-table-alter-column-failed-because-one-or-more-objects-access-this-column/ + if x.Dialect().URI().DBType == schemas.MSSQL { + indexes, err = x.Dialect().GetIndexes(x.DB(), context.Background(), tableName) + if err != nil { + return err + } + + for _, index := range indexes { + _, err = x.Exec(x.Dialect().DropIndexSQL(tableName, index)) + if err != nil { + return err + } + } + } + + defer func() { + for _, index := range indexes { + _, err = x.Exec(x.Dialect().CreateIndexSQL(tableName, index)) + if err != nil { + log.Error("Create index %s on table %s failed: %v", index.Name, tableName, err) + } + } + }() + + alterSQL := x.Dialect().ModifyColumnSQL(tableName, col) + if _, err := x.Exec(alterSQL); err != nil { + return err + } + return nil +} diff --git a/models/migrations/v165.go b/models/migrations/v165.go index 5c083f238d594..d7df0f07a97c1 100644 --- a/models/migrations/v165.go +++ b/models/migrations/v165.go @@ -5,48 +5,10 @@ package migrations import ( - "context" - - "code.gitea.io/gitea/modules/log" "xorm.io/xorm" "xorm.io/xorm/schemas" ) -func modifyColumn(x *xorm.Engine, tableName string, col *schemas.Column) error { - var indexes map[string]*schemas.Index - var err error - // MSSQL have to remove index at first, otherwise alter column will fail - // ref. https://sqlzealots.com/2018/05/09/error-message-the-index-is-dependent-on-column-alter-table-alter-column-failed-because-one-or-more-objects-access-this-column/ - if x.Dialect().URI().DBType == schemas.MSSQL { - indexes, err = x.Dialect().GetIndexes(x.DB(), context.Background(), tableName) - if err != nil { - return err - } - - for _, index := range indexes { - _, err = x.Exec(x.Dialect().DropIndexSQL(tableName, index)) - if err != nil { - return err - } - } - } - - defer func() { - for _, index := range indexes { - _, err = x.Exec(x.Dialect().CreateIndexSQL(tableName, index)) - if err != nil { - log.Error("Create index %s on table %s failed: %v", index.Name, tableName, err) - } - } - }() - - alterSQL := x.Dialect().ModifyColumnSQL(tableName, col) - if _, err := x.Exec(alterSQL); err != nil { - return err - } - return nil -} - func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error { dbType := x.Dialect().URI().DBType if dbType == schemas.SQLITE { // For SQLITE, varchar or char will always be represented as TEXT