fix(db): repair missing SQLite FK for tools.grpc_service_id#5319
Open
bogdanmariusc10 wants to merge 1 commit into
Open
Conversation
7 tasks
2143f2d to
e9f13b5
Compare
Add forward migration a54288286395 to repair SQLite databases upgraded through revision w7x8y9z0a1b2, which skipped creating the foreign key constraint for tools.grpc_service_id -> grpc_services.id. The repair migration: - Only operates on SQLite (PostgreSQL already has correct FK) - Detects and repairs missing FK using batch_alter_table - Is idempotent (safe to run multiple times) - Includes defensive checks for table/column existence Add comprehensive regression tests covering: - FK constraint existence after migrations - CASCADE delete behavior verification - Migration idempotency - Fresh install compatibility Closes #5282 Signed-off-by: Bogdan-Marius-Catanus <bogdan-marius.catanus@ibm.com>
e9f13b5 to
20c85be
Compare
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.
🔗 Related Issue
Closes #5282
📝 Summary
This PR adds a forward migration to repair SQLite databases that were upgraded through revision
w7x8y9z0a1b2, which added thetools.grpc_service_idcolumn but skipped creating the foreign key constraint togrpc_services.iddue to SQLite's limitations withALTER TABLE.What was broken:
w7x8y9z0a1b2have thegrpc_service_idcolumn but no FK constraintWhat this PR fixes:
a54288286395detects and repairs missing FK on SQLite usingbatch_alter_table(recreate="always")📏 Reviewability
triage🏷️ Type of Change
🧪 Verification
Tests Suite
Tests included:
test_tools_grpc_service_fk_exists_sqlite- Verifies FK constraint exists after migrationstest_tools_grpc_service_fk_cascade_delete_sqlite- Verifies CASCADE delete behavior workstest_tools_grpc_service_fk_repair_idempotent- Tests migration can be run multiple times safelytest_tools_grpc_service_fk_fresh_install- Ensures fresh installs have FK from db.py modelsCode Quality
uv run black <files>uv run isort <files>uv run ruff check <files>alembic heads✅ Checklist
make black isort pre-commit)📓 Notes
Files Changed
mcpgateway/alembic/versions/a54288286395_repair_sqlite_tools_grpc_service_fk.py(124 lines)0a089912b5f0_add_numeric_id_to_email_users.pytests/migration/test_tools_grpc_service_fk.py(461 lines)Migration Chain