Skip to content

Commit 797b36c

Browse files
zeripathtechknowlogick
authored and
Gitea
committed
Recreate Tables should Recreate indexes on MySQL (go-gitea#16718) (go-gitea#16739)
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> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
1 parent 6acc917 commit 797b36c

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

models/migrations/migrations.go

+15
Original file line numberDiff line numberDiff line change
@@ -590,11 +590,26 @@ func recreateTable(sess *xorm.Session, bean interface{}) error {
590590
return err
591591
}
592592

593+
if err := sess.Table(tempTableName).DropIndexes(bean); err != nil {
594+
log.Error("Unable to drop indexes on temporary table %s. Error: %v", tempTableName, err)
595+
return err
596+
}
597+
593598
// SQLite and MySQL will move all the constraints from the temporary table to the new table
594599
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` RENAME TO `%s`", tempTableName, tableName)); err != nil {
595600
log.Error("Unable to rename %s to %s. Error: %v", tempTableName, tableName, err)
596601
return err
597602
}
603+
604+
if err := sess.Table(tableName).CreateIndexes(bean); err != nil {
605+
log.Error("Unable to recreate indexes on table %s. Error: %v", tableName, err)
606+
return err
607+
}
608+
609+
if err := sess.Table(tableName).CreateUniques(bean); err != nil {
610+
log.Error("Unable to recreate uniques on table %s. Error: %v", tableName, err)
611+
return err
612+
}
598613
case setting.Database.UsePostgreSQL:
599614
var originalSequences []string
600615
type sequenceData struct {

0 commit comments

Comments
 (0)