Skip to content

Commit

Permalink
Recreate Tables should Recreate indexes on MySQL (#16718)
Browse files Browse the repository at this point in the history
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]>

Co-authored-by: 6543 <[email protected]>
  • Loading branch information
zeripath and 6543 authored Aug 19, 2021
1 parent 4aa3cac commit c9bca8c
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,11 +597,26 @@ func recreateTable(sess *xorm.Session, bean interface{}) error {
return err
}

if err := sess.Table(tempTableName).DropIndexes(bean); err != nil {
log.Error("Unable to drop indexes on temporary table %s. Error: %v", tempTableName, err)
return err
}

// SQLite and MySQL will move all the constraints from the temporary table to the new table
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` RENAME TO `%s`", tempTableName, tableName)); err != nil {
log.Error("Unable to rename %s to %s. Error: %v", tempTableName, tableName, err)
return err
}

if err := sess.Table(tableName).CreateIndexes(bean); err != nil {
log.Error("Unable to recreate indexes on table %s. Error: %v", tableName, err)
return err
}

if err := sess.Table(tableName).CreateUniques(bean); err != nil {
log.Error("Unable to recreate uniques on table %s. Error: %v", tableName, err)
return err
}
case setting.Database.UsePostgreSQL:
var originalSequences []string
type sequenceData struct {
Expand Down

0 comments on commit c9bca8c

Please sign in to comment.