-
Notifications
You must be signed in to change notification settings - Fork 5
Clean up reactions and comments when articles or gists are deleted (polymorphic resource_id / resource_type) #88
Copy link
Copy link
Open
Labels
Description
Problem
reactions and comments reference content via polymorphic fields (resource_id, resource_type), not foreign keys to articles or gists. Deleting an article or gist does not remove related rows in those tables, so orphans can remain.
Only user_id → users is set to cascade on delete; parent resources are not.
Context
- There are no PostgreSQL triggers in this repo today for this behavior; cleanup would be new work (app-layer and/or migrations).
gist_filescan still cascade fromgistsvia normal FKs; this issue is about polymorphic children only.
Goal
Ensure that when an article or gist is deleted, all associated reactions and comments (and nested comment threads if applicable) are removed consistently, without leaving orphaned rows.
Options (pick one direction in implementation)
- Application layer: In the same transaction as the article/gist delete, explicitly delete matching
reactionsandcomments(and any related rows if required by the data model). - Database layer: Add PostgreSQL
AFTER DELETEtriggers (or other DDL) to delete matching polymorphic rows; keep behavior in migrations and document it.
Trade-offs: app-layer keeps logic in TypeScript and is easier to test in existing flows; DB triggers enforce consistency even if deletes happen outside the app (e.g. admin SQL).
Acceptance criteria
- Deleting an article removes all reactions and comments where
resource_id/resource_typerefer to that article (and handles nested comments per product rules). - Deleting a gist does the same for gist-scoped reactions/comments.
- Behavior is documented briefly (code comment or internal note) so future deletes do not bypass cleanup.
- No regressions to existing publish/delete flows; errors roll back the transaction.
References
- Schema:
src/backend/persistence/schemas.ts - Delete flows: article/gist server actions under
src/backend/services/
Reactions are currently unavailable