feat: add Alembic database migrations#230
Open
harsh-kumar-patwa wants to merge 2 commits intoThe-OpenROAD-Project:masterfrom
Open
feat: add Alembic database migrations#230harsh-kumar-patwa wants to merge 2 commits intoThe-OpenROAD-Project:masterfrom
harsh-kumar-patwa wants to merge 2 commits intoThe-OpenROAD-Project:masterfrom
Conversation
Replace Base.metadata.create_all() with Alembic for versioned schema management. Migrations run automatically on startup, with fallback to create_all if Alembic fails. Pre-Alembic databases are auto-detected and stamped so existing deployments upgrade seamlessly. Signed-off-by: Harsh Kumar <harshkumar3446@gmail.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds Alembic-based schema migrations to the backend to replace the previous Base.metadata.create_all()-only initialization approach, enabling safe evolution of existing databases while keeping “auto-init on startup” behavior.
Changes:
- Introduces Alembic configuration, env, template, and an initial
0001schema migration. - Updates database initialization to run migrations on startup with a
create_all()fallback on failure. - Adds Makefile migration commands and adjusts tests/docs for the new workflow.
Reviewed changes
Copilot reviewed 9 out of 10 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| backend/pyproject.toml | Adds Alembic dependency. |
| backend/uv.lock | Locks Alembic (and transitive deps like Mako). |
| backend/alembic.ini | Adds Alembic config (URL set programmatically). |
| backend/src/database/alembic/env.py | Alembic environment wired to existing get_database_url() and Base.metadata. |
| backend/src/database/alembic/script.py.mako | Adds migration template for future revisions. |
| backend/src/database/alembic/versions/0001_initial_schema.py | Initial migration creating conversations and messages. |
| backend/src/database/config.py | Adds run_migrations() and calls it from init_database() with fallback behavior. |
| backend/tests/test_database_config.py | Updates tests to assert migration invocation and fallback to create_all(). |
| backend/Makefile | Adds make db-* Alembic convenience targets. |
| README.md | Updates database schema section and documents migration commands. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Pass disable_existing_loggers=False in env.py to preserve app logging - Use exc_info=True for migration failure logging to capture full traceback - Fix README column description (id -> uuid) - Add unit tests for run_migrations() stamp vs upgrade behavior Signed-off-by: Harsh Kumar <harshkumar3446@gmail.com>
Collaborator
|
@harsh-kumar-patwa Thanks for all your contributions! I will slowly review them as I am abit swamped currently with other matters. ORAssistant is currently in maintenance mode - and will also not be featured in GSoC 2025. You can check this link for GSoC 2026 projects. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #184
Summary
Base.metadata.create_all()approach that cannot handle schema changes on existing databasesinit_database()— no manual steps needed for deploymentsalembic_version) are auto-detected and stamped, so existing deployments upgrade seamlessly without data losscreate_all()if Alembic fails, preserving the existing graceful degradation patternChanges
backend/pyproject.tomlalembic>=1.13.0dependencybackend/alembic.inisqlalchemy.urlleft blank (set programmatically inenv.pyto avoid leaking credentials)backend/src/database/alembic/env.pyget_database_url()andBase— no URL/model duplicationbackend/src/database/alembic/script.py.makobackend/src/database/alembic/versions/0001_initial_schema.pyconversations+messagesschemabackend/src/database/config.pyrun_migrations()with auto-stamp logic; replacedinspect+create_allblock withrun_migrations()call +create_allfallbackbackend/Makefiledb-migrate,db-revision,db-downgrade,db-history,db-currenttargetsbackend/tests/test_database_config.pyrun_migrations()is called + verify fallback tocreate_allon migration failureREADME.mdbackend/uv.lockHow it works
alembic upgrade headcreates all tables via migrationalembic_version) → auto-stamps current revision, no schema changes appliedBase.metadata.create_all()(same behavior as before this PR)Developer workflow for future schema changes
Test plan
uv run pytest tests/ -v)make format && make checkcleanrun_local.sh)alembic history,alembic current)