From e7f5908d01d8b71cc23bf2e626e8e5e484d16ba1 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Thu, 10 Sep 2026 09:53:26 +0000 Subject: [PATCH] core: name the unique index on wallet_seeds The one key per device rule is meant to be lifted later. SQLite cannot drop a column with an inline UNIQUE, or its automatic index, so lifting it would have meant rebuilding the table. As a named index it is a DROP INDEX and a DROP COLUMN. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01Rvc3HbiWBTqbAvRT45G5oX --- .../Store/Postgres/Migrations/M20260908_wallet_seeds.hs | 3 ++- .../Chat/Store/Postgres/Migrations/chat_schema.sql | 9 ++++----- .../Store/SQLite/Migrations/M20260908_wallet_seeds.hs | 3 ++- src/Simplex/Chat/Store/SQLite/Migrations/chat_schema.sql | 3 ++- src/Simplex/Chat/Wallet.hs | 4 ++-- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Simplex/Chat/Store/Postgres/Migrations/M20260908_wallet_seeds.hs b/src/Simplex/Chat/Store/Postgres/Migrations/M20260908_wallet_seeds.hs index 382aa79178..a5117f0873 100644 --- a/src/Simplex/Chat/Store/Postgres/Migrations/M20260908_wallet_seeds.hs +++ b/src/Simplex/Chat/Store/Postgres/Migrations/M20260908_wallet_seeds.hs @@ -15,12 +15,13 @@ CREATE TABLE wallet_seeds ( -- see the SQLite migration next_account_index BIGINT NOT NULL DEFAULT 0, -- one key per device for now - single_seed SMALLINT NOT NULL DEFAULT 1 UNIQUE + single_seed SMALLINT NOT NULL DEFAULT 1 ); ALTER TABLE users ADD COLUMN wallet_seed_id BIGINT REFERENCES wallet_seeds ON DELETE RESTRICT; ALTER TABLE users ADD COLUMN wallet_account_index BIGINT; +CREATE UNIQUE INDEX idx_wallet_seeds_single_seed ON wallet_seeds(single_seed); CREATE INDEX idx_users_wallet_seed_id ON users(wallet_seed_id); |] diff --git a/src/Simplex/Chat/Store/Postgres/Migrations/chat_schema.sql b/src/Simplex/Chat/Store/Postgres/Migrations/chat_schema.sql index b629dd814f..090e2ebf6d 100644 --- a/src/Simplex/Chat/Store/Postgres/Migrations/chat_schema.sql +++ b/src/Simplex/Chat/Store/Postgres/Migrations/chat_schema.sql @@ -1918,11 +1918,6 @@ ALTER TABLE ONLY test_chat_schema.wallet_seeds -ALTER TABLE ONLY test_chat_schema.wallet_seeds - ADD CONSTRAINT wallet_seeds_single_seed_key UNIQUE (single_seed); - - - ALTER TABLE ONLY test_chat_schema.xftp_file_descriptions ADD CONSTRAINT xftp_file_descriptions_pkey PRIMARY KEY (file_descr_id); @@ -2676,6 +2671,10 @@ CREATE INDEX idx_users_wallet_seed_id ON test_chat_schema.users USING btree (wal +CREATE UNIQUE INDEX idx_wallet_seeds_single_seed ON test_chat_schema.wallet_seeds USING btree (single_seed); + + + CREATE INDEX idx_xftp_file_descriptions_user_id ON test_chat_schema.xftp_file_descriptions USING btree (user_id); diff --git a/src/Simplex/Chat/Store/SQLite/Migrations/M20260908_wallet_seeds.hs b/src/Simplex/Chat/Store/SQLite/Migrations/M20260908_wallet_seeds.hs index 4e7235ff40..dfacc89385 100644 --- a/src/Simplex/Chat/Store/SQLite/Migrations/M20260908_wallet_seeds.hs +++ b/src/Simplex/Chat/Store/SQLite/Migrations/M20260908_wallet_seeds.hs @@ -16,12 +16,13 @@ CREATE TABLE wallet_seeds ( -- the order they ask for a key, not the order they had next_account_index INTEGER NOT NULL DEFAULT 0, -- one key per device for now - single_seed INTEGER NOT NULL DEFAULT 1 UNIQUE + single_seed INTEGER NOT NULL DEFAULT 1 ) STRICT; ALTER TABLE users ADD COLUMN wallet_seed_id INTEGER REFERENCES wallet_seeds ON DELETE RESTRICT; ALTER TABLE users ADD COLUMN wallet_account_index INTEGER; +CREATE UNIQUE INDEX idx_wallet_seeds_single_seed ON wallet_seeds(single_seed); CREATE INDEX idx_users_wallet_seed_id ON users(wallet_seed_id); |] diff --git a/src/Simplex/Chat/Store/SQLite/Migrations/chat_schema.sql b/src/Simplex/Chat/Store/SQLite/Migrations/chat_schema.sql index 7a3b18af4d..04d830fee5 100644 --- a/src/Simplex/Chat/Store/SQLite/Migrations/chat_schema.sql +++ b/src/Simplex/Chat/Store/SQLite/Migrations/chat_schema.sql @@ -862,7 +862,7 @@ CREATE TABLE wallet_seeds( -- the order they ask for a key, not the order they had next_account_index INTEGER NOT NULL DEFAULT 0, -- one key per device for now - single_seed INTEGER NOT NULL DEFAULT 1 UNIQUE + single_seed INTEGER NOT NULL DEFAULT 1 ) STRICT; CREATE INDEX contact_profiles_index ON contact_profiles( display_name, @@ -1398,6 +1398,7 @@ CREATE INDEX idx_files_roster_transfer_id ON files(roster_transfer_id); CREATE INDEX idx_chat_items_item_signed_by_group_member_id ON chat_items( item_signed_by_group_member_id ); +CREATE UNIQUE INDEX idx_wallet_seeds_single_seed ON wallet_seeds(single_seed); CREATE INDEX idx_users_wallet_seed_id ON users(wallet_seed_id); CREATE TRIGGER on_group_members_insert_update_summary AFTER INSERT ON group_members diff --git a/src/Simplex/Chat/Wallet.hs b/src/Simplex/Chat/Wallet.hs index 8a15eb806b..458a3e9a54 100644 --- a/src/Simplex/Chat/Wallet.hs +++ b/src/Simplex/Chat/Wallet.hs @@ -2,8 +2,8 @@ -- | BIP-39 seeds and the keys derived from them. -- --- One key per name, not one per profile, so that exporting a name's key hands --- over that name only. +-- Two layers: one account path per profile, and one key per name under it. A +-- per-profile key would hand over every name that profile owns. module Simplex.Chat.Wallet ( SeedId (..), WalletSeed (..),