fix: 单列唯一索引改用 unique 标签,修复迁移因 DROP uni_ 索引报 1091 中断导致建表失败
- 缺表时直接 CreateTable,避免列级差异中断整表迁移 - packages.currency 手动迁移改用 HasColumn 判断,修正 TEXT 默认值在 MySQL 下非法
This commit is contained in:
@@ -243,7 +243,14 @@ func runMigrations() {
|
||||
&model.CloudVariableRecord{},
|
||||
}
|
||||
for _, m := range models {
|
||||
if err := DB.AutoMigrate(m); err != nil {
|
||||
var err error
|
||||
if DB.Migrator().HasTable(m) {
|
||||
err = DB.AutoMigrate(m)
|
||||
} else {
|
||||
// 表不存在时直接建表,避免个别列级差异中断整表迁移
|
||||
err = DB.Migrator().CreateTable(m)
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("Failed to migrate %T: %v\n", m, err)
|
||||
}
|
||||
}
|
||||
@@ -318,20 +325,9 @@ func runMigrations() {
|
||||
}
|
||||
|
||||
// 手动迁移:检查并添加 packages 表的 currency 列
|
||||
var packageColumns []struct {
|
||||
Name string
|
||||
}
|
||||
DB.Raw("PRAGMA table_info(packages)").Scan(&packageColumns)
|
||||
hasCurrency := false
|
||||
for _, col := range packageColumns {
|
||||
if col.Name == "currency" {
|
||||
hasCurrency = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasCurrency {
|
||||
if !DB.Migrator().HasColumn(&model.Package{}, "currency") {
|
||||
log.Println("Adding currency column to packages table...")
|
||||
if err := DB.Exec("ALTER TABLE packages ADD COLUMN currency TEXT DEFAULT 'CNY'").Error; err != nil {
|
||||
if err := DB.Exec("ALTER TABLE packages ADD COLUMN currency VARCHAR(10) DEFAULT 'CNY'").Error; err != nil {
|
||||
log.Println("Failed to add currency column:", err.Error())
|
||||
} else {
|
||||
log.Println("currency column added successfully")
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
// User 平台用户模型(管理员、代理商)
|
||||
type User struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Username string `gorm:"uniqueIndex;size:50" json:"username"`
|
||||
Email *string `gorm:"uniqueIndex;size:100" json:"email"`
|
||||
Username string `gorm:"unique;size:50" json:"username"`
|
||||
Email *string `gorm:"unique;size:100" json:"email"`
|
||||
Password string `gorm:"size:255" json:"-"`
|
||||
Avatar string `gorm:"size:255" json:"avatar"`
|
||||
Signature string `gorm:"size:255" json:"signature"`
|
||||
@@ -135,7 +135,7 @@ type DynamicCode struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
ApplicationID uint `gorm:"index;not null" json:"application_id"`
|
||||
Name string `gorm:"size:100;not null" json:"name"`
|
||||
Key string `gorm:"size:100;uniqueIndex;not null" json:"key"`
|
||||
Key string `gorm:"size:100;unique;not null" json:"key"`
|
||||
Code string `gorm:"type:longtext;not null" json:"code"`
|
||||
Description string `gorm:"type:text" json:"description"`
|
||||
Status string `gorm:"size:20;default:active" json:"status"`
|
||||
@@ -168,7 +168,7 @@ type Application struct {
|
||||
Name string `gorm:"size:100" json:"name"`
|
||||
Description string `gorm:"type:text" json:"description"`
|
||||
IconURL string `gorm:"size:255" json:"icon_url"`
|
||||
AppKey string `gorm:"uniqueIndex;size:50" json:"app_key"`
|
||||
AppKey string `gorm:"unique;size:50" json:"app_key"`
|
||||
BillingType string `gorm:"size:20" json:"billing_type"`
|
||||
LoginPolicy string `gorm:"size:20;default:loose" json:"login_policy"`
|
||||
EncryptType string `gorm:"size:20" json:"encrypt_type"`
|
||||
@@ -299,7 +299,7 @@ type Card struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
ApplicationID uint `gorm:"index;not null" json:"application_id"`
|
||||
CardTypeID uint `gorm:"index;not null" json:"card_type_id"`
|
||||
CardKey string `gorm:"uniqueIndex;size:100" json:"card_key"`
|
||||
CardKey string `gorm:"unique;size:100" json:"card_key"`
|
||||
CreatorID uint `gorm:"index" json:"creator_id"`
|
||||
AgentID *uint `gorm:"index" json:"agent_id"`
|
||||
AppUserID *uint `gorm:"index" json:"app_user_id"`
|
||||
@@ -336,7 +336,7 @@ type Announcement struct {
|
||||
// Order 订单模型
|
||||
type Order struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
OrderNo string `gorm:"uniqueIndex;size:100" json:"order_no"`
|
||||
OrderNo string `gorm:"unique;size:100" json:"order_no"`
|
||||
UserID uint `gorm:"index" json:"user_id"`
|
||||
ApplicationID *uint `gorm:"index" json:"application_id"`
|
||||
PackageID *uint `json:"package_id"`
|
||||
@@ -391,7 +391,7 @@ type Log struct {
|
||||
type RechargeRecord struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
UserID uint `gorm:"index" json:"user_id"`
|
||||
OrderNo string `gorm:"size:50;uniqueIndex" json:"order_no"`
|
||||
OrderNo string `gorm:"size:50;unique" json:"order_no"`
|
||||
CardID *uint `json:"card_id"`
|
||||
CardCode string `gorm:"size:100" json:"card_code"`
|
||||
Amount float64 `gorm:"type:decimal(10,2)" json:"amount"`
|
||||
@@ -411,7 +411,7 @@ type ConsumptionRecord struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
UserID uint `gorm:"index" json:"user_id"`
|
||||
ApplicationID uint `gorm:"index" json:"application_id"`
|
||||
OrderNo string `gorm:"size:50;uniqueIndex" json:"order_no"`
|
||||
OrderNo string `gorm:"size:50;unique" json:"order_no"`
|
||||
Type string `gorm:"size:50" json:"type"` // verification, card_purchase, subscription, feature, manual_deduct
|
||||
Content string `gorm:"type:text" json:"content"`
|
||||
Description string `gorm:"type:text" json:"description"`
|
||||
@@ -610,7 +610,7 @@ type DocCategory struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Name string `gorm:"size:100" json:"name"`
|
||||
NameEn string `gorm:"size:100" json:"name_en"`
|
||||
Slug string `gorm:"size:100;uniqueIndex" json:"slug"`
|
||||
Slug string `gorm:"size:100;unique" json:"slug"`
|
||||
Description string `gorm:"size:255" json:"description"`
|
||||
DescriptionEn string `gorm:"size:255" json:"description_en"`
|
||||
Icon string `gorm:"size:50;default:file-text" json:"icon"`
|
||||
@@ -715,7 +715,7 @@ type Doc struct {
|
||||
CategoryID *uint `json:"category_id"`
|
||||
Title string `gorm:"size:200" json:"title"`
|
||||
TitleEn string `gorm:"size:200" json:"title_en"`
|
||||
Slug string `gorm:"size:200;uniqueIndex" json:"slug"`
|
||||
Slug string `gorm:"size:200;unique" json:"slug"`
|
||||
Content string `gorm:"type:longtext" json:"content"`
|
||||
ContentEn string `gorm:"type:longtext" json:"content_en"`
|
||||
Summary string `gorm:"size:500" json:"summary"`
|
||||
@@ -737,7 +737,7 @@ type Doc struct {
|
||||
type Setting struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Category string `gorm:"size:50;index" json:"category"` // basic, email, sms, payment
|
||||
Key string `gorm:"size:100;uniqueIndex" json:"key"`
|
||||
Key string `gorm:"size:100;unique" json:"key"`
|
||||
Value string `gorm:"type:text" json:"value"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
@@ -746,7 +746,7 @@ type Setting struct {
|
||||
// Captcha 验证码模型
|
||||
type Captcha struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
CaptchaID string `gorm:"uniqueIndex;size:50" json:"captcha_id"`
|
||||
CaptchaID string `gorm:"unique;size:50" json:"captcha_id"`
|
||||
Code string `gorm:"size:10" json:"code"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
@@ -776,7 +776,7 @@ type SmsVerifyCode struct {
|
||||
|
||||
type AppSMTPConfig struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
ApplicationID uint `gorm:"uniqueIndex" json:"application_id"`
|
||||
ApplicationID uint `gorm:"unique" json:"application_id"`
|
||||
Host string `gorm:"size:100" json:"host"`
|
||||
Port int `gorm:"default:465" json:"port"`
|
||||
User string `gorm:"size:100" json:"user"`
|
||||
@@ -930,7 +930,7 @@ type ExtensionAPIKey struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
ApplicationID uint `gorm:"index;not null" json:"application_id"`
|
||||
Name string `gorm:"size:100;not null" json:"name"`
|
||||
AccessKey string `gorm:"uniqueIndex;size:64;not null" json:"access_key"`
|
||||
AccessKey string `gorm:"unique;size:64;not null" json:"access_key"`
|
||||
SecretKey string `gorm:"size:64;not null" json:"secret_key"`
|
||||
Permissions string `gorm:"type:text" json:"permissions"` // JSON数组存储权限列表
|
||||
Status string `gorm:"size:20;default:active" json:"status"`
|
||||
|
||||
Reference in New Issue
Block a user