From 98fb416165424e17bac17bbb598a747daabd3ce9 Mon Sep 17 00:00:00 2001 From: shum Date: Wed, 3 Jun 2026 19:35:19 +0000 Subject: [PATCH] chat: exclude soft-deleted contacts from idx_contacts_simplex_name The lookup `getContactBySimplexName` (Store/Direct.hs:781) filters `AND deleted = 0`, but the index predicate `WHERE simplex_name IS NOT NULL` covered tombstoned rows too. Forward-compat trap: once writers land a non-Nothing simplex_name, soft-deleting a contact would block re-claiming its name (UNIQUE conflict) even though the lookup reports the slot as free. Tighten the partial-index predicate to also require deleted = 0 so the constraint scope matches the live-lookup scope. Groups have no soft- delete column, so their index stays as-is. --- .../Chat/Store/Postgres/Migrations/M20260603_simplex_name.hs | 2 +- src/Simplex/Chat/Store/Postgres/Migrations/chat_schema.sql | 2 +- .../Chat/Store/SQLite/Migrations/M20260603_simplex_name.hs | 2 +- src/Simplex/Chat/Store/SQLite/Migrations/chat_schema.sql | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Simplex/Chat/Store/Postgres/Migrations/M20260603_simplex_name.hs b/src/Simplex/Chat/Store/Postgres/Migrations/M20260603_simplex_name.hs index 32b5097f0f..d7675709c9 100644 --- a/src/Simplex/Chat/Store/Postgres/Migrations/M20260603_simplex_name.hs +++ b/src/Simplex/Chat/Store/Postgres/Migrations/M20260603_simplex_name.hs @@ -15,7 +15,7 @@ ALTER TABLE connections ADD COLUMN simplex_name TEXT; CREATE UNIQUE INDEX idx_contacts_simplex_name ON contacts(user_id, simplex_name) - WHERE simplex_name IS NOT NULL; + WHERE simplex_name IS NOT NULL AND deleted = 0; CREATE UNIQUE INDEX idx_groups_simplex_name ON groups(user_id, simplex_name) diff --git a/src/Simplex/Chat/Store/Postgres/Migrations/chat_schema.sql b/src/Simplex/Chat/Store/Postgres/Migrations/chat_schema.sql index d3f82d4661..b2096e629f 100644 --- a/src/Simplex/Chat/Store/Postgres/Migrations/chat_schema.sql +++ b/src/Simplex/Chat/Store/Postgres/Migrations/chat_schema.sql @@ -2193,7 +2193,7 @@ CREATE INDEX idx_contacts_grp_direct_inv_from_member_conn_id ON test_chat_schema -CREATE UNIQUE INDEX idx_contacts_simplex_name ON test_chat_schema.contacts USING btree (user_id, simplex_name) WHERE (simplex_name IS NOT NULL); +CREATE UNIQUE INDEX idx_contacts_simplex_name ON test_chat_schema.contacts USING btree (user_id, simplex_name) WHERE ((simplex_name IS NOT NULL) AND (deleted = 0)); diff --git a/src/Simplex/Chat/Store/SQLite/Migrations/M20260603_simplex_name.hs b/src/Simplex/Chat/Store/SQLite/Migrations/M20260603_simplex_name.hs index b96b13c9e3..ec16ea8bf7 100644 --- a/src/Simplex/Chat/Store/SQLite/Migrations/M20260603_simplex_name.hs +++ b/src/Simplex/Chat/Store/SQLite/Migrations/M20260603_simplex_name.hs @@ -14,7 +14,7 @@ ALTER TABLE connections ADD COLUMN simplex_name TEXT; CREATE UNIQUE INDEX idx_contacts_simplex_name ON contacts(user_id, simplex_name) - WHERE simplex_name IS NOT NULL; + WHERE simplex_name IS NOT NULL AND deleted = 0; CREATE UNIQUE INDEX idx_groups_simplex_name ON groups(user_id, simplex_name) diff --git a/src/Simplex/Chat/Store/SQLite/Migrations/chat_schema.sql b/src/Simplex/Chat/Store/SQLite/Migrations/chat_schema.sql index 85e157f6a2..ae08b9e20b 100644 --- a/src/Simplex/Chat/Store/SQLite/Migrations/chat_schema.sql +++ b/src/Simplex/Chat/Store/SQLite/Migrations/chat_schema.sql @@ -1315,7 +1315,7 @@ ON contacts( user_id, simplex_name ) -WHERE simplex_name IS NOT NULL; +WHERE simplex_name IS NOT NULL AND deleted = 0; CREATE UNIQUE INDEX idx_groups_simplex_name ON groups( user_id,