Skip to content

Commit bc1fefc

Browse files
authored
Recreate Tables should Recreate indexes on MySQL (#16718) (#16740)
Backport #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 <[email protected]>
1 parent bb054fd commit bc1fefc

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

models/migrations/migrations.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,11 +565,26 @@ func recreateTable(sess *xorm.Session, bean interface{}) error {
565565
return err
566566
}
567567

568+
if err := sess.Table(tempTableName).DropIndexes(bean); err != nil {
569+
log.Error("Unable to drop indexes on temporary table %s. Error: %v", tempTableName, err)
570+
return err
571+
}
572+
568573
// SQLite and MySQL will move all the constraints from the temporary table to the new table
569574
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` RENAME TO `%s`", tempTableName, tableName)); err != nil {
570575
log.Error("Unable to rename %s to %s. Error: %v", tempTableName, tableName, err)
571576
return err
572577
}
578+
579+
if err := sess.Table(tableName).CreateIndexes(bean); err != nil {
580+
log.Error("Unable to recreate indexes on table %s. Error: %v", tableName, err)
581+
return err
582+
}
583+
584+
if err := sess.Table(tableName).CreateUniques(bean); err != nil {
585+
log.Error("Unable to recreate uniques on table %s. Error: %v", tableName, err)
586+
return err
587+
}
573588
case setting.Database.UsePostgreSQL:
574589
var originalSequences []string
575590
type sequenceData struct {

0 commit comments

Comments
 (0)