fix: delete TopologyServiceApplication links before TopologyService rows (fixes #5439)#6171
fix: delete TopologyServiceApplication links before TopologyService rows (fixes #5439)#6171chengyixu wants to merge 2 commits into
Conversation
When process_topology refreshes provider topology data it deletes all existing TopologyService rows for that provider. If any of those services were associated with a TopologyApplication (via the TopologyServiceApplication join table), the delete violated the foreign-key constraint on topologyserviceapplication.service_id and raised: sqlalchemy.exc.IntegrityError: ForeignKeyViolation on topologyserviceapplication — topologyservice.id still referenced The fix deletes the TopologyServiceApplication join-table rows first, in the same transaction as the dependency and service deletes, using the same filter pattern already used for TopologyServiceDependency. Closes keephq#5439 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
@chengyixu is attempting to deploy a commit to the KeepHQ Team on Vercel. A member of the Team first needs to authorize it. |
|
Hi team! This fixes the ForeignKeyViolation crash in process_topology (#5439). All CI checks passing. Happy to address any feedback! |
|
Friendly ping — this fix addresses the ForeignKeyViolation crash in |
|
Hi team — gentle ping. This PR fixes the ForeignKeyViolation crash in process_topology when deleting topology services (issue #5439). All CI checks passing. Happy to adjust anything if needed! |
|
Hi team — friendly ping on this fix for the ForeignKeyViolation crash in process_topology (issue #5439). Deletes TopologyServiceApplication links before TopologyService rows. All CI checks passing. Happy to address any feedback! |
|
Friendly ping @keephq/maintainers -- this PR has been open for 37 days. It fixes a foreign key constraint issue by deleting TopologyServiceApplication links before TopologyService rows (fixes #5439). Is there anything else needed before review? Thanks! |
|
👋 Friendly ping — this PR has been open for 37 days with all checks passing. Is there anything else needed for review? Thanks! |
|
Hi maintainers, just a friendly ping on this PR. It's been open for a while — would love a review when you have a moment. Happy to rebase or make any changes needed. Thanks! |
|
Hi! This PR is CI green and ready for merge when you have a moment. Thanks! |
|
Strong fix for the immediate symptom. One observation worth flagging for defense-in-depth, since this issue keeps coming up: The root cause is that class TopologyServiceApplication(SQLModel, table=True):
service_id: int = Field(foreign_key="topologyservice.id", primary_key=True)
application_id: UUID = Field(foreign_key="topologyapplication.id", primary_key=True)Compare with how The app-layer ordering fix in this PR is correct and unblocks #5439, but any other code path that ever does Happy to submit that schema-level follow-up if you'd like, once this lands — wanted to call it out here so it doesn't get lost. |
|
Thanks for the thorough review, @ayoubil! Great catch on the missing I agree the app-layer ordering fix in this PR is correct and unblocks #5439, but the schema-level fix would close the door permanently. I am happy to submit the CASCADE follow-up PR right after this lands:
Would you prefer I add the CASCADE changes to this PR now, or submit them as a separate follow-up once this one merges? Either way works for me. Thanks again for the detailed review! |
Adds CASCADE to both FK columns so that deleting a TopologyService or TopologyApplication automatically cleans up the join-table rows. Includes an alembic migration to alter the existing constraints. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
|
Pushed the CASCADE follow-up as discussed @ayoubil:
This closes the door on the entire FK violation bug class. The app-layer ordering fix is still in place as added safety. |
Summary
Fixes a
ForeignKeyViolationthat crashesprocess_topologywhenever a provider's topology services belong to a TopologyApplication.Root cause
process_topologyrefreshes topology data by deleting then re-inserting allTopologyServicerows for a given provider. The deletion order was:TopologyServiceDependency(correct — hasCASCADE)TopologyService(fails when a row is still referenced byTopologyServiceApplication.service_id)The
TopologyServiceApplicationjoin table does not haveON DELETE CASCADE, so step 2 raised:This is the exact traceback reported in #5439.
Fix
Delete the
TopologyServiceApplicationrows for the affected services before deleting theTopologyServicerows, using the same filter pattern already used forTopologyServiceDependency.Changes
keep/api/tasks/process_topology_task.py— addTopologyServiceApplicationdeletion steptests/test_topology.py— regression test that would have caught this (and confirms the fix)Closes #5439
/claim #5439