Skip to content

migration to another DB backend #3336#3343

Open
mkf-sysangels wants to merge 13 commits into
bunkerity:devfrom
mkf-sysangels:migration-to-another-DB-backend-#3336
Open

migration to another DB backend #3336#3343
mkf-sysangels wants to merge 13 commits into
bunkerity:devfrom
mkf-sysangels:migration-to-another-DB-backend-#3336

Conversation

@mkf-sysangels
Copy link
Copy Markdown

migration to another DB backend #3336

this PR has several other dependencies that will be published in separate PRs. Current state is ALPHA until the dependencies will be fixed.

@mkf-sysangels mkf-sysangels marked this pull request as draft March 17, 2026 10:50
Quoted env file values cause database connection failure
@TheophileDiot
Copy link
Copy Markdown
Member

Hi @mkf-sysangels, great idea. I’d prefer each P/R to be small, atomic, and independent, without depending on other open P/Rs, since GitHub recommends “small, focused pull requests that fulfill a single purpose” because they’re easier to review and merge.

source: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/getting-started/helping-others-review-your-changes

@mkf-sysangels
Copy link
Copy Markdown
Author

Hi @TheophileDiot

thank you for the information.

As the initial version is still alpha i need first fix issues to make it working on all scenarios.

First migration SQLLite > mysql-cluster was successful

Currently fixing mysql > postgresql patroni cluster

As soon as i have full cycle i will call it beta so others can test it too.

- fix postgres connection on patroni configuration (postgresql cluster)
- fix SSL connect error on wrong selected root CA PEM
- fetch root CA when cert is issued by trusted CA
- add timeouts
- add PID file
- add debug logging
- compare TLS conections for differences
- abort on critical certificate error
- fix TUI scrolling and size changes
- fix ssl connection issue
- add --auto-switch for instant migration
fix errors on migration to SQLLite
@mkf-sysangels mkf-sysangels marked this pull request as ready for review March 18, 2026 17:08
@mkf-sysangels
Copy link
Copy Markdown
Author

I successfully migrated:

SQLLite > mysql > postgrsql > mysql > SQLLite

Ready for public review

Status:
BETA until we have enough test-cases

…ces are not re-synced afterward in the migration utility.

data rows are imported with explicit id values, but sequences are not re-synced afterward in the migration utility.

adding an automatic sequence reset step right after import so next inserts use MAX(id)+1
…odel

Post-copy step: ensure primary key constraints match the SQLAlchemy model
…on the target

Post-copy step: recreate missing indexes and update index statistics on the target
@TheophileDiot
Copy link
Copy Markdown
Member

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 10, 2026

✅ Actions performed

Full review triggered.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 10, 2026

Component: src/common

Database Migration Feature – New database migration utility (bw_db_migrate.py) enabling zero-downtime migration between different database backends (SQLite, MySQL, PostgreSQL/Patroni).

Changes

Documentation added (src/common/utils/README-bw_db_migrate.md, +119 lines):

  • End-to-end control flow: schema creation via Alembic, table export to JSONL intermediates, import to target, validation via row counting
  • Operational modes: dry-run/test-target, interactive TUI for input collection
  • CLI options including --auto-switch for environment file updates and BunkerWeb service restart (systemd and alternative init support)
  • Backend-specific guidance (SQLite file permissions handling post-migration)
  • Troubleshooting notes (non-empty targets, connection encryption warnings, scheduler retry loops)

Security & Configuration Impact

  • Credential handling: Environment file integration with credential masking during migration
  • Connection security: SSL/TLS handling for Postgres/Patroni connections, root CA validation
  • Database state: Post-migration fixes for PostgreSQL sequences, primary key constraints, index recreation to match SQLAlchemy models

User-Visible Changes

  • New migration capability supporting multi-backend migrations with test coverage (SQLite → MySQL → PostgreSQL → MySQL → SQLite round-trip completed)
  • Optional auto-switch mode for seamless service restart post-migration
  • Interactive TUI mode for migration parameter collection

Testing & Documentation

  • Documentation provided for the new migration tool
  • No test suite changes indicated in the provided summary

Status: Marked BETA pending comprehensive test case coverage. Feature has undergone iterative validation across multiple database backend combinations.

Walkthrough

A new documentation file is added describing the bw_db_migrate.py migration script. It details the end-to-end control flow, operational modes including dry-run functionality, configuration sources, CLI options with auto-switch service restart capability, SQLite-specific considerations, and troubleshooting guidance.

Changes

Cohort / File(s) Summary
Database Migration Documentation
src/common/utils/README-bw_db_migrate.md
New documentation covering the complete bw_db_migrate.py workflow: schema creation via Alembic, table export to JSONL, import to target, row-count validation. Documents dry-run mode, TUI input collection, CLI options including --auto-switch for service restart, environment file configuration, SQLite target requirements (empty DB, file ownership handling), and troubleshooting for non-empty targets and scheduler retry loops.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

