From a47a37d315f063784fe2775d68b2e16709d944ec Mon Sep 17 00:00:00 2001 From: t0ng7u Date: Tue, 2 Sep 2025 02:24:17 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20refactor(db):=20remove=20legacy?= =?UTF-8?q?=20models/vendors=20index=20cleanup=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Delete dropIndexIfExists helper from `model/main.go` - Remove all calls to dropIndexIfExists in `migrateDB` and `migrateDBFast` - Drop related comments and MySQL-only DROP INDEX code paths - Keep GORM AutoMigrate as the sole migration path for `Model` and `Vendor` Why: - Simplifies migrations and avoids destructive index drops at startup - Prevents noisy MySQL 1091 errors and vendor-specific branches - Aligns with composite unique indexes (uk_model_name_delete_at, uk_vendor_name_delete_at) Impact: - No expected runtime behavior change; schema remains managed by GORM - Legacy single-column unique indexes (if any) will no longer be auto-dropped - Safe across MySQL/PostgreSQL/SQLite; MySQL Chinese charset checks remain intact Verification: - Lint passed for `model/main.go` - Confirmed no remaining `DROP INDEX` or `dropIndexIfExists` references --- model/main.go | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/model/main.go b/model/main.go index 0fe9ceef9..1a38d371b 100644 --- a/model/main.go +++ b/model/main.go @@ -64,22 +64,6 @@ var DB *gorm.DB var LOG_DB *gorm.DB -// dropIndexIfExists drops a MySQL index only if it exists to avoid noisy 1091 errors -func dropIndexIfExists(tableName string, indexName string) { - if !common.UsingMySQL { - return - } - var count int64 - // Check index existence via information_schema - err := DB.Raw( - "SELECT COUNT(1) FROM information_schema.statistics WHERE table_schema = DATABASE() AND table_name = ? AND index_name = ?", - tableName, indexName, - ).Scan(&count).Error - if err == nil && count > 0 { - _ = DB.Exec("ALTER TABLE " + tableName + " DROP INDEX " + indexName + ";").Error - } -} - func createRootAccountIfNeed() error { var user User //if user.Status != common.UserStatusEnabled { @@ -263,16 +247,6 @@ func InitLogDB() (err error) { } func migrateDB() error { - // 修复旧版本留下的唯一索引,允许软删除后重新插入同名记录 - // 删除单列唯一索引(列级 UNIQUE)及早期命名方式,防止与新复合唯一索引 (model_name, deleted_at) 冲突 - dropIndexIfExists("models", "uk_model_name") // 新版复合索引名称(若已存在) - dropIndexIfExists("models", "model_name") // 旧版列级唯一索引名称 - - dropIndexIfExists("vendors", "uk_vendor_name") // 新版复合索引名称(若已存在) - dropIndexIfExists("vendors", "name") // 旧版列级唯一索引名称 - // 清理旧索引名(兼容历史),避免与新的复合唯一索引冲突 - // 说明:仅清理旧名 uk_model_name/model_name、uk_vendor_name/name;新索引名 uk_model_name_delete_at/uk_vendor_name_delete_at 不在清理范围 - // 计划:该兼容逻辑将在后续几个版本中移除 err := DB.AutoMigrate( &Channel{}, &Token{}, @@ -299,14 +273,6 @@ func migrateDB() error { } func migrateDBFast() error { - // 清理旧索引名(兼容历史),允许软删除后重新插入同名记录 - // 说明:仅清理旧名 uk_model_name/model_name、uk_vendor_name/name;新索引名 uk_model_name_delete_at/uk_vendor_name_delete_at 不在清理范围 - // 计划:该兼容逻辑将在后续几个版本中移除 - dropIndexIfExists("models", "uk_model_name") - dropIndexIfExists("models", "model_name") - - dropIndexIfExists("vendors", "uk_vendor_name") - dropIndexIfExists("vendors", "name") var wg sync.WaitGroup