Skip to content

fix(db): scope the DM idempotency index to live channels - #9794

Open
flowq-C wants to merge 1 commit into
block:mainfrom
flowq-C:fix/dm-hash-live-channels
Open

flowq-C wants to merge 1 commit into
block:mainfrom
flowq-C:fix/dm-hash-live-channels

Conversation

@flowq-C

@flowq-C flowq-C commented Nov 20, 2026

Copy link
Copy Markdown

Fixes #9793

What

Migration 0105 rebuilds idx_channels_dm_hash as UNIQUE (community_id, participant_hash) WHERE participant_hash IS NOT NULL AND deleted_at IS NULL. schema/tables/public/channels.sql carries the same definition, and the migrator count test in runtime/migration.rs now expects 105 migrations.

Why

soft_delete_channel keeps participant_hash, while find_dm_in only matches live rows. After a DM is deleted, reopening it for the same participants inserts a new row that violates the unique index on the deleted one, so the pair can never open a DM again (a DM with these members already exists). Two live DMs for one pair stay impossible.

Migration notes (docs/database.md)

  • One transaction, SET LOCAL lock_timeout = '5s' and statement_timeout = '30s'. DROP INDEX and CREATE UNIQUE INDEX run together under the table lock, so channels is never without a uniqueness guarantee for live DMs.
  • No trigger, function, foreign key or data backfill. The new index covers a subset of the old one, so an older relay works against the migrated database.
  • The unique build scans channels, which is small in every deployment I know of. A deployment that applies migrations out of band can run the two statements in a maintenance window.

Dependencies

Independent of #9792. It is based on current main and touches different files.

Tests

Added deleted_dm_does_not_block_reopening_the_same_pair (Postgres, #[ignore] like the neighbouring DM tests): open, delete, reopen creates a new DM, a third open reuses it. Local PostgreSQL 17, migrations applied with buzz-admin migrate:

  • cargo test -p buzz-db --lib deleted_dm_does_not_block -- --ignored: passes with the migration; fails without it (reopen dm after delete: InvalidData("a DM with these members already exists")).
  • cargo test -p buzz-db --lib embedded_migrator_contains: ok.
  • cargo test -p buzz-db --lib dm -- --include-ignored: 114 passed.
  • cargo fmt -p buzz-db -- --check: ok.

Not run locally: the rest of the workspace test suite, buzz-relay integration tests, and pgschema against schema/.

🤖 Generated with Claude Code

soft_delete_channel sets deleted_at but keeps participant_hash, while
find_dm_in only matches deleted_at IS NULL. Reopening a DM for the same
participants after a delete finds nothing, inserts a new row and hits
idx_channels_dm_hash on the deleted row, so the pair can never open a DM
again. Make the unique index partial over deleted_at IS NULL (migration
0105, schema/tables/public/channels.sql).

Signed-off-by: flowq-C <134304440+flowq-C@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown

🔐 Codex Security Review

Status: review required for the current range.

The current range is a79cbb4664c175a4b69026aec610ea549bdfcb2c...dc0a5cbd78011ee7563f877b3b0e4748ade0bdf0.
A new review must complete for this exact range. When manual authorization
is required, a user with write access must comment exactly
@buzz-security-review dc0a5cbd78011ee7563f877b3b0e4748ade0bdf0 to authorize a new review.
Any previous review applies only to its recorded range.

@flowq-C
flowq-C marked this pull request as ready for review November 20, 2026 13:34

@joahg joahg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 No blocking issues found. The new deleted_at IS NULL predicate matches find_dm_in and claim_dm_identity, which already filter on live rows, and open_dm relies on the 23505 from the index (no ON CONFLICT target), so lookup and uniqueness now agree. The migration is safe on existing data (the new index covers a strict subset of the old one's rows, so it cannot fail on current data), it runs in one transaction with lock and statement budgets, and the schema file is updated to match. The regression test covers open, delete, reopen, and a third open reusing the new DM. Heads-up: draft #9772 also adds migrations/0105_* and edits the migration count assertion in runtime/migration.rs, so whichever lands second needs to renumber. This PR is from outside the Buzz maintainer team, so a human Buzz maintainer needs to review and approve it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A deleted DM blocks reopening a DM for the same participants

2 participants