Skip to content

fix: change token model_limits column from varchar(1024) to text - #3037

Merged
Calcium-Ion merged 3 commits into
QuantumNous:mainfrom
RedwindA:fix/token-model-limits-length
Mar 2, 2026
Merged

fix: change token model_limits column from varchar(1024) to text#3037
Calcium-Ion merged 3 commits into
QuantumNous:mainfrom
RedwindA:fix/token-model-limits-length

Conversation

@RedwindA

@RedwindA RedwindA commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

PR 类型

  • Bug 修复
  • 新功能
  • 文档更新
  • 其他

PR 是否包含破坏性更新?

PR 描述

fix #3033

Summary by CodeRabbit

  • Bug Fixes

    • Model limit field now supports extended content length, preventing truncation.
  • Chores

    • Database migration added to convert existing model limit data to the new format, with safer checks and logging to preserve values during upgrade.

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.
@coderabbitai

coderabbitai Bot commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 52c29e7 and 43e068c.

📒 Files selected for processing (1)
  • model/main.go

Walkthrough

Change updates Token.ModelLimits GORM tag from varchar(1024) to text and adds a dialect-aware migration function to detect and alter tokens.model_limits to text for Postgres/MySQL (skips SQLite), integrated into the DB migration flow.

Changes

Cohort / File(s) Summary
Token model
model/token.go
Updated Token.ModelLimits struct tag from gorm:"type:varchar(1024);default:''" to gorm:"type:text".
DB migration logic
model/main.go
Added migrateTokenModelLimitsToText and integrated it into migrateDB. Function checks table/column existence, queries information_schema (dialect-aware), and runs ALTER statements for Postgres/MySQL; skips SQLite and logs outcomes. Also tightened metadata queries and error logging in existing subscription price migration logic.

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
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Suggested reviewers

  • seefs001

Poem

🐰 I sniffed a varchar wall, too tight and small,
I hopped a patch to text where tokens sprawl.
Columns widened, migrations ran—hip, hooray!
No more truncation on my data buffet. ✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Out of Scope Changes check ❓ Inconclusive The PR includes an incidental refinement to migrateSubscriptionPlanPriceAmount with improved error handling and metadata querying, which is tangentially related but not directly required by issue #3033. Clarify whether the migrateSubscriptionPlanPriceAmount refinement is intentional or should be separated into a distinct PR to maintain focus on the primary objective.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: converting the Token.ModelLimits column from varchar(1024) to text, directly addressing the core issue.
Linked Issues check ✅ Passed The PR fully addresses issue #3033 by changing ModelLimits from varchar(1024) to text and adding database migration logic to handle the conversion across PostgreSQL and MySQL.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 982dc5c and db8534b.

📒 Files selected for processing (1)
  • model/token.go

Comment thread model/token.go
@seefs001

Copy link
Copy Markdown
Collaborator

这个得在代码里加迁移代码改这个表,用gorm跑sql语句,gorm的AutoMigrate不处理这种varchat -> text

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between db8534b and 52c29e7.

📒 Files selected for processing (1)
  • model/main.go

Comment thread model/main.go Outdated
Comment thread model/main.go Outdated
@Calcium-Ion
Calcium-Ion merged commit e593c11 into QuantumNous:main Mar 2, 2026
1 check passed
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
…its-length

fix: change token model_limits column from varchar(1024) to text
@RedwindA
RedwindA deleted the fix/token-model-limits-length branch July 23, 2026 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/api/token/ 接口访问报错

3 participants