Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/buzz-db/src/runtime/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions crates/buzz-db/src/store/dm_postgres_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
21 changes: 21 additions & 0 deletions migrations/0105_dm_hash_unique_live_channels.sql
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 3 additions & 1 deletion schema/tables/public/channels.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down