diff --git a/model/main.go b/model/main.go index 21a5d4c08..f37cb667c 100644 --- a/model/main.go +++ b/model/main.go @@ -250,6 +250,10 @@ func InitLogDB() (err error) { func migrateDB() error { // Migrate price_amount column from float/double to decimal for existing tables migrateSubscriptionPlanPriceAmount() + // Migrate model_limits column from varchar to text for existing tables + if err := migrateTokenModelLimitsToText(); err != nil { + return err + } err := DB.AutoMigrate( &Channel{}, @@ -445,6 +449,59 @@ PRIMARY KEY (` + "`id`" + `) return nil } +// migrateTokenModelLimitsToText migrates model_limits column from varchar(1024) to text +// This is safe to run multiple times - it checks the column type first +func migrateTokenModelLimitsToText() error { + // SQLite uses type affinity, so TEXT and VARCHAR are effectively the same — no migration needed + if common.UsingSQLite { + return nil + } + + tableName := "tokens" + columnName := "model_limits" + + if !DB.Migrator().HasTable(tableName) { + return nil + } + + if !DB.Migrator().HasColumn(&Token{}, columnName) { + return nil + } + + var alterSQL string + if common.UsingPostgreSQL { + var dataType string + if err := DB.Raw(`SELECT data_type FROM information_schema.columns + WHERE table_schema = current_schema() AND table_name = ? AND column_name = ?`, + tableName, columnName).Scan(&dataType).Error; err != nil { + common.SysLog(fmt.Sprintf("Warning: failed to query metadata for %s.%s: %v", tableName, columnName, err)) + } else if dataType == "text" { + return nil + } + alterSQL = fmt.Sprintf(`ALTER TABLE %s ALTER COLUMN %s TYPE text`, tableName, columnName) + } else if common.UsingMySQL { + var columnType string + if err := DB.Raw(`SELECT COLUMN_TYPE FROM information_schema.columns + WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?`, + tableName, columnName).Scan(&columnType).Error; err != nil { + common.SysLog(fmt.Sprintf("Warning: failed to query metadata for %s.%s: %v", tableName, columnName, err)) + } else if strings.ToLower(columnType) == "text" { + return nil + } + alterSQL = fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s text", tableName, columnName) + } else { + return nil + } + + if alterSQL != "" { + if err := DB.Exec(alterSQL).Error; err != nil { + return fmt.Errorf("failed to migrate %s.%s to text: %w", tableName, columnName, err) + } + common.SysLog(fmt.Sprintf("Successfully migrated %s.%s to text", tableName, columnName)) + } + return nil +} + // migrateSubscriptionPlanPriceAmount migrates price_amount column from float/double to decimal(10,6) // This is safe to run multiple times - it checks the column type first func migrateSubscriptionPlanPriceAmount() { @@ -471,9 +528,11 @@ func migrateSubscriptionPlanPriceAmount() { if common.UsingPostgreSQL { // PostgreSQL: Check if already decimal/numeric var dataType string - DB.Raw(`SELECT data_type FROM information_schema.columns - WHERE table_name = ? AND column_name = ?`, tableName, columnName).Scan(&dataType) - if dataType == "numeric" { + if err := DB.Raw(`SELECT data_type FROM information_schema.columns + WHERE table_schema = current_schema() AND table_name = ? AND column_name = ?`, + tableName, columnName).Scan(&dataType).Error; err != nil { + common.SysLog(fmt.Sprintf("Warning: failed to query metadata for %s.%s: %v", tableName, columnName, err)) + } else if dataType == "numeric" { return // Already decimal/numeric } alterSQL = fmt.Sprintf(`ALTER TABLE %s ALTER COLUMN %s TYPE decimal(10,6) USING %s::decimal(10,6)`, @@ -481,10 +540,11 @@ func migrateSubscriptionPlanPriceAmount() { } else if common.UsingMySQL { // MySQL: Check if already decimal var columnType string - DB.Raw(`SELECT COLUMN_TYPE FROM information_schema.columns - WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?`, - tableName, columnName).Scan(&columnType) - if strings.HasPrefix(strings.ToLower(columnType), "decimal") { + if err := DB.Raw(`SELECT COLUMN_TYPE FROM information_schema.columns + WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?`, + tableName, columnName).Scan(&columnType).Error; err != nil { + common.SysLog(fmt.Sprintf("Warning: failed to query metadata for %s.%s: %v", tableName, columnName, err)) + } else if strings.HasPrefix(strings.ToLower(columnType), "decimal") { return // Already decimal } alterSQL = fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s decimal(10,6) NOT NULL DEFAULT 0", diff --git a/model/token.go b/model/token.go index 773b2d792..1c9ad3ed7 100644 --- a/model/token.go +++ b/model/token.go @@ -23,7 +23,7 @@ type Token struct { RemainQuota int `json:"remain_quota" gorm:"default:0"` UnlimitedQuota bool `json:"unlimited_quota"` ModelLimitsEnabled bool `json:"model_limits_enabled"` - ModelLimits string `json:"model_limits" gorm:"type:varchar(1024);default:''"` + ModelLimits string `json:"model_limits" gorm:"type:text"` AllowIps *string `json:"allow_ips" gorm:"default:''"` UsedQuota int `json:"used_quota" gorm:"default:0"` // used quota Group string `json:"group" gorm:"default:''"`