From dc0a5cbd78011ee7563f877b3b0e4748ade0bdf0 Mon Sep 17 00:00:00 2001 From: flowq-C <134304440+flowq-C@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:33:47 +0100 Subject: [PATCH] fix(db): scope the DM idempotency index to live channels 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> --- crates/buzz-db/src/runtime/migration.rs | 5 +- crates/buzz-db/src/store/dm_postgres_tests.rs | 52 +++++++++++++++++++ .../0105_dm_hash_unique_live_channels.sql | 21 ++++++++ schema/tables/public/channels.sql | 4 +- 4 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 migrations/0105_dm_hash_unique_live_channels.sql diff --git a/crates/buzz-db/src/runtime/migration.rs b/crates/buzz-db/src/runtime/migration.rs index 29a0e6356ec..5c8b47993b2 100644 --- a/crates/buzz-db/src/runtime/migration.rs +++ b/crates/buzz-db/src/runtime/migration.rs @@ -879,7 +879,10 @@ mod postgres_tests { let mut migrations: Vec<_> = MIGRATOR.iter().collect(); migrations.sort_by_key(|migration| migration.version); - assert_eq!(migrations.len(), 104); + assert_eq!(migrations.len(), 105); + assert_eq!(migrations[104].version, 105); + assert!(strip_sql_comments(migrations[104].sql.as_str()) + .contains("WHERE participant_hash IS NOT NULL AND deleted_at IS NULL;")); assert_eq!(migrations[103].version, 104); let event_table_drops = strip_sql_comments(migrations[103].sql.as_str()); assert!(event_table_drops diff --git a/crates/buzz-db/src/store/dm_postgres_tests.rs b/crates/buzz-db/src/store/dm_postgres_tests.rs index fa4626c646a..096316e589c 100644 --- a/crates/buzz-db/src/store/dm_postgres_tests.rs +++ b/crates/buzz-db/src/store/dm_postgres_tests.rs @@ -147,3 +147,55 @@ async fn cluster_global_concurrent_dm_creation_reports_only_the_inserter() { db.pool().close().await; } } + +#[tokio::test] +#[ignore = "requires Postgres"] +async fn deleted_dm_does_not_block_reopening_the_same_pair() { + let url = crate::test_support::database_url(); + let db = Db::from_pool(PgPool::connect(&url).await.expect("database")); + let author = nostr::Keys::generate().public_key().to_bytes(); + let recipient = nostr::Keys::generate().public_key().to_bytes(); + let community = match db + .create_community_with_owner( + &format!("dm-reopen-{}.example.org", Uuid::new_v4().simple()), + &hex::encode(author), + &nostr::Keys::generate(), + ) + .await + .expect("community") + .outcome + { + CreateCommunityWithOwnerResult::Created(row) => row.id, + other => panic!("unexpected community: {other:?}"), + }; + for key in [&author, &recipient] { + db.ensure_user(community, key).await.expect("user"); + } + + let (first, created) = db + .open_dm(community, &[&recipient], &author, false, ALLOW) + .await + .expect("open dm"); + assert!(created); + assert!(db + .soft_delete_channel(community, first.id) + .await + .expect("delete dm")); + + // The deleted DM keeps its participant_hash; reopening must create a new + // DM instead of failing on the unique index. + let (second, created) = db + .open_dm(community, &[&recipient], &author, false, ALLOW) + .await + .expect("reopen dm after delete"); + assert!(created); + assert_ne!(second.id, first.id); + + // Two live DMs for one pair stay impossible. + let (again, created) = db + .open_dm(community, &[&recipient], &author, false, ALLOW) + .await + .expect("open dm again"); + assert!(!created); + assert_eq!(again.id, second.id); +} diff --git a/migrations/0105_dm_hash_unique_live_channels.sql b/migrations/0105_dm_hash_unique_live_channels.sql new file mode 100644 index 00000000000..dc3e12cd1fd --- /dev/null +++ b/migrations/0105_dm_hash_unique_live_channels.sql @@ -0,0 +1,21 @@ +-- Scope the DM idempotency index to live channels. +-- +-- `soft_delete_channel` sets `deleted_at` and leaves `participant_hash` in +-- place, while `find_dm_in` looks DMs up with `deleted_at IS NULL`. Reopening +-- the same participant set after a delete therefore finds nothing, inserts a +-- new row, and violates the unique index on the deleted row. The pair can +-- never open a DM again. A unique index that ignores deleted rows fixes this +-- without touching any data. +-- +-- Contract-free swap: both statements run in this one transaction, so +-- `channels` is never without a uniqueness guarantee for live DMs. The drop +-- takes ACCESS EXCLUSIVE on `channels`; the budgets fail this migration +-- rather than queue relay traffic behind it. The new index is strictly weaker +-- than the old one (a subset of rows), so an older relay running against the +-- migrated database keeps working. +SET LOCAL lock_timeout = '5s'; +SET LOCAL statement_timeout = '30s'; + +DROP INDEX idx_channels_dm_hash; +CREATE UNIQUE INDEX idx_channels_dm_hash ON channels (community_id, participant_hash) + WHERE participant_hash IS NOT NULL AND deleted_at IS NULL; diff --git a/schema/tables/public/channels.sql b/schema/tables/public/channels.sql index c3347114fe7..9c2af3674b5 100644 --- a/schema/tables/public/channels.sql +++ b/schema/tables/public/channels.sql @@ -47,10 +47,12 @@ CREATE TABLE channels ( ); -- nip29 group id and DM participant hash are unique WITHIN a community, not globally. +-- The DM hash is unique among live channels only: a soft-deleted DM keeps its +-- hash and must not block reopening the same participant set. CREATE UNIQUE INDEX idx_channels_nip29_group ON channels (community_id, nip29_group_id) WHERE nip29_group_id IS NOT NULL; CREATE UNIQUE INDEX idx_channels_dm_hash ON channels (community_id, participant_hash) - WHERE participant_hash IS NOT NULL; + WHERE participant_hash IS NOT NULL AND deleted_at IS NULL; CREATE INDEX idx_channels_community_type ON channels (community_id, channel_type); CREATE INDEX idx_channels_community_visibility ON channels (community_id, visibility); CREATE INDEX idx_channels_created_by ON channels (community_id, created_by);