Files
buzz/crates
Luke Tornquist 3bd121bc15 Check product_feedback's community in the writing transaction (#9608)
## Summary

Third app-side step of retiring the database foreign keys (after #9399
and #9404). After this, every reference the drop migrations will remove
is checked in application code.

- **`product_feedback -> communities`:** `product_feedback::insert` now
runs in a transaction. It locks the community row `FOR KEY SHARE` and
returns `NotFound` when the row is missing. This is the same guarantee
the foreign key gives. Any lifecycle state still qualifies.
`docs/database.md` says provenance-only tables stay writable while their
community is being deleted, and the purge clears their provenance.
- **Reader tolerance:** `ProductFeedbackRecord.community_id` is now
`Option<Uuid>`. The purge sets `community_id = NULL`, and `list` (used
by `buzz-admin list-product-feedback`) failed to decode those rows. The
JSON output shows `null` for them.
- **One home for reference checks:** `require_workflow` and
`require_workflow_run` move from `store/workflow.rs` into
`store/reference_check.rs`. The workflow owner and channel checks now
reuse `require_user` and `require_optional_channel`, so two duplicate
helpers are gone. A new `require_community` joins them.
- **Stale comments:** comments that justified behavior by the foreign
keys now say what holds both before and after the drop. Touched: the
purge order (`deletion.rs`), `command_executor.rs`'s `ensure_user`, the
`set_agent_owner` doc, the NIP-43 snapshot lock note
(`relay_members.rs`), `attach_scheduled_workflow_run`, and one workflow
deletion test.

## Writers and readers checked (RPR #2)

`product_feedback.community_id` writers:
- `store/product_feedback.rs` `insert`: the only insert. It is now
checked.
- `store/deletion.rs` `purge_postgres`: sets the column to `NULL`. No
check is needed.
- `store/relay_admin_actions.rs` status update: does not touch
`community_id`.

Readers:
- `product_feedback::list`: fixed to accept `NULL`.
- `store/admin_moderation.rs` inbox queries: already `LEFT JOIN` and
accept `NULL`.

## Tests

All three tests drop `product_feedback -> communities` in the per-test
database first, so a pass proves the application check and not the
constraint.

- `insert_rejects_missing_community_without_foreign_key`: a community
that never existed gets `NotFound` and no row. A fenced community still
accepts feedback. An active one accepts it, and a replay returns the
same row.
- `insert_key_share_locks_community_without_foreign_key`: an insert
waits while another transaction holds the community `FOR UPDATE`, then
proceeds.
- `list_returns_rows_with_cleared_provenance`: a row with `community_id
= NULL` is listed with `community_id: None`.

## Not in this PR

- The foreign key drop migrations. They come after this PR is deployed
(`docs/database.md` expand/contract).
- Nothing in the database stops `DELETE FROM communities` on an active
community once the community foreign keys are gone. The tombstone
trigger rejects it only for inactive communities. Production never
hard-deletes communities. This is decided with the drop migrations,
since a trigger change conflicts with the no-new-triggers rule.

## What got simpler

All in-transaction reference checks now live in one module. Two
duplicate helpers were removed. `store/workflow.rs` loses about 80
lines.

buzz-review-completed

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Luke Tornquist <tornquist@squareup.com>
Signed-off-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Tom Brow <tomb@squareup.com>
2026-11-17 16:10:58 -05:00
..