Skip to content

Fix Migration 145 on MSSQL if varchar is changed to nvarchar #12445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 10, 2020
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions models/migrations/v145.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,29 @@ 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 Name FROM SYS.DEFAULT_CONSTRAINTS WHERE " +
"PARENT_OBJECT_ID = OBJECT_ID('language_stat') AND " +
"PARENT_COLUMN_ID IN (SELECT column_id FROM sys.columns " +
"WHERE lower(NAME) = 'language' AND " +
"object_id = OBJECT_ID('language_stat'))").Find(&constraints); err != nil {
return fmt.Errorf("Find constraints: %v", err)
}
for _, constraint := range constraints {
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `language_stat` DROP CONSTRAINT `%s`", 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.CreateIndexes(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
Expand Down