Skip to content

fix(db): repair missing SQLite FK for tools.grpc_service_id#5319

Open
bogdanmariusc10 wants to merge 1 commit into
mainfrom
5282-bug-add-forward-migration-to-repair-missing-sqlite-fk-for-toolsgrpc_service_id
Open

fix(db): repair missing SQLite FK for tools.grpc_service_id#5319
bogdanmariusc10 wants to merge 1 commit into
mainfrom
5282-bug-add-forward-migration-to-repair-missing-sqlite-fk-for-toolsgrpc_service_id

Conversation

@bogdanmariusc10

Copy link
Copy Markdown
Collaborator

🔗 Related Issue

Closes #5282


📝 Summary

This PR adds a forward migration to repair SQLite databases that were upgraded through revision w7x8y9z0a1b2, which added the tools.grpc_service_id column but skipped creating the foreign key constraint to grpc_services.id due to SQLite's limitations with ALTER TABLE.

What was broken:

  • Databases upgraded through w7x8y9z0a1b2 have the grpc_service_id column but no FK constraint
  • This creates schema drift between the SQLAlchemy ORM model (which defines the FK) and the actual database schema
  • Referential integrity is not enforced, and CASCADE delete behavior doesn't work

What this PR fixes:

  • New migration a54288286395 detects and repairs missing FK on SQLite using batch_alter_table(recreate="always")
  • Migration is idempotent (safe to run multiple times)
  • PostgreSQL databases unaffected (they already have the correct FK from the original migration)
  • Comprehensive regression tests prevent future occurrences

📏 Reviewability

  • This PR has one clear purpose
  • The linked issue is not labeled triage
  • Unrelated bugs or improvements are tracked in separate issues/PRs
  • Tests are included with the code they validate
  • If AI-assisted, I understand and can explain the generated changes

🏷️ Type of Change

  • Bug fix
  • Feature / Enhancement
  • Documentation
  • Refactor
  • Chore (deps, CI, tooling)
  • Other (describe below)

🧪 Verification

Tests Suite

$ uv run pytest tests/migration/test_tools_grpc_service_fk.py -v --tb=short -k "not postgres"

============================= test session starts ==============================
platform darwin -- Python 3.11.14, pytest-9.0.3, pluggy-1.6.0
rootdir: /Users/bogdanmariuscatanus/mcp-context-forge
configfile: pyproject.toml
plugins: locust-2.44.0, instafail-0.5.0, cov-7.1.0, xdist-3.8.0, env-1.6.0, ...
collected 6 items / 2 deselected / 4 selected

tests/migration/test_tools_grpc_service_fk.py ....                       [100%]

======================= 4 passed, 2 deselected in 8.11s ========================

Tests included:

  1. test_tools_grpc_service_fk_exists_sqlite - Verifies FK constraint exists after migrations
  2. test_tools_grpc_service_fk_cascade_delete_sqlite - Verifies CASCADE delete behavior works
  3. test_tools_grpc_service_fk_repair_idempotent - Tests migration can be run multiple times safely
  4. test_tools_grpc_service_fk_fresh_install - Ensures fresh installs have FK from db.py models

Code Quality

Check Command Status
Formatting (Black) uv run black <files> ✅ Pass
Import sorting (isort) uv run isort <files> ✅ Pass
Linting (Ruff) uv run ruff check <files> ✅ Pass
Type checking Alembic migrations (no mypy) ✅ Pass
Migration head updated alembic heads ✅ a54288286395 (head)

✅ Checklist

  • Code formatted (make black isort pre-commit)
  • Tests added/updated for changes
  • Documentation updated (if applicable) - Migration includes comprehensive docstrings
  • No secrets or credentials committed

📓 Notes

Files Changed

  1. mcpgateway/alembic/versions/a54288286395_repair_sqlite_tools_grpc_service_fk.py (124 lines)

    • New forward migration
    • Follows idempotent migration patterns from 0a089912b5f0_add_numeric_id_to_email_users.py
    • Includes comprehensive docstrings and comments
  2. tests/migration/test_tools_grpc_service_fk.py (461 lines)

    • Comprehensive regression test suite
    • 4 SQLite tests + 2 PostgreSQL tests (conditional)
    • Tests FK existence, CASCADE behavior, idempotency, and fresh installs

Migration Chain

... → w7x8y9z0a1b2 (adds column, skips FK on SQLite)
  → ... → 6c0e5f8a9b1d
    → a54288286395 (repairs missing FK on SQLite) ← NEW HEAD

@bogdanmariusc10 bogdanmariusc10 added bug Something isn't working db Database and migrations labels Jun 19, 2026
@bogdanmariusc10 bogdanmariusc10 force-pushed the 5282-bug-add-forward-migration-to-repair-missing-sqlite-fk-for-toolsgrpc_service_id branch from 2143f2d to e9f13b5 Compare June 24, 2026 11:27
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>
@bogdanmariusc10 bogdanmariusc10 force-pushed the 5282-bug-add-forward-migration-to-repair-missing-sqlite-fk-for-toolsgrpc_service_id branch from e9f13b5 to 20c85be Compare June 24, 2026 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working db Database and migrations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Add forward migration to repair missing SQLite FK for tools.grpc_service_id

1 participant