fix: change token model_limits column from varchar(1024) to text - #3037
Conversation
Fixes QuantumNous#3033 — users with many model limits hit PostgreSQL's varchar length constraint. The text type is supported across all three databases (SQLite, MySQL, PostgreSQL) with no length restriction.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
WalkthroughChange updates Token.ModelLimits GORM tag from Changes
Sequence Diagram(s)sequenceDiagram
participant Migrator as Migrator
participant DB as Database
participant Logger as Logger
Migrator->>DB: Check existence of table `tokens` and column `model_limits`
DB-->>Migrator: Exists / Not found
alt exists
Migrator->>DB: Query column type (information_schema / dialect-specific)
DB-->>Migrator: Return current type
alt type != text and dialect = postgres
Migrator->>DB: Execute ALTER TABLE tokens ALTER COLUMN model_limits TYPE text
else type != text and dialect = mysql
Migrator->>DB: Execute ALTER TABLE tokens MODIFY COLUMN model_limits text
else dialect = sqlite or type = text
Migrator->>Logger: Skip migration
end
DB-->>Migrator: Success / Error
Migrator->>Logger: Log success or warning
else
Migrator->>Logger: Log missing table/column and skip
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@model/token.go`:
- Line 26: You changed the Token struct field ModelLimits to use
`gorm:"type:text"` (ModelLimits) but AutoMigrate won't alter existing column
types; add a migration to ensure tokens.model_limits is converted from
varchar(1024) to text. Implement a migration similar to the existing pattern
(e.g., add a new function alongside migrateSubscriptionPlanPriceAmount() and
register it in model/main.go before AutoMigrate()), or add an explicit SQL
migration file for the release that runs ALTER TABLE/appropriate DROP+ADD for
SQLite/MySQL/Postgres to convert tokens.model_limits to TEXT, and ensure the
migration is idempotent and tested on SQLite/MySQL/Postgres.
|
这个得在代码里加迁移代码改这个表,用gorm跑sql语句,gorm的AutoMigrate不处理这种varchat -> text |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@model/main.go`:
- Around line 253-254: The migrateTokenModelLimitsToText function currently
swallows DB.Exec(alterSQL) errors and only logs them; change
migrateTokenModelLimitsToText to return an error (update its signature) and
return the DB.Exec error when it fails (including context), then update the
startup call site (where migrateTokenModelLimitsToText() is invoked) to check
the returned error and propagate or abort startup (e.g., return the error from
main init path or exit), so migration failures do not get ignored; refer to
migrateTokenModelLimitsToText and the DB.Exec(alterSQL) call when making these
changes.
- Around line 472-473: Update the PostgreSQL metadata query to include a schema
filter by adding "table_schema = current_schema()" to the DB.Raw SELECT that
fetches data_type (the query currently using WHERE table_name = ? AND
column_name = ?), and in both places where you call .Scan(&dataType) (the
metadata fetch in the main migration and the similar metadata fetch in
migrateSubscriptionPlanPriceAmount) check the returned .Error and, on error, log
a warning following the existing warning pattern used at lines ~495-498 instead
of silently continuing; apply the same .Error check and warning logging to the
MySQL/Postgres metadata .Scan() calls referenced in these functions so failed
metadata queries are surfaced.
…and improve migration checks
…its-length fix: change token model_limits column from varchar(1024) to text
PR 类型
PR 是否包含破坏性更新?
PR 描述
fix #3033
Summary by CodeRabbit
Bug Fixes
Chores