mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-06-04 03:51:48 +00:00
core: member relations vector migration; set index in group for members; rework logic for avoiding duplicate introductions (#6445)
This commit is contained in:
@@ -21,6 +21,7 @@ import Simplex.Chat.Store.Postgres.Migrations.M20250919_group_summary
|
||||
import Simplex.Chat.Store.Postgres.Migrations.M20250922_remove_unused_connections
|
||||
import Simplex.Chat.Store.Postgres.Migrations.M20251007_connections_sync
|
||||
import Simplex.Chat.Store.Postgres.Migrations.M20251017_chat_tags_cascade
|
||||
import Simplex.Chat.Store.Postgres.Migrations.M20251117_member_relations_vector
|
||||
import Simplex.Messaging.Agent.Store.Shared (Migration (..))
|
||||
|
||||
schemaMigrations :: [(String, Text, Maybe Text)]
|
||||
@@ -41,7 +42,8 @@ schemaMigrations =
|
||||
("20250919_group_summary", m20250919_group_summary, Just down_m20250919_group_summary),
|
||||
("20250922_remove_unused_connections", m20250922_remove_unused_connections, Just down_m20250922_remove_unused_connections),
|
||||
("20251007_connections_sync", m20251007_connections_sync, Just down_m20251007_connections_sync),
|
||||
("20251017_chat_tags_cascade", m20251017_chat_tags_cascade, Just down_m20251017_chat_tags_cascade)
|
||||
("20251017_chat_tags_cascade", m20251017_chat_tags_cascade, Just down_m20251017_chat_tags_cascade),
|
||||
("20251117_member_relations_vector", m20251117_member_relations_vector, Just down_m20251117_member_relations_vector)
|
||||
]
|
||||
|
||||
-- | The list of migrations in ascending order by date
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
{-# LANGUAGE QuasiQuotes #-}
|
||||
|
||||
module Simplex.Chat.Store.Postgres.Migrations.M20251117_member_relations_vector where
|
||||
|
||||
import Data.Text (Text)
|
||||
import qualified Data.Text as T
|
||||
import Text.RawString.QQ (r)
|
||||
|
||||
m20251117_member_relations_vector :: Text
|
||||
m20251117_member_relations_vector =
|
||||
T.pack
|
||||
[r|
|
||||
ALTER TABLE group_members ADD COLUMN index_in_group BIGINT NOT NULL DEFAULT 0;
|
||||
|
||||
ALTER TABLE groups ADD COLUMN member_index BIGINT NOT NULL DEFAULT 0;
|
||||
|
||||
ALTER TABLE group_members ADD COLUMN member_relations_vector BYTEA;
|
||||
|
||||
CREATE INDEX tmp_idx_group_members_group_id_group_member_id ON group_members(group_id, group_member_id);
|
||||
|
||||
CREATE TEMPORARY TABLE tmp_members_numbered AS
|
||||
SELECT
|
||||
group_member_id,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY group_id
|
||||
ORDER BY group_member_id ASC
|
||||
) AS rn
|
||||
FROM group_members;
|
||||
|
||||
CREATE INDEX tmp_idx_members_numbered ON tmp_members_numbered(group_member_id);
|
||||
|
||||
UPDATE group_members AS gm
|
||||
SET index_in_group = n.rn
|
||||
FROM tmp_members_numbered n
|
||||
WHERE n.group_member_id = gm.group_member_id;
|
||||
|
||||
DROP INDEX tmp_idx_group_members_group_id_group_member_id;
|
||||
DROP INDEX tmp_idx_members_numbered;
|
||||
DROP TABLE tmp_members_numbered;
|
||||
|
||||
CREATE UNIQUE INDEX idx_group_members_group_id_index_in_group ON group_members(group_id, index_in_group);
|
||||
|
||||
UPDATE groups g
|
||||
SET member_index = COALESCE((
|
||||
SELECT MAX(index_in_group)
|
||||
FROM group_members
|
||||
WHERE group_members.group_id = g.group_id
|
||||
), 0);
|
||||
|]
|
||||
|
||||
down_m20251117_member_relations_vector :: Text
|
||||
down_m20251117_member_relations_vector =
|
||||
T.pack
|
||||
[r|
|
||||
DROP INDEX idx_group_members_group_id_index_in_group;
|
||||
|
||||
ALTER TABLE group_members DROP COLUMN index_in_group;
|
||||
|
||||
ALTER TABLE groups DROP COLUMN member_index;
|
||||
|
||||
ALTER TABLE group_members DROP COLUMN member_relations_vector;
|
||||
|]
|
||||
@@ -706,7 +706,9 @@ CREATE TABLE test_chat_schema.group_members (
|
||||
support_chat_items_mentions bigint DEFAULT 0 NOT NULL,
|
||||
support_chat_last_msg_from_member_ts timestamp with time zone,
|
||||
member_xcontact_id bytea,
|
||||
member_welcome_shared_msg_id bytea
|
||||
member_welcome_shared_msg_id bytea,
|
||||
index_in_group bigint DEFAULT 0 NOT NULL,
|
||||
member_relations_vector bytea
|
||||
);
|
||||
|
||||
|
||||
@@ -805,7 +807,8 @@ CREATE TABLE test_chat_schema.groups (
|
||||
request_shared_msg_id bytea,
|
||||
conn_link_prepared_connection smallint DEFAULT 0 NOT NULL,
|
||||
via_group_link_uri bytea,
|
||||
summary_current_members_count bigint DEFAULT 0 NOT NULL
|
||||
summary_current_members_count bigint DEFAULT 0 NOT NULL,
|
||||
member_index bigint DEFAULT 0 NOT NULL
|
||||
);
|
||||
|
||||
|
||||
@@ -2081,6 +2084,10 @@ CREATE INDEX idx_group_members_group_id ON test_chat_schema.group_members USING
|
||||
|
||||
|
||||
|
||||
CREATE UNIQUE INDEX idx_group_members_group_id_index_in_group ON test_chat_schema.group_members USING btree (group_id, index_in_group);
|
||||
|
||||
|
||||
|
||||
CREATE INDEX idx_group_members_invited_by ON test_chat_schema.group_members USING btree (invited_by);
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user