Skip to content

feat(store): make every delete a soft delete - #413

Merged
nourshoreibah merged 7 commits into
mainfrom
feat/soft-deletes
Oct 17, 2026
Merged

nourshoreibah merged 7 commits into
mainfrom
feat/soft-deletes

Conversation

@nourshoreibah

@nourshoreibah nourshoreibah commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

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_at timestamp. 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

Delete Before Now
expenditure / donation / report row deleted, rollup decremented row stamped, same rollup decrement
donor donations + donor deleted donor and its donations stamped with the same time; rollups backed out as before
project CASCADE removed children, memberships, rollups project + its donations, expenditures, reports stamped with the same time; memberships and rollup rows still hard-deleted (as the CASCADE did)
user memberships deleted, entered_by SET NULL, row deleted, Cognito user deleted memberships deleted, entered_by cleared, row stamped, Cognito user still deleted
S3 receipt / report files best-effort delete kept (so a restore is possible)

No restore endpoint or UI yet; recovery is by SQL.

How reads stay identical

  • Every read of the six tables filters deleted_at IS NULL. On a leftJoin the filter sits in the ON clause, so the parent row still comes back.
  • The store's Kysely has a result plugin that strips deleted_at from every row, so selectAll() and returningAll() responses don't gain a field.
  • An insert under a soft-deleted parent throws 23503, just as the FK did, so the controllers' existing 404 handling still fires. The parent check takes FOR 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 UPDATE made them conflict on DSQL). projects/test/soft-delete-concurrency.e2e.test.ts covers both; removing the index fails 3 of its 4 tests.
  • Edits to soft-deleted rows find nothing and return 404, the same as today.

Schema (10 migrations, one DDL each, dsql-lint clean)

  • ADD COLUMN deleted_at TIMESTAMP on 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 make deleted_at a 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 CONSTRAINT is 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, receiptDeleted and fileDeleted fields 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.ts statically scans the lambdas, the store and lambda-auth. Any selectFrom, join or raw FROM/JOIN on a soft-delete table without a deleted_at filter fails the test.
  • Each lambda has a 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.
  • Mutation-checked: removing one filter from listProjects fails both the parity test and the guard.

Test plan

  • tsc --noEmit passes for all 6 lambdas and the store.
  • Tests run serially against local Postgres:
    • donors 61/61
    • expenditures 137/137
    • projects 159/159
    • reports 142/142
    • users 71/71
    • store 11/11
    • lambda-auth 30/30
  • auth: 82/85. The 3 failures are HTTP tests against localhost:3000, which was occupied locally by an unrelated Next.js dev server. CI runs them on a clean runner.
  • Frontend: 382 tests pass, next build and npm run lint succeed.
  • dsql-lint@0.2.20 passes on all new migrations. The constraint names were verified by building the schema in a throwaway database.
  • CI lambda-tests and migrations jobs

Notes for reviewers

  • Migrations apply to prod before the lambda deploy. All of them are additive or relaxing, so the currently deployed code keeps working.
  • apps/frontend: only the copy changed. "permanently deletes" becomes "deletes", and stale "every FK is ON DELETE CASCADE" comments are fixed.
  • @aws-sdk/client-s3 remains in the projects lambda's package.json although nothing imports it now. It's left in so this PR doesn't touch the lockfile.

🤖 Generated with Claude Code

nourshoreibah and others added 2 commits October 15, 2026 16:27
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>
@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

This PR contains a database migration

  • apps/backend/db/migrations/V20261016230432__add_users_deleted_at.sql
  • apps/backend/db/migrations/V20261016230433__add_projects_deleted_at.sql
  • apps/backend/db/migrations/V20261016230434__add_donors_deleted_at.sql
  • apps/backend/db/migrations/V20261016230435__add_project_donations_deleted_at.sql
  • apps/backend/db/migrations/V20261016230436__add_expenditures_deleted_at.sql
  • apps/backend/db/migrations/V20261016230437__add_reports_deleted_at.sql
  • apps/backend/db/migrations/V20261016230438__drop_users_email_unique.sql

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:

  • Applied and tested locally. cd apps/backend && make migrate, then run the affected lambda's tests (cd apps/backend/lambdas/<name> && npm test). make show-migrations shows what applied.
  • Safe for the code that is live right now. During the deploy window -- and indefinitely if the deploy fails -- the currently deployed lambdas run against your new schema. Additive changes (CREATE TABLE, nullable ADD COLUMN, CREATE INDEX) are fine in one PR. DROP COLUMN, renames, ADD COLUMN NOT NULL with no default, and new UNIQUE/CHECK/FOREIGN KEY constraints need two merged PRs -- see the expand/contract rules in apps/backend/db/README.md.
  • Kept separate from unrelated changes. A migration PR should ideally contain the migration, the code that needs it, and nothing else. It changes production state, it is the one thing here that redeploying cannot roll back, and a reviewer should be able to see the whole schema change without scrolling past unrelated work.
  • No already-merged migration was edited. Fix an old migration by adding a new one; there is no down.

shared/types/db-types.d.ts is regenerated and pushed to this branch automatically -- don't hand-edit it. Expect one red migrations-fresh check before that commit lands.

This PR also changes 50 files outside apps/backend/db/. If any are unrelated to this schema change, consider splitting them into a separate PR.

@github-actions

Copy link
Copy Markdown
Contributor

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.

nourshoreibah and others added 2 commits October 16, 2026 19:05
# 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>
@github-actions

Copy link
Copy Markdown
Contributor

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>
@github-actions

Copy link
Copy Markdown
Contributor

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>
@github-actions

Copy link
Copy Markdown
Contributor

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
@github-actions

Copy link
Copy Markdown
Contributor

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.

@nourshoreibah nourshoreibah added the no-review The PR review bot won't run label Oct 17, 2026
@nourshoreibah
nourshoreibah marked this pull request as ready for review October 17, 2026 01:51
@nourshoreibah
nourshoreibah merged commit 1611060 into main Oct 17, 2026
21 checks passed
@nourshoreibah
nourshoreibah deleted the feat/soft-deletes branch October 17, 2026 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-review The PR review bot won't run

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant