|
|
@@ -64,6 +64,22 @@ 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 {
|
|
|
@@ -236,11 +252,8 @@ func InitLogDB() (err error) {
|
|
|
|
|
|
func migrateDB() error {
|
|
|
// 修复旧版本留下的唯一索引,允许软删除后重新插入同名记录
|
|
|
- if common.UsingMySQL {
|
|
|
- // 旧索引可能不存在,忽略删除错误即可
|
|
|
- _ = DB.Exec("ALTER TABLE models DROP INDEX uk_model_name;").Error
|
|
|
- _ = DB.Exec("ALTER TABLE vendors DROP INDEX uk_vendor_name;").Error
|
|
|
- }
|
|
|
+ dropIndexIfExists("models", "uk_model_name")
|
|
|
+ dropIndexIfExists("vendors", "uk_vendor_name")
|
|
|
if !common.UsingPostgreSQL {
|
|
|
return migrateDBFast()
|
|
|
}
|
|
|
@@ -271,10 +284,8 @@ func migrateDB() error {
|
|
|
|
|
|
func migrateDBFast() error {
|
|
|
// 修复旧版本留下的唯一索引,允许软删除后重新插入同名记录
|
|
|
- if common.UsingMySQL {
|
|
|
- _ = DB.Exec("ALTER TABLE models DROP INDEX uk_model_name;").Error
|
|
|
- _ = DB.Exec("ALTER TABLE vendors DROP INDEX uk_vendor_name;").Error
|
|
|
- }
|
|
|
+ dropIndexIfExists("models", "uk_model_name")
|
|
|
+ dropIndexIfExists("vendors", "uk_vendor_name")
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|