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 <art27@cantab.net>
This commit is contained in:
zeripath 2021-08-21 03:17:07 +01:00 committed by GitHub
parent bb054fdfa1
commit bc1fefce87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -565,11 +565,26 @@ func recreateTable(sess *xorm.Session, bean interface{}) error {
return err 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 // 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 { 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) log.Error("Unable to rename %s to %s. Error: %v", tempTableName, tableName, err)
return 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: case setting.Database.UsePostgreSQL:
var originalSequences []string var originalSequences []string
type sequenceData struct { type sequenceData struct {