fix: preserve legacy time casts throughout index migrations

This commit is contained in:
Yvan
2026-11-06 01:44:57 +08:00
parent bf67626830
commit 7c8148a5e5
3 changed files with 65 additions and 5 deletions
+11 -1
View File
@@ -74,7 +74,17 @@ func migrateQueryIndexes(db *gorm.DB, indexes []businessQueryIndex) error {
}
// 同一大表的多个索引合并构建,减少重复扫描。
if len(additions) > 0 {
if err := db.Exec("ALTER TABLE `" + table + "` " + strings.Join(additions, ",") + ", ALGORITHM=INPLACE, LOCK=NONE").Error; err != nil {
build := func(conn *gorm.DB) error {
return conn.Exec("ALTER TABLE `" + table + "` " + strings.Join(additions, ",") + ", ALGORITHM=INPLACE, LOCK=NONE").Error
}
// Later indexes also evaluate the historical generated time column.
// Apply the same connection-local compatibility rule as its first index.
if table == "tab_user" && db.Migrator().HasColumn(table, "sw_register_time_unix") {
err = withLegacyRegistrationCast(db, build)
} else {
err = build(db)
}
if err != nil {
return fmt.Errorf("add query indexes on %s: %w", table, err)
}
}