Skip to content

Go: add migrate database flag - #16719

Merged
JinHai-CN merged 2 commits into
infiniflow:mainfrom
JinHai-CN:fix523
Jul 7, 2026
Merged

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

Conversation

@JinHai-CN

Copy link
Copy Markdown
Member

Summary

--migrate will trigger database migration

./ragflow_main --api --migrate

@coderabbitai

coderabbitai Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

A new --migrate CLI flag is introduced. serverArgs gains a migrateDB field set by parseArgs(), which is passed to dao.InitDB(). InitDB's signature changes to accept migrateDB bool, conditionally gating schema auto-migration and RunMigrations execution.

Changes

Optional Database Migration Flag

Layer / File(s) Summary
CLI flag parsing and wiring
cmd/ragflow_server.go
Adds migrateDB field to serverArgs, sets it when --migrate is passed, and forwards it to dao.InitDB().
Conditional migration execution
internal/dao/database.go
Changes InitDB signature to InitDB(migrateDB bool) error, wrapping the model auto-migration loop and RunMigrations call (plus the success log) inside a conditional on migrateDB.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested labels: 💞 feature

Poem

A flag was born, so small and neat,
"--migrate" now makes migrations discreet.
No more auto-runs every single time,
Just a hop, a flip, and schema's fine.
🐇✨ Hooray for the conditional switch!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 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.
Title check ✅ Passed The title clearly summarizes the main change: adding a database migration flag.
Description check ✅ Passed The description covers the PR summary and shows usage, which is mostly complete for this template.

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.

@JinHai-CN
JinHai-CN marked this pull request as ready for review July 7, 2026 12:03
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jul 7, 2026
@JinHai-CN JinHai-CN added ci Continue Integration and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Jul 7, 2026
@JinHai-CN
JinHai-CN merged commit 568bac6 into infiniflow:main Jul 7, 2026
12 checks passed

@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 current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@cmd/ragflow_server.go`:
- Around line 85-86: The startup flow for ragflow_server now depends on the
migrateDB flag, but the new --migrate path is not consistently surfaced or used.
Update the argument handling in ragflow_server’s main startup logic and
printHelp so --migrate is documented and available, then make sure every
entrypoint/script that launches bin/ragflow_server passes the flag so InitDB
still performs schema migration during upgrades.

In `@internal/dao/database.go`:
- Around line 163-176: The success log in database initialization is misleading
because it always says the schema was migrated even when the migrateDB branch is
skipped. Update the final status logging in database.go around the database
connection/migration flow so it reflects whether migrateDB was true or false,
using the existing database initialization path and the migrateDB flag to choose
between a migrated-success message and a connected-without-migration message.
🪄 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: 4a8bb7fa-af63-4148-ac6d-cbfb0f576582

📥 Commits

Reviewing files that changed from the base of the PR and between 4da9429 and e056f25.

📒 Files selected for processing (2)
  • cmd/ragflow_server.go
  • internal/dao/database.go

Comment thread cmd/ragflow_server.go
Comment on lines +85 to +86
case "--migrate":
args.migrateDB = true

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.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify help text lists --migrate and find startup invocations of the server binary
rg -nP -C3 '\-\-migrate|func printHelp' cmd/ragflow_server.go
rg -nP -C2 'ragflow_main|ragflow_server' -g '!**/*.go' -g '*.{sh,yaml,yml,Dockerfile,dockerfile}' .

Repository: infiniflow/ragflow

Length of output: 5433


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== cmd/ragflow_server.go around flag parsing, help, and startup =="
sed -n '1,220p' cmd/ragflow_server.go | cat -n | sed -n '1,220p'

echo
echo "== search for migrate handling and any auto-migration calls =="
rg -n "migrateDB|migrate|auto.?migrat|migration" cmd internal docker build.sh .

echo
echo "== startup invocations of bin/ragflow_server / ragflow_server with args =="
rg -n "bin/ragflow_server|ragflow_server" docker build.sh README.md .github . -g '!**/*.go'

Repository: infiniflow/ragflow

Length of output: 50377


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== relevant DB init logic =="
sed -n '60,190p' internal/dao/database.go | cat -n

echo
echo "== help text and flag parsing around --migrate =="
sed -n '74,210p' cmd/ragflow_server.go | cat -n

echo
echo "== startup wrappers invoking bin/ragflow_server =="
rg -n -C2 "bin/ragflow_server|ragflow_server --(api|admin|ingestor|syncer)|--migrate" docker build.sh README.md .github .

echo
echo "== any direct uses of InitDB and migrateDB wiring =="
rg -n -C2 "InitDB\\(|migrateDB" cmd internal

Repository: infiniflow/ragflow

Length of output: 20733


Add --migrate to every startup path
InitDB now skips schema migration unless this flag is passed, but printHelp doesn’t list it and the current launch scripts still invoke bin/ragflow_server without it. Existing deployments will silently stop migrating on upgrade unless every entrypoint is updated.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cmd/ragflow_server.go` around lines 85 - 86, The startup flow for
ragflow_server now depends on the migrateDB flag, but the new --migrate path is
not consistently surfaced or used. Update the argument handling in
ragflow_server’s main startup logic and printHelp so --migrate is documented and
available, then make sure every entrypoint/script that launches
bin/ragflow_server passes the flag so InitDB still performs schema migration
during upgrades.

Comment thread internal/dao/database.go
Comment on lines +163 to 176
if migrateDB {
common.Info("Migrating database schema...")
for _, m := range dataModels {
if err = autoMigrateSafely(DB, m); err != nil {
return fmt.Errorf("failed to migrate model %T: %w", m, err)
}
}
}

// Run manual migrations for complex schema changes
if err = RunMigrations(DB); err != nil {
return fmt.Errorf("failed to run manual migrations: %w", err)
// Run manual migrations for complex schema changes
if err = RunMigrations(DB); err != nil {
return fmt.Errorf("failed to run manual migrations: %w", err)
}
common.Info("Database schema migrated successfully")
}

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.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Misleading success log when migration is skipped.

With migration now gated behind if migrateDB, the unconditional log at Line 184 ("Database connected and migrated successfully") still claims a migration happened even when --migrate was not passed. This can mislead operators debugging whether the schema was actually migrated. Consider reflecting the skip.

💡 Suggested wording fix (Line 184)
-	common.Info("Database connected and migrated successfully")
+	if migrateDB {
+		common.Info("Database connected and migrated successfully")
+	} else {
+		common.Info("Database connected (schema migration skipped; pass --migrate to run migrations)")
+	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if migrateDB {
common.Info("Migrating database schema...")
for _, m := range dataModels {
if err = autoMigrateSafely(DB, m); err != nil {
return fmt.Errorf("failed to migrate model %T: %w", m, err)
}
}
}
// Run manual migrations for complex schema changes
if err = RunMigrations(DB); err != nil {
return fmt.Errorf("failed to run manual migrations: %w", err)
// Run manual migrations for complex schema changes
if err = RunMigrations(DB); err != nil {
return fmt.Errorf("failed to run manual migrations: %w", err)
}
common.Info("Database schema migrated successfully")
}
if migrateDB {
common.Info("Database connected and migrated successfully")
} else {
common.Info("Database connected (schema migration skipped; pass --migrate to run migrations)")
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/dao/database.go` around lines 163 - 176, The success log in database
initialization is misleading because it always says the schema was migrated even
when the migrateDB branch is skipped. Update the final status logging in
database.go around the database connection/migration flow so it reflects whether
migrateDB was true or false, using the existing database initialization path and
the migrateDB flag to choose between a migrated-success message and a
connected-without-migration message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant