Skip to content

Go: update db model - #14423

Merged
JinHai-CN merged 2 commits into
infiniflow:mainfrom
JinHai-CN:fix403
Apr 28, 2026
Merged

JinHai-CN merged 2 commits into
infiniflow:mainfrom
JinHai-CN:fix403

Conversation

@JinHai-CN

Copy link
Copy Markdown
Member

What problem does this PR solve?

As title.

Type of change

  • Refactoring

Signed-off-by: Jin Hai <[email protected]>
@JinHai-CN JinHai-CN added the ci Continue Integration label Apr 28, 2026
@coderabbitai

coderabbitai Bot commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Two entity models are updated to include or modify Extra string fields. TenantModel gains a new Extra field persisted to a 1024-character database column with a JSON object default. TenantModelInstance.Extra has its database default value corrected from 'active' to '{}'.

Changes

Cohort / File(s) Summary
Entity Model Field Updates
internal/entity/tenant_model.go, internal/entity/tenant_model_instance.go
Adds Extra field to TenantModel with default '{}' and 1024-char size; updates TenantModelInstance.Extra default from 'active' to '{}'.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A new field hops into the schema,
Extra data, oh what a dream-a!
From 'active' strings we now break free,
JSON objects, {} for you and me!
bounces happily 🌱

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Description check ⚠️ Warning The pull request description is largely incomplete and provides minimal context, with the author deferring to the title ('As title') and offering no substantive explanation of the changes. Provide a detailed description explaining what problem the Extra field solves, why the default value changes were necessary, and any relevant context for reviewers.
Title check ❓ Inconclusive The title 'Go: update db model' is vague and generic, using non-descriptive terms that don't clearly convey the specific changes made to the database model. Refine the title to be more specific, such as 'Add Extra field to TenantModel and update default values' to clearly communicate the main changes.
✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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


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.

@JinHai-CN
JinHai-CN marked this pull request as ready for review April 28, 2026 07:08
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Apr 28, 2026

@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 `@internal/entity/tenant_model_instance.go`:
- Line 26: The schema change only sets a default for new rows but leaves legacy
values ('active', NULL, empty) in tenant_model_instance.extra that break JSON
unmarshaling; add a DB migration/backfill that updates existing rows (e.g.
UPDATE tenant_model_instance SET extra='{}' WHERE extra='active' OR extra IS
NULL OR extra='') and ensure this migration runs before the code that unmarshals
extra in internal/service/model_service.go (the methods that read/unmarshal
tenant_model_instance.Extra). Apply the change as a reversible migration and
include tests or a one-time run step in deployment to guarantee no existing rows
retain legacy values.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 12418665-f8c3-4431-a03e-c424d0af9e1a

📥 Commits

Reviewing files that changed from the base of the PR and between 18fbfaf and a5bf336.

📒 Files selected for processing (2)
  • internal/entity/tenant_model.go
  • internal/entity/tenant_model_instance.go

APIKey string `gorm:"column:api_key;size:512;not null;uniqueIndex:idx_api_key_provider_id" json:"api_key"`
Status string `gorm:"column:status;size:32;default:'active'" json:"status"`
Extra string `gorm:"column:extra;size:512;default:'active'" json:"extra"`
Extra string `gorm:"column:extra;size:512;default:'{}'" json:"extra"`

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.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Verify whether migration/backfill for tenant_model_instance.extra already exists.
fd -i 'migrat|schema|sql|ddl'

rg -n -C3 "tenant_model_instance|\\bextra\\b|default:'\\{\\}'|default:'active'|UPDATE\\s+tenant_model_instance\\s+SET\\s+extra"

# Verify runtime paths that unmarshal Extra as JSON (to confirm impact)
rg -n -C3 "json\\.Unmarshal\\(\\[\\]byte\\(instance\\.Extra\\)|instance\\.Extra"

Repository: infiniflow/ragflow

Length of output: 50378


🏁 Script executed:

# Find the tenant_model_instance.go file and examine it
find . -name "tenant_model_instance.go" -type f | head -5

Repository: infiniflow/ragflow

Length of output: 145


🏁 Script executed:

# Look at the Go struct definition
rg -n "Extra.*string.*gorm" internal/entity/tenant_model_instance.go -A 1 -B 1

Repository: infiniflow/ragflow

Length of output: 243


🏁 Script executed:

# Check for JSON unmarshal patterns in model_service.go
rg -n "json\.Unmarshal.*Extra|instance\.Extra" internal/service/model_service.go -B 2 -A 2

Repository: infiniflow/ragflow

Length of output: 1695


🏁 Script executed:

# Check the migration script for any backfill or UPDATE statements related to extra field
rg -n "UPDATE.*tenant_model_instance|extra.*=|backfill" tools/scripts/mysql_migration.py -B 2 -A 2

Repository: infiniflow/ragflow

Length of output: 44


🏁 Script executed:

# Check the internal/dao/migration.go for any backfill related to tenant_model_instance.extra
rg -n "tenant_model_instance|extra.*=" internal/dao/migration.go -B 3 -A 3

Repository: infiniflow/ragflow

Length of output: 44


🏁 Script executed:

# Search for any migration files that might handle the extra field conversion
find . -name "*migration*" -type f | grep -E "\.(go|sql)$" | head -10

Repository: infiniflow/ragflow

Length of output: 90


🏁 Script executed:

# Check if there's any migration logic in internal/dao/migration.go related to tenant_model_instance
cat internal/dao/migration.go | head -100

Repository: infiniflow/ragflow

Length of output: 3249


🏁 Script executed:

