From 52c29e75829f145337bea23d64fb9fc991571c29 Mon Sep 17 00:00:00 2001 From: RedwindA Date: Sat, 28 Feb 2026 18:49:06 +0800 Subject: [PATCH] fix: migrate model_limits column from varchar(1024) to text for existing tables --- model/main.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/model/main.go b/model/main.go index 21a5d4c08..fc2b4ff3d 100644 --- a/model/main.go +++ b/model/main.go @@ -250,6 +250,8 @@ 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 + migrateTokenModelLimitsToText() err := DB.AutoMigrate( &Channel{}, @@ -445,6 +447,56 @@ 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() { + // SQLite uses type affinity, so TEXT and VARCHAR are effectively the same — no migration needed + if common.UsingSQLite { + return + } + + tableName := "tokens" + columnName := "model_limits" + + if !DB.Migrator().HasTable(tableName) { + return + } + + if !DB.Migrator().HasColumn(&Token{}, columnName) { + return + } + + var alterSQL string + if common.UsingPostgreSQL { + var dataType string + DB.Raw(`SELECT data_type FROM information_schema.columns + WHERE table_name = ? AND column_name = ?`, tableName, columnName).Scan(&dataType) + if dataType == "text" { + return + } + alterSQL = fmt.Sprintf(`ALTER TABLE %s ALTER COLUMN %s TYPE text`, tableName, columnName) + } else if common.UsingMySQL { + 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.ToLower(columnType) == "text" { + return + } + alterSQL = fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s text", tableName, columnName) + } else { + return + } + + if alterSQL != "" { + if err := DB.Exec(alterSQL).Error; err != nil { + common.SysLog(fmt.Sprintf("Warning: failed to migrate %s.%s to text: %v", tableName, columnName, err)) + } else { + common.SysLog(fmt.Sprintf("Successfully migrated %s.%s to text", tableName, columnName)) + } + } +} + // 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() {