📚 A migration tale, now clearly penned,
Schema flows through pipes that blend,
Tables dance in JSONL streams so bright,
SQLite permissions set just right—
Documentation lights the path, my friend! ✨

🚥 Pre-merge checks | ✅ 1 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title refers to the PR issue number but is vague about the actual change—it should describe what the changeset adds (documentation for bw_db_migrate.py), not just the feature area. Revise the title to follow Conventional Commits format: 'docs: add bw_db_migrate migration script documentation' or similar, clearly describing the documentation addition.
✅ Passed checks (1 passed)
Check name Status Explanation
Description check ✅ Passed The description is related to the changeset but provides only the issue reference and alpha status without explaining what the PR actually introduces or addresses.

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


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

@TheophileDiot
Copy link
Copy Markdown
Member

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 10, 2026

✅ Actions performed

Full review triggered.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

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 `@src/common/utils/README-bw_db_migrate.md`:
- Line 17: Replace the hyphenated phrase "vice-versa" with "vice versa" in the
README-bw_db_migrate.md documentation (the string "Any supported backend ->
SQLite (or vice-versa)") so it uses standard British English; update the text to
"Any supported backend -> SQLite (or vice versa)" and ensure other occurrences
of "vice-versa" in the same file follow the same change.
- Around line 49-51: Update the README examples that call bw_db_migrate.py so
they use the repository-relative path used in the script's docstring—e.g.,
replace occurrences of "python3 bw_db_migrate.py [options]" with "python3
src/common/utils/bw_db_migrate.py [options]" (or alternatively document the
required directory change like "cd src/common/utils && python3 bw_db_migrate.py
[options]"); adjust all instances mentioned in the comment (the examples around
lines showing invocation) so they match the actual invocation behavior until a
console script/entry point is added to pyproject.toml.
🪄 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: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: df56929f-d5c6-4fdd-b65e-6b79cc3db181

📥 Commits

Reviewing files that changed from the base of the PR and between a15aee6 and 16c2b89.

📒 Files selected for processing (2)
  • src/common/utils/README-bw_db_migrate.md
  • src/common/utils/bw_db_migrate.py
📜 Review details
🧰 Additional context used
📓 Path-based instructions (1)
**/*.md

⚙️ CodeRabbit configuration file

**/*.md: Documentation should be concise, accurate, and written in British English:

  • Keep the structure clear with a sensible heading hierarchy.
  • Prefer concrete instructions, accurate examples, and explicit prerequisites.
  • Check that commands, paths, and references still match the codebase and supported deployment modes.
  • When a PR changes behaviour, defaults, packaging, or security posture, ask for the corresponding documentation update.

Files:

  • src/common/utils/README-bw_db_migrate.md
🪛 LanguageTool
src/common/utils/README-bw_db_migrate.md

[duplication] ~15-~15: Possible typo: you repeated a word.
Context: ...ckend, for example: - MySQL/MariaDB -> PostgreSQL - PostgreSQL -> MySQL/MariaDB - Any supported backen...

(ENGLISH_WORD_REPEAT_RULE)


[grammar] ~17-~17: The expression “vice versa” is spelled without hyphens.
Context: ...B - Any supported backend -> SQLite (or vice-versa) ## Requirements - Python dependencie...

(VICE_VERSA)


[uncategorized] ~37-~37: Loose punctuation mark.
Context: ...ould contain at least: - DATABASE_URI: source database SQLAlchemy URI - `DB_MI...

(UNLIKELY_OPENING_PUNCTUATION)


- MySQL/MariaDB -> PostgreSQL
- PostgreSQL -> MySQL/MariaDB
- Any supported backend -> SQLite (or vice-versa)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Use “vice versa” (no hyphen).

At Line 17, “vice-versa” should be “vice versa” in standard British English.

As per coding guidelines, **/*.md: Documentation should be concise, accurate, and written in British English.

🧰 Tools
🪛 LanguageTool

[grammar] ~17-~17: The expression “vice versa” is spelled without hyphens.
Context: ...B - Any supported backend -> SQLite (or vice-versa) ## Requirements - Python dependencie...

(VICE_VERSA)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/common/utils/README-bw_db_migrate.md` at line 17, Replace the hyphenated
phrase "vice-versa" with "vice versa" in the README-bw_db_migrate.md
documentation (the string "Any supported backend -> SQLite (or vice-versa)") so
it uses standard British English; update the text to "Any supported backend ->
SQLite (or vice versa)" and ensure other occurrences of "vice-versa" in the same
file follow the same change.

Comment on lines +49 to +51
Run:

