Skip to content

Commit e51fce5

Browse files
committed
Recreate Tables should Recreate indexes on MySQL
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 274aeb3 commit e51fce5

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
@@ -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)