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
chore(db): retire legacy comment schema and final PDF column read (#7360)
gbirman6e79afeb5608
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.jsonindex d80a102e92..1224fb1db5 100644--- a/.github/workspace-dep-closures.json+++ b/.github/workspace-dep-closures.json@@ -8010,12 +8010,7 @@"crates/workspace-hack"],"macro_db_migrator": [- "crates/database_env_vars","crates/macro_db_migrator",- "crates/macro_entrypoint",- "crates/macro_env",- "crates/macro_env_var",- "crates/macro_uuid","crates/workspace-hack"],"macro_entrypoint": [diff --git a/.sqlx/query-038db5b91138c574e5927816834bdedef8ac3d00b378999c7370d1b6f8ec22be.json b/.sqlx/query-038db5b91138c574e5927816834bdedef8ac3d00b378999c7370d1b6f8ec22be.jsondeleted file mode 100644index 94d118fc91..0000000000--- a/.sqlx/query-038db5b91138c574e5927816834bdedef8ac3d00b378999c7370d1b6f8ec22be.json+++ /dev/null@@ -1,22 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "INSERT INTO comms_messages (\n id, parent_entity_type, parent_entity_id, sender_id, content,\n created_at, updated_at, deleted_at\n )\n SELECT tm.root_id, 'document', t.\"documentId\", t.owner, '',\n t.\"createdAt\" AT TIME ZONE 'UTC', t.\"updatedAt\" AT TIME ZONE 'UTC',\n COALESCE(t.\"deletedAt\", t.\"updatedAt\")\n FROM \"Thread\" t\n JOIN migrated_comment_thread_id tm ON tm.thread_id = t.id\n WHERE t.\"documentId\" = ANY($1)\n AND NOT EXISTS (SELECT 1 FROM \"Comment\" c WHERE c.\"threadId\" = t.id)\n ON CONFLICT (id) DO UPDATE SET\n updated_at = EXCLUDED.updated_at,\n deleted_at = EXCLUDED.deleted_at\n WHERE comms_messages.updated_at <= EXCLUDED.updated_at\n AND (comms_messages.updated_at, comms_messages.deleted_at)\n IS DISTINCT FROM (EXCLUDED.updated_at, EXCLUDED.deleted_at)\n RETURNING (xmax = 0) AS \"inserted!\"",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "inserted!",- "type_info": "Bool"- }- ],- "parameters": {- "Left": [- "TextArray"- ]- },- "nullable": [- null- ]- },- "hash": "038db5b91138c574e5927816834bdedef8ac3d00b378999c7370d1b6f8ec22be"-}diff --git a/.sqlx/query-0942cc627f9a1efe46c01de642b422f58d7368eed6d671887e2d65bb29858cd1.json b/.sqlx/query-0942cc627f9a1efe46c01de642b422f58d7368eed6d671887e2d65bb29858cd1.jsonnew file mode 100644index 0000000000..7ec56065e1--- /dev/null+++ b/.sqlx/query-0942cc627f9a1efe46c01de642b422f58d7368eed6d671887e2d65bb29858cd1.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT root_id FROM \"PdfPlaceableCommentAnchor\" WHERE uuid = $1",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "root_id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "0942cc627f9a1efe46c01de642b422f58d7368eed6d671887e2d65bb29858cd1"+}diff --git a/.sqlx/query-099de7baaa6a5bbb26a23719ae554f5e71c055fd4a36eba60c158d663d8d65b6.json b/.sqlx/query-099de7baaa6a5bbb26a23719ae554f5e71c055fd4a36eba60c158d663d8d65b6.jsondeleted file mode 100644index 605567a034..0000000000--- a/.sqlx/query-099de7baaa6a5bbb26a23719ae554f5e71c055fd4a36eba60c158d663d8d65b6.json+++ /dev/null@@ -1,16 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "INSERT INTO migrated_comment_thread_id (thread_id, root_id, document_id)\n SELECT * FROM UNNEST($1::bigint[], $2::uuid[], $3::text[])",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Int8Array",- "UuidArray",- "TextArray"- ]- },- "nullable": []- },- "hash": "099de7baaa6a5bbb26a23719ae554f5e71c055fd4a36eba60c158d663d8d65b6"-}diff --git a/.sqlx/query-b39d7843d42b9394afb3046afd88b86ca7eef61e6c716991043d8403fc2a309f.json b/.sqlx/query-0bfe681b90c8ded2a384825ab9305b4235a04392113f52f2ab8139bd0a7607eb.jsonsimilarity index 65%rename from .sqlx/query-b39d7843d42b9394afb3046afd88b86ca7eef61e6c716991043d8403fc2a309f.jsonrename to .sqlx/query-0bfe681b90c8ded2a384825ab9305b4235a04392113f52f2ab8139bd0a7607eb.jsonindex f6f6581656..73b20604a4 100644--- a/.sqlx/query-b39d7843d42b9394afb3046afd88b86ca7eef61e6c716991043d8403fc2a309f.json+++ b/.sqlx/query-0bfe681b90c8ded2a384825ab9305b4235a04392113f52f2ab8139bd0a7607eb.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "UPDATE \"PdfHighlightAnchor\" SET root_id = $1\n WHERE uuid = $2 AND \"documentId\" = $3 AND root_id IS NULL AND \"threadId\" IS NULL\n AND \"deletedAt\" IS NULL",+ "query": "UPDATE \"PdfHighlightAnchor\" SET root_id = $1\n WHERE uuid = $2 AND \"documentId\" = $3 AND root_id IS NULL\n AND \"deletedAt\" IS NULL","describe": {"columns": [],"parameters": {@@ -12,5 +12,5 @@},"nullable": []},- "hash": "b39d7843d42b9394afb3046afd88b86ca7eef61e6c716991043d8403fc2a309f"+ "hash": "0bfe681b90c8ded2a384825ab9305b4235a04392113f52f2ab8139bd0a7607eb"}diff --git a/.sqlx/query-0d0b3ef3f9f49f24dd11513be24ad93b12fd6c16c6265aaf9afcb9089a01c674.json b/.sqlx/query-0d0b3ef3f9f49f24dd11513be24ad93b12fd6c16c6265aaf9afcb9089a01c674.jsondeleted file mode 100644index 9f111e3fb5..0000000000--- a/.sqlx/query-0d0b3ef3f9f49f24dd11513be24ad93b12fd6c16c6265aaf9afcb9089a01c674.json+++ /dev/null@@ -1,38 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT channel_id AS \"channel_id!\", id AS message_id, created_at\n FROM comms_messages\n WHERE channel_id IS NOT NULL\n AND ($2::timestamptz IS NULL OR (created_at, id) > ($2, $3::uuid))\n AND ($4::uuid[] IS NULL OR channel_id = ANY($4))\n AND (\n $5::bool IS NULL\n OR ($5 AND deleted_at IS NOT NULL)\n OR (NOT $5 AND deleted_at IS NULL)\n )\n ORDER BY created_at ASC, id ASC\n LIMIT $1\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "channel_id!",- "type_info": "Uuid"- },- {- "ordinal": 1,- "name": "message_id",- "type_info": "Uuid"- },- {- "ordinal": 2,- "name": "created_at",- "type_info": "Timestamptz"- }- ],- "parameters": {- "Left": [- "Int8",- "Timestamptz",- "Uuid",- "UuidArray",- "Bool"- ]- },- "nullable": [- true,- false,- false- ]- },- "hash": "0d0b3ef3f9f49f24dd11513be24ad93b12fd6c16c6265aaf9afcb9089a01c674"-}diff --git a/.sqlx/query-1135a9b12335dbcf6feaa871d478efff6395e63361552cceca5ca30c9faf3f2d.json b/.sqlx/query-1135a9b12335dbcf6feaa871d478efff6395e63361552cceca5ca30c9faf3f2d.jsondeleted file mode 100644index ca67d44f61..0000000000--- a/.sqlx/query-1135a9b12335dbcf6feaa871d478efff6395e63361552cceca5ca30c9faf3f2d.json+++ /dev/null@@ -1,20 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "INSERT INTO comms_messages (\n id, parent_entity_type, parent_entity_id, thread_id, sender_id, content,\n created_at, updated_at, edited_at, deleted_at, import_metadata\n )\n SELECT c.id,\n CASE WHEN t.contact_id IS NULL THEN 'crm_company' ELSE 'crm_contact' END,\n COALESCE(t.contact_id, t.company_id)::text,\n NULLIF(first_value(c.id) OVER (PARTITION BY c.thread_id ORDER BY c.created_at, c.id), c.id),\n c.owner, c.text, c.created_at, c.updated_at,\n CASE WHEN c.updated_at > c.created_at THEN c.updated_at AT TIME ZONE 'UTC' END,\n COALESCE(c.deleted_at, t.deleted_at) AT TIME ZONE 'UTC',\n jsonb_build_object('source', 'crm_comment', 'legacy_thread_id', c.thread_id)\n FROM crm_comment c\n JOIN crm_thread t ON t.id = c.thread_id\n ON CONFLICT (id) DO UPDATE SET\n content = EXCLUDED.content,\n updated_at = EXCLUDED.updated_at,\n edited_at = EXCLUDED.edited_at,\n deleted_at = EXCLUDED.deleted_at\n WHERE comms_messages.import_metadata->>'source' = 'crm_comment'\n AND comms_messages.updated_at <= EXCLUDED.updated_at\n AND (comms_messages.content, comms_messages.updated_at, comms_messages.edited_at,\n comms_messages.deleted_at)\n IS DISTINCT FROM\n (EXCLUDED.content, EXCLUDED.updated_at, EXCLUDED.edited_at, EXCLUDED.deleted_at)\n RETURNING (xmax = 0) AS \"inserted!\"",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "inserted!",- "type_info": "Bool"- }- ],- "parameters": {- "Left": []- },- "nullable": [- null- ]- },- "hash": "1135a9b12335dbcf6feaa871d478efff6395e63361552cceca5ca30c9faf3f2d"-}diff --git a/.sqlx/query-165b5f553dcfe46dfc36a49121d1cfb90f6124b44dbdace772e4d606ffa4fb60.json b/.sqlx/query-165b5f553dcfe46dfc36a49121d1cfb90f6124b44dbdace772e4d606ffa4fb60.jsondeleted file mode 100644index 399f5ff10f..0000000000--- a/.sqlx/query-165b5f553dcfe46dfc36a49121d1cfb90f6124b44dbdace772e4d606ffa4fb60.json+++ /dev/null@@ -1,56 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "SELECT\n (SELECT count(*) FROM crm_thread WHERE metadata IS NOT NULL)\n + (SELECT count(*) FROM crm_comment WHERE metadata IS NOT NULL) AS \"metadata!\",\n (SELECT count(*) FROM crm_comment WHERE sender IS NOT NULL AND sender <> owner) AS \"foreign_senders!\",\n (SELECT count(*) FROM crm_comment WHERE \"order\" IS NOT NULL) AS \"explicit_order!\",\n (SELECT count(*) FROM crm_comment c JOIN comms_messages m ON m.id = c.id\n WHERE m.import_metadata->>'source' IS DISTINCT FROM 'crm_comment') AS \"id_collisions!\",\n (SELECT count(*) FROM crm_comment c\n WHERE NOT EXISTS (SELECT 1 FROM comms_messages m WHERE m.id = c.id)) AS \"pending_comments!\",\n (SELECT count(*) FROM crm_comment c\n JOIN crm_thread t ON t.id = c.thread_id\n JOIN comms_messages m ON m.id = c.id\n WHERE m.import_metadata->>'source' = 'crm_comment'\n AND m.updated_at <= c.updated_at\n AND (m.content, m.updated_at, m.deleted_at)\n IS DISTINCT FROM\n (c.text, c.updated_at, COALESCE(c.deleted_at, t.deleted_at) AT TIME ZONE 'UTC')) AS \"stale_comments!\",\n (SELECT count(*) FROM crm_thread t\n WHERE NOT EXISTS (SELECT 1 FROM crm_comment c WHERE c.thread_id = t.id)) AS \"empty_threads!\"",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "metadata!",- "type_info": "Int8"- },- {- "ordinal": 1,- "name": "foreign_senders!",- "type_info": "Int8"- },- {- "ordinal": 2,- "name": "explicit_order!",- "type_info": "Int8"- },- {- "ordinal": 3,- "name": "id_collisions!",- "type_info": "Int8"- },- {- "ordinal": 4,- "name": "pending_comments!",- "type_info": "Int8"- },- {- "ordinal": 5,- "name": "stale_comments!",- "type_info": "Int8"- },- {- "ordinal": 6,- "name": "empty_threads!",- "type_info": "Int8"- }- ],- "parameters": {- "Left": []- },- "nullable": [- null,- null,- null,- null,- null,- null,- null- ]- },- "hash": "165b5f553dcfe46dfc36a49121d1cfb90f6124b44dbdace772e4d606ffa4fb60"-}diff --git a/.sqlx/query-180a9c059064bca1eab390de5c76bf41bc489612d22ae37c8bf52989ba8e062a.json b/.sqlx/query-180a9c059064bca1eab390de5c76bf41bc489612d22ae37c8bf52989ba8e062a.jsondeleted file mode 100644index 261ad9c71a..0000000000--- a/.sqlx/query-180a9c059064bca1eab390de5c76bf41bc489612d22ae37c8bf52989ba8e062a.json+++ /dev/null@@ -1,14 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "UPDATE \"PdfPlaceableCommentAnchor\" a SET root_id = tm.root_id\n FROM migrated_comment_thread_id tm\n WHERE a.\"threadId\" = tm.thread_id AND a.\"documentId\" = ANY($1)\n AND a.root_id IS DISTINCT FROM tm.root_id",- "describe": {- "columns": [],- "parameters": {- "Left": [- "TextArray"- ]- },- "nullable": []- },- "hash": "180a9c059064bca1eab390de5c76bf41bc489612d22ae37c8bf52989ba8e062a"-}diff --git a/.sqlx/query-18550ce144d61e0c7b74342212f687d463723d2de3f525739a509b0644197e8b.json b/.sqlx/query-18550ce144d61e0c7b74342212f687d463723d2de3f525739a509b0644197e8b.jsonnew file mode 100644index 0000000000..47e578bf77--- /dev/null+++ b/.sqlx/query-18550ce144d61e0c7b74342212f687d463723d2de3f525739a509b0644197e8b.json@@ -0,0 +1,40 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT m.id, m.parent_entity_type, m.parent_entity_id, a.entity_id\n FROM comms_messages m JOIN comms_attachments a ON a.message_id = m.id\n WHERE m.id = ANY($1) ORDER BY m.created_at",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "parent_entity_type",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "parent_entity_id",+ "type_info": "Text"+ },+ {+ "ordinal": 3,+ "name": "entity_id",+ "type_info": "Varchar"+ }+ ],+ "parameters": {+ "Left": [+ "UuidArray"+ ]+ },+ "nullable": [+ false,+ false,+ false,+ false+ ]+ },+ "hash": "18550ce144d61e0c7b74342212f687d463723d2de3f525739a509b0644197e8b"+}diff --git a/.sqlx/query-1c61d555aab014d5f8eb5b32f3fd24df70bd6ae04c912b80e3ee172c983e3a83.json b/.sqlx/query-1c61d555aab014d5f8eb5b32f3fd24df70bd6ae04c912b80e3ee172c983e3a83.jsonnew file mode 100644index 0000000000..8cb8f2ce67--- /dev/null+++ b/.sqlx/query-1c61d555aab014d5f8eb5b32f3fd24df70bd6ae04c912b80e3ee172c983e3a83.json@@ -0,0 +1,34 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT m.id AS message_id, m.parent_entity_id::uuid AS \"channel_id!\", t.root_id\n FROM comms_messages m\n JOIN comms_message_threads t ON t.root_id = coalesce(m.thread_id, m.id)\n JOIN comms_messages root ON root.id = t.root_id\n WHERE m.id = ANY($1) AND m.deleted_at IS NULL AND t.deleted_at IS NULL\n AND root.deleted_at IS NULL AND root.thread_id IS NULL\n AND m.parent_entity_type = 'channel'\n AND t.parent_entity_type = 'channel' AND t.parent_entity_id = m.parent_entity_id\n AND root.parent_entity_type = 'channel'\n AND root.parent_entity_id = m.parent_entity_id",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "message_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "channel_id!",+ "type_info": "Uuid"+ },+ {+ "ordinal": 2,+ "name": "root_id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "UuidArray"+ ]+ },+ "nullable": [+ false,+ null,+ false+ ]+ },+ "hash": "1c61d555aab014d5f8eb5b32f3fd24df70bd6ae04c912b80e3ee172c983e3a83"+}diff --git a/.sqlx/query-8da419734f41296de7dd848d4b2659623a2e31379ba795b68a366b2d6439a516.json b/.sqlx/query-208832639d67b1e0442db145f285aa5932dfc5b12c2895b94071a60337e66996.jsonsimilarity index 50%rename from .sqlx/query-8da419734f41296de7dd848d4b2659623a2e31379ba795b68a366b2d6439a516.jsonrename to .sqlx/query-208832639d67b1e0442db145f285aa5932dfc5b12c2895b94071a60337e66996.jsonindex c7196a52c7..21fd704cd4 100644--- a/.sqlx/query-8da419734f41296de7dd848d4b2659623a2e31379ba795b68a366b2d6439a516.json+++ b/.sqlx/query-208832639d67b1e0442db145f285aa5932dfc5b12c2895b94071a60337e66996.json@@ -1,22 +1,22 @@{"db_name": "PostgreSQL",- "query": "SELECT pg_try_advisory_lock($1) AS \"acquired!\"",+ "query": "SELECT EXISTS(SELECT 1 FROM \"PdfPlaceableCommentAnchor\" WHERE uuid = $1) AS \"exists!\"","describe": {"columns": [{"ordinal": 0,- "name": "acquired!",+ "name": "exists!","type_info": "Bool"}],"parameters": {"Left": [- "Int8"+ "Uuid"]},"nullable": [null]},- "hash": "8da419734f41296de7dd848d4b2659623a2e31379ba795b68a366b2d6439a516"+ "hash": "208832639d67b1e0442db145f285aa5932dfc5b12c2895b94071a60337e66996"}diff --git a/.sqlx/query-24869f147463bf664432de6852f5f44aa9853414132fc73a4611b191bf892bab.json b/.sqlx/query-24869f147463bf664432de6852f5f44aa9853414132fc73a4611b191bf892bab.jsondeleted file mode 100644index 5701e969cd..0000000000--- a/.sqlx/query-24869f147463bf664432de6852f5f44aa9853414132fc73a4611b191bf892bab.json+++ /dev/null@@ -1,34 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "SELECT m.id AS message_id, m.channel_id AS \"channel_id!\", t.root_id\n FROM comms_messages m\n JOIN comms_message_threads t ON t.root_id = coalesce(m.thread_id, m.id)\n JOIN comms_messages root ON root.id = t.root_id\n WHERE m.id = ANY($1) AND m.deleted_at IS NULL AND t.deleted_at IS NULL\n AND root.deleted_at IS NULL AND root.thread_id IS NULL\n AND m.parent_entity_type = 'channel' AND m.parent_entity_id = m.channel_id::text\n AND t.parent_entity_type = 'channel' AND t.parent_entity_id = m.channel_id::text\n AND root.channel_id = m.channel_id AND root.parent_entity_type = 'channel'\n AND root.parent_entity_id = m.channel_id::text",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "message_id",- "type_info": "Uuid"- },- {- "ordinal": 1,- "name": "channel_id!",- "type_info": "Uuid"- },- {- "ordinal": 2,- "name": "root_id",- "type_info": "Uuid"- }- ],- "parameters": {- "Left": [- "UuidArray"- ]- },- "nullable": [- false,- true,- false- ]- },- "hash": "24869f147463bf664432de6852f5f44aa9853414132fc73a4611b191bf892bab"-}diff --git a/.sqlx/query-27842b5a110508929ca65447af264f8d76a23b6008d0b1e46929221c21c708b8.json b/.sqlx/query-27842b5a110508929ca65447af264f8d76a23b6008d0b1e46929221c21c708b8.jsondeleted file mode 100644index d3758d1fa2..0000000000--- a/.sqlx/query-27842b5a110508929ca65447af264f8d76a23b6008d0b1e46929221c21c708b8.json+++ /dev/null@@ -1,66 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT\n f.entity_type as \"entity_type!\",\n f.entity_id as \"entity_id!\",\n f.sort_order as \"sort_order!\",\n f.created_at as \"created_at!\",\n d.\"fileType\" as \"file_type?\",\n dt.sub_type::text as \"document_sub_type?\",\n ch.channel_type::text as \"channel_type?\",\n cm.channel_id::text as \"channel_id?\"\n FROM favorite f\n -- The comms tables key on uuid while favorite.entity_id is text.\n -- Compare in uuid (casting the favorite side) so their\n -- primary-key indexes are usable; casting the table side to text\n -- forced a full scan of each table on every listing. The regex\n -- guard keeps non-uuid entity_ids from failing the cast: they\n -- yield NULL and simply don't hydrate.\n CROSS JOIN LATERAL (\n SELECT CASE\n WHEN f.entity_id ~ '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$'\n THEN f.entity_id::uuid\n END AS entity_uuid\n ) fid\n LEFT JOIN \"Document\" d ON f.entity_type = 'document' AND d.id = f.entity_id\n LEFT JOIN document_sub_type dt ON f.entity_type = 'document' AND dt.document_id = f.entity_id\n LEFT JOIN \"Chat\" c ON f.entity_type = 'chat' AND c.id = f.entity_id\n LEFT JOIN \"Project\" p ON f.entity_type = 'project' AND p.id = f.entity_id\n LEFT JOIN comms_channels ch ON f.entity_type = 'channel' AND ch.id = fid.entity_uuid\n LEFT JOIN comms_messages cm ON f.entity_type = 'channel_message' AND cm.id = fid.entity_uuid\n WHERE f.user_id = $1\n AND ($2::text[] IS NULL OR f.entity_type = ANY($2))\n AND ($3::text[] IS NULL OR f.entity_id = ANY($3))\n AND (f.entity_type <> 'document' OR (d.id IS NOT NULL AND d.\"deletedAt\" IS NULL))\n AND (f.entity_type <> 'chat' OR (c.id IS NOT NULL AND c.\"deletedAt\" IS NULL))\n AND (f.entity_type <> 'project' OR (p.id IS NOT NULL AND p.\"deletedAt\" IS NULL))\n ORDER BY f.sort_order ASC, f.created_at ASC\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "entity_type!",- "type_info": "Text"- },- {- "ordinal": 1,- "name": "entity_id!",- "type_info": "Text"- },- {- "ordinal": 2,- "name": "sort_order!",- "type_info": "Float8"- },- {- "ordinal": 3,- "name": "created_at!",- "type_info": "Timestamptz"- },- {- "ordinal": 4,- "name": "file_type?",- "type_info": "Text"- },- {- "ordinal": 5,- "name": "document_sub_type?",- "type_info": "Text"- },- {- "ordinal": 6,- "name": "channel_type?",- "type_info": "Text"- },- {- "ordinal": 7,- "name": "channel_id?",- "type_info": "Text"- }- ],- "parameters": {- "Left": [- "Text",- "TextArray",- "TextArray"- ]- },- "nullable": [- false,- false,- false,- false,- true,- null,- null,- null- ]- },- "hash": "27842b5a110508929ca65447af264f8d76a23b6008d0b1e46929221c21c708b8"-}diff --git a/.sqlx/query-27dba71899c33b28e6f3daf31030b8e9f609ee7e6b0a62bba74ac168ac76f8cd.json b/.sqlx/query-27dba71899c33b28e6f3daf31030b8e9f609ee7e6b0a62bba74ac168ac76f8cd.jsondeleted file mode 100644index 9cf00258a3..0000000000--- a/.sqlx/query-27dba71899c33b28e6f3daf31030b8e9f609ee7e6b0a62bba74ac168ac76f8cd.json+++ /dev/null@@ -1,23 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "SELECT count(*) AS \"count!\" FROM notification n\n WHERE n.event_item_type = 'document' AND n.event_item_id = ANY($1)\n AND n.notification_event_type = ANY($2)\n AND (n.metadata->>'commentId' ~ '^[0-9]+$' OR n.metadata->>'threadId' ~ '^[0-9]+$')",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "count!",- "type_info": "Int8"- }- ],- "parameters": {- "Left": [- "TextArray",- "TextArray"- ]- },- "nullable": [- null- ]- },- "hash": "27dba71899c33b28e6f3daf31030b8e9f609ee7e6b0a62bba74ac168ac76f8cd"-}diff --git a/.sqlx/query-2a4a7696028910df7ef37d1b510e79c2ab19f8c4615b37da35f85581df995724.json b/.sqlx/query-2a4a7696028910df7ef37d1b510e79c2ab19f8c4615b37da35f85581df995724.jsonindex 9a928dbd75..df541b0214 100644--- a/.sqlx/query-2a4a7696028910df7ef37d1b510e79c2ab19f8c4615b37da35f85581df995724.json+++ b/.sqlx/query-2a4a7696028910df7ef37d1b510e79c2ab19f8c4615b37da35f85581df995724.json@@ -93,7 +93,7 @@false,false,false,- true,+ false,false,false,false,diff --git a/.sqlx/query-2b67d08f6da6ad42d94604cae8435d68ee4057f63b3a588ac0ca17e5486f0698.json b/.sqlx/query-2b67d08f6da6ad42d94604cae8435d68ee4057f63b3a588ac0ca17e5486f0698.jsonnew file mode 100644index 0000000000..e211287ed3--- /dev/null+++ b/.sqlx/query-2b67d08f6da6ad42d94604cae8435d68ee4057f63b3a588ac0ca17e5486f0698.json@@ -0,0 +1,17 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO comms_messages(id, parent_entity_type, parent_entity_id, sender_id, content) VALUES ($1, $2, $3, $4, 'test')",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Text",+ "Text",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "2b67d08f6da6ad42d94604cae8435d68ee4057f63b3a588ac0ca17e5486f0698"+}diff --git a/.sqlx/query-31046e81937ed209e30272e8fdacd5eb8d547c9f7dc47c30a387005d7021ae8d.json b/.sqlx/query-31046e81937ed209e30272e8fdacd5eb8d547c9f7dc47c30a387005d7021ae8d.jsonnew file mode 100644index 0000000000..27db92a19e--- /dev/null+++ b/.sqlx/query-31046e81937ed209e30272e8fdacd5eb8d547c9f7dc47c30a387005d7021ae8d.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT count(*) FROM user_notification WHERE notification_id = $1",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "count",+ "type_info": "Int8"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ null+ ]+ },+ "hash": "31046e81937ed209e30272e8fdacd5eb8d547c9f7dc47c30a387005d7021ae8d"+}diff --git a/.sqlx/query-370764911f1740f87020c23f75698124a953a3ee175b20eafbb522bf7cf8d1bf.json b/.sqlx/query-370764911f1740f87020c23f75698124a953a3ee175b20eafbb522bf7cf8d1bf.jsondeleted file mode 100644index d24dbdcc50..0000000000--- a/.sqlx/query-370764911f1740f87020c23f75698124a953a3ee175b20eafbb522bf7cf8d1bf.json+++ /dev/null@@ -1,22 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "SELECT pg_try_advisory_xact_lock($1) AS \"acquired!\"",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "acquired!",- "type_info": "Bool"- }- ],- "parameters": {- "Left": [- "Int8"- ]- },- "nullable": [- null- ]- },- "hash": "370764911f1740f87020c23f75698124a953a3ee175b20eafbb522bf7cf8d1bf"-}diff --git a/.sqlx/query-38c286776a6f6d9cf792a170769cbc1cb2b7540552079d908f912d631adb2c43.json b/.sqlx/query-38c286776a6f6d9cf792a170769cbc1cb2b7540552079d908f912d631adb2c43.jsonnew file mode 100644index 0000000000..5ef5022702--- /dev/null+++ b/.sqlx/query-38c286776a6f6d9cf792a170769cbc1cb2b7540552079d908f912d631adb2c43.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO migrated_comment_id (comment_id, message_id, document_id)\n VALUES (10, $1, 'message-doc-a'), (11, $2, 'message-doc-a')",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "38c286776a6f6d9cf792a170769cbc1cb2b7540552079d908f912d631adb2c43"+}diff --git a/.sqlx/query-3d92901e27e9ca5ff9f78b9f09e02c76b9e44ee49efec6c4afc9ea620cb27f2f.json b/.sqlx/query-3d92901e27e9ca5ff9f78b9f09e02c76b9e44ee49efec6c4afc9ea620cb27f2f.jsondeleted file mode 100644index 7fbf5c1431..0000000000--- a/.sqlx/query-3d92901e27e9ca5ff9f78b9f09e02c76b9e44ee49efec6c4afc9ea620cb27f2f.json+++ /dev/null@@ -1,35 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "SELECT m.id, i.\"documentId\" AS document_id, m.\"modificationData\" AS payload\n FROM \"DocumentInstanceModificationData\" m\n JOIN \"DocumentInstance\" i ON i.id = m.\"documentInstanceId\"\n JOIN \"Document\" d ON d.id = i.\"documentId\"\n WHERE i.\"documentId\" = ANY($1) AND d.\"fileType\" = 'pdf'\n AND jsonb_path_exists(m.\"modificationData\", $2::text::jsonpath)\n ORDER BY m.id",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "id",- "type_info": "Int8"- },- {- "ordinal": 1,- "name": "document_id",- "type_info": "Text"- },- {- "ordinal": 2,- "name": "payload",- "type_info": "Jsonb"- }- ],- "parameters": {- "Left": [- "TextArray",- "Text"- ]- },- "nullable": [- false,- false,- false- ]- },- "hash": "3d92901e27e9ca5ff9f78b9f09e02c76b9e44ee49efec6c4afc9ea620cb27f2f"-}diff --git a/.sqlx/query-4027823662c88d28cb2a95792b615423ead933fb94bbd3dad5e6e5e7ff59ff53.json b/.sqlx/query-4027823662c88d28cb2a95792b615423ead933fb94bbd3dad5e6e5e7ff59ff53.jsondeleted file mode 100644index 733b7ba987..0000000000--- a/.sqlx/query-4027823662c88d28cb2a95792b615423ead933fb94bbd3dad5e6e5e7ff59ff53.json+++ /dev/null@@ -1,22 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "SELECT pg_advisory_unlock($1) AS \"released!\"",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "released!",- "type_info": "Bool"- }- ],- "parameters": {- "Left": [- "Int8"- ]- },- "nullable": [- null- ]- },- "hash": "4027823662c88d28cb2a95792b615423ead933fb94bbd3dad5e6e5e7ff59ff53"-}diff --git a/.sqlx/query-48fbddcebd915244c16d53a6f2c8b68ad4ffd6735cd44817ee2cec34aa6bbe9c.json b/.sqlx/query-48fbddcebd915244c16d53a6f2c8b68ad4ffd6735cd44817ee2cec34aa6bbe9c.jsondeleted file mode 100644index f94c37e726..0000000000--- a/.sqlx/query-48fbddcebd915244c16d53a6f2c8b68ad4ffd6735cd44817ee2cec34aa6bbe9c.json+++ /dev/null@@ -1,22 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "SELECT t.id FROM \"Thread\" t\n LEFT JOIN \"PdfPlaceableCommentAnchor\" p ON p.\"threadId\" = t.id AND NOT p.\"wasDeleted\"\n LEFT JOIN \"PdfHighlightAnchor\" h ON h.\"threadId\" = t.id AND h.\"deletedAt\" IS NULL\n WHERE t.\"deletedAt\" IS NULL\n GROUP BY t.id HAVING count(DISTINCT p.uuid) + count(DISTINCT h.uuid) > 1\n ORDER BY t.id LIMIT $1",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "id",- "type_info": "Int8"- }- ],- "parameters": {- "Left": [- "Int8"- ]- },- "nullable": [- false- ]- },- "hash": "48fbddcebd915244c16d53a6f2c8b68ad4ffd6735cd44817ee2cec34aa6bbe9c"-}diff --git a/.sqlx/query-4a126845898ffc207a6ca3713374086ca8462a6ebffe8d6c003d3301fdec8c77.json b/.sqlx/query-4a126845898ffc207a6ca3713374086ca8462a6ebffe8d6c003d3301fdec8c77.jsonnew file mode 100644index 0000000000..1e5eff5c9f--- /dev/null+++ b/.sqlx/query-4a126845898ffc207a6ca3713374086ca8462a6ebffe8d6c003d3301fdec8c77.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO team(id, name, owner_id) VALUES ($1, 'test', $2)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "4a126845898ffc207a6ca3713374086ca8462a6ebffe8d6c003d3301fdec8c77"+}diff --git a/.sqlx/query-576c0d1c1c043d4418c8f137138c5a0cd749cc32100c3d95c0cc6792e697e8a9.json b/.sqlx/query-576c0d1c1c043d4418c8f137138c5a0cd749cc32100c3d95c0cc6792e697e8a9.jsondeleted file mode 100644index 38304c51cb..0000000000--- a/.sqlx/query-576c0d1c1c043d4418c8f137138c5a0cd749cc32100c3d95c0cc6792e697e8a9.json+++ /dev/null@@ -1,22 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "SELECT count(*) AS \"count!\" FROM \"Thread\" t\n JOIN migrated_comment_thread_id tm ON tm.thread_id = t.id\n JOIN LATERAL (\n SELECT cm.message_id FROM \"Comment\" c\n JOIN migrated_comment_id cm ON cm.comment_id = c.id\n WHERE c.\"threadId\" = t.id\n ORDER BY c.\"order\" NULLS LAST, c.\"createdAt\", c.id LIMIT 1\n ) first ON true\n WHERE t.\"documentId\" = ANY($1) AND first.message_id <> tm.root_id",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "count!",- "type_info": "Int8"- }- ],- "parameters": {- "Left": [- "TextArray"- ]- },- "nullable": [- null- ]- },- "hash": "576c0d1c1c043d4418c8f137138c5a0cd749cc32100c3d95c0cc6792e697e8a9"-}diff --git a/.sqlx/query-e8716b5ae331e16652de2e4691250b6060db0e4e7c4c744c9f157ab32f6d9224.json b/.sqlx/query-59744a37fe9d239158668457771988c10053706dd209eacb8e5997b601010ca6.jsonsimilarity index 66%rename from .sqlx/query-e8716b5ae331e16652de2e4691250b6060db0e4e7c4c744c9f157ab32f6d9224.jsonrename to .sqlx/query-59744a37fe9d239158668457771988c10053706dd209eacb8e5997b601010ca6.jsonindex 345ddd8e80..94d0b41ded 100644--- a/.sqlx/query-e8716b5ae331e16652de2e4691250b6060db0e4e7c4c744c9f157ab32f6d9224.json+++ b/.sqlx/query-59744a37fe9d239158668457771988c10053706dd209eacb8e5997b601010ca6.json@@ -1,30 +1,25 @@{"db_name": "PostgreSQL",- "query": "SELECT channel_id, parent_entity_type, parent_entity_id, import_metadata, import_order FROM comms_messages WHERE id = $1",+ "query": "SELECT parent_entity_type, parent_entity_id, import_metadata, import_order FROM comms_messages WHERE id = $1","describe": {"columns": [{"ordinal": 0,- "name": "channel_id",- "type_info": "Uuid"- },- {- "ordinal": 1,"name": "parent_entity_type","type_info": "Text"},{- "ordinal": 2,+ "ordinal": 1,"name": "parent_entity_id","type_info": "Text"},{- "ordinal": 3,+ "ordinal": 2,"name": "import_metadata","type_info": "Jsonb"},{- "ordinal": 4,+ "ordinal": 3,"name": "import_order","type_info": "Int8"}@@ -35,12 +30,11 @@]},"nullable": [- true,false,false,true,true]},- "hash": "e8716b5ae331e16652de2e4691250b6060db0e4e7c4c744c9f157ab32f6d9224"+ "hash": "59744a37fe9d239158668457771988c10053706dd209eacb8e5997b601010ca6"}diff --git a/.sqlx/query-5c23ce84d469b4732bbda39b3869cbe6a0b0e54ac14498eb7c80629e83a78dba.json b/.sqlx/query-5c23ce84d469b4732bbda39b3869cbe6a0b0e54ac14498eb7c80629e83a78dba.jsonnew file mode 100644index 0000000000..fb9d80731e--- /dev/null+++ b/.sqlx/query-5c23ce84d469b4732bbda39b3869cbe6a0b0e54ac14498eb7c80629e83a78dba.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT count(*) FROM comms_messages WHERE id = ANY($1)",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "count",+ "type_info": "Int8"+ }+ ],+ "parameters": {+ "Left": [+ "UuidArray"+ ]+ },+ "nullable": [+ null+ ]+ },+ "hash": "5c23ce84d469b4732bbda39b3869cbe6a0b0e54ac14498eb7c80629e83a78dba"+}diff --git a/.sqlx/query-5e56844164b2f53aab67022cb6141c25b3c78d4e19eef674cfef968816559980.json b/.sqlx/query-5e56844164b2f53aab67022cb6141c25b3c78d4e19eef674cfef968816559980.jsondeleted file mode 100644index cf3ce2e710..0000000000--- a/.sqlx/query-5e56844164b2f53aab67022cb6141c25b3c78d4e19eef674cfef968816559980.json+++ /dev/null@@ -1,38 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "WITH legacy AS (\n SELECT c.id, c.text, c.owner, c.created_at, c.updated_at,\n COALESCE(c.deleted_at, t.deleted_at) AT TIME ZONE 'UTC' AS deleted_at,\n CASE WHEN t.contact_id IS NULL THEN 'crm_company' ELSE 'crm_contact' END AS parent_type,\n COALESCE(t.contact_id, t.company_id)::text AS parent_id,\n NULLIF(first_value(c.id) OVER (PARTITION BY c.thread_id ORDER BY c.created_at, c.id), c.id) AS root_id\n FROM crm_comment c\n JOIN crm_thread t ON t.id = c.thread_id\n )\n SELECT\n count(*) FILTER (WHERE m.id IS NULL) AS \"missing_messages!\",\n count(*) FILTER (\n WHERE m.id IS NOT NULL\n AND (m.parent_entity_type, m.parent_entity_id, m.thread_id)\n IS DISTINCT FROM (l.parent_type, l.parent_id, l.root_id)\n ) AS \"misplaced_messages!\",\n count(*) FILTER (\n WHERE m.id IS NOT NULL AND l.root_id IS NULL\n AND NOT EXISTS (SELECT 1 FROM comms_message_threads th WHERE th.root_id = m.id)\n ) AS \"roots_without_threads!\",\n count(*) FILTER (\n WHERE m.updated_at <= l.updated_at\n AND (m.content, m.sender_id, m.created_at, m.deleted_at)\n IS DISTINCT FROM (l.text, l.owner, l.created_at, l.deleted_at)\n ) AS \"content_mismatches!\"\n FROM legacy l\n LEFT JOIN comms_messages m ON m.id = l.id",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "missing_messages!",- "type_info": "Int8"- },- {- "ordinal": 1,- "name": "misplaced_messages!",- "type_info": "Int8"- },- {- "ordinal": 2,- "name": "roots_without_threads!",- "type_info": "Int8"- },- {- "ordinal": 3,- "name": "content_mismatches!",- "type_info": "Int8"- }- ],- "parameters": {- "Left": []- },- "nullable": [- null,- null,- null,- null- ]- },- "hash": "5e56844164b2f53aab67022cb6141c25b3c78d4e19eef674cfef968816559980"-}diff --git a/.sqlx/query-624ac87b24d542ae21dabeb58d067643329cd92029864abda17d5376dc999739.json b/.sqlx/query-624ac87b24d542ae21dabeb58d067643329cd92029864abda17d5376dc999739.jsondeleted file mode 100644index a0d957a6c9..0000000000--- a/.sqlx/query-624ac87b24d542ae21dabeb58d067643329cd92029864abda17d5376dc999739.json+++ /dev/null@@ -1,15 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "UPDATE \"DocumentInstanceModificationData\" SET \"modificationData\" = $1 WHERE id = $2",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Jsonb",- "Int8"- ]- },- "nullable": []- },- "hash": "624ac87b24d542ae21dabeb58d067643329cd92029864abda17d5376dc999739"-}diff --git a/.sqlx/query-67ac072e2efaf7d6bb79bbdc10a2ac4ebd904a7bd4d0ff635fb3578c00890a81.json b/.sqlx/query-67ac072e2efaf7d6bb79bbdc10a2ac4ebd904a7bd4d0ff635fb3578c00890a81.jsonnew file mode 100644index 0000000000..c2d3d07168--- /dev/null+++ b/.sqlx/query-67ac072e2efaf7d6bb79bbdc10a2ac4ebd904a7bd4d0ff635fb3578c00890a81.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT count(*) FROM notification WHERE id = $1",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "count",+ "type_info": "Int8"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ null+ ]+ },+ "hash": "67ac072e2efaf7d6bb79bbdc10a2ac4ebd904a7bd4d0ff635fb3578c00890a81"+}diff --git a/.sqlx/query-6ad281c07800007bd048990b1d839838e9f059b92690acc84a22b9720e7abd35.json b/.sqlx/query-6ad281c07800007bd048990b1d839838e9f059b92690acc84a22b9720e7abd35.jsonnew file mode 100644index 0000000000..a3b1c2ba9b--- /dev/null+++ b/.sqlx/query-6ad281c07800007bd048990b1d839838e9f059b92690acc84a22b9720e7abd35.json@@ -0,0 +1,24 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO comms_messages\n (id, parent_entity_type, parent_entity_id, thread_id,\n sender_id, imported_author, content, created_at, updated_at, edited_at,\n import_metadata, import_order)\n SELECT input.id, 'channel', $1, input.thread_id, input.sender_id,\n input.imported_author, input.content, input.created_at, input.updated_at,\n input.edited_at, input.import_metadata, input.import_order\n FROM UNNEST($2::uuid[], $3::uuid[], $4::text[], $5::text[], $6::text[],\n $7::timestamptz[], $8::timestamptz[], $9::timestamp[], $10::jsonb[], $11::bigint[])\n AS input(id, thread_id, sender_id, imported_author, content, created_at,\n updated_at, edited_at, import_metadata, import_order)\n ON CONFLICT (id) DO NOTHING",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "UuidArray",+ "UuidArray",+ "TextArray",+ "TextArray",+ "TextArray",+ "TimestamptzArray",+ "TimestamptzArray",+ "TimestampArray",+ "JsonbArray",+ "Int8Array"+ ]+ },+ "nullable": []+ },+ "hash": "6ad281c07800007bd048990b1d839838e9f059b92690acc84a22b9720e7abd35"+}diff --git a/.sqlx/query-6f5af43c8c300d18592d183b9d272a115811d56d6e7fa9235b7deec3ed03b751.json b/.sqlx/query-6f5af43c8c300d18592d183b9d272a115811d56d6e7fa9235b7deec3ed03b751.jsonnew file mode 100644index 0000000000..026afd6c9b--- /dev/null+++ b/.sqlx/query-6f5af43c8c300d18592d183b9d272a115811d56d6e7fa9235b7deec3ed03b751.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT count(*) FROM notification WHERE id = ANY($1)",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "count",+ "type_info": "Int8"+ }+ ],+ "parameters": {+ "Left": [+ "UuidArray"+ ]+ },+ "nullable": [+ null+ ]+ },+ "hash": "6f5af43c8c300d18592d183b9d272a115811d56d6e7fa9235b7deec3ed03b751"+}diff --git a/.sqlx/query-6fedf690a9e09ba50fd448225ca6971a2181429dba4b1c4daaa72d911871dfc9.json b/.sqlx/query-6fedf690a9e09ba50fd448225ca6971a2181429dba4b1c4daaa72d911871dfc9.jsonnew file mode 100644index 0000000000..0b51e2b517--- /dev/null+++ b/.sqlx/query-6fedf690a9e09ba50fd448225ca6971a2181429dba4b1c4daaa72d911871dfc9.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO crm_companies(id, team_id, first_interaction, last_interaction) VALUES ($1, $2, now(), now())",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "6fedf690a9e09ba50fd448225ca6971a2181429dba4b1c4daaa72d911871dfc9"+}diff --git a/.sqlx/query-70840b3a7e70aa1120dfb953aa6b2014f4c001de2b2c7f6de5122962197b9e21.json b/.sqlx/query-70840b3a7e70aa1120dfb953aa6b2014f4c001de2b2c7f6de5122962197b9e21.jsonnew file mode 100644index 0000000000..92695b7b70--- /dev/null+++ b/.sqlx/query-70840b3a7e70aa1120dfb953aa6b2014f4c001de2b2c7f6de5122962197b9e21.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT count(*) FROM comms_attachments WHERE message_id = ANY($1)",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "count",+ "type_info": "Int8"+ }+ ],+ "parameters": {+ "Left": [+ "UuidArray"+ ]+ },+ "nullable": [+ null+ ]+ },+ "hash": "70840b3a7e70aa1120dfb953aa6b2014f4c001de2b2c7f6de5122962197b9e21"+}diff --git a/.sqlx/query-7148c51a64de5cc3721334828de9c6349171f0a9c46fbe5be239c16e007fbef8.json b/.sqlx/query-7148c51a64de5cc3721334828de9c6349171f0a9c46fbe5be239c16e007fbef8.jsonnew file mode 100644index 0000000000..c99125b0fe--- /dev/null+++ b/.sqlx/query-7148c51a64de5cc3721334828de9c6349171f0a9c46fbe5be239c16e007fbef8.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT count(*) FROM comms_message_threads WHERE root_id = $1",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "count",+ "type_info": "Int8"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ null+ ]+ },+ "hash": "7148c51a64de5cc3721334828de9c6349171f0a9c46fbe5be239c16e007fbef8"+}diff --git a/.sqlx/query-74275afbf7e9f3348efbbd7d8c7eb565faee20148c38b8ca42582c02628af0e7.json b/.sqlx/query-74275afbf7e9f3348efbbd7d8c7eb565faee20148c38b8ca42582c02628af0e7.jsondeleted file mode 100644index c2e34c72c1..0000000000--- a/.sqlx/query-74275afbf7e9f3348efbbd7d8c7eb565faee20148c38b8ca42582c02628af0e7.json+++ /dev/null@@ -1,16 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "INSERT INTO migrated_comment_id (comment_id, message_id, document_id)\n SELECT * FROM UNNEST($1::bigint[], $2::uuid[], $3::text[])",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Int8Array",- "UuidArray",- "TextArray"- ]- },- "nullable": []- },- "hash": "74275afbf7e9f3348efbbd7d8c7eb565faee20148c38b8ca42582c02628af0e7"-}diff --git a/.sqlx/query-74842f5645a90d4bdefd392a080062648d83a046b36d7d83ed963c3842657a41.json b/.sqlx/query-74842f5645a90d4bdefd392a080062648d83a046b36d7d83ed963c3842657a41.jsondeleted file mode 100644index 27ff206084..0000000000--- a/.sqlx/query-74842f5645a90d4bdefd392a080062648d83a046b36d7d83ed963c3842657a41.json+++ /dev/null@@ -1,35 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "SELECT t.\"documentId\" AS document_id, t.metadata->>'markId' AS \"mark_id!\",\n array_agg(t.id ORDER BY t.id) AS \"thread_ids!\"\n FROM \"Thread\" t\n WHERE t.\"deletedAt\" IS NULL\n AND t.metadata->>'markId' ~ $1\n AND EXISTS (SELECT 1 FROM \"Comment\" c WHERE c.\"threadId\" = t.id)\n AND NOT EXISTS (SELECT 1 FROM \"PdfPlaceableCommentAnchor\" p WHERE p.\"threadId\" = t.id AND NOT p.\"wasDeleted\")\n AND NOT EXISTS (SELECT 1 FROM \"PdfHighlightAnchor\" h\n WHERE h.\"threadId\" = t.id AND h.\"deletedAt\" IS NULL)\n GROUP BY 1, 2 HAVING count(*) > 1\n ORDER BY 1, 2 LIMIT $2",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "document_id",- "type_info": "Text"- },- {- "ordinal": 1,- "name": "mark_id!",- "type_info": "Text"- },- {- "ordinal": 2,- "name": "thread_ids!",- "type_info": "Int8Array"- }- ],- "parameters": {- "Left": [- "Text",- "Int8"- ]- },- "nullable": [- false,- null,- null- ]- },- "hash": "74842f5645a90d4bdefd392a080062648d83a046b36d7d83ed963c3842657a41"-}diff --git a/.sqlx/query-f4387247669703803329aa0d090c0b6427a7b17676d1fdf8fdfd950dbf685dd0.json b/.sqlx/query-74c1f97b51a3b15e2ad5e662e85c19b874d47235b28c13b57d73ee2d1ab2cce7.jsonsimilarity index 51%rename from .sqlx/query-f4387247669703803329aa0d090c0b6427a7b17676d1fdf8fdfd950dbf685dd0.jsonrename to .sqlx/query-74c1f97b51a3b15e2ad5e662e85c19b874d47235b28c13b57d73ee2d1ab2cce7.jsonindex e0e7f4d8d0..b93653a377 100644--- a/.sqlx/query-f4387247669703803329aa0d090c0b6427a7b17676d1fdf8fdfd950dbf685dd0.json+++ b/.sqlx/query-74c1f97b51a3b15e2ad5e662e85c19b874d47235b28c13b57d73ee2d1ab2cce7.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "SELECT count(DISTINCT \"documentId\") AS \"count!\" FROM \"Thread\"",+ "query": "SELECT count(*) AS \"count!\" FROM comms_messages\n WHERE parent_entity_type = 'document' AND parent_entity_id = 'message-doc-a'","describe": {"columns": [{@@ -16,5 +16,5 @@null]},- "hash": "f4387247669703803329aa0d090c0b6427a7b17676d1fdf8fdfd950dbf685dd0"+ "hash": "74c1f97b51a3b15e2ad5e662e85c19b874d47235b28c13b57d73ee2d1ab2cce7"}diff --git a/.sqlx/query-7679b2e926cd4130da8993cfd3984c6ed07268f5779072d63c116cc80ec4defb.json b/.sqlx/query-7679b2e926cd4130da8993cfd3984c6ed07268f5779072d63c116cc80ec4defb.jsondeleted file mode 100644index e71eed77db..0000000000--- a/.sqlx/query-7679b2e926cd4130da8993cfd3984c6ed07268f5779072d63c116cc80ec4defb.json+++ /dev/null@@ -1,17 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "INSERT INTO comms_messages (id, channel_id, sender_id, content) VALUES ($1, $2, $3, $4)",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Uuid",- "Uuid",- "Text",- "Text"- ]- },- "nullable": []- },- "hash": "7679b2e926cd4130da8993cfd3984c6ed07268f5779072d63c116cc80ec4defb"-}diff --git a/.sqlx/query-76f3e71eaea836800a3e5857a00621edab609f4eaba0ce9a4da11743cc2b1482.json b/.sqlx/query-76f3e71eaea836800a3e5857a00621edab609f4eaba0ce9a4da11743cc2b1482.jsondeleted file mode 100644index fb2702be6c..0000000000--- a/.sqlx/query-76f3e71eaea836800a3e5857a00621edab609f4eaba0ce9a4da11743cc2b1482.json+++ /dev/null@@ -1,15 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "UPDATE notification n SET metadata = jsonb_set(\n jsonb_set(n.metadata, '{commentId}', to_jsonb(cm.message_id::text)),\n '{threadId}', to_jsonb(tm.root_id::text)\n )\n FROM migrated_comment_id cm, migrated_comment_thread_id tm\n WHERE n.event_item_type = 'document' AND n.event_item_id = ANY($1)\n AND n.notification_event_type = ANY($2)\n AND cm.document_id = n.event_item_id AND tm.document_id = n.event_item_id\n AND n.metadata->>'commentId' = cm.comment_id::text\n AND n.metadata->>'threadId' = tm.thread_id::text",- "describe": {- "columns": [],- "parameters": {- "Left": [- "TextArray",- "TextArray"- ]- },- "nullable": []- },- "hash": "76f3e71eaea836800a3e5857a00621edab609f4eaba0ce9a4da11743cc2b1482"-}diff --git a/.sqlx/query-7a46646d6f977892d26d63d547e9f42fe01f8a7b19a3dade9420553615125f6a.json b/.sqlx/query-7a46646d6f977892d26d63d547e9f42fe01f8a7b19a3dade9420553615125f6a.jsonnew file mode 100644index 0000000000..05debfe9e0--- /dev/null+++ b/.sqlx/query-7a46646d6f977892d26d63d547e9f42fe01f8a7b19a3dade9420553615125f6a.json@@ -0,0 +1,12 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO document_sub_type(document_id, sub_type) VALUES ('message-doc-a', 'task')",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": []+ },+ "nullable": []+ },+ "hash": "7a46646d6f977892d26d63d547e9f42fe01f8a7b19a3dade9420553615125f6a"+}diff --git a/.sqlx/query-7a590cadb532aa035907f3c2889166dd7c5cea5d61ab9e7fedf9f8a5186295e7.json b/.sqlx/query-7a590cadb532aa035907f3c2889166dd7c5cea5d61ab9e7fedf9f8a5186295e7.jsondeleted file mode 100644index 3ef0c29241..0000000000--- a/.sqlx/query-7a590cadb532aa035907f3c2889166dd7c5cea5d61ab9e7fedf9f8a5186295e7.json+++ /dev/null@@ -1,14 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "UPDATE \"PdfHighlightAnchor\" a SET root_id = tm.root_id\n FROM migrated_comment_thread_id tm\n WHERE a.\"threadId\" = tm.thread_id AND a.\"documentId\" = ANY($1)\n AND a.root_id IS DISTINCT FROM tm.root_id",- "describe": {- "columns": [],- "parameters": {- "Left": [- "TextArray"- ]- },- "nullable": []- },- "hash": "7a590cadb532aa035907f3c2889166dd7c5cea5d61ab9e7fedf9f8a5186295e7"-}diff --git a/.sqlx/query-7a6fdeac3b3c02c186e827f2ac20e0083bc01c0392e43def49a00704c527b83e.json b/.sqlx/query-7a6fdeac3b3c02c186e827f2ac20e0083bc01c0392e43def49a00704c527b83e.jsonnew file mode 100644index 0000000000..2a456c2a13--- /dev/null+++ b/.sqlx/query-7a6fdeac3b3c02c186e827f2ac20e0083bc01c0392e43def49a00704c527b83e.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO user_notification(user_id, notification_id) VALUES ($1, $2)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "7a6fdeac3b3c02c186e827f2ac20e0083bc01c0392e43def49a00704c527b83e"+}diff --git a/.sqlx/query-7ee8fafbf316792909b43741a5d2afdfe798b1e893a1b116519efc97a7f5553d.json b/.sqlx/query-7ee8fafbf316792909b43741a5d2afdfe798b1e893a1b116519efc97a7f5553d.jsonnew file mode 100644index 0000000000..07a5062e64--- /dev/null+++ b/.sqlx/query-7ee8fafbf316792909b43741a5d2afdfe798b1e893a1b116519efc97a7f5553d.json@@ -0,0 +1,28 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT root_id, text FROM \"PdfHighlightAnchor\" WHERE uuid = $1",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "root_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "text",+ "type_info": "Text"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ true,+ false+ ]+ },+ "hash": "7ee8fafbf316792909b43741a5d2afdfe798b1e893a1b116519efc97a7f5553d"+}diff --git a/.sqlx/query-7ee9ba5adfac304c7ac930943538ecca7428b1054d451fb2f8973cf9c11caf45.json b/.sqlx/query-7ee9ba5adfac304c7ac930943538ecca7428b1054d451fb2f8973cf9c11caf45.jsondeleted file mode 100644index fbdbdf128a..0000000000--- a/.sqlx/query-7ee9ba5adfac304c7ac930943538ecca7428b1054d451fb2f8973cf9c11caf45.json+++ /dev/null@@ -1,118 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n WITH input_ids AS (\n SELECT UNNEST($1::uuid[]) AS channel_id\n )\n SELECT\n i.channel_id AS \"channel_id!\",\n l.message_id AS \"l_message_id?: uuid::Uuid\",\n l.thread_id AS \"l_thread_id?: uuid::Uuid\",\n l.sender_id AS \"l_sender_id?: String\",\n l.content AS \"l_content?: String\",\n l.created_at AS \"l_created_at?: chrono::DateTime<chrono::Utc>\",\n l.updated_at AS \"l_updated_at?: chrono::DateTime<chrono::Utc>\",\n l.deleted_at AS \"l_deleted_at?: chrono::DateTime<chrono::Utc>\",\n l.mentions AS \"l_mentions?: Vec<String>\",\n n.message_id AS \"n_message_id?: uuid::Uuid\",\n n.thread_id AS \"n_thread_id?: uuid::Uuid\",\n n.sender_id AS \"n_sender_id?: String\",\n n.content AS \"n_content?: String\",\n n.created_at AS \"n_created_at?: chrono::DateTime<chrono::Utc>\",\n n.updated_at AS \"n_updated_at?: chrono::DateTime<chrono::Utc>\",\n n.deleted_at AS \"n_deleted_at?: chrono::DateTime<chrono::Utc>\",\n n.mentions AS \"n_mentions?: Vec<String>\"\n FROM input_ids i\n LEFT JOIN LATERAL (\n SELECT\n m.id AS message_id,\n m.thread_id,\n m.sender_id,\n m.content,\n m.created_at,\n m.updated_at,\n m.deleted_at::timestamptz AS deleted_at,\n COALESCE(\n ARRAY(\n SELECT entity_type || ':' || entity_id\n FROM comms_entity_mentions em\n WHERE em.source_entity_type = 'message'\n AND em.source_entity_id = m.id::text\n ),\n '{}'::text[]\n ) AS mentions\n FROM comms_messages m\n WHERE m.channel_id = i.channel_id\n AND m.deleted_at IS NULL\n ORDER BY m.created_at DESC\n LIMIT 1\n ) l ON TRUE\n LEFT JOIN LATERAL (\n SELECT\n m.id AS message_id,\n m.thread_id,\n m.sender_id,\n m.content,\n m.created_at,\n m.updated_at,\n m.deleted_at::timestamptz AS deleted_at,\n COALESCE(\n ARRAY(\n SELECT entity_type || ':' || entity_id\n FROM comms_entity_mentions em\n WHERE em.source_entity_type = 'message'\n AND em.source_entity_id = m.id::text\n ),\n '{}'::text[]\n ) AS mentions\n FROM comms_messages m\n WHERE m.channel_id = i.channel_id\n AND m.deleted_at IS NULL\n AND m.thread_id IS NULL\n ORDER BY m.created_at DESC\n LIMIT 1\n ) n ON TRUE\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "channel_id!",- "type_info": "Uuid"- },- {- "ordinal": 1,- "name": "l_message_id?: uuid::Uuid",- "type_info": "Uuid"- },- {- "ordinal": 2,- "name": "l_thread_id?: uuid::Uuid",- "type_info": "Uuid"- },- {- "ordinal": 3,- "name": "l_sender_id?: String",- "type_info": "Text"- },- {- "ordinal": 4,- "name": "l_content?: String",- "type_info": "Text"- },- {- "ordinal": 5,- "name": "l_created_at?: chrono::DateTime<chrono::Utc>",- "type_info": "Timestamptz"- },- {- "ordinal": 6,- "name": "l_updated_at?: chrono::DateTime<chrono::Utc>",- "type_info": "Timestamptz"- },- {- "ordinal": 7,- "name": "l_deleted_at?: chrono::DateTime<chrono::Utc>",- "type_info": "Timestamptz"- },- {- "ordinal": 8,- "name": "l_mentions?: Vec<String>",- "type_info": "TextArray"- },- {- "ordinal": 9,- "name": "n_message_id?: uuid::Uuid",- "type_info": "Uuid"- },- {- "ordinal": 10,- "name": "n_thread_id?: uuid::Uuid",- "type_info": "Uuid"- },- {- "ordinal": 11,- "name": "n_sender_id?: String",- "type_info": "Text"- },- {- "ordinal": 12,- "name": "n_content?: String",- "type_info": "Text"- },- {- "ordinal": 13,- "name": "n_created_at?: chrono::DateTime<chrono::Utc>",- "type_info": "Timestamptz"- },- {- "ordinal": 14,- "name": "n_updated_at?: chrono::DateTime<chrono::Utc>",- "type_info": "Timestamptz"- },- {- "ordinal": 15,- "name": "n_deleted_at?: chrono::DateTime<chrono::Utc>",- "type_info": "Timestamptz"- },- {- "ordinal": 16,- "name": "n_mentions?: Vec<String>",- "type_info": "TextArray"- }- ],- "parameters": {- "Left": [- "UuidArray"- ]- },- "nullable": [- null,- true,- true,- true,- true,- true,- true,- true,- true,- true,- true,- true,- true,- true,- true,- true,- true- ]- },- "hash": "7ee9ba5adfac304c7ac930943538ecca7428b1054d451fb2f8973cf9c11caf45"-}diff --git a/.sqlx/query-85d9d5595305c691fc07ce2c4727ca3cb728a0cad52430047808b2776555a1f1.json b/.sqlx/query-85d9d5595305c691fc07ce2c4727ca3cb728a0cad52430047808b2776555a1f1.jsonnew file mode 100644index 0000000000..12e0f5ddec--- /dev/null+++ b/.sqlx/query-85d9d5595305c691fc07ce2c4727ca3cb728a0cad52430047808b2776555a1f1.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO \"User\" (id, email, macro_user_id) VALUES ($1, '[email protected]', $2)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "85d9d5595305c691fc07ce2c4727ca3cb728a0cad52430047808b2776555a1f1"+}diff --git a/.sqlx/query-865ff79fbb79a924267e0a407a3506c09d8ac22046625a32300225aa7f4c6ec7.json b/.sqlx/query-865ff79fbb79a924267e0a407a3506c09d8ac22046625a32300225aa7f4c6ec7.jsondeleted file mode 100644index 4c304fa37e..0000000000--- a/.sqlx/query-865ff79fbb79a924267e0a407a3506c09d8ac22046625a32300225aa7f4c6ec7.json+++ /dev/null@@ -1,22 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT DISTINCT id AS \"user_id!\" FROM (\n SELECT m.sender_id AS id\n FROM comms_messages m\n JOIN comms_channel_participants cp\n ON cp.channel_id = m.channel_id AND cp.user_id = m.sender_id\n WHERE (m.id = $1 OR m.thread_id = $1)\n AND m.deleted_at IS NULL\n AND cp.left_at IS NULL\n UNION\n SELECT em.entity_id AS id\n FROM comms_entity_mentions em\n JOIN comms_messages m ON m.id::text = em.source_entity_id\n JOIN comms_channel_participants cp\n ON cp.channel_id = m.channel_id AND cp.user_id = em.entity_id\n WHERE (m.id = $1 OR m.thread_id = $1)\n AND m.deleted_at IS NULL\n AND em.source_entity_type = 'message'\n AND em.entity_type = 'user'\n AND cp.left_at IS NULL\n ) AS combined\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "user_id!",- "type_info": "Text"- }- ],- "parameters": {- "Left": [- "Uuid"- ]- },- "nullable": [- null- ]- },- "hash": "865ff79fbb79a924267e0a407a3506c09d8ac22046625a32300225aa7f4c6ec7"-}diff --git a/.sqlx/query-8d1d17c1cc6c6582a909c1f723083efa174a50417040df24cf99e4ce7a473a6c.json b/.sqlx/query-8d1d17c1cc6c6582a909c1f723083efa174a50417040df24cf99e4ce7a473a6c.jsonnew file mode 100644index 0000000000..59c7889aba--- /dev/null+++ b/.sqlx/query-8d1d17c1cc6c6582a909c1f723083efa174a50417040df24cf99e4ce7a473a6c.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, sender_id, content, created_at, deleted_at)\n SELECT id, 'channel', channel_id::text, 'macro|[email protected]', 'history',\n '2020-01-01T00:00:00.123456Z'::timestamptz, deleted_at\n FROM UNNEST($1::uuid[], $2::uuid[], $3::timestamptz[]) AS rows(id, channel_id, deleted_at)\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "UuidArray",+ "UuidArray",+ "TimestamptzArray"+ ]+ },+ "nullable": []+ },+ "hash": "8d1d17c1cc6c6582a909c1f723083efa174a50417040df24cf99e4ce7a473a6c"+}diff --git a/.sqlx/query-8d93f45713ffdb3fe4adcb601729b958ee72c830caab6de09e36758eb2a0534b.json b/.sqlx/query-8d93f45713ffdb3fe4adcb601729b958ee72c830caab6de09e36758eb2a0534b.jsonnew file mode 100644index 0000000000..454b2b5487--- /dev/null+++ b/.sqlx/query-8d93f45713ffdb3fe4adcb601729b958ee72c830caab6de09e36758eb2a0534b.json@@ -0,0 +1,18 @@+{+ "db_name": "PostgreSQL",+ "query": "UPDATE comms_messages SET content = $5\n WHERE id = $1\n AND parent_entity_type = 'channel' AND parent_entity_id = $2::text\n AND content = $3 AND content <> $5 AND import_metadata @> $4\n AND deleted_at IS NULL AND edited_at IS NULL AND updated_at = created_at\n AND NOT EXISTS (SELECT 1 FROM comms_message_threads t\n WHERE t.root_id = coalesce(comms_messages.thread_id, comms_messages.id)\n AND t.deleted_at IS NOT NULL)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Text",+ "Text",+ "Jsonb",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "8d93f45713ffdb3fe4adcb601729b958ee72c830caab6de09e36758eb2a0534b"+}diff --git a/.sqlx/query-0bbe4ec1f097ea1f596bad71d08b089e225aeb6403fe3ee5746023f2ab00e8e8.json b/.sqlx/query-917476e4149b4ba5da14be9cc082ed9c816d2ebef19181c9a111a9827361cf4d.jsonsimilarity index 57%rename from .sqlx/query-0bbe4ec1f097ea1f596bad71d08b089e225aeb6403fe3ee5746023f2ab00e8e8.jsonrename to .sqlx/query-917476e4149b4ba5da14be9cc082ed9c816d2ebef19181c9a111a9827361cf4d.jsonindex 099c195505..397c93183c 100644--- a/.sqlx/query-0bbe4ec1f097ea1f596bad71d08b089e225aeb6403fe3ee5746023f2ab00e8e8.json+++ b/.sqlx/query-917476e4149b4ba5da14be9cc082ed9c816d2ebef19181c9a111a9827361cf4d.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "SELECT thread_id FROM comms_messages WHERE channel_id = $1 AND id <> $2",+ "query": "SELECT thread_id FROM comms_messages WHERE parent_entity_type = 'channel' AND parent_entity_id = $1::uuid::text AND id <> $2","describe": {"columns": [{@@ -19,5 +19,5 @@true]},- "hash": "0bbe4ec1f097ea1f596bad71d08b089e225aeb6403fe3ee5746023f2ab00e8e8"+ "hash": "917476e4149b4ba5da14be9cc082ed9c816d2ebef19181c9a111a9827361cf4d"}diff --git a/.sqlx/query-93e1fa2f9b76113cb2c2e424d40e75ddab6d2d1b839d0deabd1998cbcefa8d03.json b/.sqlx/query-93e1fa2f9b76113cb2c2e424d40e75ddab6d2d1b839d0deabd1998cbcefa8d03.jsonnew file mode 100644index 0000000000..126309b011--- /dev/null+++ b/.sqlx/query-93e1fa2f9b76113cb2c2e424d40e75ddab6d2d1b839d0deabd1998cbcefa8d03.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO entity_properties(id, entity_id, entity_type, property_definition_id, values)\n VALUES ($1, 'message-doc-a', 'DOCUMENT', $2, $3)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Uuid",+ "Jsonb"+ ]+ },+ "nullable": []+ },+ "hash": "93e1fa2f9b76113cb2c2e424d40e75ddab6d2d1b839d0deabd1998cbcefa8d03"+}diff --git a/.sqlx/query-99086fa5c096f71daeb129691382932436bf3ce8a810b8a868655f399e0fc3f5.json b/.sqlx/query-99086fa5c096f71daeb129691382932436bf3ce8a810b8a868655f399e0fc3f5.jsondeleted file mode 100644index baa475902d..0000000000--- a/.sqlx/query-99086fa5c096f71daeb129691382932436bf3ce8a810b8a868655f399e0fc3f5.json+++ /dev/null@@ -1,22 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "UPDATE comms_message_threads s SET deleted_at = now(), updated_at = now()\n FROM migrated_comment_thread_id tm\n WHERE tm.root_id = s.root_id AND tm.document_id = ANY($1) AND s.deleted_at IS NULL\n AND NOT EXISTS (SELECT 1 FROM \"Thread\" t WHERE t.id = tm.thread_id)\n RETURNING s.root_id",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "root_id",- "type_info": "Uuid"- }- ],- "parameters": {- "Left": [- "TextArray"- ]- },- "nullable": [- false- ]- },- "hash": "99086fa5c096f71daeb129691382932436bf3ce8a810b8a868655f399e0fc3f5"-}diff --git a/.sqlx/query-992c71f6028b7a7dec1db3d7288915a53e000a00a2a183a715fe55ee4e3eac4c.json b/.sqlx/query-992c71f6028b7a7dec1db3d7288915a53e000a00a2a183a715fe55ee4e3eac4c.jsonnew file mode 100644index 0000000000..8cfa11a9c8--- /dev/null+++ b/.sqlx/query-992c71f6028b7a7dec1db3d7288915a53e000a00a2a183a715fe55ee4e3eac4c.json@@ -0,0 +1,38 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT parent_entity_id::uuid AS \"channel_id!\", id AS message_id, created_at\n FROM comms_messages\n WHERE parent_entity_type = 'channel'\n AND ($2::timestamptz IS NULL OR (created_at, id) > ($2, $3::uuid))\n AND ($4::text[] IS NULL OR parent_entity_id = ANY($4))\n AND (\n $5::bool IS NULL\n OR ($5 AND deleted_at IS NOT NULL)\n OR (NOT $5 AND deleted_at IS NULL)\n )\n ORDER BY created_at ASC, id ASC\n LIMIT $1\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "channel_id!",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "message_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 2,+ "name": "created_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Int8",+ "Timestamptz",+ "Uuid",+ "TextArray",+ "Bool"+ ]+ },+ "nullable": [+ null,+ false,+ false+ ]+ },+ "hash": "992c71f6028b7a7dec1db3d7288915a53e000a00a2a183a715fe55ee4e3eac4c"+}diff --git a/.sqlx/query-993f8bd336bc9ba45ab51edac700887af626bf74de28e395ab8a1875bc9fe9ac.json b/.sqlx/query-993f8bd336bc9ba45ab51edac700887af626bf74de28e395ab8a1875bc9fe9ac.jsondeleted file mode 100644index 44028b170c..0000000000--- a/.sqlx/query-993f8bd336bc9ba45ab51edac700887af626bf74de28e395ab8a1875bc9fe9ac.json+++ /dev/null@@ -1,65 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT\n m.id AS \"message_id!\",\n m.thread_id,\n m.sender_id AS \"sender_id!\",\n m.content AS \"content!\",\n m.created_at AS \"created_at!\",\n m.updated_at AS \"updated_at!\",\n m.deleted_at::timestamptz AS \"deleted_at?\",\n COALESCE(\n ARRAY(\n SELECT entity_type || ':' || entity_id\n FROM comms_entity_mentions em\n WHERE em.source_entity_type = 'message'\n AND em.source_entity_id = m.id::text\n ),\n '{}'::text[]\n ) AS \"mentions!\"\n FROM comms_messages m\n WHERE m.channel_id = $1\n AND m.deleted_at IS NULL\n ORDER BY m.created_at DESC\n LIMIT $2\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "message_id!",- "type_info": "Uuid"- },- {- "ordinal": 1,- "name": "thread_id",- "type_info": "Uuid"- },- {- "ordinal": 2,- "name": "sender_id!",- "type_info": "Text"- },- {- "ordinal": 3,- "name": "content!",- "type_info": "Text"- },- {- "ordinal": 4,- "name": "created_at!",- "type_info": "Timestamptz"- },- {- "ordinal": 5,- "name": "updated_at!",- "type_info": "Timestamptz"- },- {- "ordinal": 6,- "name": "deleted_at?",- "type_info": "Timestamptz"- },- {- "ordinal": 7,- "name": "mentions!",- "type_info": "TextArray"- }- ],- "parameters": {- "Left": [- "Uuid",- "Int8"- ]- },- "nullable": [- false,- true,- false,- false,- false,- false,- null,- null- ]- },- "hash": "993f8bd336bc9ba45ab51edac700887af626bf74de28e395ab8a1875bc9fe9ac"-}diff --git a/.sqlx/query-a65e5f3ae0b0506fb5204aa83ea49fa70745c460e53ec316be184a309f492754.json b/.sqlx/query-a65e5f3ae0b0506fb5204aa83ea49fa70745c460e53ec316be184a309f492754.jsondeleted file mode 100644index 102b4ff269..0000000000--- a/.sqlx/query-a65e5f3ae0b0506fb5204aa83ea49fa70745c460e53ec316be184a309f492754.json+++ /dev/null@@ -1,14 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "UPDATE comms_messages SET deleted_at = now(), updated_at = now()\n WHERE (id = ANY($1) OR thread_id = ANY($1)) AND deleted_at IS NULL",- "describe": {- "columns": [],- "parameters": {- "Left": [- "UuidArray"- ]- },- "nullable": []- },- "hash": "a65e5f3ae0b0506fb5204aa83ea49fa70745c460e53ec316be184a309f492754"-}diff --git a/.sqlx/query-a6fe5e93cc6b7ed25934216cfa647b8591213be9dc848d874b3b3f2a78226669.json b/.sqlx/query-a6fe5e93cc6b7ed25934216cfa647b8591213be9dc848d874b3b3f2a78226669.jsondeleted file mode 100644index 5dd86cd9e5..0000000000--- a/.sqlx/query-a6fe5e93cc6b7ed25934216cfa647b8591213be9dc848d874b3b3f2a78226669.json+++ /dev/null@@ -1,22 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "INSERT INTO comms_messages (\n id, parent_entity_type, parent_entity_id, thread_id, sender_id, imported_author,\n content, created_at, updated_at, edited_at, deleted_at, import_metadata, import_order\n )\n SELECT cm.message_id, 'document', t.\"documentId\",\n CASE WHEN cm.message_id = tm.root_id THEN NULL ELSE tm.root_id END,\n c.owner, c.sender, c.text,\n c.\"createdAt\" AT TIME ZONE 'UTC', c.\"updatedAt\" AT TIME ZONE 'UTC',\n CASE WHEN c.\"updatedAt\" > c.\"createdAt\" THEN c.\"updatedAt\" END,\n COALESCE(c.\"deletedAt\", t.\"deletedAt\"), c.metadata,\n row_number() OVER (PARTITION BY t.id ORDER BY c.\"order\" NULLS LAST, c.\"createdAt\", c.id)\n FROM \"Comment\" c\n JOIN \"Thread\" t ON t.id = c.\"threadId\"\n JOIN migrated_comment_id cm ON cm.comment_id = c.id\n JOIN migrated_comment_thread_id tm ON tm.thread_id = t.id\n WHERE t.\"documentId\" = ANY($1)\n ON CONFLICT (id) DO UPDATE SET\n content = EXCLUDED.content,\n updated_at = EXCLUDED.updated_at,\n edited_at = EXCLUDED.edited_at,\n deleted_at = EXCLUDED.deleted_at,\n imported_author = EXCLUDED.imported_author,\n import_metadata = EXCLUDED.import_metadata,\n import_order = EXCLUDED.import_order\n WHERE comms_messages.updated_at <= EXCLUDED.updated_at\n AND (comms_messages.content, comms_messages.updated_at, comms_messages.edited_at,\n comms_messages.deleted_at, comms_messages.imported_author,\n comms_messages.import_metadata, comms_messages.import_order)\n IS DISTINCT FROM\n (EXCLUDED.content, EXCLUDED.updated_at, EXCLUDED.edited_at, EXCLUDED.deleted_at,\n EXCLUDED.imported_author, EXCLUDED.import_metadata, EXCLUDED.import_order)\n RETURNING (xmax = 0) AS \"inserted!\"",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "inserted!",- "type_info": "Bool"- }- ],- "parameters": {- "Left": [- "TextArray"- ]- },- "nullable": [- null- ]- },- "hash": "a6fe5e93cc6b7ed25934216cfa647b8591213be9dc848d874b3b3f2a78226669"-}diff --git a/.sqlx/query-a857929264fb9bd01aff3e0fd4e0e9de760dda83189644be2326bc6f90f45936.json b/.sqlx/query-a857929264fb9bd01aff3e0fd4e0e9de760dda83189644be2326bc6f90f45936.jsonnew file mode 100644index 0000000000..a346ab521f--- /dev/null+++ b/.sqlx/query-a857929264fb9bd01aff3e0fd4e0e9de760dda83189644be2326bc6f90f45936.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO migrated_comment_thread_id (thread_id, root_id, document_id) VALUES (1, $1, 'message-doc-a')",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "a857929264fb9bd01aff3e0fd4e0e9de760dda83189644be2326bc6f90f45936"+}diff --git a/.sqlx/query-afa173e2c3daae4e2a9d27a0fd037677df9d49b06ddc95c73cad60e4fa051b1c.json b/.sqlx/query-afa173e2c3daae4e2a9d27a0fd037677df9d49b06ddc95c73cad60e4fa051b1c.jsonindex 17e436fe1b..5fca465736 100644--- a/.sqlx/query-afa173e2c3daae4e2a9d27a0fd037677df9d49b06ddc95c73cad60e4fa051b1c.json+++ b/.sqlx/query-afa173e2c3daae4e2a9d27a0fd037677df9d49b06ddc95c73cad60e4fa051b1c.json@@ -106,7 +106,7 @@false,false,false,- true,+ false,false,false,false,diff --git a/.sqlx/query-b0d50f06983bc88d5ca6e118f0e08183aa05b11cb4165977e7a68d39721a049e.json b/.sqlx/query-b0d50f06983bc88d5ca6e118f0e08183aa05b11cb4165977e7a68d39721a049e.jsonnew file mode 100644index 0000000000..a962353884--- /dev/null+++ b/.sqlx/query-b0d50f06983bc88d5ca6e118f0e08183aa05b11cb4165977e7a68d39721a049e.json@@ -0,0 +1,12 @@+{+ "db_name": "PostgreSQL",+ "query": "UPDATE \"Document\" SET \"deletedAt\" = now() WHERE id = 'message-doc-a'",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": []+ },+ "nullable": []+ },+ "hash": "b0d50f06983bc88d5ca6e118f0e08183aa05b11cb4165977e7a68d39721a049e"+}diff --git a/.sqlx/query-b2d33e673b47de4353142b3169c7aa72261006f750423b9bee516c130face1ee.json b/.sqlx/query-b2d33e673b47de4353142b3169c7aa72261006f750423b9bee516c130face1ee.jsonnew file mode 100644index 0000000000..7916c4afe5--- /dev/null+++ b/.sqlx/query-b2d33e673b47de4353142b3169c7aa72261006f750423b9bee516c130face1ee.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO \"PdfHighlightAnchor\"\n (uuid, \"documentId\", owner, page, red, green, blue, alpha, type, text, \"pageViewportWidth\", \"pageViewportHeight\")\n VALUES ($1, 'message-doc-a', $2, 1, 255, 255, 0, 0.5, 1, 'selected text', 600, 800)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "b2d33e673b47de4353142b3169c7aa72261006f750423b9bee516c130face1ee"+}diff --git a/.sqlx/query-b44f233a1a834c1ae81dbfda9c248a7b7daf2813c73521fbb8691d4f34ca28ea.json b/.sqlx/query-b44f233a1a834c1ae81dbfda9c248a7b7daf2813c73521fbb8691d4f34ca28ea.jsondeleted file mode 100644index bae7d68881..0000000000--- a/.sqlx/query-b44f233a1a834c1ae81dbfda9c248a7b7daf2813c73521fbb8691d4f34ca28ea.json+++ /dev/null@@ -1,20 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "SELECT count(*) AS \"count!\" FROM \"Thread\" t\n WHERE NOT EXISTS (SELECT 1 FROM migrated_comment_thread_id m WHERE m.thread_id = t.id)",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "count!",- "type_info": "Int8"- }- ],- "parameters": {- "Left": []- },- "nullable": [- null- ]- },- "hash": "b44f233a1a834c1ae81dbfda9c248a7b7daf2813c73521fbb8691d4f34ca28ea"-}diff --git a/.sqlx/query-b9075c63faef0a5076087f8b0c701950824da41cd5ac8049135fd9e30ecc4cfc.json b/.sqlx/query-b9075c63faef0a5076087f8b0c701950824da41cd5ac8049135fd9e30ecc4cfc.jsonnew file mode 100644index 0000000000..62ad01b577--- /dev/null+++ b/.sqlx/query-b9075c63faef0a5076087f8b0c701950824da41cd5ac8049135fd9e30ecc4cfc.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, sender_id, content, imported_author)\n VALUES ($1, 'channel', $2::uuid::text, 'macro|[email protected]', 'Historical content', $3)\n ON CONFLICT (id) DO UPDATE SET imported_author = EXCLUDED.imported_author\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Uuid",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "b9075c63faef0a5076087f8b0c701950824da41cd5ac8049135fd9e30ecc4cfc"+}diff --git a/.sqlx/query-17c9482742dc820a4aee1e1dea6ae53ff717f781135b9baa9d687e831668b124.json b/.sqlx/query-b90c462998e700aee35f0733a9b393a90ff7dfcf249e99cbaec0bd3b895b36c7.jsonsimilarity index 55%rename from .sqlx/query-17c9482742dc820a4aee1e1dea6ae53ff717f781135b9baa9d687e831668b124.jsonrename to .sqlx/query-b90c462998e700aee35f0733a9b393a90ff7dfcf249e99cbaec0bd3b895b36c7.jsonindex 2be3a17380..85195efbe6 100644--- a/.sqlx/query-17c9482742dc820a4aee1e1dea6ae53ff717f781135b9baa9d687e831668b124.json+++ b/.sqlx/query-b90c462998e700aee35f0733a9b393a90ff7dfcf249e99cbaec0bd3b895b36c7.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "SELECT t.root_id FROM comms_message_threads t\n JOIN comms_messages m ON m.id = t.root_id\n WHERE t.root_id = ANY($1) AND t.parent_entity_type = 'channel'\n AND t.parent_entity_id = $2 AND m.channel_id = $3\n AND m.thread_id IS NULL AND m.deleted_at IS NULL AND t.deleted_at IS NULL\n ORDER BY t.root_id FOR UPDATE OF t, m",+ "query": "SELECT t.root_id FROM comms_message_threads t\n JOIN comms_messages m ON m.id = t.root_id\n WHERE t.root_id = ANY($1) AND t.parent_entity_type = 'channel'\n AND t.parent_entity_id = $2\n AND m.parent_entity_type = 'channel' AND m.parent_entity_id = $2\n AND m.thread_id IS NULL AND m.deleted_at IS NULL AND t.deleted_at IS NULL\n ORDER BY t.root_id FOR UPDATE OF t, m","describe": {"columns": [{@@ -12,13 +12,12 @@"parameters": {"Left": ["UuidArray",- "Text",- "Uuid"+ "Text"]},"nullable": [false]},- "hash": "17c9482742dc820a4aee1e1dea6ae53ff717f781135b9baa9d687e831668b124"+ "hash": "b90c462998e700aee35f0733a9b393a90ff7dfcf249e99cbaec0bd3b895b36c7"}diff --git a/.sqlx/query-b90d70a8ccab3c00ac870f0ea456e1dafcbc3aac244610129c64b67d4b3c74bb.json b/.sqlx/query-b90d70a8ccab3c00ac870f0ea456e1dafcbc3aac244610129c64b67d4b3c74bb.jsondeleted file mode 100644index 9691599283..0000000000--- a/.sqlx/query-b90d70a8ccab3c00ac870f0ea456e1dafcbc3aac244610129c64b67d4b3c74bb.json+++ /dev/null@@ -1,18 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "UPDATE comms_messages SET content = $5\n WHERE id = $1 AND channel_id = $2\n AND parent_entity_type = 'channel' AND parent_entity_id = $2::text\n AND content = $3 AND content <> $5 AND import_metadata @> $4\n AND deleted_at IS NULL AND edited_at IS NULL AND updated_at = created_at\n AND NOT EXISTS (SELECT 1 FROM comms_message_threads t\n WHERE t.root_id = coalesce(comms_messages.thread_id, comms_messages.id)\n AND t.deleted_at IS NOT NULL)",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Uuid",- "Uuid",- "Text",- "Jsonb",- "Text"- ]- },- "nullable": []- },- "hash": "b90d70a8ccab3c00ac870f0ea456e1dafcbc3aac244610129c64b67d4b3c74bb"-}diff --git a/.sqlx/query-bc996dcd91b86d30e494387bacf11fc307ac363e4d1701439b0bc6ad59e29d16.json b/.sqlx/query-bc996dcd91b86d30e494387bacf11fc307ac363e4d1701439b0bc6ad59e29d16.jsondeleted file mode 100644index 05bb70f569..0000000000--- a/.sqlx/query-bc996dcd91b86d30e494387bacf11fc307ac363e4d1701439b0bc6ad59e29d16.json+++ /dev/null@@ -1,23 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "SELECT d.id FROM \"Document\" d\n WHERE ($1::text IS NULL OR d.id > $1)\n AND (EXISTS (SELECT 1 FROM \"Thread\" t WHERE t.\"documentId\" = d.id)\n OR EXISTS (SELECT 1 FROM migrated_comment_thread_id m WHERE m.document_id = d.id))\n ORDER BY d.id LIMIT $2",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "id",- "type_info": "Text"- }- ],- "parameters": {- "Left": [- "Text",- "Int8"- ]- },- "nullable": [- false- ]- },- "hash": "bc996dcd91b86d30e494387bacf11fc307ac363e4d1701439b0bc6ad59e29d16"-}diff --git a/.sqlx/query-c1b85553ac1040afcabace1af994a892161e8da09e0f05f1fb742a85a4671b9b.json b/.sqlx/query-c1b85553ac1040afcabace1af994a892161e8da09e0f05f1fb742a85a4671b9b.jsondeleted file mode 100644index 67407d942c..0000000000--- a/.sqlx/query-c1b85553ac1040afcabace1af994a892161e8da09e0f05f1fb742a85a4671b9b.json+++ /dev/null@@ -1,16 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n INSERT INTO comms_messages (id, channel_id, sender_id, content, created_at, deleted_at)\n SELECT id, channel_id, 'macro|[email protected]', 'history',\n '2020-01-01T00:00:00.123456Z'::timestamptz, deleted_at\n FROM UNNEST($1::uuid[], $2::uuid[], $3::timestamptz[]) AS rows(id, channel_id, deleted_at)\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "UuidArray",- "UuidArray",- "TimestamptzArray"- ]- },- "nullable": []- },- "hash": "c1b85553ac1040afcabace1af994a892161e8da09e0f05f1fb742a85a4671b9b"-}diff --git a/.sqlx/query-c32423d596dca1eb14bef93e22d6b2a37d52cfbf3cf650d1965df042056ad9ce.json b/.sqlx/query-c32423d596dca1eb14bef93e22d6b2a37d52cfbf3cf650d1965df042056ad9ce.jsondeleted file mode 100644index 62ee47591e..0000000000--- a/.sqlx/query-c32423d596dca1eb14bef93e22d6b2a37d52cfbf3cf650d1965df042056ad9ce.json+++ /dev/null@@ -1,24 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "SELECT count(*) AS \"count!\" FROM \"Thread\" t\n WHERE t.\"documentId\" = ANY($1) AND t.\"deletedAt\" IS NULL\n AND t.metadata->>'markId' IS NOT NULL\n AND t.metadata->>'markId' !~ $2\n AND t.metadata->>'markId' NOT LIKE $3\n AND NOT EXISTS (SELECT 1 FROM \"PdfPlaceableCommentAnchor\" p WHERE p.\"threadId\" = t.id AND NOT p.\"wasDeleted\")\n AND NOT EXISTS (SELECT 1 FROM \"PdfHighlightAnchor\" h\n WHERE h.\"threadId\" = t.id AND h.\"deletedAt\" IS NULL)",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "count!",- "type_info": "Int8"- }- ],- "parameters": {- "Left": [- "TextArray",- "Text",- "Text"- ]- },- "nullable": [- null- ]- },- "hash": "c32423d596dca1eb14bef93e22d6b2a37d52cfbf3cf650d1965df042056ad9ce"-}diff --git a/.sqlx/query-c5e01d489c1b1316ab38953bf81a99b5d98b5f73d5a4ce47c5499d9bf958ce6e.json b/.sqlx/query-c5e01d489c1b1316ab38953bf81a99b5d98b5f73d5a4ce47c5499d9bf958ce6e.jsondeleted file mode 100644index e0b1610c7f..0000000000--- a/.sqlx/query-c5e01d489c1b1316ab38953bf81a99b5d98b5f73d5a4ce47c5499d9bf958ce6e.json+++ /dev/null@@ -1,16 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "INSERT INTO comms_message_threads (\n root_id, parent_entity_type, parent_entity_id, user_id, resolved, anchor,\n import_metadata, created_at, updated_at, deleted_at\n )\n SELECT tm.root_id, 'document', t.\"documentId\", t.owner, t.resolved,\n CASE\n WHEN pa.uuid IS NOT NULL\n THEN jsonb_build_object('type', 'pdf_placeable', 'anchor_id', pa.uuid)\n WHEN ph.uuid IS NOT NULL\n THEN jsonb_build_object('type', 'pdf_highlight', 'anchor_id', ph.uuid)\n WHEN t.metadata->>'markId' ~ $3\n THEN jsonb_build_object('type', 'markdown', 'mark_id', (t.metadata->>'markId')::uuid)\n END,\n t.metadata, t.\"createdAt\" AT TIME ZONE 'UTC', t.\"updatedAt\" AT TIME ZONE 'UTC',\n CASE\n WHEN t.\"deletedAt\" IS NOT NULL THEN t.\"deletedAt\" AT TIME ZONE 'UTC'\n WHEN NOT EXISTS (SELECT 1 FROM \"Comment\" c WHERE c.\"threadId\" = t.id)\n THEN t.\"updatedAt\" AT TIME ZONE 'UTC'\n END\n FROM \"Thread\" t\n JOIN migrated_comment_thread_id tm ON tm.thread_id = t.id\n LEFT JOIN LATERAL (\n SELECT a.uuid FROM \"PdfPlaceableCommentAnchor\" a\n WHERE a.\"threadId\" = t.id AND NOT a.\"wasDeleted\"\n ORDER BY a.uuid LIMIT 1\n ) pa ON true\n LEFT JOIN LATERAL (\n SELECT a.uuid FROM \"PdfHighlightAnchor\" a\n WHERE a.\"threadId\" = t.id AND a.\"deletedAt\" IS NULL\n ORDER BY a.uuid LIMIT 1\n ) ph ON true\n WHERE t.\"documentId\" = ANY($1)\n ON CONFLICT (root_id) DO UPDATE SET\n user_id = EXCLUDED.user_id,\n resolved = EXCLUDED.resolved,\n anchor = EXCLUDED.anchor,\n import_metadata = EXCLUDED.import_metadata,\n created_at = EXCLUDED.created_at,\n updated_at = EXCLUDED.updated_at,\n deleted_at = EXCLUDED.deleted_at\n WHERE (comms_message_threads.root_id = ANY($2)\n OR comms_message_threads.updated_at <= EXCLUDED.updated_at)\n AND (comms_message_threads.user_id, comms_message_threads.resolved,\n comms_message_threads.anchor, comms_message_threads.import_metadata,\n comms_message_threads.created_at, comms_message_threads.updated_at,\n comms_message_threads.deleted_at)\n IS DISTINCT FROM\n (EXCLUDED.user_id, EXCLUDED.resolved, EXCLUDED.anchor, EXCLUDED.import_metadata,\n EXCLUDED.created_at, EXCLUDED.updated_at, EXCLUDED.deleted_at)",- "describe": {- "columns": [],- "parameters": {- "Left": [- "TextArray",- "UuidArray",- "Text"- ]- },- "nullable": []- },- "hash": "c5e01d489c1b1316ab38953bf81a99b5d98b5f73d5a4ce47c5499d9bf958ce6e"-}diff --git a/.sqlx/query-cad533fdf9dff2cb374956bb810b6672179e9e5afc7bc6e059b3ba14f4cd0dd0.json b/.sqlx/query-cad533fdf9dff2cb374956bb810b6672179e9e5afc7bc6e059b3ba14f4cd0dd0.jsondeleted file mode 100644index 63e2c7761a..0000000000--- a/.sqlx/query-cad533fdf9dff2cb374956bb810b6672179e9e5afc7bc6e059b3ba14f4cd0dd0.json+++ /dev/null@@ -1,34 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "SELECT t.id, t.\"documentId\" AS document_id, first.message_id AS \"root_id?\"\n FROM \"Thread\" t\n LEFT JOIN LATERAL (\n SELECT m.message_id FROM \"Comment\" c\n JOIN migrated_comment_id m ON m.comment_id = c.id\n WHERE c.\"threadId\" = t.id\n ORDER BY c.\"order\" NULLS LAST, c.\"createdAt\", c.id LIMIT 1\n ) first ON true\n WHERE t.\"documentId\" = ANY($1)\n AND NOT EXISTS (SELECT 1 FROM migrated_comment_thread_id m WHERE m.thread_id = t.id)\n ORDER BY t.id",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "id",- "type_info": "Int8"- },- {- "ordinal": 1,- "name": "document_id",- "type_info": "Text"- },- {- "ordinal": 2,- "name": "root_id?",- "type_info": "Uuid"- }- ],- "parameters": {- "Left": [- "TextArray"- ]- },- "nullable": [- false,- false,- false- ]- },- "hash": "cad533fdf9dff2cb374956bb810b6672179e9e5afc7bc6e059b3ba14f4cd0dd0"-}diff --git a/.sqlx/query-cb451b00fb7ced38c8fc2dd05631d9963cb51453ed57b244c83523ac52d7397e.json b/.sqlx/query-cb451b00fb7ced38c8fc2dd05631d9963cb51453ed57b244c83523ac52d7397e.jsondeleted file mode 100644index 42107d4549..0000000000--- a/.sqlx/query-cb451b00fb7ced38c8fc2dd05631d9963cb51453ed57b244c83523ac52d7397e.json+++ /dev/null@@ -1,64 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT id, message_id, channel_id AS \"channel_id!\", entity_type, entity_id, width, height, created_at\n FROM comms_attachments\n WHERE message_id = $1\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "id",- "type_info": "Uuid"- },- {- "ordinal": 1,- "name": "message_id",- "type_info": "Uuid"- },- {- "ordinal": 2,- "name": "channel_id!",- "type_info": "Uuid"- },- {- "ordinal": 3,- "name": "entity_type",- "type_info": "Varchar"- },- {- "ordinal": 4,- "name": "entity_id",- "type_info": "Varchar"- },- {- "ordinal": 5,- "name": "width",- "type_info": "Int4"- },- {- "ordinal": 6,- "name": "height",- "type_info": "Int4"- },- {- "ordinal": 7,- "name": "created_at",- "type_info": "Timestamptz"- }- ],- "parameters": {- "Left": [- "Uuid"- ]- },- "nullable": [- false,- false,- true,- false,- false,- true,- true,- false- ]- },- "hash": "cb451b00fb7ced38c8fc2dd05631d9963cb51453ed57b244c83523ac52d7397e"-}diff --git a/.sqlx/query-cba527a9ad52c0021a4efbc387beb723e26e3f62d07340217a5875493306acae.json b/.sqlx/query-cba527a9ad52c0021a4efbc387beb723e26e3f62d07340217a5875493306acae.jsondeleted file mode 100644index 81fba78f56..0000000000--- a/.sqlx/query-cba527a9ad52c0021a4efbc387beb723e26e3f62d07340217a5875493306acae.json+++ /dev/null@@ -1,25 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "INSERT INTO comms_messages\n (id, channel_id, parent_entity_type, parent_entity_id, thread_id,\n sender_id, imported_author, content, created_at, updated_at, edited_at,\n import_metadata, import_order)\n SELECT input.id, $1, 'channel', $2, input.thread_id, input.sender_id,\n input.imported_author, input.content, input.created_at, input.updated_at,\n input.edited_at, input.import_metadata, input.import_order\n FROM UNNEST($3::uuid[], $4::uuid[], $5::text[], $6::text[], $7::text[],\n $8::timestamptz[], $9::timestamptz[], $10::timestamp[], $11::jsonb[], $12::bigint[])\n AS input(id, thread_id, sender_id, imported_author, content, created_at,\n updated_at, edited_at, import_metadata, import_order)\n ON CONFLICT (id) DO NOTHING",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Uuid",- "Text",- "UuidArray",- "UuidArray",- "TextArray",- "TextArray",- "TextArray",- "TimestamptzArray",- "TimestamptzArray",- "TimestampArray",- "JsonbArray",- "Int8Array"- ]- },- "nullable": []- },- "hash": "cba527a9ad52c0021a4efbc387beb723e26e3f62d07340217a5875493306acae"-}diff --git a/.sqlx/query-72c6260a4c0cdfb61d8273040c5d79d6c615c3e1dcbb4bbfabc9a61e2cead4cd.json b/.sqlx/query-cdce8a94205aa3154b6a1205180d23697c8707004cc794f6f92412938df3cc34.jsonsimilarity index 51%rename from .sqlx/query-72c6260a4c0cdfb61d8273040c5d79d6c615c3e1dcbb4bbfabc9a61e2cead4cd.jsonrename to .sqlx/query-cdce8a94205aa3154b6a1205180d23697c8707004cc794f6f92412938df3cc34.jsonindex 57c0fe1328..fe0d7a7d02 100644--- a/.sqlx/query-72c6260a4c0cdfb61d8273040c5d79d6c615c3e1dcbb4bbfabc9a61e2cead4cd.json+++ b/.sqlx/query-cdce8a94205aa3154b6a1205180d23697c8707004cc794f6f92412938df3cc34.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "SELECT count(*) AS \"count!\" FROM \"Comment\" c\n WHERE NOT EXISTS (SELECT 1 FROM migrated_comment_id m WHERE m.comment_id = c.id)",+ "query": "SELECT count(*) AS \"count!\" FROM comms_message_threads\n WHERE parent_entity_type = 'document' AND parent_entity_id = 'message-doc-a'","describe": {"columns": [{@@ -16,5 +16,5 @@null]},- "hash": "72c6260a4c0cdfb61d8273040c5d79d6c615c3e1dcbb4bbfabc9a61e2cead4cd"+ "hash": "cdce8a94205aa3154b6a1205180d23697c8707004cc794f6f92412938df3cc34"}diff --git a/.sqlx/query-d801f390a1cb9d8e8d6c49da272a054a99561c94177134a23a31bf9149571b0a.json b/.sqlx/query-d801f390a1cb9d8e8d6c49da272a054a99561c94177134a23a31bf9149571b0a.jsondeleted file mode 100644index 293f2e99d1..0000000000--- a/.sqlx/query-d801f390a1cb9d8e8d6c49da272a054a99561c94177134a23a31bf9149571b0a.json+++ /dev/null@@ -1,12 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "INSERT INTO comms_message_threads (\n root_id, parent_entity_type, parent_entity_id, user_id, resolved,\n import_metadata, created_at, updated_at, deleted_at\n )\n SELECT root.id,\n CASE WHEN t.contact_id IS NULL THEN 'crm_company' ELSE 'crm_contact' END,\n COALESCE(t.contact_id, t.company_id)::text,\n t.owner, t.resolved,\n jsonb_build_object('source', 'crm_thread', 'legacy_thread_id', t.id),\n t.created_at, t.updated_at, t.deleted_at\n FROM crm_thread t\n CROSS JOIN LATERAL (\n SELECT c.id FROM crm_comment c\n WHERE c.thread_id = t.id\n ORDER BY c.created_at, c.id\n LIMIT 1\n ) root\n ON CONFLICT (root_id) DO UPDATE SET\n user_id = EXCLUDED.user_id,\n resolved = EXCLUDED.resolved,\n import_metadata = EXCLUDED.import_metadata,\n created_at = EXCLUDED.created_at,\n updated_at = EXCLUDED.updated_at,\n deleted_at = EXCLUDED.deleted_at\n WHERE (comms_message_threads.import_metadata IS NULL\n OR (comms_message_threads.import_metadata->>'source' = 'crm_thread'\n AND comms_message_threads.updated_at <= EXCLUDED.updated_at))\n AND (comms_message_threads.import_metadata,\n comms_message_threads.user_id, comms_message_threads.resolved,\n comms_message_threads.created_at, comms_message_threads.updated_at,\n comms_message_threads.deleted_at)\n IS DISTINCT FROM\n (EXCLUDED.import_metadata, EXCLUDED.user_id, EXCLUDED.resolved,\n EXCLUDED.created_at, EXCLUDED.updated_at, EXCLUDED.deleted_at)",- "describe": {- "columns": [],- "parameters": {- "Left": []- },- "nullable": []- },- "hash": "d801f390a1cb9d8e8d6c49da272a054a99561c94177134a23a31bf9149571b0a"-}diff --git a/.sqlx/query-da3817408f1d38bf8ed5eda805e205068b651e3f93655552fd942ad5b2e19aef.json b/.sqlx/query-da3817408f1d38bf8ed5eda805e205068b651e3f93655552fd942ad5b2e19aef.jsonnew file mode 100644index 0000000000..5e1ea242e4--- /dev/null+++ b/.sqlx/query-da3817408f1d38bf8ed5eda805e205068b651e3f93655552fd942ad5b2e19aef.json@@ -0,0 +1,17 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO notification(id, notification_event_type, event_item_type, event_item_id, service_sender, metadata) VALUES ($1, 'test', $2, $3, 'test', $4)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Text",+ "Text",+ "Jsonb"+ ]+ },+ "nullable": []+ },+ "hash": "da3817408f1d38bf8ed5eda805e205068b651e3f93655552fd942ad5b2e19aef"+}diff --git a/.sqlx/query-dd70275d1f21a505bddab63286b1e7a031aaa7d0be090d1c9e74430de679f478.json b/.sqlx/query-dd70275d1f21a505bddab63286b1e7a031aaa7d0be090d1c9e74430de679f478.jsonnew file mode 100644index 0000000000..caf2cf07d7--- /dev/null+++ b/.sqlx/query-dd70275d1f21a505bddab63286b1e7a031aaa7d0be090d1c9e74430de679f478.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "UPDATE comms_messages SET deleted_at = NULL, content = 'edited' WHERE id = $1",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "dd70275d1f21a505bddab63286b1e7a031aaa7d0be090d1c9e74430de679f478"+}diff --git a/.sqlx/query-ddd55650f9c7be80c0b4d815a4512ec66e0cbd8ca43ae14b5c8337f83be81ee5.json b/.sqlx/query-ddd55650f9c7be80c0b4d815a4512ec66e0cbd8ca43ae14b5c8337f83be81ee5.jsonnew file mode 100644index 0000000000..bbf90bd407--- /dev/null+++ b/.sqlx/query-ddd55650f9c7be80c0b4d815a4512ec66e0cbd8ca43ae14b5c8337f83be81ee5.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO crm_contacts(id, company_id, email, first_interaction, last_interaction) VALUES ($1, $2, '[email protected]', now(), now())",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "ddd55650f9c7be80c0b4d815a4512ec66e0cbd8ca43ae14b5c8337f83be81ee5"+}diff --git a/.sqlx/query-e354f41fcd88a03d4522e7819087d30035e5395628c815eee057693b49f44509.json b/.sqlx/query-e354f41fcd88a03d4522e7819087d30035e5395628c815eee057693b49f44509.jsonnew file mode 100644index 0000000000..fcf06dacdd--- /dev/null+++ b/.sqlx/query-e354f41fcd88a03d4522e7819087d30035e5395628c815eee057693b49f44509.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "UPDATE comms_messages SET deleted_at = now() WHERE parent_entity_type = 'channel' AND parent_entity_id = $1::uuid::text",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "e354f41fcd88a03d4522e7819087d30035e5395628c815eee057693b49f44509"+}diff --git a/.sqlx/query-e44bd90bfad892091b1ca8d4e1f0302d470e21754b9c26bcb502ab46ddf1f449.json b/.sqlx/query-e44bd90bfad892091b1ca8d4e1f0302d470e21754b9c26bcb502ab46ddf1f449.jsonnew file mode 100644index 0000000000..8921a19a69--- /dev/null+++ b/.sqlx/query-e44bd90bfad892091b1ca8d4e1f0302d470e21754b9c26bcb502ab46ddf1f449.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO migrated_comment_id (comment_id, message_id, document_id) VALUES (12, $1, 'message-doc-a')",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "e44bd90bfad892091b1ca8d4e1f0302d470e21754b9c26bcb502ab46ddf1f449"+}diff --git a/.sqlx/query-e558ac1741835623b34dea5518c43f58913909a51b4f5f6ef057f8cccdd8eab7.json b/.sqlx/query-e558ac1741835623b34dea5518c43f58913909a51b4f5f6ef057f8cccdd8eab7.jsondeleted file mode 100644index c831efb930..0000000000--- a/.sqlx/query-e558ac1741835623b34dea5518c43f58913909a51b4f5f6ef057f8cccdd8eab7.json+++ /dev/null@@ -1,28 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "SELECT c.id, t.\"documentId\" AS document_id FROM \"Comment\" c\n JOIN \"Thread\" t ON t.id = c.\"threadId\"\n WHERE t.\"documentId\" = ANY($1)\n AND NOT EXISTS (SELECT 1 FROM migrated_comment_id m WHERE m.comment_id = c.id)\n ORDER BY c.id",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "id",- "type_info": "Int8"- },- {- "ordinal": 1,- "name": "document_id",- "type_info": "Text"- }- ],- "parameters": {- "Left": [- "TextArray"- ]- },- "nullable": [- false,- false- ]- },- "hash": "e558ac1741835623b34dea5518c43f58913909a51b4f5f6ef057f8cccdd8eab7"-}diff --git a/.sqlx/query-e70125689debc2ca94cbea8959d80e38d7d0e9b31c0c40843d6a70ed7acd312a.json b/.sqlx/query-e70125689debc2ca94cbea8959d80e38d7d0e9b31c0c40843d6a70ed7acd312a.jsonnew file mode 100644index 0000000000..1829e389e8--- /dev/null+++ b/.sqlx/query-e70125689debc2ca94cbea8959d80e38d7d0e9b31c0c40843d6a70ed7acd312a.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO macro_user (id, username, email, stripe_customer_id)\n VALUES ($1, 'message-test', '[email protected]', 'message-test')",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "e70125689debc2ca94cbea8959d80e38d7d0e9b31c0c40843d6a70ed7acd312a"+}diff --git a/.sqlx/query-e9f33d6edca87d88c7403b13d19fe3aa835c20b17993e9e8cdabfe20ed5efa34.json b/.sqlx/query-e9f33d6edca87d88c7403b13d19fe3aa835c20b17993e9e8cdabfe20ed5efa34.jsondeleted file mode 100644index d4071cbc7a..0000000000--- a/.sqlx/query-e9f33d6edca87d88c7403b13d19fe3aa835c20b17993e9e8cdabfe20ed5efa34.json+++ /dev/null@@ -1,14 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "UPDATE comms_messages m SET deleted_at = now(), updated_at = now()\n FROM migrated_comment_id cm\n WHERE cm.message_id = m.id AND cm.document_id = ANY($1) AND m.deleted_at IS NULL\n AND NOT EXISTS (SELECT 1 FROM \"Comment\" c WHERE c.id = cm.comment_id)",- "describe": {- "columns": [],- "parameters": {- "Left": [- "TextArray"- ]- },- "nullable": []- },- "hash": "e9f33d6edca87d88c7403b13d19fe3aa835c20b17993e9e8cdabfe20ed5efa34"-}diff --git a/.sqlx/query-12eba8d33668ec91d11bf8787de9c90499802d0ebe46f7a5b2e5d0f958c12364.json b/.sqlx/query-ee89350343f16092d0e364414f2b182eb2c2a8e4823ffbb20f8312c268a4a7dc.jsonsimilarity index 50%rename from .sqlx/query-12eba8d33668ec91d11bf8787de9c90499802d0ebe46f7a5b2e5d0f958c12364.jsonrename to .sqlx/query-ee89350343f16092d0e364414f2b182eb2c2a8e4823ffbb20f8312c268a4a7dc.jsonindex c16520dedf..94a46342fa 100644--- a/.sqlx/query-12eba8d33668ec91d11bf8787de9c90499802d0ebe46f7a5b2e5d0f958c12364.json+++ b/.sqlx/query-ee89350343f16092d0e364414f2b182eb2c2a8e4823ffbb20f8312c268a4a7dc.json@@ -1,22 +1,22 @@{"db_name": "PostgreSQL",- "query": "SELECT comment_id, message_id FROM migrated_comment_id WHERE document_id = $1",+ "query": "SELECT root_id, \"xPct\" AS x FROM \"PdfPlaceableCommentAnchor\" WHERE uuid = $1","describe": {"columns": [{"ordinal": 0,- "name": "comment_id",- "type_info": "Int8"+ "name": "root_id",+ "type_info": "Uuid"},{"ordinal": 1,- "name": "message_id",- "type_info": "Uuid"+ "name": "x",+ "type_info": "Float8"}],"parameters": {"Left": [- "Text"+ "Uuid"]},"nullable": [@@ -24,5 +24,5 @@false]},- "hash": "12eba8d33668ec91d11bf8787de9c90499802d0ebe46f7a5b2e5d0f958c12364"+ "hash": "ee89350343f16092d0e364414f2b182eb2c2a8e4823ffbb20f8312c268a4a7dc"}diff --git a/.sqlx/query-f3f25971844627d5ff05aaeed82062ff9a5e824988baaba7e6da332485a62d2e.json b/.sqlx/query-f3f25971844627d5ff05aaeed82062ff9a5e824988baaba7e6da332485a62d2e.jsonnew file mode 100644index 0000000000..d23bdea5c1--- /dev/null+++ b/.sqlx/query-f3f25971844627d5ff05aaeed82062ff9a5e824988baaba7e6da332485a62d2e.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO \"Document\" (id, name, owner, \"fileType\") VALUES ($1, $1, $2, 'md')",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "f3f25971844627d5ff05aaeed82062ff9a5e824988baaba7e6da332485a62d2e"+}diff --git a/.sqlx/query-f4508457e87ff8fe3702c661f4c5a11eadf7d30b7be63741ca4aad8ee2c2b8c9.json b/.sqlx/query-f4508457e87ff8fe3702c661f4c5a11eadf7d30b7be63741ca4aad8ee2c2b8c9.jsondeleted file mode 100644index b459b2ee13..0000000000--- a/.sqlx/query-f4508457e87ff8fe3702c661f4c5a11eadf7d30b7be63741ca4aad8ee2c2b8c9.json+++ /dev/null@@ -1,14 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "UPDATE comms_messages SET deleted_at = now() WHERE channel_id = $1",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Uuid"- ]- },- "nullable": []- },- "hash": "f4508457e87ff8fe3702c661f4c5a11eadf7d30b7be63741ca4aad8ee2c2b8c9"-}diff --git a/.sqlx/query-f72a2833ed5fedef8253772b6f583426895b080a6de4394c94dba28e04578747.json b/.sqlx/query-f72a2833ed5fedef8253772b6f583426895b080a6de4394c94dba28e04578747.jsonnew file mode 100644index 0000000000..e2b442ac7b--- /dev/null+++ b/.sqlx/query-f72a2833ed5fedef8253772b6f583426895b080a6de4394c94dba28e04578747.json@@ -0,0 +1,17 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, sender_id, content) VALUES ($1, 'channel', $2::uuid::text, $3, $4)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Uuid",+ "Text",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "f72a2833ed5fedef8253772b6f583426895b080a6de4394c94dba28e04578747"+}diff --git a/.sqlx/query-f77d0dbbbc6bc89b676be55a9939ffbd908da49d228156dbed63c097fd25d39b.json b/.sqlx/query-f77d0dbbbc6bc89b676be55a9939ffbd908da49d228156dbed63c097fd25d39b.jsondeleted file mode 100644index 9433c7fcc7..0000000000--- a/.sqlx/query-f77d0dbbbc6bc89b676be55a9939ffbd908da49d228156dbed63c097fd25d39b.json+++ /dev/null@@ -1,101 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT\n c.id as \"channel_id\",\n c.name as \"name\",\n c.channel_type AS \"channel_type: ChannelType\",\n c.org_id as \"org_id\",\n m.id as \"message_id\",\n m.thread_id as \"thread_id\",\n m.sender_id as \"sender_id\",\n m.imported_author,\n m.content as \"content\",\n m.created_at as \"created_at\",\n m.updated_at as \"updated_at\",\n m.deleted_at::timestamptz as \"deleted_at\"\n FROM\n comms_messages m\n JOIN\n comms_channels c on c.\"id\" = m.\"channel_id\"\n WHERE\n m.id = $1\n AND c.id = $2\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "channel_id",- "type_info": "Uuid"- },- {- "ordinal": 1,- "name": "name",- "type_info": "Varchar"- },- {- "ordinal": 2,- "name": "channel_type: ChannelType",- "type_info": {- "Custom": {- "name": "comms_channel_type",- "kind": {- "Enum": [- "public",- "private",- "direct_message",- "team"- ]- }- }- }- },- {- "ordinal": 3,- "name": "org_id",- "type_info": "Int8"- },- {- "ordinal": 4,- "name": "message_id",- "type_info": "Uuid"- },- {- "ordinal": 5,- "name": "thread_id",- "type_info": "Uuid"- },- {- "ordinal": 6,- "name": "sender_id",- "type_info": "Text"- },- {- "ordinal": 7,- "name": "imported_author",- "type_info": "Text"- },- {- "ordinal": 8,- "name": "content",- "type_info": "Text"- },- {- "ordinal": 9,- "name": "created_at",- "type_info": "Timestamptz"- },- {- "ordinal": 10,- "name": "updated_at",- "type_info": "Timestamptz"- },- {- "ordinal": 11,- "name": "deleted_at",- "type_info": "Timestamptz"- }- ],- "parameters": {- "Left": [- "Uuid",- "Uuid"- ]- },- "nullable": [- false,- true,- false,- true,- false,- true,- false,- true,- false,- false,- false,- null- ]- },- "hash": "f77d0dbbbc6bc89b676be55a9939ffbd908da49d228156dbed63c097fd25d39b"-}diff --git a/.sqlx/query-f9a5d3c291386c430cf997762a6bca3730901a18d73fe8c5ac86c3d9aff05ae6.json b/.sqlx/query-f9a5d3c291386c430cf997762a6bca3730901a18d73fe8c5ac86c3d9aff05ae6.jsonnew file mode 100644index 0000000000..083edc04c2--- /dev/null+++ b/.sqlx/query-f9a5d3c291386c430cf997762a6bca3730901a18d73fe8c5ac86c3d9aff05ae6.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "DELETE FROM comms_channels WHERE id = $1",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "f9a5d3c291386c430cf997762a6bca3730901a18d73fe8c5ac86c3d9aff05ae6"+}diff --git a/.sqlx/query-fe76c5e489b1ccab4b37fbe0b479d7c93335334b2e934f7bad97904c9696372e.json b/.sqlx/query-fe76c5e489b1ccab4b37fbe0b479d7c93335334b2e934f7bad97904c9696372e.jsonnew file mode 100644index 0000000000..5c3e92099e--- /dev/null+++ b/.sqlx/query-fe76c5e489b1ccab4b37fbe0b479d7c93335334b2e934f7bad97904c9696372e.json@@ -0,0 +1,12 @@+{+ "db_name": "PostgreSQL",+ "query": "DROP TRIGGER trg_initialize_comms_message_thread ON comms_messages",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": []+ },+ "nullable": []+ },+ "hash": "fe76c5e489b1ccab4b37fbe0b479d7c93335334b2e934f7bad97904c9696372e"+}diff --git a/.sqlx/query-ff4f9706948ceb43f91a086786fc478181bf3cd54dbcef505e9407a4350d0538.json b/.sqlx/query-ff4f9706948ceb43f91a086786fc478181bf3cd54dbcef505e9407a4350d0538.jsondeleted file mode 100644index adf271abfc..0000000000--- a/.sqlx/query-ff4f9706948ceb43f91a086786fc478181bf3cd54dbcef505e9407a4350d0538.json+++ /dev/null@@ -1,16 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n INSERT INTO comms_messages (id, channel_id, sender_id, content, imported_author)\n VALUES ($1, $2, 'macro|[email protected]', 'Historical content', $3)\n ON CONFLICT (id) DO UPDATE SET imported_author = EXCLUDED.imported_author\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Uuid",- "Uuid",- "Text"- ]- },- "nullable": []- },- "hash": "ff4f9706948ceb43f91a086786fc478181bf3cd54dbcef505e9407a4350d0538"-}diff --git a/Cargo.lock b/Cargo.lockindex 79d82e3a3e..a2c137e7da 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -11312,17 +11312,8 @@ dependencies = [name = "macro_db_migrator"version = "0.1.0"dependencies = [- "chrono",- "clap",- "database_env_vars",- "macro_entrypoint",- "macro_env",- "macro_uuid",- "rootcause",- "serde_json","sqlx","tokio",- "tracing","workspace-hack",]diff --git a/crates/comms_db_client/src/messages/get_channel_message/test.rs b/crates/comms_db_client/src/messages/get_channel_message/test.rsindex fd55ebf249..13814c2567 100644--- a/crates/comms_db_client/src/messages/get_channel_message/test.rs+++ b/crates/comms_db_client/src/messages/get_channel_message/test.rs@@ -11,8 +11,8 @@ async fn search_projection_preserves_author_and_sender(pool: sqlx::PgPool) -> anfor author in [Some("unique-archive-bot"), None] {sqlx::query!(r#"- INSERT INTO comms_messages (id, channel_id, sender_id, content, imported_author)- VALUES ($1, $2, 'macro|[email protected]', 'Historical content', $3)+ INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, sender_id, content, imported_author)+ VALUES ($1, 'channel', $2::uuid::text, 'macro|[email protected]', 'Historical content', $3)ON CONFLICT (id) DO UPDATE SET imported_author = EXCLUDED.imported_author"#,message_id,diff --git a/crates/comms_db_client/src/messages/get_messages.rs b/crates/comms_db_client/src/messages/get_messages.rsindex 23f41cb102..5fb0879720 100644--- a/crates/comms_db_client/src/messages/get_messages.rs+++ b/crates/comms_db_client/src/messages/get_messages.rs@@ -29,14 +29,15 @@ pub async fn get_channel_messages_for_search_backfill(only_deleted: Option<bool>,) -> Result<Vec<ChannelMessageBackfillRow>> {let (created_at, message_id) = cursor.unzip();+ let channel_ids = channel_ids.map(|ids| ids.iter().map(Uuid::to_string).collect::<Vec<_>>());sqlx::query_as!(ChannelMessageBackfillRow,r#"- SELECT channel_id AS "channel_id!", id AS message_id, created_at+ SELECT parent_entity_id::uuid AS "channel_id!", id AS message_id, created_atFROM comms_messages- WHERE channel_id IS NOT NULL+ WHERE parent_entity_type = 'channel'AND ($2::timestamptz IS NULL OR (created_at, id) > ($2, $3::uuid))- AND ($4::uuid[] IS NULL OR channel_id = ANY($4))+ AND ($4::text[] IS NULL OR parent_entity_id = ANY($4))AND ($5::bool IS NULLOR ($5 AND deleted_at IS NOT NULL)@@ -48,7 +49,7 @@ pub async fn get_channel_messages_for_search_backfill(limit,created_at,message_id,- channel_ids,+ channel_ids.as_deref(),only_deleted,).fetch_all(db)diff --git a/crates/comms_db_client/src/messages/get_messages/test.rs b/crates/comms_db_client/src/messages/get_messages/test.rsindex 0ba6da9f71..c9fc1418af 100644--- a/crates/comms_db_client/src/messages/get_messages/test.rs+++ b/crates/comms_db_client/src/messages/get_messages/test.rs@@ -9,8 +9,8 @@ async fn seed(pool: &Pool<Postgres>) -> Result<Vec<Uuid>> {ids.sort_unstable();sqlx::query!(r#"- INSERT INTO comms_messages (id, channel_id, sender_id, content, created_at, deleted_at)- SELECT id, channel_id, 'macro|[email protected]', 'history',+ INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, sender_id, content, created_at, deleted_at)+ SELECT id, 'channel', channel_id::text, 'macro|[email protected]', 'history','2020-01-01T00:00:00.123456Z'::timestamptz, deleted_atFROM UNNEST($1::uuid[], $2::uuid[], $3::timestamptz[]) AS rows(id, channel_id, deleted_at)"#,diff --git a/crates/comms_db_client/tests/messages_parent_schema.rs b/crates/comms_db_client/tests/messages_parent_schema.rsindex 2f3190d70b..68664fca04 100644--- a/crates/comms_db_client/tests/messages_parent_schema.rs+++ b/crates/comms_db_client/tests/messages_parent_schema.rs@@ -13,14 +13,12 @@ const OTHER_DOC: &str = "doc-2";#[derive(Debug, PartialEq, sqlx::FromRow)]struct ParentColumns {- channel_id: Option<Uuid>,parent_entity_type: String,parent_entity_id: String,}fn channel_parent(channel_id: Uuid) -> ParentColumns {ParentColumns {- channel_id: Some(channel_id),parent_entity_type: "channel".to_owned(),parent_entity_id: channel_id.to_string(),}@@ -28,7 +26,7 @@ fn channel_parent(channel_id: Uuid) -> ParentColumns {async fn parent_columns(pool: &Pool<Postgres>, id: Uuid) -> anyhow::Result<ParentColumns> {Ok(sqlx::query_as(- "SELECT channel_id, parent_entity_type, parent_entity_id FROM comms_messages WHERE id = $1",+ "SELECT parent_entity_type, parent_entity_id FROM comms_messages WHERE id = $1",).bind(id).fetch_one(pool)@@ -47,10 +45,10 @@ async fn thread_rows(pool: &Pool<Postgres>, root_id: Uuid) -> anyhow::Result<i64async fn insert_channel_root(pool: &Pool<Postgres>, channel_id: Uuid) -> anyhow::Result<Uuid> {let id = macro_uuid::generate_uuid_v7();sqlx::query(- "INSERT INTO comms_messages (id, channel_id, sender_id, content) VALUES ($1, $2, $3, 'root')",+ "INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, sender_id, content) VALUES ($1, 'channel', $2, $3, 'root')",).bind(id)- .bind(channel_id)+ .bind(channel_id.to_string()).bind(USER1).execute(pool).await?;@@ -92,17 +90,11 @@ fn constraint_name(err: &sqlx::Error) -> Option<&str> {err.as_database_error().and_then(|e| e.constraint())}-fn error_code(err: &sqlx::Error) -> Option<String> {- err.as_database_error()- .and_then(|e| e.code())- .map(|code| code.into_owned())-}-#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS",fixtures(path = "../fixtures", scripts("channels")))]-async fn old_style_channel_insert_gets_parent_columns_and_thread_row(+async fn channel_insert_gets_parent_columns_and_thread_row(pool: Pool<Postgres>,) -> anyhow::Result<()> {const _: &sqlx::migrate::Migrator = &MACRO_DB_MIGRATIONS; // Dummy reference for IDE@@ -135,7 +127,7 @@ async fn old_style_channel_insert_gets_parent_columns_and_thread_row(migrator = "MACRO_DB_MIGRATIONS",fixtures(path = "../fixtures", scripts("channels")))]-async fn parent_only_channel_insert_gets_channel_id(pool: Pool<Postgres>) -> anyhow::Result<()> {+async fn parent_only_channel_insert_gets_thread_row(pool: Pool<Postgres>) -> anyhow::Result<()> {let id = macro_uuid::generate_uuid_v7();sqlx::query("INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, sender_id, content)@@ -161,7 +153,6 @@ async fn document_root_without_channel_inserts_and_gets_thread_row(assert_eq!(parent_columns(&pool, root_id).await?,ParentColumns {- channel_id: None,parent_entity_type: "document".to_owned(),parent_entity_id: DOC.to_owned(),}@@ -221,11 +212,11 @@ async fn reply_with_different_parent_than_root_is_rejected(let channel_root = insert_channel_root(&pool, CH1).await?;let err = sqlx::query(- "INSERT INTO comms_messages (id, channel_id, thread_id, sender_id, content)- VALUES ($1, $2, $3, $4, 'reply')",+ "INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, thread_id, sender_id, content)+ VALUES ($1, 'channel', $2, $3, $4, 'reply')",).bind(macro_uuid::generate_uuid_v7())- .bind(CH3)+ .bind(CH3.to_string()).bind(channel_root).bind(USER2).execute(&pool)@@ -238,44 +229,6 @@ async fn reply_with_different_parent_than_root_is_rejected(Ok(())}-#[sqlx::test(- migrator = "MACRO_DB_MIGRATIONS",- fixtures(path = "../fixtures", scripts("channels"))-)]-async fn disagreeing_channel_id_and_parent_are_rejected(- pool: Pool<Postgres>,-) -> anyhow::Result<()> {- let err = sqlx::query(- "INSERT INTO comms_messages (id, channel_id, parent_entity_type, parent_entity_id, sender_id, content)- VALUES ($1, $2, 'channel', $3, $4, 'hi')",- )- .bind(macro_uuid::generate_uuid_v7())- .bind(CH1)- .bind(CH3.to_string())- .bind(USER1)- .execute(&pool)- .await- .expect_err("channel_id and parent_entity_id must agree");- assert_eq!(error_code(&err).as_deref(), Some("23514"));-- let err = sqlx::query(- "INSERT INTO comms_messages (id, channel_id, parent_entity_type, parent_entity_id, sender_id, content)- VALUES ($1, $2, 'document', $3, $4, 'hi')",- )- .bind(macro_uuid::generate_uuid_v7())- .bind(CH1)- .bind(DOC)- .bind(USER1)- .execute(&pool)- .await- .expect_err("a document message cannot carry a channel_id");- assert_eq!(- constraint_name(&err),- Some("comms_messages_channel_parent_check")- );- Ok(())-}-#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS",fixtures(path = "../fixtures", scripts("channels"))@@ -409,12 +362,6 @@ async fn pdf_anchors_reference_document_thread_rows(pool: Pool<Postgres>) -> any.bind(USER1).execute(&pool).await?;- sqlx::query(r#"INSERT INTO "Thread" (id, owner, "documentId") VALUES (901, $1, $2)"#)- .bind(USER1)- .bind(DOC)- .execute(&pool)- .await?;-let root_id = insert_document_root(&pool, DOC).await?;let placeable = Uuid::from_u128(0xf0000000_0000_0000_0000_000000000001);let highlight = Uuid::from_u128(0xf0000000_0000_0000_0000_000000000002);@@ -422,9 +369,9 @@ async fn pdf_anchors_reference_document_thread_rows(pool: Pool<Postgres>) -> anyr#"INSERT INTO "PdfPlaceableCommentAnchor" (uuid, "documentId", owner, page, "wasEdited", "wasDeleted", "shouldLockOnSave","originalPage", "originalIndex", "xPct", "yPct", "widthPct", "heightPct", rotation,- "threadId", root_id+ root_id)- VALUES ($1, $2, $3, 1, false, false, false, 1, 0, 0.1, 0.1, 0.2, 0.2, 0, 901, $4)"#,+ VALUES ($1, $2, $3, 1, false, false, false, 1, 0, 0.1, 0.1, 0.2, 0.2, 0, $4)"#,).bind(placeable).bind(DOC)diff --git a/crates/macro_db_client/migrations/20261002150422_drop_legacy_comment_schema.sql b/crates/macro_db_client/migrations/20261002150422_drop_legacy_comment_schema.sqlnew file mode 100644index 0000000000..3af7d06f1a--- /dev/null+++ b/crates/macro_db_client/migrations/20261002150422_drop_legacy_comment_schema.sql@@ -0,0 +1,66 @@+-- Contract release #6732 is deployed in v2026.10.1.0. This PR includes the+-- final PDF threadId query fix; the user accepted old-instance PDF failures+-- during the same-release rollout. Recovery requires successful service rollout.+-- Before deployment, verify a production snapshot and external SQL-reader check+-- (see docs/comment-schema-drop.md). This PR does not authorize deployment.+SET LOCAL lock_timeout = '5s';++-- Replace the function before removing the column its old body references.+-- Document notifications use commentId; channel, initiative and CRM use+-- messageId (with message_id supported for historical metadata).+CREATE OR REPLACE FUNCTION cascade_comms_message_delete_to_notifications()+RETURNS trigger LANGUAGE plpgsql AS $$+BEGIN+ IF OLD.deleted_at IS NULL AND NEW.deleted_at IS NOT NULL THEN+ DELETE FROM notification+ WHERE event_item_type = NEW.parent_entity_type+ AND event_item_id = NEW.parent_entity_id+ AND (+ metadata->>'messageId' = NEW.id::text+ OR metadata->>'message_id' = NEW.id::text+ OR (NEW.parent_entity_type = 'document' AND metadata->>'commentId' = NEW.id::text)+ );+ END IF;+ RETURN NEW;+END;+$$;++DROP TRIGGER trg_sync_comms_message_parent ON comms_messages;+DROP FUNCTION sync_comms_message_parent();++-- Preserve channel referential integrity and hard-delete cascades using the+-- same generated-parent FK pattern as CRM. This stored column rewrites the+-- table; schedule the migration for a quiet window. Attachments still cascade+-- through their message FK. Existing parent timeline/history/activity indexes+-- already replace the channel indexes removed with channel_id.+ALTER TABLE comms_messages+ ADD COLUMN channel_message_parent_id uuid GENERATED ALWAYS AS (+ CASE WHEN parent_entity_type = 'channel' THEN parent_entity_id::uuid END+ ) STORED REFERENCES comms_channels (id) ON DELETE CASCADE,+ DROP COLUMN channel_id;++CREATE INDEX idx_comms_messages_channel_parent+ ON comms_messages (channel_message_parent_id)+ WHERE channel_message_parent_id IS NOT NULL;++-- DROP COLUMN also removes indexes that INCLUDE the column. Rebuild this+-- entity lookup index explicitly so reverse attachment queries keep coverage.+DROP INDEX idx_comms_attachments_entity_created;+ALTER TABLE comms_attachments DROP COLUMN channel_id;+CREATE INDEX idx_comms_attachments_entity_created+ ON comms_attachments (entity_type, entity_id, created_at DESC) INCLUDE (message_id);++-- A placeable must still belong to a discussion. Fail rather than silently+-- discard any rootless legacy placeables. Highlights may stand alone.+ALTER TABLE "PdfPlaceableCommentAnchor"+ ALTER COLUMN root_id SET NOT NULL,+ DROP COLUMN "threadId";+ALTER TABLE "PdfHighlightAnchor" DROP COLUMN "threadId";++-- Use RESTRICT (the default), not CASCADE: unknown dependents must stop the+-- migration. Keep both migrated_comment_* mapping tables for old links.+DROP TABLE "Comment";+DROP TABLE "ThreadAnchor";+DROP TABLE "Thread";+DROP TABLE crm_comment;+DROP TABLE crm_thread;diff --git a/crates/macro_db_migrator/Cargo.toml b/crates/macro_db_migrator/Cargo.tomlindex b314e84285..59785bd536 100644--- a/crates/macro_db_migrator/Cargo.toml+++ b/crates/macro_db_migrator/Cargo.toml@@ -4,50 +4,9 @@ name = "macro_db_migrator"publish = falseversion = "0.1.0"-[features]-comment-import = [- "dep:clap",- "dep:database_env_vars",- "dep:macro_entrypoint",- "dep:macro_env",- "dep:macro_uuid",- "dep:rootcause",- "dep:serde_json",- "dep:tokio",- "dep:tracing",-]--crm-comment-import = [- "dep:clap",- "dep:database_env_vars",- "dep:macro_entrypoint",- "dep:macro_env",- "dep:macro_uuid",- "dep:rootcause",- "dep:serde_json",- "dep:tokio",-]--[[bin]]-name = "comment_import"-required-features = ["comment-import"]--[[bin]]-name = "crm_comment_import"-required-features = ["crm-comment-import"]-[dependencies]-clap = { workspace = true, optional = true }-database_env_vars = { path = "../database_env_vars", optional = true }-macro_entrypoint = { path = "../macro_entrypoint", optional = true }-macro_env = { path = "../macro_env", optional = true }-macro_uuid = { path = "../macro_uuid", optional = true }-rootcause = { workspace = true, optional = true }-serde_json = { workspace = true, optional = true }sqlx = { workspace = true, features = ["migrate"] }-tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"], optional = true }-tracing = { workspace = true, optional = true }workspace-hack = { version = "0.1", path = "../workspace-hack" }[dev-dependencies]-chrono = { workspace = true }+tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }diff --git a/crates/macro_db_migrator/src/bin/comment_import.rs b/crates/macro_db_migrator/src/bin/comment_import.rsdeleted file mode 100644index 6496b370b1..0000000000--- a/crates/macro_db_migrator/src/bin/comment_import.rs+++ /dev/null@@ -1,45 +0,0 @@-//! Online, idempotent import of legacy document comments into the shared-//! message store. Safe to rerun while the application keeps serving requests.--#[path = "comment_import/runner.rs"]-mod runner;--use clap::Parser;-use database_env_vars::DatabaseUrl;-use macro_entrypoint::MacroEntrypoint;-use macro_env::Environment;-use sqlx::{Connection, PgConnection};--#[derive(Debug, Parser)]-#[command(- about = "Import legacy document comments into comms_messages while the application stays live"-)]-struct Args {- /// Documents imported per transaction. Smaller batches hold the legacy- /// table lock for less time; larger batches finish sooner.- #[arg(long, default_value_t = runner::DEFAULT_DOCUMENTS_PER_BATCH)]- documents_per_batch: i64,- /// Run the preflight and report pending work without writing anything.- #[arg(long)]- check: bool,-}--#[tokio::main]-async fn main() -> Result<(), rootcause::Report> {- let args = Args::parse();- let entrypoint = MacroEntrypoint::new(Environment::Local).init();- let database_url = DatabaseUrl::new()?;- let mut connection = PgConnection::connect(database_url.as_ref()).await?;- let report = if args.check {- runner::check(&mut connection).await?.to_string()- } else {- let options = runner::Options {- documents_per_batch: args.documents_per_batch,- };- runner::run(&mut connection, options).await?.to_string()- };- println!("{report}");- connection.close().await?;- entrypoint.shutdown();- Ok(())-}diff --git a/crates/macro_db_migrator/src/bin/comment_import/legacy_fixture.sql b/crates/macro_db_migrator/src/bin/comment_import/legacy_fixture.sqldeleted file mode 100644index d404905602..0000000000--- a/crates/macro_db_migrator/src/bin/comment_import/legacy_fixture.sql+++ /dev/null@@ -1,46 +0,0 @@-INSERT INTO macro_user(id, username, email, stripe_customer_id)-VALUES ('01990000-0000-7000-8000-000000000001', 'migration', '[email protected]', 'migration');-INSERT INTO "User"(id, email, macro_user_id)-VALUES ('macro|[email protected]', '[email protected]', '01990000-0000-7000-8000-000000000001');-INSERT INTO comms_channels(id, name, channel_type, owner_id)-VALUES ('01990000-0000-7000-8000-000000000005', 'Existing channel', 'private', 'macro|[email protected]');-INSERT INTO comms_messages(id, channel_id, thread_id, sender_id, content) VALUES- ('01990000-0000-7000-8000-000000000006', '01990000-0000-7000-8000-000000000005', null, 'macro|[email protected]', 'Channel root'),- ('01990000-0000-7000-8000-000000000007', '01990000-0000-7000-8000-000000000005', '01990000-0000-7000-8000-000000000006', 'macro|[email protected]', 'Channel reply');-INSERT INTO "Document"(id, name, owner, "fileType") VALUES- ('legacy-md', 'Legacy', 'macro|[email protected]', 'md'),- ('legacy-pdf', 'Legacy PDF', 'macro|[email protected]', 'pdf');-INSERT INTO "Thread"(id, owner, "documentId", resolved, metadata, "createdAt", "updatedAt") VALUES- (1, 'macro|[email protected]', 'legacy-md', true, '{"markId":"01990000-0000-7000-8000-000000000002"}', '2020-01-01', '2020-01-02'),- (2, 'macro|[email protected]', 'legacy-md', false, '{"markId":"DISCUSSION:legacy"}', '2020-01-05', '2020-01-06'),- (3, 'macro|[email protected]', 'legacy-pdf', false, '{}', '2020-01-01', '2020-01-01'),- (4, 'macro|[email protected]', 'legacy-pdf', false, '{}', '2020-01-01', '2020-01-01'),- (5, 'macro|[email protected]', 'legacy-md', false, '{"markId":"DISCUSSION:01990000-0000-7000-8000-000000000009"}', '2020-01-07', '2020-01-07');-INSERT INTO "Comment"(id, "threadId", owner, sender, text, "order", "createdAt", "updatedAt", "deletedAt") VALUES- (10, 1, 'macro|[email protected]', 'Original author', 'Deleted root', 1, '2020-01-01', '2020-01-01', '2020-01-03'),- (11, 1, 'macro|[email protected]', null, 'Surviving reply', 2, '2020-01-02', '2020-01-02', null),- (12, 3, 'macro|[email protected]', 'PDF Author', 'PDF original', 1, '2020-01-01', '2020-01-01', null),- (13, 4, 'macro|[email protected]', null, 'Highlight comment', 1, '2020-01-01', '2020-01-01', null),- (14, 5, 'macro|[email protected]', null, 'Discussion root', null, '2020-01-07', '2020-01-08', null);-INSERT INTO "PdfPlaceableCommentAnchor"(- uuid, "documentId", owner, page, "wasEdited", "wasDeleted", "shouldLockOnSave",- "originalPage", "originalIndex", "xPct", "yPct", "widthPct", "heightPct", rotation, "threadId"-) VALUES ('01990000-0000-7000-8000-000000000003', 'legacy-pdf', 'macro|[email protected]',- 2, false, false, false, 2, 0, 0.1, 0.2, 0.3, 0.4, 0, 3);-INSERT INTO "PdfHighlightAnchor"(- uuid, "documentId", owner, page, red, green, blue, alpha, type, text,- "pageViewportWidth", "pageViewportHeight", "threadId"-) VALUES ('01990000-0000-7000-8000-000000000004', 'legacy-pdf', 'macro|[email protected]',- 1, 255, 255, 0, 0.5, 0, 'Highlighted text', 600, 800, 4);-INSERT INTO "PdfHighlightRect"(top, "left", width, height, "pdfHighlightAnchorId")-VALUES (0.1, 0.2, 0.3, 0.4, '01990000-0000-7000-8000-000000000004');-INSERT INTO "DocumentInstance"(id, "documentId", sha) VALUES (1, 'legacy-pdf', 'fixture');-INSERT INTO "DocumentInstanceModificationData"("documentInstanceId", "modificationData")-VALUES (1, '{"pages":[{"highlights":[{"comments":[{"id":12,"text":"PDF original"},{"id":"999","text":"Stale"},{"id":"external-pdf-id","text":"External"}]}]}]}');-INSERT INTO notification(id, notification_event_type, event_item_id, event_item_type, service_sender, metadata)-SELECT gen_random_uuid(), kind, 'legacy-md', 'document', 'dss',- '{"commentId":11,"threadId":1,"commentText":"Surviving reply"}'::jsonb-FROM unnest(ARRAY['commented_on_document', 'replied_to_document_comment_thread', 'mentioned_in_document_comment']) kind;-INSERT INTO notification(id, notification_event_type, event_item_id, event_item_type, service_sender, metadata)-VALUES (gen_random_uuid(), 'commented_on_document', 'legacy-md', 'document', 'dss',- '{"commentId":424242,"threadId":1,"commentText":"Removed long ago"}'::jsonb);diff --git a/crates/macro_db_migrator/src/bin/comment_import/runner.rs b/crates/macro_db_migrator/src/bin/comment_import/runner.rsdeleted file mode 100644index 9b7c11394c..0000000000--- a/crates/macro_db_migrator/src/bin/comment_import/runner.rs+++ /dev/null@@ -1,781 +0,0 @@-//! Database-only operator tooling. No application request or authorization path.-//!-//! Every run projects the legacy `"Comment"` / `"Thread"` rows onto the shared-//! message tables one batch of documents at a time. Rows whose projection-//! already matches are left alone, so a rerun is a no-op and a run after new-//! legacy writes carries only the delta.--#[path = "summary.rs"]-mod summary;--use std::collections::HashMap;-use std::time::Duration;--use macro_uuid::{Uuid, generate_uuid_v7};-use sqlx::{Connection, PgConnection, Postgres, Transaction};-pub(super) use summary::{Pending, Summary};--const LOCK_ID: i64 = 732_847_301;-/// Documents handled per transaction when the operator gives no override.-pub(super) const DEFAULT_DOCUMENTS_PER_BATCH: i64 = 250;-const MAPPING_CHUNK: usize = 1_000;-const LOCK_RETRIES: u32 = 5;-const LOCK_RETRY_DELAY: Duration = Duration::from_secs(2);-const LOCK_NOT_AVAILABLE: &str = "55P03";-const DEADLOCK_DETECTED: &str = "40P01";-const PREFLIGHT_SAMPLE: i64 = 20;-const UUID_PATTERN: &str =- "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$";-const DISCUSSION_MARK_PREFIX: &str = "DISCUSSION:";-const COMMENT_NOTIFICATION_TYPES: [&str; 3] = [- "commented_on_document",- "replied_to_document_comment_thread",- "mentioned_in_document_comment",-];-/// Saved PDF export payloads whose `comments[].id` still carries a legacy id.-const NUMERIC_COMMENT_ID_PATH: &str =- r#"$.**.comments[*].id ? (@.type() == "number" || @ like_regex "^[0-9]+$")"#;--#[derive(Debug, Clone, Copy)]-pub(super) struct Options {- pub documents_per_batch: i64,-}--impl Default for Options {- fn default() -> Self {- Self {- documents_per_batch: DEFAULT_DOCUMENTS_PER_BATCH,- }- }-}--/// The caller owns this dedicated connection; dropping it also releases the lock.-pub(super) async fn run(- connection: &mut PgConnection,- options: Options,-) -> Result<Summary, rootcause::Report> {- if options.documents_per_batch < 1 {- rootcause::bail!("documents_per_batch must be at least 1");- }- with_lock(connection, |connection| {- Box::pin(run_locked(connection, options))- })- .await-}--/// Preflight and pending counts; never writes.-pub(super) async fn check(connection: &mut PgConnection) -> Result<Pending, rootcause::Report> {- with_lock(connection, |connection| Box::pin(check_locked(connection))).await-}--type Locked<'a, T> =- std::pin::Pin<Box<dyn Future<Output = Result<T, rootcause::Report>> + Send + 'a>>;--async fn with_lock<T>(- connection: &mut PgConnection,- body: impl for<'a> FnOnce(&'a mut PgConnection) -> Locked<'a, T>,-) -> Result<T, rootcause::Report> {- let acquired =- sqlx::query_scalar!(r#"SELECT pg_try_advisory_lock($1) AS "acquired!""#, LOCK_ID)- .fetch_one(&mut *connection)- .await?;- if !acquired {- rootcause::bail!("Another comment import is running");- }- let result = body(connection).await;- let unlock = sqlx::query_scalar!(r#"SELECT pg_advisory_unlock($1) AS "released!""#, LOCK_ID)- .fetch_one(&mut *connection)- .await;- let outcome = result?;- unlock?;- Ok(outcome)-}--async fn check_locked(connection: &mut PgConnection) -> Result<Pending, rootcause::Report> {- preflight(connection).await?;- let comments_without_mapping = sqlx::query_scalar!(- r#"SELECT count(*) AS "count!" FROM "Comment" c- WHERE NOT EXISTS (SELECT 1 FROM migrated_comment_id m WHERE m.comment_id = c.id)"#- )- .fetch_one(&mut *connection)- .await?;- let threads_without_mapping = sqlx::query_scalar!(- r#"SELECT count(*) AS "count!" FROM "Thread" t- WHERE NOT EXISTS (SELECT 1 FROM migrated_comment_thread_id m WHERE m.thread_id = t.id)"#- )- .fetch_one(&mut *connection)- .await?;- let documents_with_legacy_comments =- sqlx::query_scalar!(r#"SELECT count(DISTINCT "documentId") AS "count!" FROM "Thread""#)- .fetch_one(&mut *connection)- .await?;- Ok(Pending {- comments_without_mapping,- threads_without_mapping,- documents_with_legacy_comments,- })-}--async fn run_locked(- connection: &mut PgConnection,- options: Options,-) -> Result<Summary, rootcause::Report> {- preflight(connection).await?;- let mut summary = Summary::default();- let mut cursor: Option<String> = None;- loop {- let documents = sqlx::query_scalar!(- r#"SELECT d.id FROM "Document" d- WHERE ($1::text IS NULL OR d.id > $1)- AND (EXISTS (SELECT 1 FROM "Thread" t WHERE t."documentId" = d.id)- OR EXISTS (SELECT 1 FROM migrated_comment_thread_id m WHERE m.document_id = d.id))- ORDER BY d.id LIMIT $2"#,- cursor,- options.documents_per_batch,- )- .fetch_all(&mut *connection)- .await?;- let Some(last) = documents.last() else { break };- cursor = Some(last.clone());- import_batch_with_retry(connection, &documents, &mut summary).await?;- summary.documents += documents.len() as u64;- summary.batches += 1;- }- Ok(summary)-}--/// Legacy data that cannot be represented and must be resolved by hand first.-async fn preflight(connection: &mut PgConnection) -> Result<(), rootcause::Report> {- let ambiguous = sqlx::query_scalar!(- r#"SELECT t.id FROM "Thread" t- LEFT JOIN "PdfPlaceableCommentAnchor" p ON p."threadId" = t.id AND NOT p."wasDeleted"- LEFT JOIN "PdfHighlightAnchor" h ON h."threadId" = t.id AND h."deletedAt" IS NULL- WHERE t."deletedAt" IS NULL- GROUP BY t.id HAVING count(DISTINCT p.uuid) + count(DISTINCT h.uuid) > 1- ORDER BY t.id LIMIT $1"#,- PREFLIGHT_SAMPLE,- )- .fetch_all(&mut *connection)- .await?;- if !ambiguous.is_empty() {- rootcause::bail!(- "Multiple PDF anchors on a legacy thread; resolve before importing (thread ids {ambiguous:?})"- );- }- let duplicates = sqlx::query!(- r#"SELECT t."documentId" AS document_id, t.metadata->>'markId' AS "mark_id!",- array_agg(t.id ORDER BY t.id) AS "thread_ids!"- FROM "Thread" t- WHERE t."deletedAt" IS NULL- AND t.metadata->>'markId' ~ $1- AND EXISTS (SELECT 1 FROM "Comment" c WHERE c."threadId" = t.id)- AND NOT EXISTS (SELECT 1 FROM "PdfPlaceableCommentAnchor" p WHERE p."threadId" = t.id AND NOT p."wasDeleted")- AND NOT EXISTS (SELECT 1 FROM "PdfHighlightAnchor" h- WHERE h."threadId" = t.id AND h."deletedAt" IS NULL)- GROUP BY 1, 2 HAVING count(*) > 1- ORDER BY 1, 2 LIMIT $2"#,- UUID_PATTERN,- PREFLIGHT_SAMPLE,- )- .fetch_all(&mut *connection)- .await?;- if let Some(first) = duplicates.first() {- rootcause::bail!(- "Multiple active legacy threads share the same Markdown mark; delete the duplicates before importing (document {} mark {} threads {:?}, {} conflicts sampled)",- first.document_id,- first.mark_id,- first.thread_ids,- duplicates.len()- );- }- Ok(())-}--async fn import_batch_with_retry(- connection: &mut PgConnection,- documents: &[String],- summary: &mut Summary,-) -> Result<(), rootcause::Report> {- let mut attempt = 0;- loop {- match import_batch(connection, documents, summary).await {- Ok(()) => return Ok(()),- Err(error) if attempt < LOCK_RETRIES && is_retryable(&error) => {- attempt += 1;- tracing::warn!(- error = %error,- attempt,- documents = documents.len(),- "legacy tables busy; retrying batch"- );- tokio::time::sleep(LOCK_RETRY_DELAY).await;- }- Err(error) => return Err(error.into()),- }- }-}--fn is_retryable(error: &sqlx::Error) -> bool {- error- .as_database_error()- .and_then(|e| e.code())- .is_some_and(|code| code == LOCK_NOT_AVAILABLE || code == DEADLOCK_DETECTED)-}--/// One transaction per batch: mapping allocation and the rows those mappings-/// point at commit together, so the mapping foreign keys hold at every boundary.-/// Counts join the run summary only once the batch has committed, so a retried-/// batch is counted once.-async fn import_batch(- connection: &mut PgConnection,- documents: &[String],- summary: &mut Summary,-) -> Result<(), sqlx::Error> {- let mut tx = connection.begin().await?;- let mut batch = Summary::default();- lock_legacy_tables(&mut tx).await?;- batch.comment_mappings_allocated = allocate_comment_mappings(&mut tx, documents).await?;- let new_roots = allocate_thread_mappings(&mut tx, documents).await?;- batch.thread_mappings_allocated = new_roots.len() as u64;-- let (inserted, updated) = upsert_messages(&mut tx, documents).await?;- batch.messages_inserted += inserted;- batch.messages_updated += updated;- let (inserted, updated) = upsert_structural_roots(&mut tx, documents).await?;- batch.messages_inserted += inserted;- batch.messages_updated += updated;- batch.messages_tombstoned += tombstone_orphan_messages(&mut tx, documents).await?;-- batch.threads_written = upsert_thread_state(&mut tx, documents, &new_roots).await?;- let (threads, messages) = tombstone_orphan_threads(&mut tx, documents).await?;- batch.threads_tombstoned = threads;- batch.messages_tombstoned += messages;-- batch.anchors_linked = link_anchors(&mut tx, documents).await?;- let (remapped, unmapped) = remap_notifications(&mut tx, documents).await?;- batch.notifications_remapped = remapped;- batch.warnings.unmapped_notifications = unmapped;- let (remapped, unmapped) = remap_pdf_payloads(&mut tx, documents).await?;- batch.pdf_payloads_remapped = remapped;- batch.warnings.unmapped_pdf_comment_ids = unmapped;-- batch.warnings.invalid_mark_ids = count_invalid_mark_ids(&mut tx, documents).await?;- batch.warnings.root_order_drift = count_root_order_drift(&mut tx, documents).await?;- tx.commit().await?;- summary.absorb(batch);- Ok(())-}--#[expect(- clippy::disallowed_methods,- reason = "utility statements carry no result shape for SQLx to check"-)]-async fn lock_legacy_tables(tx: &mut Transaction<'_, Postgres>) -> Result<(), sqlx::Error> {- sqlx::query("SET LOCAL lock_timeout = '5s'")- .execute(&mut **tx)- .await?;- sqlx::query(r#"LOCK TABLE "Comment", "Thread" IN SHARE ROW EXCLUSIVE MODE"#)- .execute(&mut **tx)- .await?;- // The 5s timeout bounds only the table-lock wait. Clear it so later batch- // statements wait normally for unrelated row locks instead of aborting.- sqlx::query("SET LOCAL lock_timeout = 0")- .execute(&mut **tx)- .await?;- Ok(())-}--async fn allocate_comment_mappings(- tx: &mut Transaction<'_, Postgres>,- documents: &[String],-) -> Result<u64, sqlx::Error> {- let missing = sqlx::query!(- r#"SELECT c.id, t."documentId" AS document_id FROM "Comment" c- JOIN "Thread" t ON t.id = c."threadId"- WHERE t."documentId" = ANY($1)- AND NOT EXISTS (SELECT 1 FROM migrated_comment_id m WHERE m.comment_id = c.id)- ORDER BY c.id"#,- documents,- )- .fetch_all(&mut **tx)- .await?;- for chunk in missing.chunks(MAPPING_CHUNK) {- let ids: Vec<i64> = chunk.iter().map(|row| row.id).collect();- let uuids: Vec<Uuid> = chunk.iter().map(|_| generate_uuid_v7()).collect();- let document_ids: Vec<String> = chunk.iter().map(|row| row.document_id.clone()).collect();- sqlx::query!(- "INSERT INTO migrated_comment_id (comment_id, message_id, document_id)- SELECT * FROM UNNEST($1::bigint[], $2::uuid[], $3::text[])",- &ids,- &uuids,- &document_ids,- )- .execute(&mut **tx)- .await?;- }- Ok(missing.len() as u64)-}--/// Roots follow legacy display order, then creation time, then id, including-/// deleted comments. Empty threads receive a fresh root id for a structural tombstone.-async fn allocate_thread_mappings(- tx: &mut Transaction<'_, Postgres>,- documents: &[String],-) -> Result<Vec<Uuid>, sqlx::Error> {- let missing = sqlx::query!(- r#"SELECT t.id, t."documentId" AS document_id, first.message_id AS "root_id?"- FROM "Thread" t- LEFT JOIN LATERAL (- SELECT m.message_id FROM "Comment" c- JOIN migrated_comment_id m ON m.comment_id = c.id- WHERE c."threadId" = t.id- ORDER BY c."order" NULLS LAST, c."createdAt", c.id LIMIT 1- ) first ON true- WHERE t."documentId" = ANY($1)- AND NOT EXISTS (SELECT 1 FROM migrated_comment_thread_id m WHERE m.thread_id = t.id)- ORDER BY t.id"#,- documents,- )- .fetch_all(&mut **tx)- .await?;- let mut roots = Vec::with_capacity(missing.len());- for chunk in missing.chunks(MAPPING_CHUNK) {- let ids: Vec<i64> = chunk.iter().map(|row| row.id).collect();- let chunk_roots: Vec<Uuid> = chunk- .iter()- .map(|row| row.root_id.unwrap_or_else(generate_uuid_v7))- .collect();- let document_ids: Vec<String> = chunk.iter().map(|row| row.document_id.clone()).collect();- sqlx::query!(- "INSERT INTO migrated_comment_thread_id (thread_id, root_id, document_id)- SELECT * FROM UNNEST($1::bigint[], $2::uuid[], $3::text[])",- &ids,- &chunk_roots,- &document_ids,- )- .execute(&mut **tx)- .await?;- roots.extend(chunk_roots);- }- Ok(roots)-}--fn count_inserts(rows: &[bool]) -> (u64, u64) {- let inserted = rows.iter().filter(|inserted| **inserted).count() as u64;- (inserted, rows.len() as u64 - inserted)-}--/// Every mapped comment. Existing rows change only when the legacy row differs-/// and is not older than the stored row, so an edit made through the new API-/// after a legacy row went stale is never reverted.-async fn upsert_messages(- tx: &mut Transaction<'_, Postgres>,- documents: &[String],-) -> Result<(u64, u64), sqlx::Error> {- let rows = sqlx::query_scalar!(- r#"INSERT INTO comms_messages (- id, parent_entity_type, parent_entity_id, thread_id, sender_id, imported_author,- content, created_at, updated_at, edited_at, deleted_at, import_metadata, import_order- )- SELECT cm.message_id, 'document', t."documentId",- CASE WHEN cm.message_id = tm.root_id THEN NULL ELSE tm.root_id END,- c.owner, c.sender, c.text,- c."createdAt" AT TIME ZONE 'UTC', c."updatedAt" AT TIME ZONE 'UTC',- CASE WHEN c."updatedAt" > c."createdAt" THEN c."updatedAt" END,- COALESCE(c."deletedAt", t."deletedAt"), c.metadata,- row_number() OVER (PARTITION BY t.id ORDER BY c."order" NULLS LAST, c."createdAt", c.id)- FROM "Comment" c- JOIN "Thread" t ON t.id = c."threadId"- JOIN migrated_comment_id cm ON cm.comment_id = c.id- JOIN migrated_comment_thread_id tm ON tm.thread_id = t.id- WHERE t."documentId" = ANY($1)- ON CONFLICT (id) DO UPDATE SET- content = EXCLUDED.content,- updated_at = EXCLUDED.updated_at,- edited_at = EXCLUDED.edited_at,- deleted_at = EXCLUDED.deleted_at,- imported_author = EXCLUDED.imported_author,- import_metadata = EXCLUDED.import_metadata,- import_order = EXCLUDED.import_order- WHERE comms_messages.updated_at <= EXCLUDED.updated_at- AND (comms_messages.content, comms_messages.updated_at, comms_messages.edited_at,- comms_messages.deleted_at, comms_messages.imported_author,- comms_messages.import_metadata, comms_messages.import_order)- IS DISTINCT FROM- (EXCLUDED.content, EXCLUDED.updated_at, EXCLUDED.edited_at, EXCLUDED.deleted_at,- EXCLUDED.imported_author, EXCLUDED.import_metadata, EXCLUDED.import_order)- RETURNING (xmax = 0) AS "inserted!""#,- documents,- )- .fetch_all(&mut **tx)- .await?;- Ok(count_inserts(&rows))-}--/// Empty legacy threads keep a deleted root so their identity, anchors, and-/// external links stay representable without rendering as a discussion.-async fn upsert_structural_roots(- tx: &mut Transaction<'_, Postgres>,- documents: &[String],-) -> Result<(u64, u64), sqlx::Error> {- let rows = sqlx::query_scalar!(- r#"INSERT INTO comms_messages (- id, parent_entity_type, parent_entity_id, sender_id, content,- created_at, updated_at, deleted_at- )- SELECT tm.root_id, 'document', t."documentId", t.owner, '',- t."createdAt" AT TIME ZONE 'UTC', t."updatedAt" AT TIME ZONE 'UTC',- COALESCE(t."deletedAt", t."updatedAt")- FROM "Thread" t- JOIN migrated_comment_thread_id tm ON tm.thread_id = t.id- WHERE t."documentId" = ANY($1)- AND NOT EXISTS (SELECT 1 FROM "Comment" c WHERE c."threadId" = t.id)- ON CONFLICT (id) DO UPDATE SET- updated_at = EXCLUDED.updated_at,- deleted_at = EXCLUDED.deleted_at- WHERE comms_messages.updated_at <= EXCLUDED.updated_at- AND (comms_messages.updated_at, comms_messages.deleted_at)- IS DISTINCT FROM (EXCLUDED.updated_at, EXCLUDED.deleted_at)- RETURNING (xmax = 0) AS "inserted!""#,- documents,- )- .fetch_all(&mut **tx)- .await?;- Ok(count_inserts(&rows))-}--/// A mapped comment whose legacy row was removed outright reads as deleted.-async fn tombstone_orphan_messages(- tx: &mut Transaction<'_, Postgres>,- documents: &[String],-) -> Result<u64, sqlx::Error> {- let result = sqlx::query!(- r#"UPDATE comms_messages m SET deleted_at = now(), updated_at = now()- FROM migrated_comment_id cm- WHERE cm.message_id = m.id AND cm.document_id = ANY($1) AND m.deleted_at IS NULL- AND NOT EXISTS (SELECT 1 FROM "Comment" c WHERE c.id = cm.comment_id)"#,- documents,- )- .execute(&mut **tx)- .await?;- Ok(result.rows_affected())-}--/// Thread state for every mapped thread. The root insert trigger has already-/// created a placeholder row for roots written in this batch; those are always-/// overwritten. Older rows follow the same newer-wins rule as messages.-async fn upsert_thread_state(- tx: &mut Transaction<'_, Postgres>,- documents: &[String],- new_roots: &[Uuid],-) -> Result<u64, sqlx::Error> {- let result = sqlx::query!(- r#"INSERT INTO comms_message_threads (- root_id, parent_entity_type, parent_entity_id, user_id, resolved, anchor,- import_metadata, created_at, updated_at, deleted_at- )- SELECT tm.root_id, 'document', t."documentId", t.owner, t.resolved,- CASE- WHEN pa.uuid IS NOT NULL- THEN jsonb_build_object('type', 'pdf_placeable', 'anchor_id', pa.uuid)- WHEN ph.uuid IS NOT NULL- THEN jsonb_build_object('type', 'pdf_highlight', 'anchor_id', ph.uuid)- WHEN t.metadata->>'markId' ~ $3- THEN jsonb_build_object('type', 'markdown', 'mark_id', (t.metadata->>'markId')::uuid)- END,- t.metadata, t."createdAt" AT TIME ZONE 'UTC', t."updatedAt" AT TIME ZONE 'UTC',- CASE- WHEN t."deletedAt" IS NOT NULL THEN t."deletedAt" AT TIME ZONE 'UTC'- WHEN NOT EXISTS (SELECT 1 FROM "Comment" c WHERE c."threadId" = t.id)- THEN t."updatedAt" AT TIME ZONE 'UTC'- END- FROM "Thread" t- JOIN migrated_comment_thread_id tm ON tm.thread_id = t.id- LEFT JOIN LATERAL (- SELECT a.uuid FROM "PdfPlaceableCommentAnchor" a- WHERE a."threadId" = t.id AND NOT a."wasDeleted"- ORDER BY a.uuid LIMIT 1- ) pa ON true- LEFT JOIN LATERAL (- SELECT a.uuid FROM "PdfHighlightAnchor" a- WHERE a."threadId" = t.id AND a."deletedAt" IS NULL- ORDER BY a.uuid LIMIT 1- ) ph ON true- WHERE t."documentId" = ANY($1)- ON CONFLICT (root_id) DO UPDATE SET- user_id = EXCLUDED.user_id,- resolved = EXCLUDED.resolved,- anchor = EXCLUDED.anchor,- import_metadata = EXCLUDED.import_metadata,- created_at = EXCLUDED.created_at,- updated_at = EXCLUDED.updated_at,- deleted_at = EXCLUDED.deleted_at- WHERE (comms_message_threads.root_id = ANY($2)- OR comms_message_threads.updated_at <= EXCLUDED.updated_at)- AND (comms_message_threads.user_id, comms_message_threads.resolved,- comms_message_threads.anchor, comms_message_threads.import_metadata,- comms_message_threads.created_at, comms_message_threads.updated_at,- comms_message_threads.deleted_at)- IS DISTINCT FROM- (EXCLUDED.user_id, EXCLUDED.resolved, EXCLUDED.anchor, EXCLUDED.import_metadata,- EXCLUDED.created_at, EXCLUDED.updated_at, EXCLUDED.deleted_at)"#,- documents,- new_roots,- UUID_PATTERN,- )- .execute(&mut **tx)- .await?;- Ok(result.rows_affected())-}--/// A mapped thread whose legacy row was removed outright reads as deleted, with its messages.-async fn tombstone_orphan_threads(- tx: &mut Transaction<'_, Postgres>,- documents: &[String],-) -> Result<(u64, u64), sqlx::Error> {- let roots = sqlx::query_scalar!(- r#"UPDATE comms_message_threads s SET deleted_at = now(), updated_at = now()- FROM migrated_comment_thread_id tm- WHERE tm.root_id = s.root_id AND tm.document_id = ANY($1) AND s.deleted_at IS NULL- AND NOT EXISTS (SELECT 1 FROM "Thread" t WHERE t.id = tm.thread_id)- RETURNING s.root_id"#,- documents,- )- .fetch_all(&mut **tx)- .await?;- if roots.is_empty() {- return Ok((0, 0));- }- let messages = sqlx::query!(- r#"UPDATE comms_messages SET deleted_at = now(), updated_at = now()- WHERE (id = ANY($1) OR thread_id = ANY($1)) AND deleted_at IS NULL"#,- &roots,- )- .execute(&mut **tx)- .await?;- Ok((roots.len() as u64, messages.rows_affected()))-}--async fn link_anchors(- tx: &mut Transaction<'_, Postgres>,- documents: &[String],-) -> Result<u64, sqlx::Error> {- let placeables = sqlx::query!(- r#"UPDATE "PdfPlaceableCommentAnchor" a SET root_id = tm.root_id- FROM migrated_comment_thread_id tm- WHERE a."threadId" = tm.thread_id AND a."documentId" = ANY($1)- AND a.root_id IS DISTINCT FROM tm.root_id"#,- documents,- )- .execute(&mut **tx)- .await?;- let highlights = sqlx::query!(- r#"UPDATE "PdfHighlightAnchor" a SET root_id = tm.root_id- FROM migrated_comment_thread_id tm- WHERE a."threadId" = tm.thread_id AND a."documentId" = ANY($1)- AND a.root_id IS DISTINCT FROM tm.root_id"#,- documents,- )- .execute(&mut **tx)- .await?;- Ok(placeables.rows_affected() + highlights.rows_affected())-}--/// Historical document comment notifications point at message ids once both-/// the comment and its thread have mappings. Rewritten ids never match again.-async fn remap_notifications(- tx: &mut Transaction<'_, Postgres>,- documents: &[String],-) -> Result<(u64, u64), sqlx::Error> {- let types: Vec<String> = COMMENT_NOTIFICATION_TYPES- .iter()- .map(|kind| (*kind).to_owned())- .collect();- let remapped = sqlx::query!(- r#"UPDATE notification n SET metadata = jsonb_set(- jsonb_set(n.metadata, '{commentId}', to_jsonb(cm.message_id::text)),- '{threadId}', to_jsonb(tm.root_id::text)- )- FROM migrated_comment_id cm, migrated_comment_thread_id tm- WHERE n.event_item_type = 'document' AND n.event_item_id = ANY($1)- AND n.notification_event_type = ANY($2)- AND cm.document_id = n.event_item_id AND tm.document_id = n.event_item_id- AND n.metadata->>'commentId' = cm.comment_id::text- AND n.metadata->>'threadId' = tm.thread_id::text"#,- documents,- &types,- )- .execute(&mut **tx)- .await?;- let unmapped = sqlx::query_scalar!(- r#"SELECT count(*) AS "count!" FROM notification n- WHERE n.event_item_type = 'document' AND n.event_item_id = ANY($1)- AND n.notification_event_type = ANY($2)- AND (n.metadata->>'commentId' ~ '^[0-9]+$' OR n.metadata->>'threadId' ~ '^[0-9]+$')"#,- documents,- &types,- )- .fetch_one(&mut **tx)- .await?;- Ok((remapped.rows_affected(), unmapped as u64))-}--/// Saved PDF export payloads are relational metadata; their comment ids follow-/// the mapping. Ids without a mapping stay untouched and are counted.-async fn remap_pdf_payloads(- tx: &mut Transaction<'_, Postgres>,- documents: &[String],-) -> Result<(u64, u64), sqlx::Error> {- let payloads = sqlx::query!(- r#"SELECT m.id, i."documentId" AS document_id, m."modificationData" AS payload- FROM "DocumentInstanceModificationData" m- JOIN "DocumentInstance" i ON i.id = m."documentInstanceId"- JOIN "Document" d ON d.id = i."documentId"- WHERE i."documentId" = ANY($1) AND d."fileType" = 'pdf'- AND jsonb_path_exists(m."modificationData", $2::text::jsonpath)- ORDER BY m.id"#,- documents,- NUMERIC_COMMENT_ID_PATH,- )- .fetch_all(&mut **tx)- .await?;- let mut remapped = 0;- let mut unmapped = 0;- let mut mappings: HashMap<String, HashMap<i64, Uuid>> = HashMap::new();- for row in payloads {- if !mappings.contains_key(&row.document_id) {- let rows = sqlx::query!(- "SELECT comment_id, message_id FROM migrated_comment_id WHERE document_id = $1",- row.document_id,- )- .fetch_all(&mut **tx)- .await?;- mappings.insert(- row.document_id.clone(),- rows.into_iter()- .map(|mapping| (mapping.comment_id, mapping.message_id))- .collect(),- );- }- let mut payload = row.payload;- let stats = remap_comment_ids(&mut payload, &mappings[&row.document_id]);- unmapped += stats.unmapped;- if stats.remapped > 0 {- sqlx::query!(- r#"UPDATE "DocumentInstanceModificationData" SET "modificationData" = $1 WHERE id = $2"#,- payload,- row.id,- )- .execute(&mut **tx)- .await?;- remapped += 1;- }- }- Ok((remapped, unmapped))-}--#[derive(Debug, Default, PartialEq, Eq)]-struct RemapStats {- remapped: u64,- unmapped: u64,-}--/// Rewrite every `comments[].id` holding a legacy comment id, at any depth.-fn remap_comment_ids(value: &mut serde_json::Value, mappings: &HashMap<i64, Uuid>) -> RemapStats {- let mut stats = RemapStats::default();- match value {- serde_json::Value::Array(items) => {- for item in items {- let inner = remap_comment_ids(item, mappings);- stats.remapped += inner.remapped;- stats.unmapped += inner.unmapped;- }- }- serde_json::Value::Object(fields) => {- for (key, child) in fields.iter_mut() {- if key == "comments" && child.is_array() {- for comment in child.as_array_mut().into_iter().flatten() {- if let Some(id) = comment.get("id").and_then(legacy_comment_id) {- match mappings.get(&id) {- Some(message_id) => {- comment["id"] =- serde_json::Value::String(message_id.to_string());- stats.remapped += 1;- }- None => stats.unmapped += 1,- }- }- // A comments array may nest inside a comment object.- let inner = remap_comment_ids(comment, mappings);- stats.remapped += inner.remapped;- stats.unmapped += inner.unmapped;- }- } else {- let inner = remap_comment_ids(child, mappings);- stats.remapped += inner.remapped;- stats.unmapped += inner.unmapped;- }- }- }- _ => {}- }- stats-}--fn legacy_comment_id(id: &serde_json::Value) -> Option<i64> {- match id {- serde_json::Value::Number(number) => number.as_i64(),- serde_json::Value::String(text) if text.bytes().all(|byte| byte.is_ascii_digit()) => {- text.parse().ok()- }- _ => None,- }-}--async fn count_invalid_mark_ids(- tx: &mut Transaction<'_, Postgres>,- documents: &[String],-) -> Result<u64, sqlx::Error> {- let count = sqlx::query_scalar!(- r#"SELECT count(*) AS "count!" FROM "Thread" t- WHERE t."documentId" = ANY($1) AND t."deletedAt" IS NULL- AND t.metadata->>'markId' IS NOT NULL- AND t.metadata->>'markId' !~ $2- AND t.metadata->>'markId' NOT LIKE $3- AND NOT EXISTS (SELECT 1 FROM "PdfPlaceableCommentAnchor" p WHERE p."threadId" = t.id AND NOT p."wasDeleted")- AND NOT EXISTS (SELECT 1 FROM "PdfHighlightAnchor" h- WHERE h."threadId" = t.id AND h."deletedAt" IS NULL)"#,- documents,- UUID_PATTERN,- format!("{DISCUSSION_MARK_PREFIX}%"),- )- .fetch_one(&mut **tx)- .await?;- Ok(count as u64)-}--/// Roots are fixed at first import; a legacy reorder afterwards is reported, not applied.-async fn count_root_order_drift(- tx: &mut Transaction<'_, Postgres>,- documents: &[String],-) -> Result<u64, sqlx::Error> {- let count = sqlx::query_scalar!(- r#"SELECT count(*) AS "count!" FROM "Thread" t- JOIN migrated_comment_thread_id tm ON tm.thread_id = t.id- JOIN LATERAL (- SELECT cm.message_id FROM "Comment" c- JOIN migrated_comment_id cm ON cm.comment_id = c.id- WHERE c."threadId" = t.id- ORDER BY c."order" NULLS LAST, c."createdAt", c.id LIMIT 1- ) first ON true- WHERE t."documentId" = ANY($1) AND first.message_id <> tm.root_id"#,- documents,- )- .fetch_one(&mut **tx)- .await?;- Ok(count as u64)-}--#[cfg(test)]-#[path = "test.rs"]-mod test;diff --git a/crates/macro_db_migrator/src/bin/comment_import/summary.rs b/crates/macro_db_migrator/src/bin/comment_import/summary.rsdeleted file mode 100644index 4e1bedd7b8..0000000000--- a/crates/macro_db_migrator/src/bin/comment_import/summary.rs+++ /dev/null@@ -1,189 +0,0 @@-//! Run reports printed to the operator.--use std::fmt;--/// What one run wrote. Every counter is zero when the store already matched.-#[derive(Debug, Default, Clone, PartialEq, Eq)]-pub struct Summary {- pub documents: u64,- pub batches: u64,- pub comment_mappings_allocated: u64,- pub thread_mappings_allocated: u64,- pub messages_inserted: u64,- pub messages_updated: u64,- pub messages_tombstoned: u64,- pub threads_written: u64,- pub threads_tombstoned: u64,- pub anchors_linked: u64,- pub notifications_remapped: u64,- pub pdf_payloads_remapped: u64,- pub warnings: Warnings,-}--/// Legacy data the import could not represent exactly. Nothing here aborts a run.-#[derive(Debug, Default, Clone, PartialEq, Eq)]-pub struct Warnings {- /// Document comment notifications still pointing at a legacy id without a mapping.- pub unmapped_notifications: u64,- /// Numeric comment ids in saved PDF payloads without a mapping.- pub unmapped_pdf_comment_ids: u64,- /// Threads whose mark id is neither a UUID nor a `DISCUSSION:` mark; imported unanchored.- pub invalid_mark_ids: u64,- /// Threads whose first legacy comment is no longer the mapped root.- pub root_order_drift: u64,-}--impl Summary {- /// Add a committed batch to the run.- pub fn absorb(&mut self, batch: Self) {- self.documents += batch.documents;- self.batches += batch.batches;- self.comment_mappings_allocated += batch.comment_mappings_allocated;- self.thread_mappings_allocated += batch.thread_mappings_allocated;- self.messages_inserted += batch.messages_inserted;- self.messages_updated += batch.messages_updated;- self.messages_tombstoned += batch.messages_tombstoned;- self.threads_written += batch.threads_written;- self.threads_tombstoned += batch.threads_tombstoned;- self.anchors_linked += batch.anchors_linked;- self.notifications_remapped += batch.notifications_remapped;- self.pdf_payloads_remapped += batch.pdf_payloads_remapped;- self.warnings.unmapped_notifications += batch.warnings.unmapped_notifications;- self.warnings.unmapped_pdf_comment_ids += batch.warnings.unmapped_pdf_comment_ids;- self.warnings.invalid_mark_ids += batch.warnings.invalid_mark_ids;- self.warnings.root_order_drift += batch.warnings.root_order_drift;- }-- /// True when the run changed nothing.- pub fn is_noop(&self) -> bool {- self.comment_mappings_allocated == 0- && self.thread_mappings_allocated == 0- && self.messages_inserted == 0- && self.messages_updated == 0- && self.messages_tombstoned == 0- && self.threads_written == 0- && self.threads_tombstoned == 0- && self.anchors_linked == 0- && self.notifications_remapped == 0- && self.pdf_payloads_remapped == 0- }-}--impl fmt::Display for Summary {- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {- if self.is_noop() {- writeln!(- f,- "Nothing to import: {} documents with legacy comments already match the message store.",- self.documents- )?;- } else {- writeln!(- f,- "Imported legacy comments across {} documents in {} batches.",- self.documents, self.batches- )?;- writeln!(- f,- " comment mappings allocated: {}",- self.comment_mappings_allocated- )?;- writeln!(- f,- " thread mappings allocated: {}",- self.thread_mappings_allocated- )?;- writeln!(- f,- " messages inserted: {}",- self.messages_inserted- )?;- writeln!(f, " messages updated: {}", self.messages_updated)?;- writeln!(- f,- " messages tombstoned: {}",- self.messages_tombstoned- )?;- writeln!(f, " thread states written: {}", self.threads_written)?;- writeln!(- f,- " thread states tombstoned: {}",- self.threads_tombstoned- )?;- writeln!(f, " PDF anchors linked: {}", self.anchors_linked)?;- writeln!(- f,- " notifications remapped: {}",- self.notifications_remapped- )?;- writeln!(- f,- " PDF payloads remapped: {}",- self.pdf_payloads_remapped- )?;- }- write!(f, "{}", self.warnings)- }-}--impl fmt::Display for Warnings {- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {- let lines = [- (- self.unmapped_notifications,- "document comment notifications still reference a legacy id without a mapping",- ),- (- self.unmapped_pdf_comment_ids,- "numeric comment ids in saved PDF payloads have no mapping and were left as is",- ),- (- self.invalid_mark_ids,- "threads carry a mark id that is not a UUID and were imported unanchored",- ),- (- self.root_order_drift,- "threads whose first legacy comment is no longer the mapped root",- ),- ];- for (count, text) in lines {- if count > 0 {- writeln!(f, " warning: {count} {text}")?;- }- }- Ok(())- }-}--/// Work still ahead of the next run, without writing anything.-#[derive(Debug, Default, Clone, PartialEq, Eq)]-pub struct Pending {- pub comments_without_mapping: i64,- pub threads_without_mapping: i64,- pub documents_with_legacy_comments: i64,-}--impl fmt::Display for Pending {- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {- writeln!(f, "Preflight passed.")?;- writeln!(- f,- " documents with legacy comments: {}",- self.documents_with_legacy_comments- )?;- writeln!(- f,- " comments without a mapping: {}",- self.comments_without_mapping- )?;- writeln!(- f,- " threads without a mapping: {}",- self.threads_without_mapping- )?;- write!(- f,- "Edits and deletions of already imported comments are only detected by a real run."- )- }-}diff --git a/crates/macro_db_migrator/src/bin/comment_import/test.rs b/crates/macro_db_migrator/src/bin/comment_import/test.rsdeleted file mode 100644index a20de15d6a..0000000000--- a/crates/macro_db_migrator/src/bin/comment_import/test.rs+++ /dev/null@@ -1,817 +0,0 @@-use super::*;-use macro_db_migrator::MACRO_DB_MIGRATIONS;-use serde_json::{Value, json};-use sqlx::PgPool;-use summary::Warnings;--const USER: &str = "macro|[email protected]";--async fn legacy_database(pool: &PgPool) -> PgConnection {- let mut connection = pool.acquire().await.unwrap().detach();- sqlx::raw_sql(include_str!("legacy_fixture.sql"))- .execute(&mut connection)- .await- .unwrap();- connection-}--async fn mappings(connection: &mut PgConnection) -> Vec<(i64, Uuid)> {- sqlx::query!("SELECT comment_id, message_id FROM migrated_comment_id ORDER BY comment_id")- .fetch_all(connection)- .await- .unwrap()- .into_iter()- .map(|row| (row.comment_id, row.message_id))- .collect()-}--async fn thread_mappings(connection: &mut PgConnection) -> Vec<(i64, Uuid)> {- sqlx::query!("SELECT thread_id, root_id FROM migrated_comment_thread_id ORDER BY thread_id")- .fetch_all(connection)- .await- .unwrap()- .into_iter()- .map(|row| (row.thread_id, row.root_id))- .collect()-}--fn mapped(mappings: &[(i64, Uuid)], legacy_id: i64) -> Uuid {- mappings- .iter()- .find(|(id, _)| *id == legacy_id)- .map(|(_, uuid)| *uuid)- .unwrap()-}--#[derive(Debug, PartialEq)]-struct StoredMessage {- content: String,- thread_id: Option<Uuid>,- imported_author: Option<String>,- deleted: bool,- import_order: Option<i64>,-}--async fn message(connection: &mut PgConnection, id: Uuid) -> StoredMessage {- sqlx::query!(- r#"SELECT content, thread_id, imported_author, deleted_at IS NOT NULL AS "deleted!", import_order- FROM comms_messages WHERE id = $1"#,- id- )- .map(|row| StoredMessage {- content: row.content,- thread_id: row.thread_id,- imported_author: row.imported_author,- deleted: row.deleted,- import_order: row.import_order,- })- .fetch_one(connection)- .await- .unwrap()-}--#[derive(Debug, PartialEq)]-struct StoredThread {- resolved: bool,- anchor: Option<Value>,- deleted: bool,-}--async fn thread(connection: &mut PgConnection, root_id: Uuid) -> StoredThread {- sqlx::query!(- r#"SELECT resolved, anchor, deleted_at IS NOT NULL AS "deleted!"- FROM comms_message_threads WHERE root_id = $1"#,- root_id- )- .map(|row| StoredThread {- resolved: row.resolved,- anchor: row.anchor,- deleted: row.deleted,- })- .fetch_one(connection)- .await- .unwrap()-}--#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn imports_roots_replies_empty_threads_anchors_and_remaps(pool: PgPool) {- let mut connection = legacy_database(&pool).await;- let summary = run(&mut connection, Options::default()).await.unwrap();- assert_eq!(- summary,- Summary {- documents: 2,- batches: 1,- comment_mappings_allocated: 5,- thread_mappings_allocated: 5,- // Five comments and one structural root for the empty thread.- messages_inserted: 6,- messages_updated: 0,- messages_tombstoned: 0,- threads_written: 5,- threads_tombstoned: 0,- anchors_linked: 2,- notifications_remapped: 3,- pdf_payloads_remapped: 1,- warnings: Warnings {- unmapped_notifications: 1,- unmapped_pdf_comment_ids: 1,- invalid_mark_ids: 0,- root_order_drift: 0,- },- }- );- assert!(!summary.is_noop());- assert!(- summary- .to_string()- .contains("messages inserted: 6")- );-- let comments = mappings(&mut connection).await;- let threads = thread_mappings(&mut connection).await;- assert_eq!(comments.len(), 5);- assert_eq!(threads.len(), 5);- assert!(comments.iter().all(|(_, id)| id.get_version_num() == 7));- assert!(threads.iter().all(|(_, id)| id.get_version_num() == 7));- // Roots follow legacy order: the deleted root keeps its identity as the thread root.- assert_eq!(mapped(&threads, 1), mapped(&comments, 10));- assert_eq!(mapped(&threads, 3), mapped(&comments, 12));- assert_eq!(mapped(&threads, 4), mapped(&comments, 13));- assert_eq!(mapped(&threads, 5), mapped(&comments, 14));-- let root = message(&mut connection, mapped(&comments, 10)).await;- assert_eq!(- root,- StoredMessage {- content: "Deleted root".into(),- thread_id: None,- imported_author: Some("Original author".into()),- deleted: true,- import_order: Some(1),- }- );- let reply = message(&mut connection, mapped(&comments, 11)).await;- assert_eq!(- reply,- StoredMessage {- content: "Surviving reply".into(),- thread_id: Some(mapped(&threads, 1)),- imported_author: None,- deleted: false,- import_order: Some(2),- }- );- let created_at: chrono::DateTime<chrono::Utc> = sqlx::query_scalar!(- r#"SELECT created_at FROM comms_messages WHERE id = $1"#,- mapped(&comments, 11)- )- .fetch_one(&mut connection)- .await- .unwrap();- assert_eq!(created_at.to_rfc3339(), "2020-01-02T00:00:00+00:00");-- // Markdown mark, resolution, and an unanchored Discussion thread.- assert_eq!(- thread(&mut connection, mapped(&threads, 1)).await,- StoredThread {- resolved: true,- anchor: Some(- json!({"type": "markdown", "mark_id": "01990000-0000-7000-8000-000000000002"})- ),- deleted: false,- }- );- assert_eq!(- thread(&mut connection, mapped(&threads, 5)).await,- StoredThread {- resolved: false,- anchor: None,- deleted: false,- }- );- // The empty thread is a deleted structural root and a deleted thread.- let structural = message(&mut connection, mapped(&threads, 2)).await;- assert!(structural.deleted && structural.content.is_empty() && structural.thread_id.is_none());- assert!(comments.iter().all(|(_, id)| *id != mapped(&threads, 2)));- assert!(thread(&mut connection, mapped(&threads, 2)).await.deleted);-- // Notifications point at message ids; the stale one keeps its legacy id.- let notices: Vec<Value> = sqlx::query_scalar!(- r#"SELECT metadata AS "metadata!" FROM notification ORDER BY metadata->>'commentText'"#- )- .fetch_all(&mut connection)- .await- .unwrap();- assert_eq!(notices.len(), 4);- assert_eq!(notices[0]["commentId"], 424242);- for notice in ¬ices[1..] {- assert_eq!(notice["commentId"], mapped(&comments, 11).to_string());- assert_eq!(notice["threadId"], mapped(&threads, 1).to_string());- assert_eq!(notice["commentText"], "Surviving reply");- }- let pdf: Value =- sqlx::query_scalar!(r#"SELECT "modificationData" FROM "DocumentInstanceModificationData""#)- .fetch_one(&mut connection)- .await- .unwrap();- assert_eq!(- pdf["pages"][0]["highlights"][0]["comments"],- json!([- {"id": mapped(&comments, 12), "text": "PDF original"},- {"id": "999", "text": "Stale"},- {"id": "external-pdf-id", "text": "External"}- ])- );-- // PDF anchors keep their legacy thread id and gain the root; thread state points back.- let placeable = sqlx::query!(- r#"SELECT a."threadId" AS thread_id, a.root_id, a."xPct" AS x_pct, t.anchor- FROM "PdfPlaceableCommentAnchor" a JOIN comms_message_threads t ON t.root_id = a.root_id"#- )- .fetch_one(&mut connection)- .await- .unwrap();- assert_eq!(placeable.thread_id, Some(3));- assert_eq!(placeable.root_id, Some(mapped(&threads, 3)));- assert_eq!(placeable.x_pct, 0.1);- assert_eq!(- placeable.anchor,- Some(json!({"type": "pdf_placeable", "anchor_id": "01990000-0000-7000-8000-000000000003"}))- );- let highlight = sqlx::query!(- r#"SELECT a."threadId" AS thread_id, a.root_id, a.text, r.top, t.anchor- FROM "PdfHighlightAnchor" a JOIN comms_message_threads t ON t.root_id = a.root_id- JOIN "PdfHighlightRect" r ON r."pdfHighlightAnchorId" = a.uuid"#- )- .fetch_one(&mut connection)- .await- .unwrap();- assert_eq!(highlight.thread_id, Some(4));- assert_eq!(highlight.root_id, Some(mapped(&threads, 4)));- assert_eq!(highlight.text, "Highlighted text");- assert_eq!(highlight.top, 0.1);- assert_eq!(- highlight.anchor,- Some(json!({"type": "pdf_highlight", "anchor_id": "01990000-0000-7000-8000-000000000004"}))- );-- // Legacy rows are untouched and channel messages are not affected.- let legacy_count: i64 = sqlx::query_scalar!(r#"SELECT count(*) AS "count!" FROM "Comment""#)- .fetch_one(&mut connection)- .await- .unwrap();- assert_eq!(legacy_count, 5);- let channel_messages: Vec<(String, Option<Uuid>)> = sqlx::query!(- "SELECT content, thread_id FROM comms_messages WHERE parent_entity_type = 'channel' ORDER BY id"- )- .fetch_all(&mut connection)- .await- .unwrap()- .into_iter()- .map(|row| (row.content, row.thread_id))- .collect();- assert_eq!(- channel_messages,- vec![- ("Channel root".into(), None),- (- "Channel reply".into(),- Some(Uuid::parse_str("01990000-0000-7000-8000-000000000006").unwrap())- ),- ]- );-- // The imported thread accepts new replies and edits through the message store.- let reply_id = generate_uuid_v7();- sqlx::query!(- "INSERT INTO comms_messages(id, parent_entity_type, parent_entity_id, thread_id, sender_id, content)- VALUES ($1, 'document', 'legacy-md', $2, $3, 'After import')",- reply_id,- mapped(&threads, 1),- USER- )- .execute(&mut connection)- .await- .unwrap();-- // A second run finds nothing to do and keeps every id.- let again = run(&mut connection, Options::default()).await.unwrap();- assert!(again.is_noop(), "{again:?}");- assert_eq!(again.documents, 2);- assert!(again.to_string().starts_with("Nothing to import"));- assert_eq!(again.warnings.unmapped_notifications, 1);- assert_eq!(mappings(&mut connection).await, comments);- assert_eq!(thread_mappings(&mut connection).await, threads);- assert_eq!(- message(&mut connection, reply_id).await.content,- "After import"- );-- let pending = check(&mut connection).await.unwrap();- assert_eq!(- pending,- Pending {- comments_without_mapping: 0,- threads_without_mapping: 0,- documents_with_legacy_comments: 2,- }- );-}--#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn delta_runs_carry_new_edited_and_deleted_legacy_comments(pool: PgPool) {- let mut connection = legacy_database(&pool).await;- run(&mut connection, Options::default()).await.unwrap();- let comments = mappings(&mut connection).await;- let threads = thread_mappings(&mut connection).await;-- // Legacy writers keep going: a new reply, an edit, a comment deletion without- // an updatedAt bump, a thread deletion, and a comment added to the empty thread.- sqlx::raw_sql(- r#"- INSERT INTO "Comment"(id, "threadId", owner, text, "createdAt", "updatedAt")- VALUES (15, 1, 'macro|[email protected]', 'Late reply', '2020-02-01', '2020-02-01');- UPDATE "Thread" SET "updatedAt" = '2020-02-01' WHERE id = 1;- UPDATE "Comment" SET text = 'Surviving reply, edited', "updatedAt" = '2020-02-02' WHERE id = 11;- UPDATE "Comment" SET "deletedAt" = '2020-02-03' WHERE id = 14;- UPDATE "Thread" SET "deletedAt" = '2020-02-04' WHERE id = 4;- UPDATE "Comment" SET "deletedAt" = '2020-02-04' WHERE "threadId" = 4;- INSERT INTO "Comment"(id, "threadId", owner, text, "createdAt", "updatedAt")- VALUES (16, 2, 'macro|[email protected]', 'Revived discussion', '2020-02-05', '2020-02-05');- UPDATE "Thread" SET "updatedAt" = '2020-02-05' WHERE id = 2;- "#,- )- .execute(&mut connection)- .await- .unwrap();-- let delta = run(&mut connection, Options::default()).await.unwrap();- assert_eq!(delta.comment_mappings_allocated, 2);- assert_eq!(delta.thread_mappings_allocated, 0);- assert_eq!(delta.messages_inserted, 2);- // Edited reply, deleted comment 14, deleted comment 13. The revived thread's- // structural root keeps its tombstone untouched.- assert_eq!(delta.messages_updated, 3);- assert_eq!(delta.messages_tombstoned, 0);- // Thread 1 (updated_at), thread 4 (deleted), thread 2 (revived) changed.- assert_eq!(delta.threads_written, 3);- assert_eq!(delta.warnings.root_order_drift, 1);-- let after = mappings(&mut connection).await;- assert_eq!(after.len(), 7);- assert!(after.starts_with(&comments));- assert_eq!(thread_mappings(&mut connection).await, threads);-- let late = message(&mut connection, mapped(&after, 15)).await;- assert_eq!(late.content, "Late reply");- assert_eq!(late.thread_id, Some(mapped(&threads, 1)));- assert_eq!(late.import_order, Some(3));- let edited = message(&mut connection, mapped(&after, 11)).await;- assert_eq!(edited.content, "Surviving reply, edited");- assert!(!edited.deleted);- let edited_at: Option<chrono::NaiveDateTime> = sqlx::query_scalar!(- "SELECT edited_at FROM comms_messages WHERE id = $1",- mapped(&after, 11)- )- .fetch_one(&mut connection)- .await- .unwrap();- assert_eq!(edited_at.unwrap().to_string(), "2020-02-02 00:00:00");- assert!(message(&mut connection, mapped(&after, 14)).await.deleted);- assert!(message(&mut connection, mapped(&after, 13)).await.deleted);- assert!(thread(&mut connection, mapped(&threads, 4)).await.deleted);- // The highlight keeps its root so the deleted discussion stays addressable.- let highlight_root: Option<Uuid> =- sqlx::query_scalar!(r#"SELECT root_id FROM "PdfHighlightAnchor""#)- .fetch_one(&mut connection)- .await- .unwrap();- assert_eq!(highlight_root, Some(mapped(&threads, 4)));-- // The formerly empty thread is live again: structural root stays a tombstone,- // the new comment is a reply to it.- let revived = thread(&mut connection, mapped(&threads, 2)).await;- assert!(!revived.deleted);- let revived_comment = message(&mut connection, mapped(&after, 16)).await;- assert_eq!(revived_comment.thread_id, Some(mapped(&threads, 2)));- assert!(message(&mut connection, mapped(&threads, 2)).await.deleted);-- let settled = run(&mut connection, Options::default()).await.unwrap();- assert!(settled.is_noop(), "{settled:?}");- assert_eq!(settled.warnings.root_order_drift, 1);-}--#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn newer_writes_through_the_message_store_are_never_reverted(pool: PgPool) {- let mut connection = legacy_database(&pool).await;- run(&mut connection, Options::default()).await.unwrap();- let comments = mappings(&mut connection).await;- let threads = thread_mappings(&mut connection).await;- let reply = mapped(&comments, 11);-- // The new UI edits the imported reply and resolves the Discussion thread- // after the legacy rows went stale.- sqlx::query!(- "UPDATE comms_messages SET content = 'Edited in the new UI', updated_at = now(), edited_at = now() WHERE id = $1",- reply- )- .execute(&mut connection)- .await- .unwrap();- sqlx::query!(- "UPDATE comms_message_threads SET resolved = true, updated_at = now() WHERE root_id = $1",- mapped(&threads, 5)- )- .execute(&mut connection)- .await- .unwrap();-- let rerun = run(&mut connection, Options::default()).await.unwrap();- assert!(rerun.is_noop(), "{rerun:?}");- assert_eq!(- message(&mut connection, reply).await.content,- "Edited in the new UI"- );- assert!(thread(&mut connection, mapped(&threads, 5)).await.resolved);-- // A legacy edit that is newer than the stored row still wins.- sqlx::query!(- r#"UPDATE "Comment" SET text = 'Edited again in legacy', "updatedAt" = now() + interval '1 hour' WHERE id = 11"#- )- .execute(&mut connection)- .await- .unwrap();- let legacy_wins = run(&mut connection, Options::default()).await.unwrap();- assert_eq!(legacy_wins.messages_updated, 1);- assert_eq!(- message(&mut connection, reply).await.content,- "Edited again in legacy"- );-}--#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn removed_legacy_rows_become_tombstones(pool: PgPool) {- let mut connection = legacy_database(&pool).await;- run(&mut connection, Options::default()).await.unwrap();- let comments = mappings(&mut connection).await;- let threads = thread_mappings(&mut connection).await;- sqlx::raw_sql(- r#"- DELETE FROM "Comment" WHERE id = 14;- DELETE FROM "Thread" WHERE id = 3;- "#,- )- .execute(&mut connection)- .await- .unwrap();- let summary = run(&mut connection, Options::default()).await.unwrap();- // Comment 14 alone, then thread 3 with its single comment.- assert_eq!(summary.messages_tombstoned, 2);- assert_eq!(summary.threads_tombstoned, 1);- assert!(- message(&mut connection, mapped(&comments, 14))- .await- .deleted- );- assert!(- message(&mut connection, mapped(&comments, 12))- .await- .deleted- );- assert!(thread(&mut connection, mapped(&threads, 3)).await.deleted);- assert_eq!(mappings(&mut connection).await, comments);- assert!(- run(&mut connection, Options::default())- .await- .unwrap()- .is_noop()- );-}--#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn rejects_concurrent_runner_ambiguous_anchors_and_duplicate_marks(pool: PgPool) {- let mut connection = legacy_database(&pool).await;- let mut other = pool.acquire().await.unwrap().detach();- sqlx::query!("SELECT pg_advisory_lock($1)", LOCK_ID)- .execute(&mut other)- .await- .unwrap();- assert!(- run(&mut connection, Options::default())- .await- .unwrap_err()- .to_string()- .contains("Another comment import")- );- release_import_lock(&mut other).await;- other.close().await.unwrap();-- sqlx::query!(r#"UPDATE "PdfHighlightAnchor" SET "threadId" = 3"#)- .execute(&mut connection)- .await- .unwrap();- assert!(- run(&mut connection, Options::default())- .await- .unwrap_err()- .to_string()- .contains("Multiple PDF anchors")- );- sqlx::query!(r#"UPDATE "PdfHighlightAnchor" SET "threadId" = 4"#)- .execute(&mut connection)- .await- .unwrap();-- sqlx::query!(- r#"UPDATE "Thread" SET metadata = '{"markId":"01990000-0000-7000-8000-000000000002"}' WHERE id = 5"#- )- .execute(&mut connection)- .await- .unwrap();- let error = run(&mut connection, Options::default())- .await- .unwrap_err()- .to_string();- assert!(error.contains("share the same Markdown mark"), "{error}");- assert!(error.contains("[1, 5]"), "{error}");- assert!(mappings(&mut connection).await.is_empty());-- // The lock is released after a failed preflight.- let mut probe = other_connection(&pool).await;- let acquired: bool =- sqlx::query_scalar!(r#"SELECT pg_try_advisory_lock($1) AS "acquired!""#, LOCK_ID)- .fetch_one(&mut probe)- .await- .unwrap();- assert!(acquired);- release_import_lock(&mut probe).await;- probe.close().await.unwrap();-- sqlx::query!(r#"UPDATE "Thread" SET metadata = '{"markId":"not-a-mark"}' WHERE id = 5"#)- .execute(&mut connection)- .await- .unwrap();- let summary = run(&mut connection, Options::default()).await.unwrap();- assert_eq!(summary.warnings.invalid_mark_ids, 1);- let threads = thread_mappings(&mut connection).await;- assert_eq!(- thread(&mut connection, mapped(&threads, 5)).await.anchor,- None- );-}--async fn other_connection(pool: &PgPool) -> PgConnection {- pool.acquire().await.unwrap().detach()-}--async fn release_import_lock(connection: &mut PgConnection) {- // Closing the client does not wait for PostgreSQL to release session locks.- // Await an explicit unlock before another connection tries to run the import.- assert!(- sqlx::query_scalar!(r#"SELECT pg_advisory_unlock($1) AS "released!""#, LOCK_ID)- .fetch_one(connection)- .await- .unwrap()- );-}--#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn failed_batch_rolls_back_and_can_retry(pool: PgPool) {- let mut connection = legacy_database(&pool).await;- // A discussion created through the message store already owns the mark of- // legacy thread 1, which the preflight cannot see.- let conflicting_root = generate_uuid_v7();- sqlx::query!(- "INSERT INTO comms_messages(id, parent_entity_type, parent_entity_id, sender_id, content)- VALUES ($1, 'document', 'legacy-md', $2, 'New store discussion')",- conflicting_root,- USER- )- .execute(&mut connection)- .await- .unwrap();- sqlx::query!(- r#"UPDATE comms_message_threads SET anchor = '{"type":"markdown","mark_id":"01990000-0000-7000-8000-000000000002"}' WHERE root_id = $1"#,- conflicting_root- )- .execute(&mut connection)- .await- .unwrap();-- let error = run(&mut connection, Options::default()).await.unwrap_err();- assert!(- error- .to_string()- .contains("idx_comms_message_threads_markdown_mark"),- "{error}"- );- assert!(mappings(&mut connection).await.is_empty());- assert!(thread_mappings(&mut connection).await.is_empty());- let imported: i64 = sqlx::query_scalar!(- r#"SELECT count(*) AS "count!" FROM comms_messages WHERE parent_entity_type = 'document'"#- )- .fetch_one(&mut connection)- .await- .unwrap();- assert_eq!(imported, 1);- let legacy_count: i64 = sqlx::query_scalar!(r#"SELECT count(*) AS "count!" FROM "Comment""#)- .fetch_one(&mut connection)- .await- .unwrap();- assert_eq!(legacy_count, 5);-- sqlx::query!(- "UPDATE comms_message_threads SET anchor = NULL WHERE root_id = $1",- conflicting_root- )- .execute(&mut connection)- .await- .unwrap();- let summary = run(&mut connection, Options::default()).await.unwrap();- assert_eq!(summary.comment_mappings_allocated, 5);- assert_eq!(summary.thread_mappings_allocated, 5);-}--#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn mappings_cross_batch_boundaries_and_keep_ids_on_retry(pool: PgPool) {- let mut connection = legacy_database(&pool).await;- let extra = MAPPING_CHUNK as i64 + 100;- sqlx::query!(- r#"INSERT INTO "Thread"(id, owner, "documentId", resolved)- SELECT id, $1, 'legacy-md', false FROM generate_series(100, $2::bigint) id"#,- USER,- extra- )- .execute(&mut connection)- .await- .unwrap();- sqlx::query!(- r#"INSERT INTO "Comment"(id, "threadId", owner, text)- SELECT id, id, $1, 'Batch comment' FROM generate_series(100, $2::bigint) id"#,- USER,- extra- )- .execute(&mut connection)- .await- .unwrap();- let options = Options {- documents_per_batch: 1,- };- let summary = run(&mut connection, options).await.unwrap();- assert_eq!(summary.batches, 2);- assert_eq!(summary.documents, 2);- assert_eq!(summary.comment_mappings_allocated, MAPPING_CHUNK as u64 + 6);- let first = mappings(&mut connection).await;- assert_eq!(first.len(), MAPPING_CHUNK + 6);- assert!(run(&mut connection, options).await.unwrap().is_noop());- assert_eq!(mappings(&mut connection).await, first);- let count: i64 = sqlx::query_scalar!(- r#"SELECT count(*) AS "count!" FROM comms_message_threads WHERE parent_entity_type = 'document'"#- )- .fetch_one(&mut connection)- .await- .unwrap();- assert_eq!(count, MAPPING_CHUNK as i64 + 6);-}--#[test]-fn pdf_comment_ids_are_remapped_at_any_depth_and_unmapped_ids_are_counted() {- let mapped_id = Uuid::from_u128(12);- let mappings = HashMap::from([(12_i64, mapped_id)]);- let mut payload = json!({- "placeables": [{"payload": {"comments": [- {"id": "12", "content": "a"},- {"id": "13", "content": "b"},- {"id": "wrapper", "comments": [{"id": 12}]}- ]}}],- "highlights": {"1": [{"thread": {"comments": [{"id": 12}, {"id": "external"}, "not an object"]}}]},- "comments": "not an array",- });- let stats = remap_comment_ids(&mut payload, &mappings);- // Two top-level 12s plus the 12 nested inside the "wrapper" comment.- assert_eq!(- stats,- RemapStats {- remapped: 3,- unmapped: 1,- }- );- assert_eq!(- payload["placeables"][0]["payload"]["comments"][2]["comments"][0]["id"],- mapped_id.to_string()- );- assert_eq!(- payload["placeables"][0]["payload"]["comments"][0]["id"],- mapped_id.to_string()- );- assert_eq!(- payload["placeables"][0]["payload"]["comments"][1]["id"],- "13"- );- assert_eq!(- payload["highlights"]["1"][0]["thread"]["comments"][0]["id"],- mapped_id.to_string()- );- assert_eq!(- payload["highlights"]["1"][0]["thread"]["comments"][1]["id"],- "external"- );- // A second pass rewrites nothing and still reports the id that has no mapping.- assert_eq!(- remap_comment_ids(&mut payload, &mappings),- RemapStats {- remapped: 0,- unmapped: 1,- }- );-}--#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn channel_read_projections_keep_channel_scoped_index_plans(pool: PgPool) {- let mut connection = legacy_database(&pool).await;- run(&mut connection, Options::default()).await.unwrap();- // A small target channel among many unrelated channels and non-UUID documents.- // Never disable sequential scans: test the planner's actual chosen access path.- sqlx::raw_sql(- r#"- INSERT INTO comms_channels(id, name, channel_type, owner_id)- SELECT md5('plan-channel-' || n)::uuid, 'Plan fixture', 'private', 'macro|[email protected]'- FROM generate_series(1, 200) n;- INSERT INTO comms_messages(id, parent_entity_type, parent_entity_id, sender_id, content, created_at)- SELECT md5('plan-message-' || c || '-' || n)::uuid, 'channel', md5('plan-channel-' || c)::uuid::text,- 'macro|[email protected]', 'Unrelated channel', now() - n * interval '1 second'- FROM generate_series(1, 200) c CROSS JOIN generate_series(1, 100) n;- INSERT INTO comms_messages(id, parent_entity_type, parent_entity_id, sender_id, content)- SELECT md5('plan-document-' || n)::uuid, 'document', 'legacy-md',- 'macro|[email protected]', 'Unrelated document'- FROM generate_series(1, 10000) n;- ANALYZE comms_messages;- "#,- )- .execute(&mut connection)- .await- .unwrap();- let channel = Uuid::parse_str("01990000-0000-7000-8000-000000000005").unwrap();- let queries = [- // Timeline includes tombstone roots with surviving replies, in either direction.- "SELECT m.id FROM comms_messages m WHERE m.parent_entity_type = 'channel' AND m.parent_entity_id = $1::uuid::text AND m.thread_id IS NULL- AND (m.deleted_at IS NULL OR EXISTS (SELECT 1 FROM comms_messages r WHERE r.thread_id = m.id AND r.deleted_at IS NULL))- ORDER BY m.created_at DESC, m.id DESC LIMIT 50",- "SELECT m.id FROM comms_messages m WHERE m.parent_entity_type = 'channel' AND m.parent_entity_id = $1::uuid::text AND m.thread_id IS NULL- AND (m.deleted_at IS NULL OR EXISTS (SELECT 1 FROM comms_messages r WHERE r.thread_id = m.id AND r.deleted_at IS NULL))- ORDER BY m.created_at ASC, m.id ASC LIMIT 50",- // Batched latest reads use a parameterized lateral scan for each channel.- "SELECT latest.id FROM unnest(ARRAY[$1::uuid]) i(channel_id) LEFT JOIN LATERAL (- SELECT m.id FROM comms_messages m WHERE m.parent_entity_type = 'channel' AND m.parent_entity_id = i.channel_id::text AND m.deleted_at IS NULL- ORDER BY m.created_at DESC LIMIT 1) latest ON true",- "SELECT latest.id FROM unnest(ARRAY[$1::uuid]) i(channel_id) LEFT JOIN LATERAL (- SELECT m.id FROM comms_messages m WHERE m.parent_entity_type = 'channel' AND m.parent_entity_id = i.channel_id::text AND m.deleted_at IS NULL AND m.thread_id IS NULL- ORDER BY m.created_at DESC LIMIT 1) latest ON true",- // Unfiltered reads still need a channel index, including deleted replies.- "SELECT m.id FROM comms_messages m WHERE m.parent_entity_type = 'channel' AND m.parent_entity_id = $1::uuid::text ORDER BY m.created_at DESC LIMIT 50",- ];- for query in queries {- let plan = explain(&mut connection, query, channel).await;- let mut nodes = vec![];- collect_plan_nodes(&plan[0]["Plan"], &mut nodes);- assert!(- nodes.iter().any(|node| {- node["Index Name"]- .as_str()- .is_some_and(|name| name.starts_with("idx_comms_messages_parent_"))- && node["Index Cond"]- .as_str()- .is_some_and(|condition| condition.contains("parent_entity_id"))- }),- "Channel predicate must be an index condition, not a post-scan filter: {plan:#}"- );- assert!(- nodes- .iter()- .all(|node| node["Rows Removed by Filter"].as_u64().unwrap_or(0) < 100),- "Channel reads must not filter thousands of unrelated messages: {plan:#}"- );- }-}--#[expect(- clippy::disallowed_methods,- reason = "EXPLAIN over test-provided SQL cannot be checked at compile time"-)]-async fn explain(connection: &mut PgConnection, query: &str, channel: Uuid) -> Value {- sqlx::query_scalar(&format!("EXPLAIN (ANALYZE, FORMAT JSON) {query}"))- .bind(channel)- .fetch_one(connection)- .await- .unwrap()-}--fn collect_plan_nodes<'a>(node: &'a Value, nodes: &mut Vec<&'a Value>) {- nodes.push(node);- if let Some(children) = node["Plans"].as_array() {- for child in children {- collect_plan_nodes(child, nodes);- }- }-}diff --git a/crates/macro_db_migrator/src/bin/crm_comment_import.rs b/crates/macro_db_migrator/src/bin/crm_comment_import.rsdeleted file mode 100644index 4acec505f2..0000000000--- a/crates/macro_db_migrator/src/bin/crm_comment_import.rs+++ /dev/null@@ -1,38 +0,0 @@-//! Idempotent import of legacy CRM company and contact comments into the shared-//! message store. Safe to rerun while the application keeps serving requests.--#[path = "crm_comment_import/runner.rs"]-mod runner;--use clap::Parser;-use database_env_vars::DatabaseUrl;-use macro_entrypoint::MacroEntrypoint;-use macro_env::Environment;-use sqlx::{Connection, PgConnection};--#[derive(Debug, Parser)]-#[command(- about = "Import legacy CRM comments into comms_messages while the application stays live"-)]-struct Args {- /// Run the preflight and invariants and report pending work without writing.- #[arg(long)]- check: bool,-}--#[tokio::main]-async fn main() -> Result<(), rootcause::Report> {- let args = Args::parse();- let entrypoint = MacroEntrypoint::new(Environment::Local).init();- let database_url = DatabaseUrl::new()?;- let mut connection = PgConnection::connect(database_url.as_ref()).await?;- let report = if args.check {- runner::check(&mut connection).await?- } else {- runner::run(&mut connection).await?- };- println!("{report}");- connection.close().await?;- entrypoint.shutdown();- Ok(())-}diff --git a/crates/macro_db_migrator/src/bin/crm_comment_import/runner.rs b/crates/macro_db_migrator/src/bin/crm_comment_import/runner.rsdeleted file mode 100644index 6ca3c1f8f4..0000000000--- a/crates/macro_db_migrator/src/bin/crm_comment_import/runner.rs+++ /dev/null@@ -1,336 +0,0 @@-//! Database-only operator tooling. No application request or authorization path.-//!-//! Every run projects the legacy `crm_thread` / `crm_comment` rows onto the-//! shared message tables in one transaction. A comment keeps its id as its-//! message id, and a thread's root is its first comment by creation time, so-//! reruns agree on identity without a mapping table. Rows whose projection-//! already matches are left alone, and a store row that changed after it was-//! imported is never reverted by an older legacy row.--use std::fmt;--use sqlx::{Connection, PgConnection, Postgres, Transaction};--#[cfg(test)]-mod test;--/// Distinct from the document comment importer's lock.-const LOCK_ID: i64 = 732_847_302;--/// What a run found and wrote.-#[derive(Debug, Default, Clone, PartialEq, Eq)]-pub(super) struct Report {- /// Legacy data the store cannot represent; any makes a run abort.- pub blockers: Blockers,- /// Legacy comments with no message yet, before this run.- pub pending_comments: i64,- /// Legacy comments that differ from their imported message and are at- /// least as new, before this run.- pub stale_comments: i64,- /// Legacy threads with no comments, which have nothing to import.- pub empty_threads: i64,- /// Messages this run inserted.- pub messages_inserted: u64,- /// Messages this run updated from newer legacy rows.- pub messages_updated: u64,- /// Thread rows this run inserted or updated.- pub threads_written: u64,- /// Import invariants after the run; all zero on success.- pub invariants: Invariants,-}--/// Legacy fields with no place in the message store.-#[derive(Debug, Default, Clone, PartialEq, Eq)]-pub(super) struct Blockers {- /// Threads or comments carrying client metadata.- pub metadata: i64,- /// Comments whose sender differs from their owner.- pub foreign_senders: i64,- /// Comments with an explicit display order.- pub explicit_order: i64,- /// Comment ids already used by a message this importer did not write.- pub id_collisions: i64,-}--impl Blockers {- fn any(&self) -> bool {- self.metadata + self.foreign_senders + self.explicit_order + self.id_collisions > 0- }-}--/// Checks that must all be zero once a run commits.-#[derive(Debug, Default, Clone, PartialEq, Eq)]-pub(super) struct Invariants {- /// Legacy comments in a non-empty thread with no message.- pub missing_messages: i64,- /// Imported messages on another parent or thread than their legacy comment.- pub misplaced_messages: i64,- /// Imported roots with no thread row.- pub roots_without_threads: i64,- /// Imported messages whose content, sender, creation or deletion differs- /// from a legacy comment that is at least as new.- pub content_mismatches: i64,-}--impl Invariants {- fn any(&self) -> bool {- self.missing_messages- + self.misplaced_messages- + self.roots_without_threads- + self.content_mismatches- > 0- }-}--impl fmt::Display for Report {- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {- let b = &self.blockers;- let i = &self.invariants;- writeln!(- f,- "blockers: metadata={} foreign_senders={} explicit_order={} id_collisions={}",- b.metadata, b.foreign_senders, b.explicit_order, b.id_collisions- )?;- writeln!(- f,- "pending: comments={} stale={} empty_threads={}",- self.pending_comments, self.stale_comments, self.empty_threads- )?;- writeln!(- f,- "written: messages_inserted={} messages_updated={} threads_written={}",- self.messages_inserted, self.messages_updated, self.threads_written- )?;- write!(- f,- "invariants: missing_messages={} misplaced_messages={} roots_without_threads={} content_mismatches={}",- i.missing_messages, i.misplaced_messages, i.roots_without_threads, i.content_mismatches- )- }-}--/// Report blockers, pending work and invariants without writing.-pub(super) async fn check(connection: &mut PgConnection) -> Result<Report, rootcause::Report> {- let mut tx = connection.begin().await?;- let mut report = preflight(&mut tx).await?;- report.invariants = invariants(&mut tx).await?;- tx.rollback().await?;- Ok(report)-}--/// Import everything pending in one transaction. Nothing is written when a-/// blocker is found or an invariant fails afterwards.-pub(super) async fn run(connection: &mut PgConnection) -> Result<Report, rootcause::Report> {- let mut tx = connection.begin().await?;- let acquired = sqlx::query_scalar!(- r#"SELECT pg_try_advisory_xact_lock($1) AS "acquired!""#,- LOCK_ID- )- .fetch_one(&mut *tx)- .await?;- if !acquired {- rootcause::bail!("another CRM comment import is running");- }- lock_legacy_tables(&mut tx).await?;- let mut report = preflight(&mut tx).await?;- if report.blockers.any() {- tx.rollback().await?;- rootcause::bail!("preflight blocked the import:\n{report}");- }- let (inserted, updated) = upsert_messages(&mut tx).await?;- report.messages_inserted = inserted;- report.messages_updated = updated;- report.threads_written = upsert_threads(&mut tx).await?;- report.invariants = invariants(&mut tx).await?;- if report.invariants.any() {- tx.rollback().await?;- rootcause::bail!("import invariants failed, nothing was written:\n{report}");- }- tx.commit().await?;- Ok(report)-}--/// Legacy writes wait for the run instead of landing between the snapshot-/// and the invariants.-#[expect(- clippy::disallowed_methods,- reason = "utility statements carry no result shape for SQLx to check"-)]-async fn lock_legacy_tables(tx: &mut Transaction<'_, Postgres>) -> Result<(), sqlx::Error> {- sqlx::query("SET LOCAL lock_timeout = '5s'")- .execute(&mut **tx)- .await?;- sqlx::query("LOCK TABLE crm_thread, crm_comment IN SHARE ROW EXCLUSIVE MODE")- .execute(&mut **tx)- .await?;- // The 5s timeout bounds only the table-lock wait.- sqlx::query("SET LOCAL lock_timeout = 0")- .execute(&mut **tx)- .await?;- Ok(())-}--async fn preflight(tx: &mut Transaction<'_, Postgres>) -> Result<Report, rootcause::Report> {- let row = sqlx::query!(- r#"SELECT- (SELECT count(*) FROM crm_thread WHERE metadata IS NOT NULL)- + (SELECT count(*) FROM crm_comment WHERE metadata IS NOT NULL) AS "metadata!",- (SELECT count(*) FROM crm_comment WHERE sender IS NOT NULL AND sender <> owner) AS "foreign_senders!",- (SELECT count(*) FROM crm_comment WHERE "order" IS NOT NULL) AS "explicit_order!",- (SELECT count(*) FROM crm_comment c JOIN comms_messages m ON m.id = c.id- WHERE m.import_metadata->>'source' IS DISTINCT FROM 'crm_comment') AS "id_collisions!",- (SELECT count(*) FROM crm_comment c- WHERE NOT EXISTS (SELECT 1 FROM comms_messages m WHERE m.id = c.id)) AS "pending_comments!",- (SELECT count(*) FROM crm_comment c- JOIN crm_thread t ON t.id = c.thread_id- JOIN comms_messages m ON m.id = c.id- WHERE m.import_metadata->>'source' = 'crm_comment'- AND m.updated_at <= c.updated_at- AND (m.content, m.updated_at, m.deleted_at)- IS DISTINCT FROM- (c.text, c.updated_at, COALESCE(c.deleted_at, t.deleted_at) AT TIME ZONE 'UTC')) AS "stale_comments!",- (SELECT count(*) FROM crm_thread t- WHERE NOT EXISTS (SELECT 1 FROM crm_comment c WHERE c.thread_id = t.id)) AS "empty_threads!""#- )- .fetch_one(&mut **tx)- .await?;- Ok(Report {- blockers: Blockers {- metadata: row.metadata,- foreign_senders: row.foreign_senders,- explicit_order: row.explicit_order,- id_collisions: row.id_collisions,- },- pending_comments: row.pending_comments,- stale_comments: row.stale_comments,- empty_threads: row.empty_threads,- ..Default::default()- })-}--/// Every legacy comment. A comment in a deleted thread reads as deleted. An-/// imported message changes only when the legacy row differs and is not older,-/// so an edit made through the new API is never reverted.-async fn upsert_messages(- tx: &mut Transaction<'_, Postgres>,-) -> Result<(u64, u64), rootcause::Report> {- let rows = sqlx::query_scalar!(- r#"INSERT INTO comms_messages (- id, parent_entity_type, parent_entity_id, thread_id, sender_id, content,- created_at, updated_at, edited_at, deleted_at, import_metadata- )- SELECT c.id,- CASE WHEN t.contact_id IS NULL THEN 'crm_company' ELSE 'crm_contact' END,- COALESCE(t.contact_id, t.company_id)::text,- NULLIF(first_value(c.id) OVER (PARTITION BY c.thread_id ORDER BY c.created_at, c.id), c.id),- c.owner, c.text, c.created_at, c.updated_at,- CASE WHEN c.updated_at > c.created_at THEN c.updated_at AT TIME ZONE 'UTC' END,- COALESCE(c.deleted_at, t.deleted_at) AT TIME ZONE 'UTC',- jsonb_build_object('source', 'crm_comment', 'legacy_thread_id', c.thread_id)- FROM crm_comment c- JOIN crm_thread t ON t.id = c.thread_id- ON CONFLICT (id) DO UPDATE SET- content = EXCLUDED.content,- updated_at = EXCLUDED.updated_at,- edited_at = EXCLUDED.edited_at,- deleted_at = EXCLUDED.deleted_at- WHERE comms_messages.import_metadata->>'source' = 'crm_comment'- AND comms_messages.updated_at <= EXCLUDED.updated_at- AND (comms_messages.content, comms_messages.updated_at, comms_messages.edited_at,- comms_messages.deleted_at)- IS DISTINCT FROM- (EXCLUDED.content, EXCLUDED.updated_at, EXCLUDED.edited_at, EXCLUDED.deleted_at)- RETURNING (xmax = 0) AS "inserted!""#- )- .fetch_all(&mut **tx)- .await?;- let inserted = rows.iter().filter(|inserted| **inserted).count() as u64;- Ok((inserted, rows.len() as u64 - inserted))-}--/// One thread row per non-empty legacy thread, keyed by its root comment, with-/// the same newer-wins rule as messages. Inserting a root makes the store's-/// trigger initialize a bare thread row; the id-collision blocker guarantees any-/// such row on an imported root came from this run, so it is taken over.-async fn upsert_threads(tx: &mut Transaction<'_, Postgres>) -> Result<u64, rootcause::Report> {- let written = sqlx::query!(- r#"INSERT INTO comms_message_threads (- root_id, parent_entity_type, parent_entity_id, user_id, resolved,- import_metadata, created_at, updated_at, deleted_at- )- SELECT root.id,- CASE WHEN t.contact_id IS NULL THEN 'crm_company' ELSE 'crm_contact' END,- COALESCE(t.contact_id, t.company_id)::text,- t.owner, t.resolved,- jsonb_build_object('source', 'crm_thread', 'legacy_thread_id', t.id),- t.created_at, t.updated_at, t.deleted_at- FROM crm_thread t- CROSS JOIN LATERAL (- SELECT c.id FROM crm_comment c- WHERE c.thread_id = t.id- ORDER BY c.created_at, c.id- LIMIT 1- ) root- ON CONFLICT (root_id) DO UPDATE SET- user_id = EXCLUDED.user_id,- resolved = EXCLUDED.resolved,- import_metadata = EXCLUDED.import_metadata,- created_at = EXCLUDED.created_at,- updated_at = EXCLUDED.updated_at,- deleted_at = EXCLUDED.deleted_at- WHERE (comms_message_threads.import_metadata IS NULL- OR (comms_message_threads.import_metadata->>'source' = 'crm_thread'- AND comms_message_threads.updated_at <= EXCLUDED.updated_at))- AND (comms_message_threads.import_metadata,- comms_message_threads.user_id, comms_message_threads.resolved,- comms_message_threads.created_at, comms_message_threads.updated_at,- comms_message_threads.deleted_at)- IS DISTINCT FROM- (EXCLUDED.import_metadata, EXCLUDED.user_id, EXCLUDED.resolved,- EXCLUDED.created_at, EXCLUDED.updated_at, EXCLUDED.deleted_at)"#- )- .execute(&mut **tx)- .await?- .rows_affected();- Ok(written)-}--async fn invariants(tx: &mut Transaction<'_, Postgres>) -> Result<Invariants, rootcause::Report> {- let row = sqlx::query!(- r#"WITH legacy AS (- SELECT c.id, c.text, c.owner, c.created_at, c.updated_at,- COALESCE(c.deleted_at, t.deleted_at) AT TIME ZONE 'UTC' AS deleted_at,- CASE WHEN t.contact_id IS NULL THEN 'crm_company' ELSE 'crm_contact' END AS parent_type,- COALESCE(t.contact_id, t.company_id)::text AS parent_id,- NULLIF(first_value(c.id) OVER (PARTITION BY c.thread_id ORDER BY c.created_at, c.id), c.id) AS root_id- FROM crm_comment c- JOIN crm_thread t ON t.id = c.thread_id- )- SELECT- count(*) FILTER (WHERE m.id IS NULL) AS "missing_messages!",- count(*) FILTER (- WHERE m.id IS NOT NULL- AND (m.parent_entity_type, m.parent_entity_id, m.thread_id)- IS DISTINCT FROM (l.parent_type, l.parent_id, l.root_id)- ) AS "misplaced_messages!",- count(*) FILTER (- WHERE m.id IS NOT NULL AND l.root_id IS NULL- AND NOT EXISTS (SELECT 1 FROM comms_message_threads th WHERE th.root_id = m.id)- ) AS "roots_without_threads!",- count(*) FILTER (- WHERE m.updated_at <= l.updated_at- AND (m.content, m.sender_id, m.created_at, m.deleted_at)- IS DISTINCT FROM (l.text, l.owner, l.created_at, l.deleted_at)- ) AS "content_mismatches!"- FROM legacy l- LEFT JOIN comms_messages m ON m.id = l.id"#- )- .fetch_one(&mut **tx)- .await?;- Ok(Invariants {- missing_messages: row.missing_messages,- misplaced_messages: row.misplaced_messages,- roots_without_threads: row.roots_without_threads,- content_mismatches: row.content_mismatches,- })-}diff --git a/crates/macro_db_migrator/src/bin/crm_comment_import/test.rs b/crates/macro_db_migrator/src/bin/crm_comment_import/test.rsdeleted file mode 100644index 195a017c62..0000000000--- a/crates/macro_db_migrator/src/bin/crm_comment_import/test.rs+++ /dev/null@@ -1,452 +0,0 @@-use super::*;-use macro_db_migrator::MACRO_DB_MIGRATIONS;-use macro_uuid::Uuid;-use serde_json::{Value, json};-use sqlx::PgPool;--const OWNER: &str = "macro|[email protected]";-const MEMBER: &str = "macro|[email protected]";--struct Crm {- company: Uuid,- contact: Uuid,-}--async fn user(pool: &PgPool, id: &str) {- let macro_user = Uuid::now_v7();- sqlx::query(- "INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, $2, $2, $3)",- )- .bind(macro_user)- .bind(id)- .bind(format!("stripe_{macro_user}"))- .execute(pool)- .await- .unwrap();- sqlx::query(r#"INSERT INTO "User" (id, email, macro_user_id) VALUES ($1, $1, $2)"#)- .bind(id)- .bind(macro_user)- .execute(pool)- .await- .unwrap();-}--async fn crm(pool: &PgPool) -> Crm {- user(pool, OWNER).await;- user(pool, MEMBER).await;- let team = Uuid::now_v7();- sqlx::query("INSERT INTO team (id, name, owner_id) VALUES ($1, 'team', $2)")- .bind(team)- .bind(OWNER)- .execute(pool)- .await- .unwrap();- let company = Uuid::now_v7();- sqlx::query(- "INSERT INTO crm_companies (id, team_id, email_sync, first_interaction, last_interaction)- VALUES ($1, $2, true, now(), now())",- )- .bind(company)- .bind(team)- .execute(pool)- .await- .unwrap();- let contact = Uuid::now_v7();- sqlx::query(- "INSERT INTO crm_contacts (id, company_id, email, first_interaction, last_interaction)- VALUES ($1, $2, '[email protected]', now(), now())",- )- .bind(contact)- .bind(company)- .execute(pool)- .await- .unwrap();- Crm { company, contact }-}--enum Record {- Company(Uuid),- Contact(Uuid),-}--async fn thread(pool: &PgPool, record: Record, owner: &str, resolved: bool) -> Uuid {- let (company, contact) = match record {- Record::Company(id) => (Some(id), None),- Record::Contact(id) => (None, Some(id)),- };- let id = Uuid::now_v7();- sqlx::query(- "INSERT INTO crm_thread (id, company_id, contact_id, owner, resolved, created_at, updated_at)- VALUES ($1, $2, $3, $4, $5, now() - interval '3 hours', now() - interval '3 hours')",- )- .bind(id)- .bind(company)- .bind(contact)- .bind(owner)- .bind(resolved)- .execute(pool)- .await- .unwrap();- id-}--/// A comment created `hours_ago`, last updated `updated_hours_ago`.-async fn comment(- pool: &PgPool,- thread: Uuid,- owner: &str,- text: &str,- hours_ago: i32,- updated_hours_ago: i32,-) -> Uuid {- let id = Uuid::now_v7();- sqlx::query(- "INSERT INTO crm_comment (id, thread_id, owner, sender, text, created_at, updated_at)- VALUES ($1, $2, $3, $3, $4,- now() - make_interval(hours => $5), now() - make_interval(hours => $6))",- )- .bind(id)- .bind(thread)- .bind(owner)- .bind(text)- .bind(hours_ago)- .bind(updated_hours_ago)- .execute(pool)- .await- .unwrap();- id-}--#[derive(Debug, PartialEq)]-struct Stored {- parent: (String, String),- thread_id: Option<Uuid>,- sender: String,- content: String,- edited: bool,- deleted: bool,- import_metadata: Option<Value>,-}--async fn message(pool: &PgPool, id: Uuid) -> Stored {- let row: (- String,- String,- Option<Uuid>,- String,- String,- bool,- bool,- Option<Value>,- ) = sqlx::query_as(- "SELECT parent_entity_type, parent_entity_id, thread_id, sender_id, content,- edited_at IS NOT NULL, deleted_at IS NOT NULL, import_metadata- FROM comms_messages WHERE id = $1",- )- .bind(id)- .fetch_one(pool)- .await- .unwrap();- Stored {- parent: (row.0, row.1),- thread_id: row.2,- sender: row.3,- content: row.4,- edited: row.5,- deleted: row.6,- import_metadata: row.7,- }-}--#[derive(Debug, PartialEq)]-struct StoredThread {- parent: (String, String),- user_id: String,- resolved: bool,- deleted: bool,-}--async fn stored_thread(pool: &PgPool, root: Uuid) -> Option<StoredThread> {- sqlx::query_as::<_, (String, String, String, bool, bool)>(- "SELECT parent_entity_type, parent_entity_id, user_id, resolved, deleted_at IS NOT NULL- FROM comms_message_threads WHERE root_id = $1",- )- .bind(root)- .fetch_optional(pool)- .await- .unwrap()- .map(|row| StoredThread {- parent: (row.0, row.1),- user_id: row.2,- resolved: row.3,- deleted: row.4,- })-}--async fn message_count(pool: &PgPool) -> i64 {- sqlx::query_scalar("SELECT count(*) FROM comms_messages")- .fetch_one(pool)- .await- .unwrap()-}--#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn legacy_threads_become_discussions_keyed_by_their_first_comment(pool: PgPool) {- let crm = crm(&pool).await;- let company_thread = thread(&pool, Record::Company(crm.company), OWNER, true).await;- let root = comment(&pool, company_thread, OWNER, "root", 3, 3).await;- let edited_reply = comment(&pool, company_thread, MEMBER, "edited reply", 2, 1).await;- let contact_thread = thread(&pool, Record::Contact(crm.contact), MEMBER, false).await;- let contact_root = comment(&pool, contact_thread, MEMBER, "on the contact", 2, 2).await;- let deleted_reply = comment(&pool, contact_thread, OWNER, "gone", 1, 1).await;- sqlx::query("UPDATE crm_comment SET deleted_at = now() WHERE id = $1")- .bind(deleted_reply)- .execute(&pool)- .await- .unwrap();-- let mut connection = pool.acquire().await.unwrap();- let report = run(&mut connection).await.unwrap();-- assert_eq!(report.blockers, Blockers::default());- assert_eq!(report.invariants, Invariants::default());- assert_eq!(report.pending_comments, 4);- assert_eq!((report.messages_inserted, report.messages_updated), (4, 0));- assert_eq!(report.threads_written, 2);-- let company = ("crm_company".to_owned(), crm.company.to_string());- let contact = ("crm_contact".to_owned(), crm.contact.to_string());- assert_eq!(- message(&pool, root).await,- Stored {- parent: company.clone(),- thread_id: None,- sender: OWNER.to_owned(),- content: "root".to_owned(),- edited: false,- deleted: false,- import_metadata: Some(- json!({"source": "crm_comment", "legacy_thread_id": company_thread})- ),- }- );- assert_eq!(- message(&pool, edited_reply).await,- Stored {- parent: company.clone(),- thread_id: Some(root),- sender: MEMBER.to_owned(),- content: "edited reply".to_owned(),- edited: true,- deleted: false,- import_metadata: Some(- json!({"source": "crm_comment", "legacy_thread_id": company_thread})- ),- }- );- assert_eq!(message(&pool, contact_root).await.parent, contact);- let deleted = message(&pool, deleted_reply).await;- assert_eq!(- (deleted.thread_id, deleted.deleted),- (Some(contact_root), true)- );-- assert_eq!(- stored_thread(&pool, root).await,- Some(StoredThread {- parent: company,- user_id: OWNER.to_owned(),- resolved: true,- deleted: false,- })- );- assert_eq!(- stored_thread(&pool, contact_root).await,- Some(StoredThread {- parent: contact,- user_id: MEMBER.to_owned(),- resolved: false,- deleted: false,- })- );- assert_eq!(stored_thread(&pool, edited_reply).await, None);-- let rerun = run(&mut connection).await.unwrap();- assert_eq!(- (rerun.pending_comments, rerun.stale_comments),- (0, 0),- "{rerun}"- );- assert_eq!(- (- rerun.messages_inserted,- rerun.messages_updated,- rerun.threads_written- ),- (0, 0, 0)- );-}--#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn a_deleted_legacy_thread_deletes_every_message(pool: PgPool) {- let crm = crm(&pool).await;- let legacy = thread(&pool, Record::Company(crm.company), OWNER, false).await;- let root = comment(&pool, legacy, OWNER, "root", 2, 2).await;- let reply = comment(&pool, legacy, OWNER, "reply", 1, 1).await;- sqlx::query("UPDATE crm_thread SET deleted_at = now() WHERE id = $1")- .bind(legacy)- .execute(&pool)- .await- .unwrap();-- let mut connection = pool.acquire().await.unwrap();- run(&mut connection).await.unwrap();-- assert!(message(&pool, root).await.deleted);- assert!(message(&pool, reply).await.deleted);- assert!(stored_thread(&pool, root).await.unwrap().deleted);-}--#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn newer_legacy_rows_update_and_newer_store_rows_win(pool: PgPool) {- let crm = crm(&pool).await;- let legacy = thread(&pool, Record::Company(crm.company), OWNER, false).await;- let root = comment(&pool, legacy, OWNER, "root", 2, 2).await;- let reply = comment(&pool, legacy, OWNER, "reply", 1, 1).await;- let mut connection = pool.acquire().await.unwrap();- run(&mut connection).await.unwrap();-- sqlx::query("UPDATE crm_comment SET text = 'edited in legacy' WHERE id = $1")- .bind(root)- .execute(&pool)- .await- .unwrap();- sqlx::query("UPDATE crm_thread SET resolved = true WHERE id = $1")- .bind(legacy)- .execute(&pool)- .await- .unwrap();- sqlx::query(- "UPDATE comms_messages SET content = 'edited in store', updated_at = now() + interval '1 hour'- WHERE id = $1",- )- .bind(reply)- .execute(&pool)- .await- .unwrap();-- let check = check(&mut connection).await.unwrap();- assert_eq!(check.stale_comments, 1);- assert_eq!(check.invariants.content_mismatches, 1);-- let report = run(&mut connection).await.unwrap();- assert_eq!((report.messages_inserted, report.messages_updated), (0, 1));- assert_eq!(report.threads_written, 1);- assert_eq!(report.invariants, Invariants::default());- let root = message(&pool, root).await;- assert_eq!(- (root.content.as_str(), root.edited),- ("edited in legacy", true)- );- assert_eq!(message(&pool, reply).await.content, "edited in store");- assert!(- stored_thread(&pool, root_id(&pool, legacy).await)- .await- .unwrap()- .resolved- );-}--async fn root_id(pool: &PgPool, legacy_thread: Uuid) -> Uuid {- sqlx::query_scalar(- "SELECT root_id FROM comms_message_threads- WHERE import_metadata->>'legacy_thread_id' = $1::text",- )- .bind(legacy_thread)- .fetch_one(pool)- .await- .unwrap()-}--#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn unrepresentable_legacy_rows_block_the_whole_import(pool: PgPool) {- let crm = crm(&pool).await;- let legacy = thread(&pool, Record::Company(crm.company), OWNER, false).await;- comment(&pool, legacy, OWNER, "fine", 2, 2).await;- let foreign = comment(&pool, legacy, OWNER, "sent for someone", 1, 1).await;- sqlx::query("UPDATE crm_comment SET sender = $2 WHERE id = $1")- .bind(foreign)- .bind(MEMBER)- .execute(&pool)- .await- .unwrap();- sqlx::query("UPDATE crm_thread SET metadata = '{}' WHERE id = $1")- .bind(legacy)- .execute(&pool)- .await- .unwrap();-- let mut connection = pool.acquire().await.unwrap();- let error = run(&mut connection).await.unwrap_err();-- assert!(error.to_string().contains("preflight blocked"), "{error}");- assert_eq!(message_count(&pool).await, 0);- let report = check(&mut connection).await.unwrap();- assert_eq!(- report.blockers,- Blockers {- metadata: 1,- foreign_senders: 1,- explicit_order: 0,- id_collisions: 0,- }- );-}--#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn a_comment_id_used_by_another_message_blocks_the_import(pool: PgPool) {- let crm = crm(&pool).await;- let legacy = thread(&pool, Record::Company(crm.company), OWNER, false).await;- let id = comment(&pool, legacy, OWNER, "legacy", 1, 1).await;- sqlx::query(- "INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, sender_id, content)- VALUES ($1, 'crm_company', $2, $3, 'posted through the store')",- )- .bind(id)- .bind(crm.company.to_string())- .bind(OWNER)- .execute(&pool)- .await- .unwrap();-- let mut connection = pool.acquire().await.unwrap();- assert!(run(&mut connection).await.is_err());-- assert_eq!(- check(&mut connection).await.unwrap().blockers.id_collisions,- 1- );- assert_eq!(message(&pool, id).await.content, "posted through the store");-}--#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn check_writes_nothing_and_empty_threads_are_skipped(pool: PgPool) {- let crm = crm(&pool).await;- thread(&pool, Record::Company(crm.company), OWNER, false).await;- let legacy = thread(&pool, Record::Contact(crm.contact), OWNER, false).await;- comment(&pool, legacy, OWNER, "only comment", 1, 1).await;-- let mut connection = pool.acquire().await.unwrap();- let report = check(&mut connection).await.unwrap();- assert_eq!((report.pending_comments, report.empty_threads), (1, 1));- assert_eq!(report.invariants.missing_messages, 1);- assert_eq!(message_count(&pool).await, 0);-- let report = run(&mut connection).await.unwrap();- assert_eq!(report.threads_written, 1);- let threads: i64 = sqlx::query_scalar("SELECT count(*) FROM comms_message_threads")- .fetch_one(&pool)- .await- .unwrap();- assert_eq!(threads, 1);-}diff --git a/crates/macro_db_migrator/src/lib.rs b/crates/macro_db_migrator/src/lib.rsindex df03b53299..0bf932f0da 100644--- a/crates/macro_db_migrator/src/lib.rs+++ b/crates/macro_db_migrator/src/lib.rs@@ -3,3 +3,6 @@//! We explicitly do not want these migrations to exist as part of macro_db_client crate because that crate is very heavy.pub static MACRO_DB_MIGRATIONS: sqlx::migrate::Migrator =sqlx::migrate!("../macro_db_client/migrations");++#[cfg(test)]+mod test;diff --git a/crates/macro_db_migrator/src/test.rs b/crates/macro_db_migrator/src/test.rsnew file mode 100644index 0000000000..e2031a95c4--- /dev/null+++ b/crates/macro_db_migrator/src/test.rs@@ -0,0 +1,57 @@+use super::MACRO_DB_MIGRATIONS;+use sqlx::{PgPool, migrate::Migrator};+use std::borrow::Cow;++const SCHEMA_DROP_VERSION: i64 = 20261002150422;++async fn before_drop(pool: &PgPool) {+ let migrator = Migrator {+ migrations: Cow::Owned(+ MACRO_DB_MIGRATIONS+ .iter()+ .filter(|migration| migration.version < SCHEMA_DROP_VERSION)+ .cloned()+ .collect(),+ ),+ ..Migrator::DEFAULT+ };+ migrator.run(pool).await.unwrap();+ // These fixture queries intentionally target the historical schema, which+ // cannot be checked against the current compile-time database.+ sqlx::raw_sql(include_str!("test/before_schema_drop.sql"))+ .execute(pool)+ .await+ .unwrap();+}++#[sqlx::test(migrations = false)]+async fn schema_drop_preserves_imported_rows_and_parent_cascades(pool: PgPool) {+ before_drop(&pool).await;+ MACRO_DB_MIGRATIONS.run(&pool).await.unwrap();+ sqlx::raw_sql(include_str!("test/after_schema_drop.sql"))+ .execute(&pool)+ .await+ .unwrap();+ // The SQLx migration ledger must also make subsequent runs no-ops.+ MACRO_DB_MIGRATIONS.run(&pool).await.unwrap();+}++#[sqlx::test(migrations = false)]+async fn schema_drop_rejects_rootless_placeables_atomically(pool: PgPool) {+ before_drop(&pool).await;+ sqlx::raw_sql(r#"UPDATE "PdfPlaceableCommentAnchor" SET root_id = NULL"#)+ .execute(&pool)+ .await+ .unwrap();+ assert!(MACRO_DB_MIGRATIONS.run(&pool).await.is_err());+ // PostgreSQL rolls back the preceding function/column changes as well.+ sqlx::raw_sql(+ r#"SELECT channel_id FROM comms_messages;+ SELECT "threadId" FROM "PdfPlaceableCommentAnchor";+ SELECT * FROM "Comment";+ SELECT 'sync_comms_message_parent()'::regprocedure;"#,+ )+ .execute(&pool)+ .await+ .unwrap();+}diff --git a/crates/macro_db_migrator/src/test/after_schema_drop.sql b/crates/macro_db_migrator/src/test/after_schema_drop.sqlnew file mode 100644index 0000000000..78199b14ae--- /dev/null+++ b/crates/macro_db_migrator/src/test/after_schema_drop.sql@@ -0,0 +1,51 @@+DO $$+BEGIN+ IF (SELECT count(*) FROM comms_messages) <> 3+ OR (SELECT count(*) FROM comms_message_threads) <> 3+ OR (SELECT count(*) FROM comms_attachments) <> 1+ OR (SELECT count(*) FROM migrated_comment_id WHERE comment_id = 2 AND document_id = 'drop-doc') <> 1+ OR (SELECT count(*) FROM migrated_comment_thread_id WHERE thread_id = 1 AND document_id = 'drop-doc') <> 1 THEN+ RAISE EXCEPTION 'migration lost messages, attachments, threads, or legacy mappings';+ END IF;+ IF NOT EXISTS (SELECT 1 FROM "PdfPlaceableCommentAnchor" WHERE root_id = '019a0000-0000-7000-8000-000000000003' AND "xPct" = 0.1)+ OR NOT EXISTS (SELECT 1 FROM "PdfHighlightAnchor" WHERE root_id = '019a0000-0000-7000-8000-000000000003' AND text = 'text') THEN+ RAISE EXCEPTION 'migration lost PDF geometry or root linkage';+ END IF;+ IF to_regclass('"Comment"') IS NOT NULL OR to_regclass('"Thread"') IS NOT NULL+ OR to_regclass('"ThreadAnchor"') IS NOT NULL OR to_regclass('crm_comment') IS NOT NULL+ OR to_regclass('crm_thread') IS NOT NULL THEN+ RAISE EXCEPTION 'legacy tables remain';+ END IF;+ IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'public'+ AND ((table_name IN ('comms_messages', 'comms_attachments') AND column_name = 'channel_id')+ OR (table_name IN ('PdfPlaceableCommentAnchor', 'PdfHighlightAnchor') AND column_name = 'threadId'))) THEN+ RAISE EXCEPTION 'legacy columns remain';+ END IF;+ -- NEW.channel_id is valid on unrelated tables such as import_target;+ -- only message/attachment triggers lose that record field in this drop.+ IF EXISTS (SELECT 1 FROM pg_proc p WHERE pronamespace = 'public'::regnamespace+ AND ((prosrc LIKE '%NEW.channel_id%' AND EXISTS (+ SELECT 1 FROM pg_trigger t WHERE t.tgfoid = p.oid+ AND t.tgrelid IN ('comms_messages'::regclass, 'comms_attachments'::regclass)))+ OR prosrc LIKE '%"threadId"%' OR prosrc LIKE '%crm_comment%')) THEN+ RAISE EXCEPTION 'function still depends on retired schema';+ END IF;+ IF to_regclass('idx_comms_messages_parent_timeline') IS NULL+ OR to_regclass('idx_comms_messages_parent_history') IS NULL+ OR to_regclass('idx_comms_messages_parent_activity') IS NULL+ OR to_regclass('idx_comms_attachments_entity_created') IS NULL THEN+ RAISE EXCEPTION 'required lookup index missing';+ END IF;+END;+$$;++UPDATE comms_messages SET deleted_at = now() WHERE id = '019a0000-0000-7000-8000-000000000003';+DELETE FROM comms_channels WHERE id = '019a0000-0000-7000-8000-000000000002';+DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM notification) OR EXISTS (SELECT 1 FROM comms_attachments)+ OR (SELECT count(*) FROM comms_messages) <> 2 OR (SELECT count(*) FROM comms_message_threads) <> 2 THEN+ RAISE EXCEPTION 'notification cleanup or channel deletion cascade failed';+ END IF;+END;+$$;diff --git a/crates/macro_db_migrator/src/test/before_schema_drop.sql b/crates/macro_db_migrator/src/test/before_schema_drop.sqlnew file mode 100644index 0000000000..8fa5d20f1c--- /dev/null+++ b/crates/macro_db_migrator/src/test/before_schema_drop.sql@@ -0,0 +1,40 @@+INSERT INTO macro_user (id, username, email, stripe_customer_id)+VALUES ('019a0000-0000-7000-8000-000000000001', 'drop-test', '[email protected]', 'drop-test');+INSERT INTO "User" (id, email, macro_user_id)+VALUES ('macro|[email protected]', '[email protected]', '019a0000-0000-7000-8000-000000000001');+INSERT INTO "Document" (id, name, owner, "fileType")+VALUES ('drop-doc', 'drop.pdf', 'macro|[email protected]', 'pdf');+INSERT INTO comms_channels (id, channel_type, owner_id)+VALUES ('019a0000-0000-7000-8000-000000000002', 'private', 'macro|[email protected]');+INSERT INTO "Thread" (id, owner, "documentId") VALUES (1, 'macro|[email protected]', 'drop-doc');+INSERT INTO "Comment" (id, "threadId", owner, text) VALUES (2, 1, 'macro|[email protected]', 'imported');+INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, sender_id, content)+VALUES ('019a0000-0000-7000-8000-000000000003', 'document', 'drop-doc', 'macro|[email protected]', 'imported'),+ ('019a0000-0000-7000-8000-000000000004', 'channel', '019a0000-0000-7000-8000-000000000002', 'macro|[email protected]', 'channel');+INSERT INTO migrated_comment_id VALUES (2, '019a0000-0000-7000-8000-000000000003', 'drop-doc');+INSERT INTO migrated_comment_thread_id VALUES (1, '019a0000-0000-7000-8000-000000000003', 'drop-doc');+INSERT INTO comms_attachments (id, message_id, entity_type, entity_id, channel_id)+VALUES ('019a0000-0000-7000-8000-000000000005', '019a0000-0000-7000-8000-000000000004', 'document', 'drop-doc', '019a0000-0000-7000-8000-000000000002');+INSERT INTO "PdfPlaceableCommentAnchor" (+ uuid, "documentId", owner, page, "wasEdited", "wasDeleted", "shouldLockOnSave",+ "originalPage", "originalIndex", "xPct", "yPct", "widthPct", "heightPct", rotation, "threadId", root_id+) VALUES ('019a0000-0000-7000-8000-000000000006', 'drop-doc', 'macro|[email protected]', 1, false, false, false,+ 1, 0, 0.1, 0.2, 0.3, 0.4, 0, 1, '019a0000-0000-7000-8000-000000000003');+INSERT INTO "PdfHighlightAnchor" (+ uuid, "documentId", owner, page, red, green, blue, alpha, type, text,+ "pageViewportWidth", "pageViewportHeight", "threadId", root_id+) VALUES ('019a0000-0000-7000-8000-000000000007', 'drop-doc', 'macro|[email protected]', 1, 255, 255, 0, 0.5, 1, 'text',+ 600, 800, 1, '019a0000-0000-7000-8000-000000000003');+INSERT INTO notification (id, notification_event_type, event_item_id, event_item_type, service_sender, metadata)+VALUES ('019a0000-0000-7000-8000-000000000008', 'commented_on_document', 'drop-doc', 'document', 'test',+ '{"commentId":"019a0000-0000-7000-8000-000000000003"}');+INSERT INTO team (id, name, owner_id)+VALUES ('019a0000-0000-7000-8000-000000000009', 'drop team', 'macro|[email protected]');+INSERT INTO crm_companies (id, team_id, first_interaction, last_interaction)+VALUES ('019a0000-0000-7000-8000-000000000010', '019a0000-0000-7000-8000-000000000009', now(), now());+INSERT INTO crm_thread (id, company_id, owner)+VALUES ('019a0000-0000-7000-8000-000000000011', '019a0000-0000-7000-8000-000000000010', 'macro|[email protected]');+INSERT INTO crm_comment (id, thread_id, owner, text)+VALUES ('019a0000-0000-7000-8000-000000000012', '019a0000-0000-7000-8000-000000000011', 'macro|[email protected]', 'CRM imported');+INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, sender_id, content)+VALUES ('019a0000-0000-7000-8000-000000000012', 'crm_company', '019a0000-0000-7000-8000-000000000010', 'macro|[email protected]', 'CRM imported');diff --git a/crates/messages/src/outbound/pg_message_repo.rs b/crates/messages/src/outbound/pg_message_repo.rsindex dc7b18980a..7e3d811815 100644--- a/crates/messages/src/outbound/pg_message_repo.rs+++ b/crates/messages/src/outbound/pg_message_repo.rs@@ -415,7 +415,7 @@ impl PgMessageRepository {Some(NewThreadAnchor::PdfHighlight { anchor_id }) => {let changed = sqlx::query!(r#"UPDATE "PdfHighlightAnchor" SET root_id = $1- WHERE uuid = $2 AND "documentId" = $3 AND root_id IS NULL AND "threadId" IS NULL+ WHERE uuid = $2 AND "documentId" = $3 AND root_id IS NULLAND "deletedAt" IS NULL"#,root_id,anchor_id,diff --git a/crates/messages/src/outbound/pg_message_repo/historical.rs b/crates/messages/src/outbound/pg_message_repo/historical.rsindex c5f7569031..14ded3c930 100644--- a/crates/messages/src/outbound/pg_message_repo/historical.rs+++ b/crates/messages/src/outbound/pg_message_repo/historical.rs@@ -22,16 +22,16 @@ impl crate::domain::ports::HistoricalMessageReader for PgMessageRepository {}sqlx::query_as!(HistoricalMessageTarget,- r#"SELECT m.id AS message_id, m.channel_id AS "channel_id!", t.root_id+ r#"SELECT m.id AS message_id, m.parent_entity_id::uuid AS "channel_id!", t.root_idFROM comms_messages mJOIN comms_message_threads t ON t.root_id = coalesce(m.thread_id, m.id)JOIN comms_messages root ON root.id = t.root_idWHERE m.id = ANY($1) AND m.deleted_at IS NULL AND t.deleted_at IS NULLAND root.deleted_at IS NULL AND root.thread_id IS NULL- AND m.parent_entity_type = 'channel' AND m.parent_entity_id = m.channel_id::text- AND t.parent_entity_type = 'channel' AND t.parent_entity_id = m.channel_id::text- AND root.channel_id = m.channel_id AND root.parent_entity_type = 'channel'- AND root.parent_entity_id = m.channel_id::text"#,+ AND m.parent_entity_type = 'channel'+ AND t.parent_entity_type = 'channel' AND t.parent_entity_id = m.parent_entity_id+ AND root.parent_entity_type = 'channel'+ AND root.parent_entity_id = m.parent_entity_id"#,ids,).fetch_all(&self.pool)@@ -67,7 +67,7 @@ impl PgMessageRepository {});Ok(sqlx::query!(r#"UPDATE comms_messages SET content = $5- WHERE id = $1 AND channel_id = $2+ WHERE id = $1AND parent_entity_type = 'channel' AND parent_entity_id = $2::textAND content = $3 AND content <> $5 AND import_metadata @> $4AND deleted_at IS NULL AND edited_at IS NULL AND updated_at = created_at@@ -75,7 +75,7 @@ impl PgMessageRepository {WHERE t.root_id = coalesce(comms_messages.thread_id, comms_messages.id)AND t.deleted_at IS NOT NULL)"#,patch.message_id,- patch.channel_id,+ patch.channel_id.to_string(),patch.expected_body,guard,patch.body,@@ -135,12 +135,12 @@ async fn insert_batch(r#"SELECT t.root_id FROM comms_message_threads tJOIN comms_messages m ON m.id = t.root_idWHERE t.root_id = ANY($1) AND t.parent_entity_type = 'channel'- AND t.parent_entity_id = $2 AND m.channel_id = $3+ AND t.parent_entity_id = $2+ AND m.parent_entity_type = 'channel' AND m.parent_entity_id = $2AND m.thread_id IS NULL AND m.deleted_at IS NULL AND t.deleted_at IS NULLORDER BY t.root_id FOR UPDATE OF t, m"#,&root_ids,batch.channel_id.to_string(),- batch.channel_id,).fetch_all(&mut **tx).await@@ -196,18 +196,17 @@ async fn insert_messages(// Its created_at/updated_at values come from these backdated message columns.let result = sqlx::query!(r#"INSERT INTO comms_messages- (id, channel_id, parent_entity_type, parent_entity_id, thread_id,+ (id, parent_entity_type, parent_entity_id, thread_id,sender_id, imported_author, content, created_at, updated_at, edited_at,import_metadata, import_order)- SELECT input.id, $1, 'channel', $2, input.thread_id, input.sender_id,+ SELECT input.id, 'channel', $1, input.thread_id, input.sender_id,input.imported_author, input.content, input.created_at, input.updated_at,input.edited_at, input.import_metadata, input.import_order- FROM UNNEST($3::uuid[], $4::uuid[], $5::text[], $6::text[], $7::text[],- $8::timestamptz[], $9::timestamptz[], $10::timestamp[], $11::jsonb[], $12::bigint[])+ FROM UNNEST($2::uuid[], $3::uuid[], $4::text[], $5::text[], $6::text[],+ $7::timestamptz[], $8::timestamptz[], $9::timestamp[], $10::jsonb[], $11::bigint[])AS input(id, thread_id, sender_id, imported_author, content, created_at,updated_at, edited_at, import_metadata, import_order)ON CONFLICT (id) DO NOTHING"#,- channel_id,channel_id.to_string(),&ids,&threads as _,diff --git a/crates/messages/src/outbound/pg_message_repo/historical/test.rs b/crates/messages/src/outbound/pg_message_repo/historical/test.rsindex fe7db1db26..f3e15386a8 100644--- a/crates/messages/src/outbound/pg_message_repo/historical/test.rs+++ b/crates/messages/src/outbound/pg_message_repo/historical/test.rs@@ -244,10 +244,9 @@ async fn roots_replies_attribution_and_order_across_batches(pool: PgPool) {vec![earlier.id, later.id]);let stored = sqlx::query!(- "SELECT channel_id, parent_entity_type, parent_entity_id, import_metadata, import_order FROM comms_messages WHERE id = $1",+ "SELECT parent_entity_type, parent_entity_id, import_metadata, import_order FROM comms_messages WHERE id = $1",root.id,).fetch_one(&pool).await.unwrap();- assert_eq!(stored.channel_id, Some(channel_id));assert_eq!(stored.parent_entity_type, "channel");assert_eq!(stored.parent_entity_id, channel_id.to_string());assert_eq!(stored.import_metadata, Some(root.import_metadata));diff --git a/crates/messages/src/outbound/pg_message_repo/test.rs b/crates/messages/src/outbound/pg_message_repo/test.rsindex 53f1098807..56c673c6ef 100644--- a/crates/messages/src/outbound/pg_message_repo/test.rs+++ b/crates/messages/src/outbound/pg_message_repo/test.rs@@ -4,6 +4,7 @@ use macro_db_migrator::MACRO_DB_MIGRATIONS;const USER: &str = "macro|[email protected]";mod initiative;+mod schema_drop;async fn setup(pool: &PgPool) {let user_id = macro_uuid::generate_uuid_v7();@@ -481,10 +482,14 @@ async fn pdf_root_tombstone_keeps_anchor_and_thread_deletion_removes_placeable(pheight_pct: 0.05,});let root = repo.create(create).await.unwrap();- let saved = sqlx::query!(r#"SELECT root_id, "threadId" AS thread_id, "xPct" AS x FROM "PdfPlaceableCommentAnchor" WHERE uuid = $1"#, anchor_id)- .fetch_one(&pool).await.unwrap();- assert_eq!(saved.root_id, Some(root.id));- assert_eq!(saved.thread_id, None);+ let saved = sqlx::query!(+ r#"SELECT root_id, "xPct" AS x FROM "PdfPlaceableCommentAnchor" WHERE uuid = $1"#,+ anchor_id+ )+ .fetch_one(&pool)+ .await+ .unwrap();+ assert_eq!(saved.root_id, root.id);assert_eq!(saved.x, 0.2);repo.delete(&root.parent, root.id).await.unwrap();assert_eq!(@@ -529,18 +534,22 @@ async fn highlight_attachment_is_scoped_and_explicit_thread_deletion_preserves_h.is_empty());create.parent = MessageParent::parse("document", "message-doc-a").unwrap();- let root = repo.create(create).await.unwrap();+ let root = repo.create(create.clone()).await.unwrap();+ // The root identity alone prevents a second discussion from stealing a highlight.+ assert!(repo.create(create.clone()).await.is_err());repo.delete_thread(&root.parent, root.id).await.unwrap();let remaining = sqlx::query!(- r#"SELECT root_id, "threadId" AS thread_id, text FROM "PdfHighlightAnchor" WHERE uuid = $1"#,+ r#"SELECT root_id, text FROM "PdfHighlightAnchor" WHERE uuid = $1"#,anchor_id).fetch_one(&pool).await.unwrap();assert!(remaining.root_id.is_none());- assert!(remaining.thread_id.is_none());assert_eq!(remaining.text, "selected text");+ // Deleting a discussion releases its highlight for a new discussion.+ let replacement = repo.create(create).await.unwrap();+ assert_ne!(replacement.id, root.id);}#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]@@ -568,7 +577,7 @@ async fn placeable_anchor_creation_is_atomic_with_its_root(pool: PgPool) {.fetch_one(&pool).await.unwrap();- assert_eq!(anchored.root_id, Some(root.id));+ assert_eq!(anchored.root_id, root.id);// Reusing the annotation id fails inside the transaction, so neither the// second root nor its thread row survive.@@ -618,9 +627,7 @@ async fn roots_get_thread_rows_without_the_bookkeeping_trigger(pool: PgPool) {}#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]-async fn channel_rows_get_the_legacy_channel_column_from_the_shim_and_attachments_do_not(- pool: PgPool,-) {+async fn channel_and_document_attachments_use_message_parent_identity(pool: PgPool) {setup(&pool).await;let channel = macro_uuid::generate_uuid_v7();sqlx::query!(@@ -646,7 +653,7 @@ async fn channel_rows_get_the_legacy_channel_column_from_the_shim_and_attachmentdocument_post.input.attachments.push(attachment);let document_message = repo.create(document_post).await.unwrap();let columns = sqlx::query!(- r#"SELECT m.id, m.channel_id, a.channel_id AS attachment_channel_id+ r#"SELECT m.id, m.parent_entity_type, m.parent_entity_id, a.entity_idFROM comms_messages m JOIN comms_attachments a ON a.message_id = m.idWHERE m.id = ANY($1) ORDER BY m.created_at"#,&[channel_message.id, document_message.id]@@ -654,10 +661,11 @@ async fn channel_rows_get_the_legacy_channel_column_from_the_shim_and_attachment.fetch_all(&pool).await.unwrap();- assert_eq!(columns[0].channel_id, Some(channel));- assert_eq!(columns[0].attachment_channel_id, None);- assert_eq!(columns[1].channel_id, None);- assert_eq!(columns[1].attachment_channel_id, None);+ assert_eq!(columns[0].parent_entity_type, "channel");+ assert_eq!(columns[0].parent_entity_id, channel.to_string());+ assert_eq!(columns[1].parent_entity_type, "document");+ assert_eq!(columns[1].parent_entity_id, "message-doc-a");+ assert!(columns.iter().all(|row| row.entity_id == "message-doc-b"));}#[cfg(feature = "delivery")]diff --git a/crates/messages/src/outbound/pg_message_repo/test/schema_drop.rs b/crates/messages/src/outbound/pg_message_repo/test/schema_drop.rsnew file mode 100644index 0000000000..c8a506f303--- /dev/null+++ b/crates/messages/src/outbound/pg_message_repo/test/schema_drop.rs@@ -0,0 +1,219 @@+use super::*;++async fn channel(pool: &PgPool) -> Uuid {+ let id = macro_uuid::generate_uuid_v7();+ sqlx::query!(+ "INSERT INTO comms_channels(id, channel_type, owner_id) VALUES ($1, 'private', $2)",+ id,+ USER+ )+ .execute(pool)+ .await+ .unwrap();+ id+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn channel_parent_fk_rejects_missing_channels_and_cascades(pool: PgPool) {+ setup(&pool).await;+ let channel = channel(&pool).await;+ let repo = PgMessageRepository::new(pool.clone());+ let mut post = command("message-doc-a", None, "channel root");+ post.parent = MessageParent::Channel(channel);+ post.input.attachments.push(NewAttachment {+ entity_type: "document".into(),+ entity_id: "message-doc-a".into(),+ width: None,+ height: None,+ });+ let root = repo.create(post.clone()).await.unwrap();+ post.input.thread_id = Some(root.id);+ let reply = repo.create(post.clone()).await.unwrap();+ let document = repo+ .create(command("message-doc-a", None, "keep"))+ .await+ .unwrap();+ sqlx::query!("DELETE FROM comms_channels WHERE id = $1", channel)+ .execute(&pool)+ .await+ .unwrap();+ assert_eq!(+ sqlx::query_scalar!(+ "SELECT count(*) FROM comms_messages WHERE id = ANY($1)",+ &[root.id, reply.id]+ )+ .fetch_one(&pool)+ .await+ .unwrap(),+ Some(0)+ );+ assert_eq!(+ sqlx::query_scalar!(+ "SELECT count(*) FROM comms_message_threads WHERE root_id = $1",+ root.id+ )+ .fetch_one(&pool)+ .await+ .unwrap(),+ Some(0)+ );+ assert_eq!(+ sqlx::query_scalar!(+ "SELECT count(*) FROM comms_attachments WHERE message_id = ANY($1)",+ &[root.id, reply.id]+ )+ .fetch_one(&pool)+ .await+ .unwrap(),+ Some(0)+ );+ assert!(+ repo.get(&document.parent, document.id)+ .await+ .unwrap()+ .is_some()+ );+ post.input.thread_id = None;+ assert!(repo.create(post).await.is_err());+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn soft_delete_cleans_only_matching_parent_message_notifications(pool: PgPool) {+ setup(&pool).await;+ let channel = channel(&pool).await;+ let team = macro_uuid::generate_uuid_v7();+ let company = macro_uuid::generate_uuid_v7();+ let contact = macro_uuid::generate_uuid_v7();+ sqlx::query!(+ "INSERT INTO team(id, name, owner_id) VALUES ($1, 'test', $2)",+ team,+ USER+ )+ .execute(&pool)+ .await+ .unwrap();+ sqlx::query!("INSERT INTO crm_companies(id, team_id, first_interaction, last_interaction) VALUES ($1, $2, now(), now())", company, team)+ .execute(&pool).await.unwrap();+ sqlx::query!("INSERT INTO crm_contacts(id, company_id, email, first_interaction, last_interaction) VALUES ($1, $2, '[email protected]', now(), now())", contact, company)+ .execute(&pool).await.unwrap();++ for (parent_type, parent_id, key) in [+ ("channel", channel.to_string(), "messageId"),+ ("channel", channel.to_string(), "message_id"),+ ("document", "message-doc-a".to_owned(), "commentId"),+ (+ "initiative",+ macro_uuid::generate_uuid_v7().to_string(),+ "messageId",+ ),+ ("crm_company", company.to_string(), "messageId"),+ ("crm_contact", contact.to_string(), "messageId"),+ ] {+ let message = macro_uuid::generate_uuid_v7();+ sqlx::query!("INSERT INTO comms_messages(id, parent_entity_type, parent_entity_id, sender_id, content) VALUES ($1, $2, $3, $4, 'test')", message, parent_type, parent_id, USER)+ .execute(&pool).await.unwrap();+ let matching = macro_uuid::generate_uuid_v7();+ let mut keep = Vec::new();+ for (id, kind, parent, metadata) in [+ (+ matching,+ parent_type,+ parent_id.clone(),+ serde_json::json!({key: message}),+ ),+ (+ macro_uuid::generate_uuid_v7(),+ parent_type,+ "different-parent".into(),+ serde_json::json!({key: message}),+ ),+ (+ macro_uuid::generate_uuid_v7(),+ "different-type",+ parent_id.clone(),+ serde_json::json!({key: message}),+ ),+ (+ macro_uuid::generate_uuid_v7(),+ parent_type,+ parent_id.clone(),+ serde_json::json!({key: macro_uuid::generate_uuid_v7()}),+ ),+ (+ macro_uuid::generate_uuid_v7(),+ parent_type,+ parent_id.clone(),+ serde_json::json!({"threadId": message}),+ ),+ (+ macro_uuid::generate_uuid_v7(),+ parent_type,+ parent_id.clone(),+ serde_json::json!({"commentId": 42}),+ ),+ ] {+ sqlx::query!("INSERT INTO notification(id, notification_event_type, event_item_type, event_item_id, service_sender, metadata) VALUES ($1, 'test', $2, $3, 'test', $4)", id, kind, parent, metadata)+ .execute(&pool).await.unwrap();+ sqlx::query!(+ "INSERT INTO user_notification(user_id, notification_id) VALUES ($1, $2)",+ USER,+ id+ )+ .execute(&pool)+ .await+ .unwrap();+ if id != matching {+ keep.push(id);+ }+ }+ // Ordinary edits must not delete notifications.+ sqlx::query!(+ "UPDATE comms_messages SET deleted_at = NULL, content = 'edited' WHERE id = $1",+ message+ )+ .execute(&pool)+ .await+ .unwrap();+ assert_eq!(+ sqlx::query_scalar!("SELECT count(*) FROM notification WHERE id = $1", matching)+ .fetch_one(&pool)+ .await+ .unwrap(),+ Some(1)+ );+ sqlx::query!(+ "UPDATE comms_messages SET deleted_at = now() WHERE id = $1",+ message+ )+ .execute(&pool)+ .await+ .unwrap();+ assert_eq!(+ sqlx::query_scalar!("SELECT count(*) FROM notification WHERE id = $1", matching)+ .fetch_one(&pool)+ .await+ .unwrap(),+ Some(0)+ );+ assert_eq!(+ sqlx::query_scalar!(+ "SELECT count(*) FROM user_notification WHERE notification_id = $1",+ matching+ )+ .fetch_one(&pool)+ .await+ .unwrap(),+ Some(0)+ );+ assert_eq!(+ sqlx::query_scalar!(+ "SELECT count(*) FROM notification WHERE id = ANY($1)",+ &keep+ )+ .fetch_one(&pool)+ .await+ .unwrap(),+ Some(keep.len() as i64)+ );+ }+}diff --git a/crates/slack_integration/src/outbound/pg_slack_import_repo/batches/test.rs b/crates/slack_integration/src/outbound/pg_slack_import_repo/batches/test.rsindex 1898006145..f10278c3c7 100644--- a/crates/slack_integration/src/outbound/pg_slack_import_repo/batches/test.rs+++ b/crates/slack_integration/src/outbound/pg_slack_import_repo/batches/test.rs@@ -45,7 +45,7 @@ async fn insert_message(batch: &mut FencedBatch<'_, '_>, message: &HistoricalMespanic!("user fixture")};sqlx::query!(- "INSERT INTO comms_messages (id, channel_id, sender_id, content) VALUES ($1, $2, $3, $4)",+ "INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, sender_id, content) VALUES ($1, 'channel', $2::uuid::text, $3, $4)",message.id,message.channel_id,sender.as_ref(),diff --git a/crates/slack_integration/tests/schema.rs b/crates/slack_integration/tests/schema.rsindex b7cbef6e0b..0bab22e303 100644--- a/crates/slack_integration/tests/schema.rs+++ b/crates/slack_integration/tests/schema.rs@@ -85,8 +85,8 @@ fn mapping() -> String {fn message() -> String {format!(- "INSERT INTO comms_messages (id, channel_id, sender_id, content)- VALUES ('{MESSAGE}', '{CHANNEL}', 'macro|[email protected]', 'Historical message')"+ "INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, sender_id, content)+ VALUES ('{MESSAGE}', 'channel', '{CHANNEL}', 'macro|[email protected]', 'Historical message')")}diff --git a/crates/soup/fixtures/notified_at.sql b/crates/soup/fixtures/notified_at.sqlindex 077c3d5eca..ca7e8cde29 100644--- a/crates/soup/fixtures/notified_at.sql+++ b/crates/soup/fixtures/notified_at.sql@@ -76,11 +76,10 @@ INSERT INTO public.comms_channels ("id", "channel_type", "owner_id", "created_atVALUES ('33333333-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'private', 'macro|[email protected]', '2023-01-07 10:00:00', '2023-01-07 10:00:00'),('33333333-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'private', 'macro|[email protected]', '2023-01-07 11:00:00', '2023-01-07 11:00:00');--- Replica mode skips the trigger that derives the parent columns from channel_id,--- so this fixture sets them itself.-INSERT INTO public.comms_messages ("id", "channel_id", "parent_entity_type", "parent_entity_id", "thread_id", "sender_id", "content", "created_at", "updated_at")+-- Message parents are identified by their entity type and id.+INSERT INTO public.comms_messages ("id", "parent_entity_type", "parent_entity_id", "thread_id", "sender_id", "content", "created_at", "updated_at")-- thread-M is a root message in channel-X that user-1 was mentioned in.-VALUES ('99999999-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '33333333-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'channel', '33333333-aaaa-aaaa-aaaa-aaaaaaaaaaaa', NULL, 'macro|[email protected]', 'hey @user-1', '2024-06-01 10:06:00+00', '2024-06-01 10:06:00+00');+VALUES ('99999999-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'channel', '33333333-aaaa-aaaa-aaaa-aaaaaaaaaaaa', NULL, 'macro|[email protected]', 'hey @user-1', '2024-06-01 10:06:00+00', '2024-06-01 10:06:00+00');INSERT INTO public.comms_channel_participants ("channel_id", "role", "user_id", "joined_at", "left_at")VALUES ('33333333-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'member', 'macro|[email protected]', '2023-01-07 10:00:00', NULL),diff --git a/docs/comment-schema-drop.md b/docs/comment-schema-drop.mdnew file mode 100644index 0000000000..4b2a173261--- /dev/null+++ b/docs/comment-schema-drop.md@@ -0,0 +1,126 @@+# Comment schema drop release++[Schema-drop PR #7360](https://github.com/macro-inc/macro/pull/7360) includes both+the final PDF highlight query fix and the legacy schema drop in one release.+The user explicitly accepted temporary PDF highlight discussion failures on+October 2, overriding the usual separate-release rule in+[Database development](DATABASE_DEVELOPMENT.md#safe-database-schema-changes)+for this change. The separate prerequisite PR #7358 is superseded.+The combined change also adapts the historical Slack message writer/reader and+channel search-backfill query added by #7311 on newer `main` to parent identity.+If a release containing the older versions of those consumers runs at drop time,+Slack archive imports and channel search backfills also fail until those consumers+are replaced. Keep import jobs stopped through rollout and resume only on the+compatible worker. Channel filtering, stable pagination, and attribution remain+unchanged.+This PR preparation does not authorize merge or deployment.++Migrations deploy before services. After the drop commits, old service instances+still executing the `"PdfHighlightAnchor"."threadId"` predicate will fail to+attach discussions to highlights until the updated service replaces them.+Recovery depends on a successful rollout; there is no guaranteed 30-minute+limit. A failed rollout or automatic rollback to the old binary leaves that+operation broken and requires rolling forward to the compatible code.++In the October 1 release, migrations finished at 22:31:53 UTC and the document+service deployment job finished at 22:33:12 UTC (79 seconds later). The service+uses `continueBeforeSteadyState: true`, so job completion does not establish when+all old instances stopped serving requests. This is context, not an outage SLA.+The database table rewrite also has its own locking impact, described below.++## Completed rollout evidence++- Contract PR [#6732](https://github.com/macro-inc/macro/pull/6732), squash+ `f7b3753db2b5b6b1660d83f68086ec751f986dab`, is an ancestor of release+ `v2026.10.1.0`, commit `4dcc0d2529c66c3f707f7e30bf8ab804d15566c1`.+- [Deployment run 36934197899](https://github.com/macro-inc/macro/actions/runs/36934197899)+ completed all Cloud Storage deployments and migrations, the web app, sync,+ and AI editing on October 1. Only the separate website deployment failed.+- Production CRM import completed September 29: 77 messages inserted, 76+ thread rows, no updates; all invariants zero and the second run a no-op.+- The dev PDF exception was approved test-data cleanup on September 30:+ comment 4261, thread 3012, its anchor link, and one rootless placeable were+ removed. Dev then had zero unmapped live comments and rootless placeables;+ production had neither exception.+- Read-only primary-database checks reported October 2 found **zero rootless+ placeables across all rows**, including soft-deleted rows, in both dev and+ production. Channel message counts were 20,257 in dev and 203,765 in+ production. These are rollout receipts, not a substitute for a fresh preflight.+- PostHog project 299018 flag 890412, `enable-unified-document-discussions`,+ was archived at `2026-10-02T15:04:31Z`: archived=true, active=false,+ status=ARCHIVED, with history/rules preserved.+- `LEGACY_COMMENT_WRITES_ENABLED` was deleted from cloud-storage-service dev+ and prd and verified absent in lcl, lcl_personal, dev, dev_personal, and prd.+ Config retirement was performed separately; this PR changes no config.+- External SDK/webhook consumer compatibility was waived for the contract.+ SDK publishing is out of scope. That waiver does **not** waive the external+ SQL-reader check below.++## Before deploying the drop++1. Release the query fix and drop together from this PR. Confirm the release+ includes the removal of the PDF highlight `threadId` predicate and that legacy+ writes remain frozen. Imports must be complete, with no importer scheduled or+ running: an old importer could overwrite a newly attached highlight root.+2. Check external SQL readers, scripts, BI/reporting, and operational jobs for+ both retired `channel_id` columns, PDF `threadId`, and the five source tables.+ Confirm that no retired importer is scheduled or running.+3. Take and verify a production snapshot and record its identifier/time and+ recovery procedure. The schema drop is irreversible by service rollback.+4. Recheck import completeness, old-link mappings, and all-row placeable+ `root_id` completeness. Investigate any mismatch; do not delete data to make+ the migration pass.+5. Schedule a quiet maintenance window. Replacing the channel FK with a stored+ generated parent key rewrites `comms_messages` and rebuilds its indexes;+ the attachment entity index is also rebuilt. Allow for disk/WAL and measure+ on a representative isolated snapshot before setting the window. Local+ synthetic-data timing is not a production duration estimate.+6. Deploy through the normal migration pipeline only after these gates are+ approved. `lock_timeout = '5s'` bounds waiting for each lock, not total+ execution time. The migration is transactional; investigate lock failures+ and retry through the pipeline, without bypassing checks.++## Schema and compatibility++The migration replaces notification soft-delete cleanup **before** removing+`channel_id`. It scopes by parent type/id and matches `messageId` or historical+`message_id`; document comment notifications match UUID `commentId`. A thread+id alone does not identify the deleted message. Recipient notifications retain+their existing FK cascade.++The generated `channel_message_parent_id` derives entirely from parent identity+and keeps channel FK integrity and hard-delete cascades, matching the existing+CRM generated-key pattern. Attachments cascade through their message FK.+Parent timeline/history/activity indexes and entity attachment lookup coverage+are retained. PDF placeables require `root_id`; highlights retain nullable roots+so standalone highlights and discussion deletion continue to work.++`Comment`, `Thread`, `ThreadAnchor`, `crm_comment`, `crm_thread`, their dependent+indexes/FKs, the parent-sync shim, and the obsolete columns are retired. Drops+use RESTRICT, so an unexpected dependency stops the migration. Historical+migrations are unchanged. Both `migrated_comment_*` mapping tables, their FKs,+and the authorized legacy-link resolution path remain. The importer binaries,+features, source-specific tests, and fixtures are removed.++## Validation and recovery++Validation uses newly created local databases only, with live SQLx tests+(`SQLX_OFFLINE` unset). Migration regressions exercise a populated pre-drop+schema, row/geometry/mapping preservation, function ordering, channel cascades,+repeat migration runs, and atomic rejection of rootless placeables. Message+regressions cover all parent notification formats, isolation of unrelated+notifications, recipient cascades, PDF anchors, and numeric-link mapping.+Package tests and regenerated workspace SQLx metadata are recorded in the PR.++After deployment, smoke-test channel/document/CRM messages and attachments,+PDF highlight/placeable discussion creation and export, old numeric document+links, notification deletion, and channel deletion. Monitor database errors+for retired identifiers and latency/lock changes.++If application rollback is necessary after the schema drop, use only a release+that no longer references any removed schema, including the PDF highlight+`threadId` predicate. Confirm service rollout completion separately from the+deployment job and roll forward if the old binary remains active.+Rolling back to #6732 or earlier is unsafe. Recovering deleted source tables+requires the approved snapshot/recovery procedure; do not fabricate an empty+replacement schema or rerun retired importers.diff --git a/docs/legacy-comment-import.md b/docs/legacy-comment-import.mdindex 2bbbc01ca8..b4bf39c612 100644--- a/docs/legacy-comment-import.md+++ b/docs/legacy-comment-import.md@@ -1,143 +1,15 @@-# Legacy comment import+# Legacy comment import (retired)-Moves the historical document comments in `"Comment"` / `"Thread"` into the-shared message store (`comms_messages`, `comms_message_threads`) while the-application keeps running. Nothing is dropped here; the legacy tables go away-in the schema-drop migration that follows the contract PR of the channel and-comment unification stack.+The document and CRM importers are retired by this schema-drop PR. Their imports+and the earlier #6732 contract deployment completed before this change. The final+PDF query fix ships together with the drop, so old instances may fail to attach+highlight discussions between the migration and successful service replacement.+Their source tables no longer exist after the migration. Do not run an importer from an older checkout against a+post-drop database.-## Operating model+See [the schema-drop release instructions](comment-schema-drop.md) for deployment+evidence, prerequisites, validation, and recovery. Historical importer code and+its operating guide remain available in Git history before this change.-The importer is a projection: each run recomputes what every legacy thread and-comment should look like in the message store and writes only the rows whose-projection differs. That gives three properties operators rely on:--- **Re-runnable.** A second run without legacy changes prints- `Nothing to import` and writes nothing. Ids never change between runs: every- comment and thread gets one UUIDv7 in `migrated_comment_id` /- `migrated_comment_thread_id` on its first sighting and keeps it.-- **Delta-aware.** Comments created, edited, or deleted (soft or hard) through- the legacy endpoints before they were removed are picked up by the next run.- New comments get mappings; changed ones are compared by text, timestamps, and- deletion.-- **Live.** Work happens in one transaction per batch of documents (250 by- default). Each batch takes a `SHARE ROW EXCLUSIVE` lock on `"Comment"` and- `"Thread"` for the duration of that transaction. Nothing writes those tables- any more, so the lock only guards against a second importer; it waits at- most five seconds and the batch is retried. Nothing takes `ACCESS EXCLUSIVE`.--Two runs cannot overlap: a session advisory lock makes the second exit with-`Another comment import is running`.--### Sequence--The legacy comment endpoints (`/annotations/comments/*`) and the-`LEGACY_COMMENT_WRITES_ENABLED` switch are gone: the document storage service-no longer writes `"Comment"` or `"Thread"`. The remaining job of the importer is-to move whatever those tables still hold before the schema-drop migration-removes them.--1. Deploy the contract release. From then on the legacy tables are frozen by- construction; the new document discussion UI is the only writer and it- writes the message store.-2. Run the importer. Repeat until a run prints `Nothing to import`; the second- consecutive run must do so.-3. Run `--check` and confirm zero unmapped comments and threads.-4. Ship the schema-drop migration. The mapping tables- (`migrated_comment_id`, `migrated_comment_thread_id`) stay for auditing.--An edit made through the new API is never reverted by a stale legacy row-(newer `updated_at` wins), so running the importer after the deploy is safe.--## Running it--Build from the repository root; the binary is feature gated so it never ships-with a service:--```sh-cargo build -p macro_db_migrator --features comment-import --bin comment_import-```--Point `DATABASE_URL` at the target. For RDS use the existing helper and never-the repository's local database recipes:--```sh-export DATABASE_URL="$(tooling/scripts/rds_database_url.sh <rds_identifier> <secret_name> <db_name>)"-cargo run -p macro_db_migrator --features comment-import --bin comment_import -- --check-cargo run -p macro_db_migrator --features comment-import --bin comment_import-cargo run -p macro_db_migrator --features comment-import --bin comment_import -- --documents-per-batch 50-```--`--check` runs the preflight and prints how many comments and threads still-lack a mapping without writing. Edits and deletions of already imported-comments are only detected by a real run.--## What a run writes--- `migrated_comment_id` / `migrated_comment_thread_id`: one row per legacy- comment and thread. Roots follow legacy display `order`, then `createdAt`,- then id, including deleted roots. An empty thread gets a fresh root id.-- `comms_messages`: one row per comment with `parent_entity_type = 'document'`,- the thread's root as `thread_id`, `sender_id` from `owner`, `imported_author`- from `sender`, `import_metadata` from the comment metadata, and- `import_order` from the legacy order. A deleted root stays a tombstone with- its replies intact. An empty thread gets an empty, deleted structural root.-- `comms_message_threads`: resolution, legacy metadata, and the anchor:- `pdf_placeable` when a placeable anchor points at the thread, else- `pdf_highlight` for a live highlight, else `markdown` when the thread's- `markId` is a UUID. `DISCUSSION:` marks, missing marks, and marks that are- not UUIDs land unanchored. Threads with no comments are marked deleted, so- the new UI never shows an empty "This message was deleted." discussion.-- `"PdfPlaceableCommentAnchor".root_id` / `"PdfHighlightAnchor".root_id` from- the thread mapping; the legacy `"threadId"` column stays until the- schema-drop migration.-- `notification.metadata` `commentId` / `threadId` for- `commented_on_document`, `replied_to_document_comment_thread`, and- `mentioned_in_document_comment` rows on the batch's documents.-- Numeric `comments[].id` values inside saved PDF- `"DocumentInstanceModificationData"` payloads.--Rows written through the message store after import are protected: a legacy-row only overwrites a message or thread state when it is at least as new as the-stored `updated_at`. Legacy deletions do not bump `updatedAt`, so they still-apply.--## Preflight failures--The run stops before writing when the legacy data cannot be represented:--- A live thread with more than one PDF anchor. Detach or delete one.-- Two live legacy threads with comments sharing the same Markdown mark on one- document. Delete the duplicate; the message store allows one active- discussion per mark.--A batch that fails for another reason (for example a discussion created through-the new API already owns a legacy thread's mark) rolls back that batch alone.-Earlier batches stay committed and the failed one is retried on the next run.--## Warnings--Warnings are reported and never abort a run:--- notifications whose `commentId` or `threadId` has no mapping (the comment was- removed before the import existed); they keep their legacy ids.-- numeric PDF comment ids without a mapping; they are left as they are.-- threads whose mark is neither a UUID nor a `DISCUSSION:` mark; imported- unanchored.-- threads whose first legacy comment is no longer the mapped root; roots are- fixed at first import, later comments are replies.--## Tests--From the repository root with `SQLX_OFFLINE` unset and local Postgres running:--```sh-cargo test -p macro_db_migrator --features comment-import-```--The suite seeds the legacy tables on the current schema and covers roots,-replies, deleted roots, empty threads, Markdown and PDF anchors, notification-and PDF payload remaps, the no-op second run, delta runs, protection of newer-message store writes, tombstones for removed legacy rows, concurrent runners,-preflight aborts, rollback of a failed batch, mapping batches beyond 1,000 rows,-and the parent-scoped index plans for channel reads.+The `migrated_comment_id` and `migrated_comment_thread_id` tables remain in use:+old numeric document comment/thread links resolve through those mappings.diff --git a/services/slack_import_worker/src/composition/test.rs b/services/slack_import_worker/src/composition/test.rsindex 4711be37f0..75ac451ccb 100644--- a/services/slack_import_worker/src/composition/test.rs+++ b/services/slack_import_worker/src/composition/test.rs@@ -119,7 +119,7 @@ async fn check_read_access_and_mentions(pool: PgPool, kind: ConversationKind) {ChannelMapping::Ready { .. }));sqlx::query!(- "UPDATE comms_messages SET deleted_at = now() WHERE channel_id = $1",+ "UPDATE comms_messages SET deleted_at = now() WHERE parent_entity_type = 'channel' AND parent_entity_id = $1::uuid::text",target.channel_id).execute(&pool)@@ -478,7 +478,7 @@ async fn overlapping_jobs_remap_speculative_parents_without_duplicate_reactions(.unwrap()[0].1;let replies = sqlx::query_scalar!(- "SELECT thread_id FROM comms_messages WHERE channel_id = $1 AND id <> $2",+ "SELECT thread_id FROM comms_messages WHERE parent_entity_type = 'channel' AND parent_entity_id = $1::uuid::text AND id <> $2",plan.channel_id,root)diff --git a/services/slack_import_worker/tests/archive_import.rs b/services/slack_import_worker/tests/archive_import.rsindex dabacb200b..534a9171f7 100644--- a/services/slack_import_worker/tests/archive_import.rs+++ b/services/slack_import_worker/tests/archive_import.rs@@ -724,7 +724,7 @@ async fn sealed_uploads_queue_replay_and_search_completion_barrier(pool: PgPool)"unproven domain must stay external");let replies = sqlx::query_scalar!(- "SELECT thread_id FROM comms_messages WHERE channel_id = $1 AND id <> $2",+ "SELECT thread_id FROM comms_messages WHERE parent_entity_type = 'channel' AND parent_entity_id = $1::uuid::text AND id <> $2",targets[index],root)