# Search more broadly for any SQL or migration that handles the extra field
rg -n "extra.*=|UPDATE.*tenant_model_instance" --type sql --type go -i

Repository: infiniflow/ragflow

Length of output: 3060


Add data backfill migration for existing rows; default-only change is incomplete.

Changing the default to '{}' on line 26 only affects new inserts. Existing tenant_model_instance.extra rows with legacy values ('active', NULL, '') will still fail JSON unmarshaling in internal/service/model_service.go at 9+ locations (lines 184, 286, 331, 378, 431, 649, 729, 887, 898). A database migration or backfill script must update legacy values before this code reaches production.

Example backfill migration
UPDATE tenant_model_instance
SET extra = '{}'
WHERE extra = 'active' OR extra IS NULL OR extra = '';
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@internal/entity/tenant_model_instance.go` at line 26, The schema change only
sets a default for new rows but leaves legacy values ('active', NULL, empty) in
tenant_model_instance.extra that break JSON unmarshaling; add a DB
migration/backfill that updates existing rows (e.g. UPDATE tenant_model_instance
SET extra='{}' WHERE extra='active' OR extra IS NULL OR extra='') and ensure
this migration runs before the code that unmarshals extra in
internal/service/model_service.go (the methods that read/unmarshal
tenant_model_instance.Extra). Apply the change as a reversible migration and
include tests or a one-time run step in deployment to guarantee no existing rows
retain legacy values.

@codecov

codecov Bot commented Apr 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.30%. Comparing base (18fbfaf) to head (a5bf336).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #14423   +/-   ##
=======================================
  Coverage   95.30%   95.30%           
=======================================
  Files          10       10           
  Lines         703      703           
  Branches      112      112           
=======================================
  Hits          670      670           
  Misses         16       16           
  Partials       17       17           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@JinHai-CN
JinHai-CN merged commit 7c25870 into infiniflow:main Apr 28, 2026
1 check passed
@coderabbitai coderabbitai Bot mentioned this pull request Apr 29, 2026
1 task done
RerankerGuo added a commit to RerankerGuo/ragflow that referenced this pull request Nov 18, 2026
… the legacy unique index before widening

Per the maintainer review of infiniflow#22424:

- The api_key column is declared varchar(4096) instead of TEXT, so it
  keeps a bounded maximum while long provider keys (IONOS exceeds 1,700
  chars) still fit. The pre-backfill widening spec targets the same
  shape.

- The legacy composite unique index idx_api_key_provider_id
  (provider_id, api_key) — declared by the entity's uniqueIndex tags in
  the releases between infiniflow#14423 and infiniflow#15460 and dropped again since — is
  removed before any widening: MODIFY COLUMN past the legacy
  varchar(512) makes MySQL rebuild the index, and the widened utf8mb4
  key exceeds the 3072-byte InnoDB key length limit, so the ALTER and
  with it the whole upgrade would fail while the index is in place. The
  step reuses dropIndexIfExists and runs in RunMigrations ahead of
  modifyColumnTypes and AutoMigrate.

Dao tests pin the bounded varchar(4096) declaration through the real
mysql dialector, the widening ALTER and its convergence, the index drop
with its missing-index and missing-table guards.
RerankerGuo added a commit to RerankerGuo/ragflow that referenced this pull request Nov 23, 2026
… the legacy unique index before widening

Per the maintainer review of infiniflow#22424:

- The api_key column is declared varchar(4096) instead of TEXT, so it
  keeps a bounded maximum while long provider keys (IONOS exceeds 1,700
  chars) still fit. The pre-backfill widening spec targets the same
  shape.

- The legacy composite unique index idx_api_key_provider_id
  (provider_id, api_key) — declared by the entity's uniqueIndex tags in
  the releases between infiniflow#14423 and infiniflow#15460 and dropped again since — is
  removed before any widening: MODIFY COLUMN past the legacy
  varchar(512) makes MySQL rebuild the index, and the widened utf8mb4
  key exceeds the 3072-byte InnoDB key length limit, so the ALTER and
  with it the whole upgrade would fail while the index is in place. The
  step reuses dropIndexIfExists and runs in RunMigrations ahead of
  modifyColumnTypes and AutoMigrate.

Dao tests pin the bounded varchar(4096) declaration through the real
mysql dialector, the widening ALTER and its convergence, the index drop
with its missing-index and missing-table guards.
RerankerGuo added a commit to RerankerGuo/ragflow that referenced this pull request Nov 23, 2026
…ackfill

Review follow-ups on infiniflow#22424:

- The backfill copies tenant_llm.api_key — TEXT, so a legacy key can exceed
  the new varchar(4096) bound on tenant_model_instance — into the bounded
  column. In strict mode one over-length key failed the whole backfill
  transaction and, with the source row kept, would fail every later startup;
  a non-strict session stored the credential silently truncated. Such rows
  are now skipped with a warning naming tenant, provider and key length
  (never the key itself); the source row stays in tenant_llm untouched.
  The bound is measured in characters, matching MySQL's varchar semantics.
- The convergence test now counts ALTER statements through a gorm raw
  callback: sqlmock rejects unregistered statements but ExpectationsWereMet
  does not report unexpected calls, so a regressed convergence guard could
  previously pass unnoticed.
- Comment nits from the maintainer review: the legacy unique index was
  introduced in infiniflow#13922, not infiniflow#14423, and the entity comment cites this PR
  (infiniflow#22424), not infiniflow#22422.

CGO_ENABLED=0 go test -count=1 ./internal/dao/ ./internal/entity/ ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant