Merge pull request #3037 from RedwindA/fix/token-model-limits-length
fix: change token model_limits column from varchar(1024) to text
This commit is contained in:
+67
-7
@@ -250,6 +250,10 @@ func InitLogDB() (err error) {
|
|||||||
func migrateDB() error {
|
func migrateDB() error {
|
||||||
// Migrate price_amount column from float/double to decimal for existing tables
|
// Migrate price_amount column from float/double to decimal for existing tables
|
||||||
migrateSubscriptionPlanPriceAmount()
|
migrateSubscriptionPlanPriceAmount()
|
||||||
|
// Migrate model_limits column from varchar to text for existing tables
|
||||||
|
if err := migrateTokenModelLimitsToText(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
err := DB.AutoMigrate(
|
err := DB.AutoMigrate(
|
||||||
&Channel{},
|
&Channel{},
|
||||||
@@ -445,6 +449,59 @@ PRIMARY KEY (` + "`id`" + `)
|
|||||||
return nil
|
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)
|
// 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
|
// This is safe to run multiple times - it checks the column type first
|
||||||
func migrateSubscriptionPlanPriceAmount() {
|
func migrateSubscriptionPlanPriceAmount() {
|
||||||
@@ -471,9 +528,11 @@ func migrateSubscriptionPlanPriceAmount() {
|
|||||||
if common.UsingPostgreSQL {
|
if common.UsingPostgreSQL {
|
||||||
// PostgreSQL: Check if already decimal/numeric
|
// PostgreSQL: Check if already decimal/numeric
|
||||||
var dataType string
|
var dataType string
|
||||||
DB.Raw(`SELECT data_type FROM information_schema.columns
|
if err := DB.Raw(`SELECT data_type FROM information_schema.columns
|
||||||
WHERE table_name = ? AND column_name = ?`, tableName, columnName).Scan(&dataType)
|
WHERE table_schema = current_schema() AND table_name = ? AND column_name = ?`,
|
||||||
if dataType == "numeric" {
|
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
|
return // Already decimal/numeric
|
||||||
}
|
}
|
||||||
alterSQL = fmt.Sprintf(`ALTER TABLE %s ALTER COLUMN %s TYPE decimal(10,6) USING %s::decimal(10,6)`,
|
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 {
|
} else if common.UsingMySQL {
|
||||||
// MySQL: Check if already decimal
|
// MySQL: Check if already decimal
|
||||||
var columnType string
|
var columnType string
|
||||||
DB.Raw(`SELECT COLUMN_TYPE FROM information_schema.columns
|
if err := DB.Raw(`SELECT COLUMN_TYPE FROM information_schema.columns
|
||||||
WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?`,
|
WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?`,
|
||||||
tableName, columnName).Scan(&columnType)
|
tableName, columnName).Scan(&columnType).Error; err != nil {
|
||||||
if strings.HasPrefix(strings.ToLower(columnType), "decimal") {
|
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
|
return // Already decimal
|
||||||
}
|
}
|
||||||
alterSQL = fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s decimal(10,6) NOT NULL DEFAULT 0",
|
alterSQL = fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s decimal(10,6) NOT NULL DEFAULT 0",
|
||||||
|
|||||||
+1
-1
@@ -23,7 +23,7 @@ type Token struct {
|
|||||||
RemainQuota int `json:"remain_quota" gorm:"default:0"`
|
RemainQuota int `json:"remain_quota" gorm:"default:0"`
|
||||||
UnlimitedQuota bool `json:"unlimited_quota"`
|
UnlimitedQuota bool `json:"unlimited_quota"`
|
||||||
ModelLimitsEnabled bool `json:"model_limits_enabled"`
|
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:''"`
|
AllowIps *string `json:"allow_ips" gorm:"default:''"`
|
||||||
UsedQuota int `json:"used_quota" gorm:"default:0"` // used quota
|
UsedQuota int `json:"used_quota" gorm:"default:0"` // used quota
|
||||||
Group string `json:"group" gorm:"default:''"`
|
Group string `json:"group" gorm:"default:''"`
|
||||||
|
|||||||
Reference in New Issue
Block a user