From e51fce58350061c6d4fedc0aae0b8a581ef31a34 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Wed, 18 Aug 2021 11:26:02 +0100 Subject: [PATCH] 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 --- models/migrations/migrations.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 28bf4f6036101..13ed70bc4f26e 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -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 {