Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(comms): trigger notif deletes on soft delete (#2789)
Sean Ayee7172b435c0d
diff --git a/rust/cloud-storage/comms_db_client/migrations/20250110210334_schema.sql b/rust/cloud-storage/comms_db_client/migrations/20250110210334_schema.sqldeleted file mode 100644index 100fc5cbfb..0000000000--- a/rust/cloud-storage/comms_db_client/migrations/20250110210334_schema.sql+++ /dev/null@@ -1,99 +0,0 @@--- stupid but necessary to make the migration work-DO $$-BEGIN- IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'channel_type') THEN- CREATE TYPE channel_type AS ENUM ('public', 'organization', 'private', 'direct_message');- END IF;-- IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'participant_role') THEN- CREATE TYPE participant_role AS ENUM ('owner', 'admin', 'member');- END IF;-END-$$;---- Channels table-CREATE TABLE IF NOT EXISTS channels (- id UUID PRIMARY KEY NOT NULL,- name VARCHAR(255),- channel_type channel_type NOT NULL,- org_id BIGINT, -- Only used for organization type- created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),- updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),- owner_id TEXT NOT NULL,- CONSTRAINT valid_org_channel CHECK (- (channel_type = 'organization' AND org_id IS NOT NULL) OR- (channel_type != 'organization' AND org_id IS NULL)- )-);---- Channel participants table-CREATE TABLE IF NOT EXISTS channel_participants (- channel_id UUID NOT NULL REFERENCES channels(id) ON DELETE CASCADE,- role participant_role NOT NULL,- user_id TEXT NOT NULL,- joined_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),- left_at TIMESTAMP WITH TIME ZONE,- PRIMARY KEY (channel_id, user_id)-);---- Messages table-CREATE TABLE IF NOT EXISTS messages (- id UUID PRIMARY KEY NOT NULL,- channel_id UUID NOT NULL REFERENCES channels(id) ON DELETE CASCADE,- thread_id UUID REFERENCES messages(id) ON DELETE CASCADE,- sender_id TEXT NOT NULL,- content TEXT NOT NULL,- created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),- updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),- edited_at TIMESTAMP DEFAULT NULL,- deleted_at TIMESTAMP DEFAULT NULL-);---- Attachments table-CREATE TABLE IF NOT EXISTS attachments (- id UUID PRIMARY KEY NOT NULL,- message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE,- channel_id UUID NOT NULL REFERENCES channels(id) ON DELETE CASCADE,- entity_type VARCHAR(32) NOT NULL,- entity_id VARCHAR NOT NULL,- created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()-);---- Message mentions table-CREATE TABLE IF NOT EXISTS message_mentions (- message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE,- user_id TEXT NOT NULL,- created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),- PRIMARY KEY (message_id, user_id)-);---- Reactions table (updated schema)-CREATE TABLE IF NOT EXISTS reactions (- message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE,- emoji VARCHAR(32) NOT NULL,- user_id TEXT NOT NULL,- created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),- PRIMARY KEY (message_id, emoji, user_id)-);---- Activity table-CREATE TABLE IF NOT EXISTS activity (- id UUID PRIMARY KEY NOT NULL,- user_id TEXT NOT NULL,- channel_id UUID NOT NULL,- created_at TIMESTAMP NOT NULL DEFAULT NOW(),- updated_at TIMESTAMP NOT NULL DEFAULT NOW(),- viewed_at TIMESTAMP,- interacted_at TIMESTAMP,- CONSTRAINT unique_user_channel UNIQUE (user_id, channel_id)-);---- Indexes-CREATE INDEX IF NOT EXISTS idx_channels_org_id ON channels(org_id) WHERE channel_type = 'organization';-CREATE INDEX IF NOT EXISTS idx_messages_channel_id ON messages(channel_id);-CREATE INDEX IF NOT EXISTS idx_messages_channel_timeline ON messages(channel_id, created_at ASC);-CREATE INDEX IF NOT EXISTS idx_messages_thread_id ON messages(thread_id) WHERE thread_id IS NOT NULL;-CREATE INDEX IF NOT EXISTS idx_messages_sender_id ON messages(sender_id);-CREATE INDEX IF NOT EXISTS idx_attachments_message_id ON attachments(message_id);-CREATE INDEX IF NOT EXISTS idx_reactions_message_id ON reactions(message_id);-CREATE INDEX IF NOT EXISTS idx_activity_user_times ON activity(user_id, updated_at ASC, created_at ASC);diff --git a/rust/cloud-storage/comms_db_client/migrations/20250207203331_nullable_channel.name_and_constraints.sql b/rust/cloud-storage/comms_db_client/migrations/20250207203331_nullable_channel.name_and_constraints.sqldeleted file mode 100644index 68899e8821..0000000000--- a/rust/cloud-storage/comms_db_client/migrations/20250207203331_nullable_channel.name_and_constraints.sql+++ /dev/null@@ -1,12 +0,0 @@--- make the name column nullable-ALTER TABLE channels ALTER COLUMN name DROP NOT NULL;---- add a check to ensure that:--- 1. direct message channels do not have a name--- 2. public channels have a name-ALTER TABLE channels ADD CONSTRAINT valid_channel_name CHECK (- (channel_type = 'direct_message' AND name IS NULL) OR -- direct message channels do not have a name- (channel_type IN ('public', 'organization') AND name is NOT NULL) OR -- public and organization channels have a name- (channel_type = 'private') -- private channels have no restrictions-);-diff --git a/rust/cloud-storage/comms_db_client/migrations/20250303194724_non_user_specific_mentions.sql b/rust/cloud-storage/comms_db_client/migrations/20250303194724_non_user_specific_mentions.sqldeleted file mode 100644index 03abf5ae40..0000000000--- a/rust/cloud-storage/comms_db_client/migrations/20250303194724_non_user_specific_mentions.sql+++ /dev/null@@ -1,14 +0,0 @@-ALTER TABLE message_mentions-ADD COLUMN entity_type VARCHAR(32),-ADD COLUMN entity_id VARCHAR;--UPDATE message_mentions-SET entity_type = 'user',- entity_id = user_id;--ALTER TABLE message_mentions-ALTER COLUMN entity_type SET NOT NULL,-ALTER COLUMN entity_id SET NOT NULL;--ALTER TABLE message_mentions-DROP COLUMN user_id;diff --git a/rust/cloud-storage/comms_db_client/migrations/20250305165720_add_mention_index_on_entity.sql b/rust/cloud-storage/comms_db_client/migrations/20250305165720_add_mention_index_on_entity.sqldeleted file mode 100644index 8a5a002ec9..0000000000--- a/rust/cloud-storage/comms_db_client/migrations/20250305165720_add_mention_index_on_entity.sql+++ /dev/null@@ -1,2 +0,0 @@--- Add migration script here-CREATE INDEX IF NOT EXISTS idx_message_mentions_entity_id ON message_mentions(entity_id);diff --git a/rust/cloud-storage/comms_db_client/migrations/20250320212539_activity_index.sql b/rust/cloud-storage/comms_db_client/migrations/20250320212539_activity_index.sqldeleted file mode 100644index 114346e2f0..0000000000--- a/rust/cloud-storage/comms_db_client/migrations/20250320212539_activity_index.sql+++ /dev/null@@ -1,3 +0,0 @@--- Add migration script here-CREATE INDEX IF NOT EXISTS idx_activity_channel_id_user_id ON activity(user_id, channel_id);-CREATE INDEX IF NOT EXISTS idx_attachments_channel_Id ON attachments(channel_id);diff --git a/rust/cloud-storage/comms_db_client/migrations/20250613182043_attachment_references_index.sql b/rust/cloud-storage/comms_db_client/migrations/20250613182043_attachment_references_index.sqldeleted file mode 100644index aa82a2f822..0000000000--- a/rust/cloud-storage/comms_db_client/migrations/20250613182043_attachment_references_index.sql+++ /dev/null@@ -1,11 +0,0 @@-CREATE INDEX IF NOT EXISTS idx_attachments_entity_created- ON attachments (entity_type, entity_id, created_at DESC)- INCLUDE (channel_id, message_id);--CREATE INDEX IF NOT EXISTS idx_cp_active_by_channel_user- ON channel_participants (channel_id, user_id)- WHERE left_at IS NULL;--CREATE INDEX IF NOT EXISTS idx_messages_active- ON messages (id)- WHERE deleted_at IS NULL;diff --git a/rust/cloud-storage/comms_db_client/migrations/20250625000000_optimize_message_mentions_index.sql b/rust/cloud-storage/comms_db_client/migrations/20250625000000_optimize_message_mentions_index.sqldeleted file mode 100644index 98d8a8341f..0000000000--- a/rust/cloud-storage/comms_db_client/migrations/20250625000000_optimize_message_mentions_index.sql+++ /dev/null@@ -1,15 +0,0 @@--- Optimize message_mentions index for get_attachment_references query--- This composite index matches the query pattern and improves performance---- Drop the existing single-column index if it exists-DROP INDEX IF EXISTS idx_message_mentions_entity_id;---- Create composite index for efficient filtering and joining-CREATE INDEX IF NOT EXISTS idx_message_mentions_entity_type_id- ON message_mentions (entity_type, entity_id)- INCLUDE (message_id);---- Add index on messages for efficient ordering when joined from message_mentions-CREATE INDEX IF NOT EXISTS idx_messages_created_at_active- ON messages (created_at DESC)- WHERE deleted_at IS NULL;\ No newline at end of filediff --git a/rust/cloud-storage/comms_db_client/migrations/20250625174641_entity_mentions.sql b/rust/cloud-storage/comms_db_client/migrations/20250625174641_entity_mentions.sqldeleted file mode 100644index 7f0c8270a5..0000000000--- a/rust/cloud-storage/comms_db_client/migrations/20250625174641_entity_mentions.sql+++ /dev/null@@ -1,53 +0,0 @@--- Rename message_mentions table to entity_mentions-ALTER TABLE message_mentions RENAME TO entity_mentions;---- Add source_entity_type and source_entity_id columns-ALTER TABLE entity_mentions-ADD COLUMN source_entity_type VARCHAR(32),-ADD COLUMN source_entity_id VARCHAR;---- Populate source columns from existing message_id-UPDATE entity_mentions-SET source_entity_type = 'message',- source_entity_id = message_id::TEXT;---- Make new columns NOT NULL-ALTER TABLE entity_mentions-ALTER COLUMN source_entity_type SET NOT NULL,-ALTER COLUMN source_entity_id SET NOT NULL;---- Drop the old message_id column-ALTER TABLE entity_mentions-DROP COLUMN message_id;---- Drop the old primary key-ALTER TABLE entity_mentions-DROP CONSTRAINT IF EXISTS message_mentions_pkey;---- Remove any duplicate rows before creating the primary key-DELETE FROM entity_mentions a-USING entity_mentions b-WHERE a.ctid < b.ctid- AND a.source_entity_type = b.source_entity_type- AND a.source_entity_id = b.source_entity_id- AND a.entity_type = b.entity_type- AND a.entity_id = b.entity_id;---- Create new composite primary key-ALTER TABLE entity_mentions-ADD PRIMARY KEY (source_entity_type, source_entity_id, entity_type, entity_id);---- Drop old indexes that reference message_id-DROP INDEX IF EXISTS idx_message_mentions_entity_type_id;---- Create new indexes for efficient querying-CREATE INDEX idx_entity_mentions_entity_type_id- ON entity_mentions (entity_type, entity_id)- INCLUDE (source_entity_type, source_entity_id);--CREATE INDEX idx_entity_mentions_source- ON entity_mentions (source_entity_type, source_entity_id);---- Add index for created_at for ordering-CREATE INDEX idx_entity_mentions_created_at- ON entity_mentions (created_at DESC);diff --git a/rust/cloud-storage/comms_db_client/migrations/20250627000000_add_uuid_to_entity_mentions.sql b/rust/cloud-storage/comms_db_client/migrations/20250627000000_add_uuid_to_entity_mentions.sqldeleted file mode 100644index b13c03a814..0000000000--- a/rust/cloud-storage/comms_db_client/migrations/20250627000000_add_uuid_to_entity_mentions.sql+++ /dev/null@@ -1,6 +0,0 @@--- Add UUID id column to entity_mentions table-ALTER TABLE entity_mentions-ADD COLUMN id UUID DEFAULT gen_random_uuid() NOT NULL;---- Create unique index on id-CREATE UNIQUE INDEX idx_entity_mentions_id ON entity_mentions (id);diff --git a/rust/cloud-storage/comms_db_client/migrations/20250627000001_allow_duplicate_entity_mentions.sql b/rust/cloud-storage/comms_db_client/migrations/20250627000001_allow_duplicate_entity_mentions.sqldeleted file mode 100644index 1af50fbc88..0000000000--- a/rust/cloud-storage/comms_db_client/migrations/20250627000001_allow_duplicate_entity_mentions.sql+++ /dev/null@@ -1,14 +0,0 @@--- Drop the composite primary key that prevents duplicates-ALTER TABLE entity_mentions-DROP CONSTRAINT entity_mentions_pkey;---- Drop the unique index on id since we'll make it the primary key-DROP INDEX idx_entity_mentions_id;---- Set the UUID id column as the new primary key-ALTER TABLE entity_mentions-ADD PRIMARY KEY (id);---- Create a non-unique index for efficient querying by entity combination-CREATE INDEX idx_entity_mentions_combination- ON entity_mentions (source_entity_type, source_entity_id, entity_type, entity_id);\ No newline at end of filediff --git a/rust/cloud-storage/comms_db_client/migrations/20250627000002_add_user_id_to_entity_mentions.sql b/rust/cloud-storage/comms_db_client/migrations/20250627000002_add_user_id_to_entity_mentions.sqldeleted file mode 100644index 9c509ea94f..0000000000--- a/rust/cloud-storage/comms_db_client/migrations/20250627000002_add_user_id_to_entity_mentions.sql+++ /dev/null@@ -1,8 +0,0 @@--- Add optional user_id field to entity_mentions table-ALTER TABLE entity_mentions-ADD COLUMN user_id UUID;---- Create index on user_id for efficient querying-CREATE INDEX idx_entity_mentions_user_id- ON entity_mentions (user_id)- WHERE user_id IS NOT NULL;\ No newline at end of filediff --git a/rust/cloud-storage/comms_db_client/migrations/20250628000000_add_user_id_string_to_entity_mentions.sql b/rust/cloud-storage/comms_db_client/migrations/20250628000000_add_user_id_string_to_entity_mentions.sqldeleted file mode 100644index b0ebe7c458..0000000000--- a/rust/cloud-storage/comms_db_client/migrations/20250628000000_add_user_id_string_to_entity_mentions.sql+++ /dev/null@@ -1,3 +0,0 @@--- Change user_id column from UUID to VARCHAR to match user ID format in the system-ALTER TABLE entity_mentions-ALTER COLUMN user_id TYPE VARCHAR;\ No newline at end of filediff --git a/rust/cloud-storage/comms_db_client/migrations/20250729201036_optimize_channel_query_indexes.sql b/rust/cloud-storage/comms_db_client/migrations/20250729201036_optimize_channel_query_indexes.sqldeleted file mode 100644index aca382a18b..0000000000--- a/rust/cloud-storage/comms_db_client/migrations/20250729201036_optimize_channel_query_indexes.sql+++ /dev/null@@ -1,5 +0,0 @@--- Optimize indexes for get_channel API queries--CREATE INDEX IF NOT EXISTS idx_activity_channel_user ON activity(channel_id, user_id);--CREATE INDEX IF NOT EXISTS idx_attachments_channel_created ON attachments(channel_id, created_at ASC);diff --git a/rust/cloud-storage/comms_db_client/migrations/20250820120000_optimize_latest_messages_index.sql b/rust/cloud-storage/comms_db_client/migrations/20250820120000_optimize_latest_messages_index.sqldeleted file mode 100644index 221a418d7f..0000000000--- a/rust/cloud-storage/comms_db_client/migrations/20250820120000_optimize_latest_messages_index.sql+++ /dev/null@@ -1,7 +0,0 @@--- Optimize latest-message batch query---- Index supports fast per-channel backward scans over active messages-CREATE INDEX IF NOT EXISTS idx_messages_channel_created_at_active- ON messages (channel_id, created_at DESC)- WHERE deleted_at IS NULL;-diff --git a/rust/cloud-storage/macro_db_client/migrations/20260423120000_cascade_comms_message_delete_to_notifications.sql b/rust/cloud-storage/macro_db_client/migrations/20260423120000_cascade_comms_message_delete_to_notifications.sqlnew file mode 100644index 0000000000..6ae2f05bc6--- /dev/null+++ b/rust/cloud-storage/macro_db_client/migrations/20260423120000_cascade_comms_message_delete_to_notifications.sql@@ -0,0 +1,33 @@+-- When a channel message is soft-deleted, remove any notification rows+-- whose metadata->>'messageId' points at it. user_notification rows cascade+-- via existing FK (migration 20260126170641).++CREATE OR REPLACE FUNCTION cascade_comms_message_delete_to_notifications()+RETURNS TRIGGER AS $$+BEGIN+ IF OLD.deleted_at IS NULL AND NEW.deleted_at IS NOT NULL THEN+ DELETE FROM notification+ WHERE event_item_type = 'channel'+ AND event_item_id = NEW.channel_id::text+ AND metadata->>'messageId' = NEW.id::text;+ END IF;+ RETURN NEW;+END;+$$ LANGUAGE plpgsql;++CREATE TRIGGER trg_cascade_comms_message_delete_to_notifications+AFTER UPDATE OF deleted_at ON comms_messages+FOR EACH ROW+EXECUTE FUNCTION cascade_comms_message_delete_to_notifications();++-- Backfill: sweep notifications for already-soft-deleted messages.+-- Predicates mirror the trigger: event_item_type, event_item_id, messageId.+DELETE FROM notification n+WHERE n.event_item_type = 'channel'+ AND n.metadata->>'messageId' IS NOT NULL+ AND EXISTS (+ SELECT 1 FROM comms_messages m+ WHERE m.id::text = n.metadata->>'messageId'+ AND m.channel_id::text = n.event_item_id+ AND m.deleted_at IS NOT NULL+ );