Files
evig/scripts
g-butandClaude Opus 4.7 9545068899 db: migration 081 — audit-actor user_id FKs → ON DELETE SET NULL (#4)
Item #4 of the post-team-rebuild backlog: review the ~47 user_id FKs
that landed across history without an explicit ON DELETE policy.
Without a policy the default is NO ACTION, which silently blocks
user deletion any time the user is referenced. Combined with the
RESTRICT policies on payment_transactions (mig 077) and refunds
(mig 078), the result is "you can almost never delete a user."

This migration handles the "audit / reviewer attribution" subset of
those columns — 7 places where the column records *who* took an
action on a row. The row itself must remain (audit history); only
the actor reference can be NULL'd out so the UI shows "Deleted user"
instead of blocking the entire delete chain.

Converted to ON DELETE SET NULL:
  decisions.closed_by
  listing_reports.reviewed_by
  staff_permission_requests.reviewed_by
  user_content_submissions.reviewed_by
  ai_extracted_products.created_by
  ai_extracted_products.reviewed_by
  workshop_materials.uploaded_by

Deliberately NOT in scope:
  - payment-trail FKs (already RESTRICT via 077/078 — must stay)
  - tasks.created_by / tasks.completed_by — gating analytics; leave
    blocking by default until GDPR-anonymize-then-soft-delete lands
  - subscription_pools.owner_id, pool_memberships.user_id,
    pool_votes.voter_id — pools have membership semantics; CASCADE
    is the right policy, not SET NULL. Separate per-table decision.

Each ALTER is wrapped in a DO block that looks up the existing FK
by (table, column, ref table) — Postgres auto-named the originals
when they were defined inline, so we don't assume the name. The new
constraints get a stable `_setnull` suffix so re-running the
migration is a no-op.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-29 10:57:39 +02:00
..