🐛 fix(db): rename composite unique indexes to avoid drop/recreate on restart
- Model: rename `uk_model_name` -> `uk_model_name_delete_at` (composite on `model_name` + `deleted_at`) - Vendor: rename `uk_vendor_name` -> `uk_vendor_name_delete_at` (composite on `name` + `deleted_at`) - Keep legacy cleanup in `model/main.go` to drop old index names (`uk_model_name`, `model_name`, `uk_vendor_name`, `name`) for compatibility. Result: idempotent GORM migrations and no unnecessary index churn on MySQL restarts. Files: - `model/model_meta.go` - `model/vendor_meta.go`
This commit is contained in:
parent
0d57b1acd4
commit
6d7c00634c
@ -270,9 +270,9 @@ func migrateDB() error {
|
|||||||
|
|
||||||
dropIndexIfExists("vendors", "uk_vendor_name") // 新版复合索引名称(若已存在)
|
dropIndexIfExists("vendors", "uk_vendor_name") // 新版复合索引名称(若已存在)
|
||||||
dropIndexIfExists("vendors", "name") // 旧版列级唯一索引名称
|
dropIndexIfExists("vendors", "name") // 旧版列级唯一索引名称
|
||||||
//if !common.UsingPostgreSQL {
|
// 清理旧索引名(兼容历史),避免与新的复合唯一索引冲突
|
||||||
// return migrateDBFast()
|
// 说明:仅清理旧名 uk_model_name/model_name、uk_vendor_name/name;新索引名 uk_model_name_delete_at/uk_vendor_name_delete_at 不在清理范围
|
||||||
//}
|
// 计划:该兼容逻辑将在后续几个版本中移除
|
||||||
err := DB.AutoMigrate(
|
err := DB.AutoMigrate(
|
||||||
&Channel{},
|
&Channel{},
|
||||||
&Token{},
|
&Token{},
|
||||||
@ -299,8 +299,9 @@ func migrateDB() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func migrateDBFast() error {
|
func migrateDBFast() error {
|
||||||
// 修复旧版本留下的唯一索引,允许软删除后重新插入同名记录
|
// 清理旧索引名(兼容历史),允许软删除后重新插入同名记录
|
||||||
// 删除单列唯一索引(列级 UNIQUE)及早期命名方式,防止与新复合唯一索引冲突
|
// 说明:仅清理旧名 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", "uk_model_name")
|
||||||
dropIndexIfExists("models", "model_name")
|
dropIndexIfExists("models", "model_name")
|
||||||
|
|
||||||
|
|||||||
@ -21,7 +21,7 @@ type BoundChannel struct {
|
|||||||
|
|
||||||
type Model struct {
|
type Model struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
ModelName string `json:"model_name" gorm:"size:128;not null;uniqueIndex:uk_model_name,priority:1"`
|
ModelName string `json:"model_name" gorm:"size:128;not null;uniqueIndex:uk_model_name_delete_at,priority:1"`
|
||||||
Description string `json:"description,omitempty" gorm:"type:text"`
|
Description string `json:"description,omitempty" gorm:"type:text"`
|
||||||
Icon string `json:"icon,omitempty" gorm:"type:varchar(128)"`
|
Icon string `json:"icon,omitempty" gorm:"type:varchar(128)"`
|
||||||
Tags string `json:"tags,omitempty" gorm:"type:varchar(255)"`
|
Tags string `json:"tags,omitempty" gorm:"type:varchar(255)"`
|
||||||
@ -30,7 +30,7 @@ type Model struct {
|
|||||||
Status int `json:"status" gorm:"default:1"`
|
Status int `json:"status" gorm:"default:1"`
|
||||||
CreatedTime int64 `json:"created_time" gorm:"bigint"`
|
CreatedTime int64 `json:"created_time" gorm:"bigint"`
|
||||||
UpdatedTime int64 `json:"updated_time" gorm:"bigint"`
|
UpdatedTime int64 `json:"updated_time" gorm:"bigint"`
|
||||||
DeletedAt gorm.DeletedAt `json:"-" gorm:"index;uniqueIndex:uk_model_name,priority:2"`
|
DeletedAt gorm.DeletedAt `json:"-" gorm:"index;uniqueIndex:uk_model_name_delete_at,priority:2"`
|
||||||
|
|
||||||
BoundChannels []BoundChannel `json:"bound_channels,omitempty" gorm:"-"`
|
BoundChannels []BoundChannel `json:"bound_channels,omitempty" gorm:"-"`
|
||||||
EnableGroups []string `json:"enable_groups,omitempty" gorm:"-"`
|
EnableGroups []string `json:"enable_groups,omitempty" gorm:"-"`
|
||||||
|
|||||||
@ -14,13 +14,13 @@ import (
|
|||||||
|
|
||||||
type Vendor struct {
|
type Vendor struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
Name string `json:"name" gorm:"size:128;not null;uniqueIndex:uk_vendor_name,priority:1"`
|
Name string `json:"name" gorm:"size:128;not null;uniqueIndex:uk_vendor_name_delete_at,priority:1"`
|
||||||
Description string `json:"description,omitempty" gorm:"type:text"`
|
Description string `json:"description,omitempty" gorm:"type:text"`
|
||||||
Icon string `json:"icon,omitempty" gorm:"type:varchar(128)"`
|
Icon string `json:"icon,omitempty" gorm:"type:varchar(128)"`
|
||||||
Status int `json:"status" gorm:"default:1"`
|
Status int `json:"status" gorm:"default:1"`
|
||||||
CreatedTime int64 `json:"created_time" gorm:"bigint"`
|
CreatedTime int64 `json:"created_time" gorm:"bigint"`
|
||||||
UpdatedTime int64 `json:"updated_time" gorm:"bigint"`
|
UpdatedTime int64 `json:"updated_time" gorm:"bigint"`
|
||||||
DeletedAt gorm.DeletedAt `json:"-" gorm:"index;uniqueIndex:uk_vendor_name,priority:2"`
|
DeletedAt gorm.DeletedAt `json:"-" gorm:"index;uniqueIndex:uk_vendor_name_delete_at,priority:2"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert 创建新的供应商记录
|
// Insert 创建新的供应商记录
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user