Repository navigation
feat(store): make every delete a soft delete - #413
Conversation
Deletes of users, projects, donors, donations, expenditures and reports now stamp a nullable deleted_at instead of removing the row. A project stamps its donations, expenditures and reports with the same time; a donor stamps its donations. Memberships and both rollups are still hard-deleted, and a user delete still clears expenditures.entered_by, so every read returns exactly what it did before. - Every read of the six tables filters deleted_at IS NULL (left joins in the ON clause). A static guard test fails a read that forgets. - The store's Kysely strips deleted_at from every result row. - Inserts under a soft-deleted parent throw 23503, as the FK did. - Drop UNIQUE(donor_id, project_id): a donor can give to a project more than once. Drop UNIQUE(email): a deleted user keeps the email, and Cognito still enforces uniqueness among live accounts. - S3 objects are kept on delete; the Cognito user is still deleted. - Per-lambda parity tests: soft-delete, snapshot every GET, purge the soft-deleted rows, snapshot again, expect identical responses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This PR contains a database migration
It will be applied to the production database automatically when this PR merges, before the new lambda code is deployed. Please confirm before requesting review:
This PR also changes 50 files outside |
|
Database Types Check Complete The database schema files were modified, but the regenerated TypeScript types are identical to the existing ones. No changes were needed and the type definitions are already up to date. |
# Conflicts: # apps/backend/lambdas/donors/test/donors.test.ts # apps/backend/lambdas/expenditures/controllers/expenditures.ts # apps/backend/lambdas/expenditures/services/expenditures.ts # apps/backend/lambdas/expenditures/test/expenditures.unit.test.ts # apps/backend/lambdas/reports/controllers/reports.ts # apps/backend/lambdas/reports/test/reports.unit.test.ts # shared/store/src/projects.ts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Database Types Check Complete The database schema files were modified, but the regenerated TypeScript types are identical to the existing ones. No changes were needed and the type definitions are already up to date. |
The new parity suites share the database with each lambda's e2e suite; jest's parallel workers truncated each other's fixtures in CI. Matches projects and reports. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Database Types Check Complete The database schema files were modified, but the regenerated TypeScript types are identical to the existing ones. No changes were needed and the type definitions are already up to date. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Database Types Check Complete The database schema files were modified, but the regenerated TypeScript types are identical to the existing ones. No changes were needed and the type definitions are already up to date. |
# Conflicts: # apps/backend/lambdas/projects/controllers/members.ts # apps/backend/lambdas/projects/services/projects.ts # apps/backend/lambdas/users/controllers/users.ts # apps/backend/lambdas/users/test/user.photos.unit.test.ts # apps/backend/lambdas/users/test/users.test.ts # shared/lambda-auth/src/authenticate.ts # shared/store/src/users.ts
|
Database Types Check Complete The database schema files were modified, but the regenerated TypeScript types are identical to the existing ones. No changes were needed and the type definitions are already up to date. |
Summary
Every user-facing delete is now a soft delete. The rows for users, projects, donors, donations, expenditures and reports stay in the table with a
deleted_attimestamp. Reads are unchanged: every GET, dashboard total and paginated count returns exactly what it returned when those rows were hard-deleted.What a delete does now
entered_bySET NULL, row deleted, Cognito user deletedentered_bycleared, row stamped, Cognito user still deletedNo restore endpoint or UI yet; recovery is by SQL.
How reads stay identical
deleted_at IS NULL. On aleftJointhe filter sits in theONclause, so the parent row still comes back.deleted_atfrom every row, soselectAll()andreturningAll()responses don't gain a field.23503, just as the FK did, so the controllers' existing 404 handling still fires. The parent check takesFOR KEY SHARE, and a unique index on(id, deleted_at)makes the soft-delete stamp a key update. So a delete conflicts with an in-flight insert, while concurrent inserts under the same parent never conflict with each other (FOR UPDATEmade them conflict on DSQL).projects/test/soft-delete-concurrency.e2e.test.tscovers both; removing the index fails 3 of its 4 tests.Schema (10 migrations, one DDL each,
dsql-lintclean)ADD COLUMN deleted_at TIMESTAMPon 6 tables.CREATE UNIQUE INDEX ${async} {projects,donors,users}_live_key ON … (id, deleted_at): these can never reject a row; they exist only to makedeleted_ata key column for row locking (see above).DROP CONSTRAINT users_email_key: a deleted user keeps their email. Cognito still enforces uniqueness among live accounts, and POST /users keeps its live-duplicate pre-check, so it still returns 409.DROP CONSTRAINTis a supported ALTER TABLE action on Aurora DSQL (per the AWS docs), and both are relaxations, so they're safe for the code that's live now.Response changes (DELETE only)
The
filesDeleted,receiptDeletedandfileDeletedfields are dropped from the DELETE responses, because no S3 delete runs any more. Nothing reads them, and openapi didn't document them.Guards
shared/store/test/soft-delete-guard.test.tsstatically scans the lambdas, the store and lambda-auth. AnyselectFrom, join or rawFROM/JOINon a soft-delete table without adeleted_atfilter fails the test.soft-delete-parity.e2e.test.ts. It deletes one of each entity through the store, snapshots the status and body of every GET, hard-purges the soft-deleted rows, snapshots again, and requires the two snapshots to be identical.listProjectsfails both the parity test and the guard.Test plan
tsc --noEmitpasses for all 6 lambdas and the store.localhost:3000, which was occupied locally by an unrelated Next.js dev server. CI runs them on a clean runner.next buildandnpm run lintsucceed.dsql-lint@0.2.20passes on all new migrations. The constraint names were verified by building the schema in a throwaway database.lambda-testsand migrations jobsNotes for reviewers
apps/frontend: only the copy changed. "permanently deletes" becomes "deletes", and stale "every FK is ON DELETE CASCADE" comments are fixed.@aws-sdk/client-s3remains in the projects lambda'spackage.jsonalthough nothing imports it now. It's left in so this PR doesn't touch the lockfile.🤖 Generated with Claude Code