`python3 bw_db_migrate.py [options]`
Copy link
Copy Markdown

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

echo "== Locate migration script and any wrapper/entrypoint mentions =="
fd -i 'bw_db_migrate.py'
rg -n -C2 --type=py 'bw_db_migrate|argparse|ArgumentParser|add_argument\('

echo
echo "== Check README command examples for invocation style =="
rg -n -C2 'python3 bw_db_migrate.py|--auto-switch|--test-target|--dry-run|--tui' src/common/utils/README-bw_db_migrate.md || true

echo
echo "== Cross-check same command style in other docs =="
rg -n -C2 'bw_db_migrate.py|--auto-switch|--test-target|--dry-run|--tui' --type=md

Repository: bunkerity/bunkerweb

Length of output: 50375


🏁 Script executed:

# Check for setup.py, pyproject.toml, or installation entry points
find . -maxdepth 3 -type f \( -name "setup.py" -o -name "pyproject.toml" -o -name "setup.cfg" \) 2>/dev/null | head -20

# Look for entry points or console_scripts references to bw_db_migrate
rg -n 'bw_db_migrate|entry.points|console_scripts' --type=py,toml,txt 2>/dev/null | grep -i 'entry\|console\|script' | head -30

# Check if there's a wrapper script or symlink mechanism
fd -type f -o -type l 'bw.?db.?migrate|bw-db-migrate' 2>/dev/null

# Check Docker or packaging files that might install the script
fd -type f \( -name "Dockerfile*" -o -name "*.sh" \) -exec rg -l 'bw_db_migrate' {} \; 2>/dev/null | head -10

Repository: bunkerity/bunkerweb

Length of output: 79


🏁 Script executed:

# Examine pyproject.toml for entry points and installation configuration
cat -n pyproject.toml | head -100

# Search specifically for bw_db_migrate in pyproject.toml
rg -n 'bw_db_migrate|console.scripts|entry' pyproject.toml

Repository: bunkerity/bunkerweb

Length of output: 522


🏁 Script executed:

# Get the full pyproject.toml file
wc -l pyproject.toml

# Look for any [project.scripts] or similar entry point sections
rg -n '\[.*script' pyproject.toml

# Check if bw_db_migrate is referenced anywhere in packaging config
rg -n 'bw_db_migrate' . --type=toml --type=py --type=txt 2>/dev/null | grep -E '(setup|script|entry|install|package)' | head -20

# Check for install/deployment files that might show how the script is deployed
fd -type f -name '*.mk' -o -name 'Makefile*' | xargs grep -l 'bw_db_migrate' 2>/dev/null

# Look at Dockerfile entries
fd -type f -name 'Dockerfile*' | xargs grep -A2 -B2 'bw_db_migrate' 2>/dev/null | head -50

Repository: bunkerity/bunkerweb

Length of output: 439


🏁 Script executed:

# Get full pyproject.toml content
cat pyproject.toml

# Search for script installation references
rg -n 'bw_db_migrate' . 2>/dev/null | grep -E '(Dockerfile|install|script|setup|entry)' | head -20

Repository: bunkerity/bunkerweb

Length of output: 389


🏁 Script executed:

# Search for Dockerfile references to bw_db_migrate
find . -name 'Dockerfile*' 2>/dev/null | xargs grep -B3 -A3 'bw_db_migrate' 2>/dev/null | head -60

# Search for install scripts or deployment docs that mention bw_db_migrate
rg -n 'bw_db_migrate' --type-add 'docker:Dockerfile*' -g '*.md' -g 'Dockerfile*' 2>/dev/null | head -40

# Look at the actual script invocations in tests or CI
rg -n 'python.*bw_db_migrate' . 2>/dev/null | head -20

Repository: bunkerity/bunkerweb

Length of output: 1554


Update command invocation paths to match installed packaging behaviour.

Lines 51, 79–97 show python3 bw_db_migrate.py, but the script's own docstring (lines 9, 14, 19 of the source file) documents invocations as python3 src/common/utils/bw_db_migrate.py. Since no entry point or console script installation is configured in pyproject.toml, the bare script name will fail with "file not found" for developers working from the repository root.

Align README examples with the correct invocation path or document that users must change to src/common/utils/ first.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/common/utils/README-bw_db_migrate.md` around lines 49 - 51, Update the
README examples that call bw_db_migrate.py so they use the repository-relative
path used in the script's docstring—e.g., replace occurrences of "python3
bw_db_migrate.py [options]" with "python3 src/common/utils/bw_db_migrate.py
[options]" (or alternatively document the required directory change like "cd
src/common/utils && python3 bw_db_migrate.py [options]"); adjust all instances
mentioned in the comment (the examples around lines showing invocation) so they
match the actual invocation behavior until a console script/entry point is added
to pyproject.toml.

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.

2 participants