Skip to content

Commit c9bca8c

Browse files
zeripath6543
andauthored
Recreate Tables should Recreate indexes on MySQL (#16718)
The MySQL indexes are not being renamed at the same time as RENAME table despite the CASCADE. Therefore it is probably better to just recreate the indexes instead. Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de>
1 parent 4aa3cac commit c9bca8c

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

models/migrations/migrations.go

+15
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,26 @@ func recreateTable(sess *xorm.Session, bean interface{}) error {
597597
return err
598598
}
599599

600+
if err := sess.Table(tempTableName).DropIndexes(bean); err != nil {
601+
log.Error("Unable to drop indexes on temporary table %s. Error: %v", tempTableName, err)
602+
return err
603+
}
604+
600605
// SQLite and MySQL will move all the constraints from the temporary table to the new table
601606
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` RENAME TO `%s`", tempTableName, tableName)); err != nil {
602607
log.Error("Unable to rename %s to %s. Error: %v", tempTableName, tableName, err)
603608
return err
604609
}
610+
611+
if err := sess.Table(tableName).CreateIndexes(bean); err != nil {
612+
log.Error("Unable to recreate indexes on table %s. Error: %v", tableName, err)
613+
return err
614+
}
615+
616+
if err := sess.Table(tableName).CreateUniques(bean); err != nil {
617+
log.Error("Unable to recreate uniques on table %s. Error: %v", tableName, err)
618+
return err
619+
}
605620
case setting.Database.UsePostgreSQL:
606621
var originalSequences []string
607622
type sequenceData struct {

0 commit comments

Comments
 (0)