From d87dbd751908051f1ca28c6658ef5170cfe8f214 Mon Sep 17 00:00:00 2001 From: zeripath Date: Mon, 10 Aug 2020 14:01:10 +0100 Subject: [PATCH] Fix Migration 145 on MSSQL if varchar is changed to nvarchar (#12445) * Handle MSSQL constraints Signed-off-by: Andrew Thornton * Update models/migrations/v145.go * Prevent hang with Sync2 * Agh it's uniques not indexes * Let us try a different way of finding constraints on the language column Signed-off-by: Andrew Thornton * minor simplification Signed-off-by: Andrew Thornton * Try drop index instead? Signed-off-by: Andrew Thornton --- models/migrations/v145.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/models/migrations/v145.go b/models/migrations/v145.go index 436c95fab..ee79c20e9 100644 --- a/models/migrations/v145.go +++ b/models/migrations/v145.go @@ -47,9 +47,31 @@ func increaseLanguageField(x *xorm.Engine) error { return err } case setting.Database.UseMSSQL: + // Yet again MSSQL just has to be awkward. + // Here we have to drop the constraints first and then rebuild them + constraints := make([]string, 0) + if err := sess.SQL(`SELECT i.name AS Name + FROM sys.indexes i INNER JOIN sys.index_columns ic + ON i.index_id = ic.index_id AND i.object_id = ic.object_id + INNER JOIN sys.tables AS t + ON t.object_id = i.object_id + INNER JOIN sys.columns c + ON t.object_id = c.object_id AND ic.column_id = c.column_id + WHERE t.name = 'language_stat' AND c.name = 'language'`).Find(&constraints); err != nil { + return fmt.Errorf("Find constraints: %v", err) + } + for _, constraint := range constraints { + if _, err := sess.Exec(fmt.Sprintf("DROP INDEX [%s] ON `language_stat`", constraint)); err != nil { + return fmt.Errorf("Drop table `language_stat` constraint `%s`: %v", constraint, err) + } + } if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE language_stat ALTER COLUMN language %s", sqlType)); err != nil { return err } + // Finally restore the constraint + if err := sess.CreateUniques(new(LanguageStat)); err != nil { + return err + } case setting.Database.UsePostgreSQL: if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE language_stat ALTER COLUMN language TYPE %s", sqlType)); err != nil { return err