From d0134da3f4da2f3de9e9b159ea33a51e58ed2c7b Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Fri, 15 Sep 2023 17:53:10 +0100 Subject: [PATCH] fix migration --- simplex-chat.cabal | 1 + src/Simplex/Chat.hs | 4 +- .../Migrations/M20230914_member_probes.hs | 91 +++++-------------- src/Simplex/Chat/Migrations/chat_schema.sql | 22 ++++- src/Simplex/Chat/Store/Migrations.hs | 4 +- 5 files changed, 50 insertions(+), 72 deletions(-) diff --git a/simplex-chat.cabal b/simplex-chat.cabal index ebd3d1d646..4c6f70be53 100644 --- a/simplex-chat.cabal +++ b/simplex-chat.cabal @@ -111,6 +111,7 @@ library Simplex.Chat.Migrations.M20230827_file_encryption Simplex.Chat.Migrations.M20230829_connections_chat_vrange Simplex.Chat.Migrations.M20230903_connections_to_subscribe + Simplex.Chat.Migrations.M20230914_member_probes Simplex.Chat.Mobile Simplex.Chat.Mobile.File Simplex.Chat.Mobile.Shared diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 510b9bf557..5fbb9fc7b1 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -3201,10 +3201,10 @@ processAgentMessageConn user@User {userId} corrId agentConnId agentMessage = do case ct_ of Nothing -> do let connectedIncognito = memberIncognito membership - when (memberCategory m == GCPreMember) $ probeMatchingContacts ct connectedIncognito + when (memberCategory m == GCPreMember) $ probeMatchingMemberContact m connectedIncognito Just ct -> do let connectedIncognito = contactConnIncognito ct || memberIncognito membership - when (memberCategory m == GCPreMember) $ probeMatchingMemberContact m connectedIncognito + when (memberCategory m == GCPreMember) $ probeMatchingContacts ct connectedIncognito -- >>= \case -- Nothing -> do -- notifyMemberConnected gInfo m Nothing diff --git a/src/Simplex/Chat/Migrations/M20230914_member_probes.hs b/src/Simplex/Chat/Migrations/M20230914_member_probes.hs index 6f6b7de196..e8f28d40d9 100644 --- a/src/Simplex/Chat/Migrations/M20230914_member_probes.hs +++ b/src/Simplex/Chat/Migrations/M20230914_member_probes.hs @@ -5,94 +5,51 @@ module Simplex.Chat.Migrations.M20230914_member_probes where import Database.SQLite.Simple (Query) import Database.SQLite.Simple.QQ (sql) -m20230903_connections_to_subscribe :: Query -m20230903_connections_to_subscribe = +m20230914_member_probes :: Query +m20230914_member_probes = [sql| -- sent_probes -ALTER TABLE sent_probes ADD COLUMN probe_contact_id INTEGER UNIQUE REFERENCES contacts ON DELETE CASCADE; -ALTER TABLE sent_probes ADD COLUMN probe_group_member_id INTEGER UNIQUE REFERENCES group_members ON DELETE CASCADE; +ALTER TABLE sent_probes ADD COLUMN probe_contact_id INTEGER REFERENCES contacts(contact_id) ON DELETE CASCADE; +ALTER TABLE sent_probes ADD COLUMN probe_group_member_id INTEGER REFERENCES group_members(group_member_id) ON DELETE CASCADE; UPDATE sent_probes SET probe_contact_id = contact_id; -ALTER TABLE sent_probes DROP COLUMN contact_id; +-- ALTER TABLE sent_probes DROP COLUMN contact_id; CREATE INDEX idx_sent_probes_user_id ON sent_probes(user_id); -CREATE INDEX idx_sent_probes_probe_contact_id ON sent_probes(probe_contact_id); -CREATE INDEX idx_sent_probes_probe_group_member_id ON sent_probes(probe_group_member_id); - --- sent_probe_hashes -ALTER TABLE sent_probe_hashes ADD COLUMN probe_contact_id INTEGER UNIQUE REFERENCES contacts ON DELETE CASCADE, -ALTER TABLE sent_probe_hashes ADD COLUMN probe_group_member_id INTEGER UNIQUE REFERENCES group_members ON DELETE CASCADE, -UPDATE sent_probe_hashes SET probe_contact_id = contact_id; - -DROP INDEX idx_sent_probe_hashes_contact_id ON sent_probe_hashes(contact_id); -ALTER TABLE sent_probe_hashes DROP COLUMN contact_id; - -CREATE INDEX idx_sent_probe_hashes_probe_contact_id ON sent_probe_hashes(probe_contact_id); -CREATE INDEX idx_sent_probe_hashes_probe_group_member_id ON sent_probe_hashes(probe_group_member_id); +CREATE UNIQUE INDEX idx_sent_probes_probe_contact_id ON sent_probes(probe_contact_id); +CREATE UNIQUE INDEX idx_sent_probes_probe_group_member_id ON sent_probes(probe_group_member_id); +CREATE INDEX idx_sent_probes_probe ON sent_probes(probe); -- received_probes -ALTER TABLE received_probes ADD COLUMN probe_contact_id INTEGER UNIQUE REFERENCES contacts ON DELETE CASCADE, -ALTER TABLE received_probes ADD COLUMN probe_group_member_id INTEGER UNIQUE REFERENCES group_members ON DELETE CASCADE, +ALTER TABLE received_probes ADD COLUMN probe_contact_id INTEGER REFERENCES contacts(contact_id) ON DELETE CASCADE; +ALTER TABLE received_probes ADD COLUMN probe_group_member_id INTEGER REFERENCES group_members(group_member_id) ON DELETE CASCADE; UPDATE received_probes SET probe_contact_id = contact_id; -DROP INDEX idx_received_probes_contact_id ON received_probes(contact_id); -ALTER TABLE received_probes DROP COLUMN contact_id; +DROP INDEX idx_received_probes_contact_id; +-- ALTER TABLE received_probes DROP COLUMN contact_id; -CREATE INDEX idx_received_probes_probe_contact_id ON received_probes(probe_contact_id); -CREATE INDEX idx_received_probes_probe_group_member_id ON received_probes(probe_group_member_id); +CREATE UNIQUE INDEX idx_received_probes_probe_contact_id ON received_probes(probe_contact_id); +CREATE UNIQUE INDEX idx_received_probes_probe_group_member_id ON received_probes(probe_group_member_id); +CREATE INDEX idx_received_probes_probe_hash ON received_probes(probe_hash); |] -down_m20230903_connections_to_subscribe :: Query -down_m20230903_connections_to_subscribe = +down_m20230914_member_probes :: Query +down_m20230914_member_probes = [sql| DROP INDEX idx_sent_probes_user_id; DROP INDEX idx_sent_probes_probe_contact_id; DROP INDEX idx_sent_probes_probe_group_member_id; --- DROP INDEX idx_sent_probe_hashes_probe_contact_id; -DROP INDEX idx_sent_probe_hashes_probe_group_member_id; --- DROP INDEX idx_received_probes_probe_contact_id; +DROP INDEX idx_sent_probes_probe; + +DROP INDEX idx_received_probes_probe_contact_id; DROP INDEX idx_received_probes_probe_group_member_id; +DROP INDEX idx_received_probes_probe_hash; DELETE FROM sent_probes WHERE probe_contact_id IS NULL; -DELETE FROM sent_probe_hashes WHERE probe_contact_id IS NULL; DELETE FROM received_probes WHERE probe_contact_id IS NULL; -ALTER TABLE sent_probes RENAME COLUMN probe_contact_id TO contact_id; -ALTER TABLE sent_probe_hashes RENAME COLUMN probe_contact_id TO contact_id; -ALTER TABLE received_probes RENAME COLUMN probe_contact_id TO contact_id; +-- ALTER TABLE sent_probes RENAME COLUMN probe_contact_id TO contact_id; +-- ALTER TABLE received_probes RENAME COLUMN probe_contact_id TO contact_id; -CREATE INDEX idx_received_probes_user_id ON received_probes(user_id); --- CREATE INDEX idx_received_probes_contact_id ON received_probes(contact_id); -CREATE INDEX idx_sent_probe_hashes_user_id ON sent_probe_hashes(user_id); --- CREATE INDEX idx_sent_probe_hashes_contact_id ON sent_probe_hashes(contact_id); +CREATE INDEX idx_received_probes_contact_id ON received_probes(contact_id); |] - - -CREATE TABLE sent_probes( - sent_probe_id INTEGER PRIMARY KEY, - contact_id INTEGER NOT NULL UNIQUE REFERENCES contacts ON DELETE CASCADE, - probe BLOB NOT NULL, - user_id INTEGER NOT NULL REFERENCES users ON DELETE CASCADE, - created_at TEXT CHECK(created_at NOT NULL), - updated_at TEXT CHECK(updated_at NOT NULL), - UNIQUE(user_id, probe) -); -CREATE TABLE sent_probe_hashes( - sent_probe_hash_id INTEGER PRIMARY KEY, - sent_probe_id INTEGER NOT NULL REFERENCES sent_probes ON DELETE CASCADE, - contact_id INTEGER NOT NULL REFERENCES contacts ON DELETE CASCADE, - user_id INTEGER NOT NULL REFERENCES users ON DELETE CASCADE, - created_at TEXT CHECK(created_at NOT NULL), - updated_at TEXT CHECK(updated_at NOT NULL), - UNIQUE(sent_probe_id, contact_id) -); -CREATE TABLE received_probes( - received_probe_id INTEGER PRIMARY KEY, - contact_id INTEGER NOT NULL REFERENCES contacts ON DELETE CASCADE, - probe BLOB, - probe_hash BLOB NOT NULL, - user_id INTEGER NOT NULL REFERENCES users ON DELETE CASCADE - , - created_at TEXT CHECK(created_at NOT NULL), - updated_at TEXT CHECK(updated_at NOT NULL) -); \ No newline at end of file diff --git a/src/Simplex/Chat/Migrations/chat_schema.sql b/src/Simplex/Chat/Migrations/chat_schema.sql index c71cc9aa90..79ac6ea7ee 100644 --- a/src/Simplex/Chat/Migrations/chat_schema.sql +++ b/src/Simplex/Chat/Migrations/chat_schema.sql @@ -82,6 +82,8 @@ CREATE TABLE sent_probes( user_id INTEGER NOT NULL REFERENCES users ON DELETE CASCADE, created_at TEXT CHECK(created_at NOT NULL), updated_at TEXT CHECK(updated_at NOT NULL), + probe_contact_id INTEGER REFERENCES contacts(contact_id) ON DELETE CASCADE, + probe_group_member_id INTEGER REFERENCES group_members(group_member_id) ON DELETE CASCADE, UNIQUE(user_id, probe) ); CREATE TABLE sent_probe_hashes( @@ -101,7 +103,9 @@ CREATE TABLE received_probes( user_id INTEGER NOT NULL REFERENCES users ON DELETE CASCADE , created_at TEXT CHECK(created_at NOT NULL), - updated_at TEXT CHECK(updated_at NOT NULL) + updated_at TEXT CHECK(updated_at NOT NULL), + probe_contact_id INTEGER REFERENCES contacts(contact_id) ON DELETE CASCADE, + probe_group_member_id INTEGER REFERENCES group_members(group_member_id) ON DELETE CASCADE ); CREATE TABLE known_servers( server_id INTEGER PRIMARY KEY, @@ -625,7 +629,6 @@ CREATE INDEX idx_pending_group_messages_group_member_id ON pending_group_message CREATE INDEX idx_rcv_file_chunks_file_id ON rcv_file_chunks(file_id); CREATE INDEX idx_rcv_files_group_member_id ON rcv_files(group_member_id); CREATE INDEX idx_received_probes_user_id ON received_probes(user_id); -CREATE INDEX idx_received_probes_contact_id ON received_probes(contact_id); CREATE INDEX idx_sent_probe_hashes_user_id ON sent_probe_hashes(user_id); CREATE INDEX idx_sent_probe_hashes_contact_id ON sent_probe_hashes(contact_id); CREATE INDEX idx_settings_user_id ON settings(user_id); @@ -713,3 +716,18 @@ CREATE INDEX idx_chat_items_user_id_item_status ON chat_items( item_status ); CREATE INDEX idx_connections_to_subscribe ON connections(to_subscribe); +CREATE INDEX idx_sent_probes_user_id ON sent_probes(user_id); +CREATE UNIQUE INDEX idx_sent_probes_probe_contact_id ON sent_probes( + probe_contact_id +); +CREATE UNIQUE INDEX idx_sent_probes_probe_group_member_id ON sent_probes( + probe_group_member_id +); +CREATE INDEX idx_sent_probes_probe ON sent_probes(probe); +CREATE UNIQUE INDEX idx_received_probes_probe_contact_id ON received_probes( + probe_contact_id +); +CREATE UNIQUE INDEX idx_received_probes_probe_group_member_id ON received_probes( + probe_group_member_id +); +CREATE INDEX idx_received_probes_probe_hash ON received_probes(probe_hash); diff --git a/src/Simplex/Chat/Store/Migrations.hs b/src/Simplex/Chat/Store/Migrations.hs index cbcc4ddd28..d72100b0e9 100644 --- a/src/Simplex/Chat/Store/Migrations.hs +++ b/src/Simplex/Chat/Store/Migrations.hs @@ -79,6 +79,7 @@ import Simplex.Chat.Migrations.M20230814_indexes import Simplex.Chat.Migrations.M20230827_file_encryption import Simplex.Chat.Migrations.M20230829_connections_chat_vrange import Simplex.Chat.Migrations.M20230903_connections_to_subscribe +import Simplex.Chat.Migrations.M20230914_member_probes import Simplex.Messaging.Agent.Store.SQLite.Migrations (Migration (..)) schemaMigrations :: [(String, Query, Maybe Query)] @@ -157,7 +158,8 @@ schemaMigrations = ("20230814_indexes", m20230814_indexes, Just down_m20230814_indexes), ("20230827_file_encryption", m20230827_file_encryption, Just down_m20230827_file_encryption), ("20230829_connections_chat_vrange", m20230829_connections_chat_vrange, Just down_m20230829_connections_chat_vrange), - ("20230903_connections_to_subscribe", m20230903_connections_to_subscribe, Just down_m20230903_connections_to_subscribe) + ("20230903_connections_to_subscribe", m20230903_connections_to_subscribe, Just down_m20230903_connections_to_subscribe), + ("20230914_member_probes", m20230914_member_probes, Just down_m20230914_member_probes) ] -- | The list of migrations in ascending order by date