alembic: add ON DELETE CASCADE to organization child FKs (#96) #108

Closed
Mark wants to merge 0 commits from fix/96-ondelete-cascade-migration into main
Owner

Closes #96.

Commit d0723c9 set ondelete="CASCADE" in the SQLModel metadata for the org_id FKs of OrganizationEmail / OrganizationPhone / OrganizationDomain (app/models.py), but no alembic migration was generated. Existing databases keep the original NO ACTION FKs, so deleting an organization raises a FK violation on child rows. This PR adds migration b53942abd63a, which drops the three <table>_org_id_fkey constraints and recreates them with ON DELETE CASCADE (downgrade restores NO ACTION).

app/models.py is untouched; the diff contains exactly one new file: backend/alembic/versions/b53942abd63a_ondelete_cascade_org_children.py.

Verification on throwaway Postgres 16 (alembic upgrade head / downgrade -1 / re-upgrade head all exit 0; alembic check reports no pending ops). psql \d:

                                Table "public.organization_email"

Foreign-key constraints:
"organization_email_org_id_fkey" FOREIGN KEY (org_id) REFERENCES organization(id) ON DELETE CASCADE

                                Table "public.organization_phone"

Foreign-key constraints:
"organization_phone_org_id_fkey" FOREIGN KEY (org_id) REFERENCES organization(id) ON DELETE CASCADE

                                Table "public.organization_domain"

Foreign-key constraints:
"organization_domain_org_id_fkey" FOREIGN KEY (org_id) REFERENCES organization(id) ON DELETE CASCADE

Cascade drill: DELETE org with 1 email/1 phone/1 domain child -> all counts 0,0,0, no FK violation. Full log: verify-b53942abd63a.log (attachment on kanban task t_a5378f87).

Closes #96. Commit d0723c9 set ondelete="CASCADE" in the SQLModel metadata for the org_id FKs of OrganizationEmail / OrganizationPhone / OrganizationDomain (app/models.py), but no alembic migration was generated. Existing databases keep the original NO ACTION FKs, so deleting an organization raises a FK violation on child rows. This PR adds migration b53942abd63a, which drops the three `<table>_org_id_fkey` constraints and recreates them with ON DELETE CASCADE (downgrade restores NO ACTION). app/models.py is untouched; the diff contains exactly one new file: backend/alembic/versions/b53942abd63a_ondelete_cascade_org_children.py. Verification on throwaway Postgres 16 (alembic upgrade head / downgrade -1 / re-upgrade head all exit 0; alembic check reports no pending ops). psql \d: Table "public.organization_email" Foreign-key constraints: "organization_email_org_id_fkey" FOREIGN KEY (org_id) REFERENCES organization(id) ON DELETE CASCADE Table "public.organization_phone" Foreign-key constraints: "organization_phone_org_id_fkey" FOREIGN KEY (org_id) REFERENCES organization(id) ON DELETE CASCADE Table "public.organization_domain" Foreign-key constraints: "organization_domain_org_id_fkey" FOREIGN KEY (org_id) REFERENCES organization(id) ON DELETE CASCADE Cascade drill: DELETE org with 1 email/1 phone/1 domain child -> all counts 0,0,0, no FK violation. Full log: verify-b53942abd63a.log (attachment on kanban task t_a5378f87).
alembic: add ON DELETE CASCADE to organization child FKs (#96)
Some checks failed
CI / Backend Lint & Format (pull_request) Failing after 4s
CI / Golden Fixture Validation (pull_request) Has been skipped
CI / Backend Tests (pull_request) Has been skipped
CI / Backend Security Tests (pull_request) Has been skipped
CI / Frontend Lint (pull_request) Failing after 23s
CI / Frontend Tests (pull_request) Has been skipped
CI / Dependency Security Scan (pull_request) Has been skipped
Stress Tests / Stress & Pipeline Tests (pull_request) Failing after 6s
Stress Tests / Full Data Pipeline Validation (pull_request) Has been skipped
Performance Regression Tests / Performance Regression Detection (pull_request) Failing after 33s
CI / Frontend E2E Tests (pull_request) Has been skipped
Performance Regression Tests / Compare with Baseline (pull_request) Has been skipped
CI / All Checks Passed (pull_request) Failing after 48s
34728532d3
Originating change: commit d0723c9 set ondelete=CASCADE on the org_id FKs of OrganizationEmail/OrganizationPhone/OrganizationDomain in app/models.py (SQLModel metadata). This migration brings existing databases in line: drops the three <table>_org_id_fkey constraints and recreates them with ON DELETE CASCADE. Downgrade restores the original NO ACTION FKs. app/models.py untouched; only this migration file is added.
Author
Owner

CI status note: run 5938 (this PR) failed, but the failure is a pre-existing baseline, not this change.

  • Duration profile: this PR run 5938 failed at 129s; the identical ci.yml run on main HEAD bd0fb0f (run 5900, push) already failed at 121s. Same short-failure signature before this PR touched anything.
  • Reproduction: ruff check app/ --config ../pyproject.toml reports 118 errors, all under app/ (pre-existing lint debt on main). CI's backend-lint job gates on exactly that command. None of the errors involve patterns this migration could influence; and CI lint scope is app/ only — it never checks backend/alembic/versions/.
  • The new migration file itself is lint-clean (ruff check on b53942abd63a_ondelete_cascade_org_children.py exits 0).
  • Diff is exactly one added file (+68), untouched app/models.py.

The "checks pass" gate cannot go green without fixing the 118 baseline ruff errors on main, which is outside this PR's scope (this PR deliberately adds only the migration file). Recommend separate tracking for the baseline lint debt if CI green is required before merge.

CI status note: run 5938 (this PR) failed, but the failure is a pre-existing baseline, not this change. - Duration profile: this PR run 5938 failed at 129s; the identical ci.yml run on main HEAD bd0fb0f (run 5900, push) already failed at 121s. Same short-failure signature before this PR touched anything. - Reproduction: `ruff check app/ --config ../pyproject.toml` reports 118 errors, all under `app/` (pre-existing lint debt on main). CI's backend-lint job gates on exactly that command. None of the errors involve patterns this migration could influence; and CI lint scope is `app/` only — it never checks `backend/alembic/versions/`. - The new migration file itself is lint-clean (`ruff check` on b53942abd63a_ondelete_cascade_org_children.py exits 0). - Diff is exactly one added file (+68), untouched app/models.py. The "checks pass" gate cannot go green without fixing the 118 baseline ruff errors on main, which is outside this PR's scope (this PR deliberately adds only the migration file). Recommend separate tracking for the baseline lint debt if CI green is required before merge.
Author
Owner

Closing as superseded: the canonical fix for #96 has landed on main via watcher recovery commit 8a891e7 (migration b2c3d4e5f6a8_add_ondelete_cascade_org_child_fks.py, 173 lines). That migration mirrors the proven f101c4scd0a1_add_ondelete_cascade_case_membership_fk pattern from #101 with an _fk_has_cascade guard for safe re-run / fresh-schema no-op. PR #103 and PR #108 are now both superseded.

Thanks for the earlier migration drafts — the discussion on idempotency and the data-preservation pattern informed the landed version.

Closing as superseded: the canonical fix for #96 has landed on `main` via watcher recovery commit `8a891e7` (migration `b2c3d4e5f6a8_add_ondelete_cascade_org_child_fks.py`, 173 lines). That migration mirrors the proven `f101c4scd0a1_add_ondelete_cascade_case_membership_fk` pattern from #101 with an `_fk_has_cascade` guard for safe re-run / fresh-schema no-op. PR #103 and PR #108 are now both superseded. Thanks for the earlier migration drafts — the discussion on idempotency and the data-preservation pattern informed the landed version.
Mark closed this pull request 2026-09-10 04:03:44 +00:00
Some checks failed
CI / Backend Lint & Format (pull_request) Failing after 4s
CI / Golden Fixture Validation (pull_request) Has been skipped
CI / Backend Tests (pull_request) Has been skipped
CI / Backend Security Tests (pull_request) Has been skipped
CI / Frontend Lint (pull_request) Failing after 23s
CI / Frontend Tests (pull_request) Has been skipped
CI / Dependency Security Scan (pull_request) Has been skipped
Stress Tests / Stress & Pipeline Tests (pull_request) Failing after 6s
Stress Tests / Full Data Pipeline Validation (pull_request) Has been skipped
Performance Regression Tests / Performance Regression Detection (pull_request) Failing after 33s
CI / Frontend E2E Tests (pull_request) Has been skipped
Performance Regression Tests / Compare with Baseline (pull_request) Has been skipped
CI / All Checks Passed (pull_request) Failing after 48s

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Mark/osint-hub!108
No description provided.