Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(messages): additive parent-aware schema for comms_messages (#6423)

gbirmane17f2c857245
diff --git a/.sqlx/query-11fa9f8cff223ea598b05cb8d734dd3a6200562694046817cbfadef2f6d67082.json b/.sqlx/query-02fcc9984915146ae91c829817326f4a2b1c781c913cf7a2e18228350bef86f0.json
similarity index 75%
rename from .sqlx/query-11fa9f8cff223ea598b05cb8d734dd3a6200562694046817cbfadef2f6d67082.json
rename to .sqlx/query-02fcc9984915146ae91c829817326f4a2b1c781c913cf7a2e18228350bef86f0.json
index 873e1e0706..ae2dc27fa9 100644
--- a/.sqlx/query-11fa9f8cff223ea598b05cb8d734dd3a6200562694046817cbfadef2f6d67082.json
+++ b/.sqlx/query-02fcc9984915146ae91c829817326f4a2b1c781c913cf7a2e18228350bef86f0.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n INSERT INTO comms_messages (id, channel_id, sender_id, triggered_by_user_id, content, thread_id)\n VALUES ($1, $2, $3, $4, $5, $6)\n RETURNING\n id,\n channel_id,\n sender_id,\n triggered_by_user_id,\n content,\n created_at,\n updated_at,\n thread_id,\n edited_at::timestamptz AS \"edited_at?\",\n deleted_at::timestamptz AS \"deleted_at?\"\n ",
+ "query": "\n INSERT INTO comms_messages (id, channel_id, sender_id, triggered_by_user_id, content, thread_id)\n VALUES ($1, $2, $3, $4, $5, $6)\n RETURNING\n id,\n channel_id AS \"channel_id!\",\n sender_id,\n triggered_by_user_id,\n content,\n created_at,\n updated_at,\n thread_id,\n edited_at::timestamptz AS \"edited_at?\",\n deleted_at::timestamptz AS \"deleted_at?\"\n ",
"describe": {
"columns": [
{
@@ -10,7 +10,7 @@
},
{
"ordinal": 1,
- "name": "channel_id",
+ "name": "channel_id!",
"type_info": "Uuid"
},
{
@@ -66,7 +66,7 @@
},
"nullable": [
false,
- false,
+ true,
false,
true,
false,
@@ -77,5 +77,5 @@
null
]
},
- "hash": "11fa9f8cff223ea598b05cb8d734dd3a6200562694046817cbfadef2f6d67082"
+ "hash": "02fcc9984915146ae91c829817326f4a2b1c781c913cf7a2e18228350bef86f0"
}
diff --git a/.sqlx/query-129607088f18761c670cb3934c5d14a14932cd9f288b4892f1101717fb2bf651.json b/.sqlx/query-129607088f18761c670cb3934c5d14a14932cd9f288b4892f1101717fb2bf651.json
new file mode 100644
index 0000000000..5b139ad384
--- /dev/null
+++ b/.sqlx/query-129607088f18761c670cb3934c5d14a14932cd9f288b4892f1101717fb2bf651.json
@@ -0,0 +1,66 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT \n m.channel_id AS \"channel_id!: uuid::Uuid\",\n c.name AS \"channel_name?\", -- Option<String>\n m.id AS \"message_id: uuid::Uuid\",\n m.thread_id AS \"thread_id?: uuid::Uuid\",\n m.sender_id AS \"sender_id!\", -- String\n m.content AS \"message_content!\", -- String\n m.created_at AS \"message_created_at!: chrono::DateTime<chrono::Utc>\",\n em.created_at AS \"attachment_created_at!: chrono::DateTime<chrono::Utc>\"\n FROM comms_entity_mentions em\n JOIN comms_messages m ON (em.source_entity_id = m.id::text AND em.source_entity_type = 'message')\n JOIN comms_channels c ON m.channel_id = c.id\n JOIN comms_channel_participants cp ON cp.channel_id = c.id\n WHERE em.entity_type = $1\n AND em.entity_id = $2\n AND cp.user_id = $3\n AND cp.left_at IS NULL\n AND m.deleted_at IS NULL\n ORDER BY em.created_at DESC\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "channel_id!: uuid::Uuid",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "channel_name?",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 2,
+ "name": "message_id: uuid::Uuid",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 3,
+ "name": "thread_id?: uuid::Uuid",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 4,
+ "name": "sender_id!",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "message_content!",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 6,
+ "name": "message_created_at!: chrono::DateTime<chrono::Utc>",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 7,
+ "name": "attachment_created_at!: chrono::DateTime<chrono::Utc>",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": [
+ true,
+ true,
+ false,
+ true,
+ false,
+ false,
+ false,
+ false
+ ]
+ },
+ "hash": "129607088f18761c670cb3934c5d14a14932cd9f288b4892f1101717fb2bf651"
+}
diff --git a/.sqlx/query-15caf267eba0d6ffee5e4b14d19d57a3f252dcbd4103166571cdfef584be1e9c.json b/.sqlx/query-15caf267eba0d6ffee5e4b14d19d57a3f252dcbd4103166571cdfef584be1e9c.json
index cdd0335ac7..728f954b68 100644
--- a/.sqlx/query-15caf267eba0d6ffee5e4b14d19d57a3f252dcbd4103166571cdfef584be1e9c.json
+++ b/.sqlx/query-15caf267eba0d6ffee5e4b14d19d57a3f252dcbd4103166571cdfef584be1e9c.json
@@ -15,7 +15,7 @@
]
},
"nullable": [
- false
+ true
]
},
"hash": "15caf267eba0d6ffee5e4b14d19d57a3f252dcbd4103166571cdfef584be1e9c"
diff --git a/.sqlx/query-1b48f86c0c9026ba92eaf46aceb33a468c430817c973c62cbd91dec399ca8d91.json b/.sqlx/query-1b48f86c0c9026ba92eaf46aceb33a468c430817c973c62cbd91dec399ca8d91.json
new file mode 100644
index 0000000000..549ed91fc5
--- /dev/null
+++ b/.sqlx/query-1b48f86c0c9026ba92eaf46aceb33a468c430817c973c62cbd91dec399ca8d91.json
@@ -0,0 +1,100 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n m.id,\n m.channel_id AS \"channel_id!\",\n m.sender_id,\n m.triggered_by_user_id,\n m.content,\n m.created_at,\n m.updated_at,\n m.edited_at::timestamptz AS \"edited_at?\",\n m.deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages m\n WHERE m.channel_id = $1\n AND m.thread_id IS NULL\n AND (m.deleted_at IS NULL OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id AND r.deleted_at IS NULL\n ))\n AND ($2::timestamptz IS NOT NULL AND (m.created_at, m.id) > ($2, $3))\n AND ($5::uuid[] IS NULL OR m.id = ANY($5))\n AND ($6::timestamptz IS NULL OR m.created_at >= $6)\n AND ($7::timestamptz IS NULL OR m.created_at < $7)\n AND ($13::timestamptz IS NULL OR m.created_at > $13)\n AND (\n ($8::timestamptz IS NULL AND $9::timestamptz IS NULL)\n OR (\n ($8::timestamptz IS NULL OR m.created_at >= $8)\n AND ($9::timestamptz IS NULL OR m.created_at < $9)\n )\n OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id\n AND r.deleted_at IS NULL\n AND ($8::timestamptz IS NULL OR r.created_at >= $8)\n AND ($9::timestamptz IS NULL OR r.created_at < $9)\n )\n )\n AND ($10::bool = FALSE OR EXISTS (\n SELECT 1\n FROM notification n\n JOIN user_notification un ON un.notification_id = n.id\n JOIN comms_messages msg ON msg.id = (n.metadata->>'messageId')::uuid\n WHERE un.user_id = $12::text\n AND un.deleted_at IS NULL\n AND un.state = ANY($11::notification_state[])\n AND n.event_item_type = 'channel'\n AND n.event_item_id = $1::uuid::text\n AND n.metadata->>'messageId' IS NOT NULL\n AND msg.channel_id = $1\n AND msg.deleted_at IS NULL\n AND COALESCE(msg.thread_id, msg.id) = m.id\n ))\n ORDER BY m.created_at ASC, m.id ASC\n LIMIT $4\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "channel_id!",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "sender_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "triggered_by_user_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "content",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 6,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 7,
+ "name": "edited_at?",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 8,
+ "name": "deleted_at?",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Timestamptz",
+ "Uuid",
+ "Int8",
+ "UuidArray",
+ "Timestamptz",
+ "Timestamptz",
+ "Timestamptz",
+ "Timestamptz",
+ "Bool",
+ {
+ "Custom": {
+ "name": "notification_state[]",
+ "kind": {
+ "Array": {
+ "Custom": {
+ "name": "notification_state",
+ "kind": {
+ "Enum": [
+ "unseen",
+ "seen",
+ "done"
+ ]
+ }
+ }
+ }
+ }
+ }
+ },
+ "Text",
+ "Timestamptz"
+ ]
+ },
+ "nullable": [
+ false,
+ true,
+ false,
+ true,
+ false,
+ false,
+ false,
+ null,
+ null
+ ]
+ },
+ "hash": "1b48f86c0c9026ba92eaf46aceb33a468c430817c973c62cbd91dec399ca8d91"
+}
diff --git a/.sqlx/query-25013685edf20cf38453ad6a817be932d82ce1b7251a897127d64ec99602e973.json b/.sqlx/query-25013685edf20cf38453ad6a817be932d82ce1b7251a897127d64ec99602e973.json
deleted file mode 100644
index 6017e4a1e3..0000000000
--- a/.sqlx/query-25013685edf20cf38453ad6a817be932d82ce1b7251a897127d64ec99602e973.json
+++ /dev/null
@@ -1,100 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT\n m.id,\n m.channel_id,\n m.sender_id,\n m.triggered_by_user_id,\n m.content,\n m.created_at,\n m.updated_at,\n m.edited_at::timestamptz AS \"edited_at?\",\n m.deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages m\n WHERE m.channel_id = $1\n AND m.thread_id IS NULL\n AND (m.deleted_at IS NULL OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id AND r.deleted_at IS NULL\n ))\n AND ($2::timestamptz IS NOT NULL AND (m.created_at, m.id) > ($2, $3))\n AND ($5::uuid[] IS NULL OR m.id = ANY($5))\n AND ($6::timestamptz IS NULL OR m.created_at >= $6)\n AND ($7::timestamptz IS NULL OR m.created_at < $7)\n AND ($13::timestamptz IS NULL OR m.created_at > $13)\n AND (\n ($8::timestamptz IS NULL AND $9::timestamptz IS NULL)\n OR (\n ($8::timestamptz IS NULL OR m.created_at >= $8)\n AND ($9::timestamptz IS NULL OR m.created_at < $9)\n )\n OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id\n AND r.deleted_at IS NULL\n AND ($8::timestamptz IS NULL OR r.created_at >= $8)\n AND ($9::timestamptz IS NULL OR r.created_at < $9)\n )\n )\n AND ($10::bool = FALSE OR EXISTS (\n SELECT 1\n FROM notification n\n JOIN user_notification un ON un.notification_id = n.id\n JOIN comms_messages msg ON msg.id = (n.metadata->>'messageId')::uuid\n WHERE un.user_id = $12::text\n AND un.deleted_at IS NULL\n AND un.state = ANY($11::notification_state[])\n AND n.event_item_type = 'channel'\n AND n.event_item_id = $1::uuid::text\n AND n.metadata->>'messageId' IS NOT NULL\n AND msg.channel_id = $1\n AND msg.deleted_at IS NULL\n AND COALESCE(msg.thread_id, msg.id) = m.id\n ))\n ORDER BY m.created_at ASC, m.id ASC\n LIMIT $4\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "id",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "channel_id",
- "type_info": "Uuid"
- },
- {
- "ordinal": 2,
- "name": "sender_id",
- "type_info": "Text"
- },
- {
- "ordinal": 3,
- "name": "triggered_by_user_id",
- "type_info": "Text"
- },
- {
- "ordinal": 4,
- "name": "content",
- "type_info": "Text"
- },
- {
- "ordinal": 5,
- "name": "created_at",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 6,
- "name": "updated_at",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 7,
- "name": "edited_at?",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 8,
- "name": "deleted_at?",
- "type_info": "Timestamptz"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid",
- "Timestamptz",
- "Uuid",
- "Int8",
- "UuidArray",
- "Timestamptz",
- "Timestamptz",
- "Timestamptz",
- "Timestamptz",
- "Bool",
- {
- "Custom": {
- "name": "notification_state[]",
- "kind": {
- "Array": {
- "Custom": {
- "name": "notification_state",
- "kind": {
- "Enum": [
- "unseen",
- "seen",
- "done"
- ]
- }
- }
- }
- }
- }
- },
- "Text",
- "Timestamptz"
- ]
- },
- "nullable": [
- false,
- false,
- false,
- true,
- false,
- false,
- false,
- null,
- null
- ]
- },
- "hash": "25013685edf20cf38453ad6a817be932d82ce1b7251a897127d64ec99602e973"
-}
diff --git a/.sqlx/query-2f26d3b3e0337d1b7b7c31345c0cde73657c5fc5e53fd86a8a2eb14ad063af2c.json b/.sqlx/query-2f26d3b3e0337d1b7b7c31345c0cde73657c5fc5e53fd86a8a2eb14ad063af2c.json
deleted file mode 100644
index d3ca62a73c..0000000000
--- a/.sqlx/query-2f26d3b3e0337d1b7b7c31345c0cde73657c5fc5e53fd86a8a2eb14ad063af2c.json
+++ /dev/null
@@ -1,30 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT\n channel_id,\n id\n FROM comms_messages\n WHERE\n $3::bool IS NULL\n OR ($3 AND deleted_at IS NOT NULL)\n OR (NOT $3 AND deleted_at IS NULL)\n ORDER BY created_at ASC\n LIMIT $1\n OFFSET $2\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "channel_id",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "id",
- "type_info": "Uuid"
- }
- ],
- "parameters": {
- "Left": [
- "Int8",
- "Int8",
- "Bool"
- ]
- },
- "nullable": [
- false,
- false
- ]
- },
- "hash": "2f26d3b3e0337d1b7b7c31345c0cde73657c5fc5e53fd86a8a2eb14ad063af2c"
-}
diff --git a/.sqlx/query-3bbe68003b14d2d9157fc8b2cadeae7ee3feaf10c4ca09c1aad337daf9bf524f.json b/.sqlx/query-3bbe68003b14d2d9157fc8b2cadeae7ee3feaf10c4ca09c1aad337daf9bf524f.json
new file mode 100644
index 0000000000..333cfa5a0a
--- /dev/null
+++ b/.sqlx/query-3bbe68003b14d2d9157fc8b2cadeae7ee3feaf10c4ca09c1aad337daf9bf524f.json
@@ -0,0 +1,30 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n channel_id AS \"channel_id!\",\n id\n FROM comms_messages\n WHERE channel_id IS NOT NULL\n AND (\n $3::bool IS NULL\n OR ($3 AND deleted_at IS NOT NULL)\n OR (NOT $3 AND deleted_at IS NULL)\n )\n ORDER BY created_at ASC\n LIMIT $1\n OFFSET $2\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "channel_id!",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Int8",
+ "Int8",
+ "Bool"
+ ]
+ },
+ "nullable": [
+ true,
+ false
+ ]
+ },
+ "hash": "3bbe68003b14d2d9157fc8b2cadeae7ee3feaf10c4ca09c1aad337daf9bf524f"
+}
diff --git a/.sqlx/query-725e8efb816dd4f03c34db74c141dc0929de9df06ec14e4c7eb93409a0bd0de8.json b/.sqlx/query-597eaea9a6359e356416e03ffc87b69e9b4697aefc5032df5b5cc5fa42237946.json
similarity index 67%
rename from .sqlx/query-725e8efb816dd4f03c34db74c141dc0929de9df06ec14e4c7eb93409a0bd0de8.json
rename to .sqlx/query-597eaea9a6359e356416e03ffc87b69e9b4697aefc5032df5b5cc5fa42237946.json
index ec2418ff4f..4df45cf28a 100644
--- a/.sqlx/query-725e8efb816dd4f03c34db74c141dc0929de9df06ec14e4c7eb93409a0bd0de8.json
+++ b/.sqlx/query-597eaea9a6359e356416e03ffc87b69e9b4697aefc5032df5b5cc5fa42237946.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n id,\n channel_id,\n thread_id,\n sender_id,\n triggered_by_user_id,\n content,\n created_at,\n updated_at,\n edited_at::timestamptz AS \"edited_at?\",\n deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages\n WHERE channel_id = $1\n AND (created_at, id) < ($2, $3)\n ORDER BY created_at DESC, id DESC\n LIMIT $4\n ",
+ "query": "\n SELECT\n id,\n channel_id AS \"channel_id!\",\n thread_id,\n sender_id,\n triggered_by_user_id,\n content,\n created_at,\n updated_at,\n edited_at::timestamptz AS \"edited_at?\",\n deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages\n WHERE channel_id = $1\n AND (created_at, id) < ($2, $3)\n ORDER BY created_at DESC, id DESC\n LIMIT $4\n ",
"describe": {
"columns": [
{
@@ -10,7 +10,7 @@
},
{
"ordinal": 1,
- "name": "channel_id",
+ "name": "channel_id!",
"type_info": "Uuid"
},
{
@@ -64,7 +64,7 @@
},
"nullable": [
false,
- false,
+ true,
true,
false,
true,
@@ -75,5 +75,5 @@
null
]
},
- "hash": "725e8efb816dd4f03c34db74c141dc0929de9df06ec14e4c7eb93409a0bd0de8"
+ "hash": "597eaea9a6359e356416e03ffc87b69e9b4697aefc5032df5b5cc5fa42237946"
}
diff --git a/.sqlx/query-91e974129e2c983925d29dabf73c8ae86c6f090cbfd28f22cd952756950a94db.json b/.sqlx/query-63bd06bd793025cd0b42fcfd69e979e9e0fee613f78709dcd690a9ae7cfe9317.json
similarity index 77%
rename from .sqlx/query-91e974129e2c983925d29dabf73c8ae86c6f090cbfd28f22cd952756950a94db.json
rename to .sqlx/query-63bd06bd793025cd0b42fcfd69e979e9e0fee613f78709dcd690a9ae7cfe9317.json
index 806760a2c8..82ec664899 100644
--- a/.sqlx/query-91e974129e2c983925d29dabf73c8ae86c6f090cbfd28f22cd952756950a94db.json
+++ b/.sqlx/query-63bd06bd793025cd0b42fcfd69e979e9e0fee613f78709dcd690a9ae7cfe9317.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n INSERT INTO comms_messages (id, channel_id, sender_id, content, thread_id)\n VALUES ($1, $2, $3, $4, $5)\n RETURNING\n id,\n channel_id,\n sender_id,\n content,\n created_at,\n updated_at,\n thread_id,\n edited_at as \"edited_at: chrono::DateTime<chrono::Utc>\",\n deleted_at as \"deleted_at: chrono::DateTime<chrono::Utc>\" \n ",
+ "query": "\n INSERT INTO comms_messages (id, channel_id, sender_id, content, thread_id)\n VALUES ($1, $2, $3, $4, $5)\n RETURNING\n id,\n channel_id AS \"channel_id!\",\n sender_id,\n content,\n created_at,\n updated_at,\n thread_id,\n edited_at as \"edited_at: chrono::DateTime<chrono::Utc>\",\n deleted_at as \"deleted_at: chrono::DateTime<chrono::Utc>\" \n ",
"describe": {
"columns": [
{
@@ -10,7 +10,7 @@
},
{
"ordinal": 1,
- "name": "channel_id",
+ "name": "channel_id!",
"type_info": "Uuid"
},
{
@@ -60,7 +60,7 @@
},
"nullable": [
false,
- false,
+ true,
false,
false,
false,
@@ -70,5 +70,5 @@
true
]
},
- "hash": "91e974129e2c983925d29dabf73c8ae86c6f090cbfd28f22cd952756950a94db"
+ "hash": "63bd06bd793025cd0b42fcfd69e979e9e0fee613f78709dcd690a9ae7cfe9317"
}
diff --git a/.sqlx/query-ec3636e62ff865a5c6808e8f87d65730837b043b9cb69ce7075c431cee32b58b.json b/.sqlx/query-731b404c0d5e7414c6c0f1d3802877c9bfd017a924e204186dac095194c83462.json
similarity index 58%
rename from .sqlx/query-ec3636e62ff865a5c6808e8f87d65730837b043b9cb69ce7075c431cee32b58b.json
rename to .sqlx/query-731b404c0d5e7414c6c0f1d3802877c9bfd017a924e204186dac095194c83462.json
index 72ef23f4f8..2bf6198fe9 100644
--- a/.sqlx/query-ec3636e62ff865a5c6808e8f87d65730837b043b9cb69ce7075c431cee32b58b.json
+++ b/.sqlx/query-731b404c0d5e7414c6c0f1d3802877c9bfd017a924e204186dac095194c83462.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n m.id,\n m.channel_id,\n m.sender_id,\n m.triggered_by_user_id,\n m.content,\n m.created_at,\n m.updated_at,\n m.edited_at::timestamptz AS \"edited_at?\",\n m.deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages m\n WHERE m.channel_id = $1\n AND m.thread_id IS NULL\n AND (m.deleted_at IS NULL OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id AND r.deleted_at IS NULL\n ))\n AND (m.created_at, m.id) < ($2, $3)\n ORDER BY m.created_at DESC, m.id DESC\n LIMIT $4\n ",
+ "query": "\n SELECT\n m.id,\n m.channel_id AS \"channel_id!\",\n m.sender_id,\n m.triggered_by_user_id,\n m.content,\n m.created_at,\n m.updated_at,\n m.edited_at::timestamptz AS \"edited_at?\",\n m.deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages m\n WHERE m.channel_id = $1\n AND m.thread_id IS NULL\n AND (m.deleted_at IS NULL OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id AND r.deleted_at IS NULL\n ))\n AND (m.created_at, m.id) > ($2, $3)\n ORDER BY m.created_at ASC, m.id ASC\n LIMIT $4\n ",
"describe": {
"columns": [
{
@@ -10,7 +10,7 @@
},
{
"ordinal": 1,
- "name": "channel_id",
+ "name": "channel_id!",
"type_info": "Uuid"
},
{
@@ -59,7 +59,7 @@
},
"nullable": [
false,
- false,
+ true,
false,
true,
false,
@@ -69,5 +69,5 @@
null
]
},
- "hash": "ec3636e62ff865a5c6808e8f87d65730837b043b9cb69ce7075c431cee32b58b"
+ "hash": "731b404c0d5e7414c6c0f1d3802877c9bfd017a924e204186dac095194c83462"
}
diff --git a/.sqlx/query-83738f54a7c60ab63b5a34bbe91fbc9d5a6ae437ae40ce93363db4b96c0ff314.json b/.sqlx/query-83738f54a7c60ab63b5a34bbe91fbc9d5a6ae437ae40ce93363db4b96c0ff314.json
deleted file mode 100644
index 038dd7c829..0000000000
--- a/.sqlx/query-83738f54a7c60ab63b5a34bbe91fbc9d5a6ae437ae40ce93363db4b96c0ff314.json
+++ /dev/null
@@ -1,66 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT\n m.channel_id AS \"channel_id: uuid::Uuid\",\n c.name AS \"channel_name?\", -- Option<String>\n m.id AS \"message_id: uuid::Uuid\",\n m.thread_id AS \"thread_id?: uuid::Uuid\",\n m.sender_id AS \"sender_id!\", -- String\n m.content AS \"message_content!\", -- String\n m.created_at AS \"message_created_at!: chrono::DateTime<chrono::Utc>\",\n em.created_at AS \"attachment_created_at!: chrono::DateTime<chrono::Utc>\"\n FROM comms_entity_mentions em\n JOIN comms_messages m ON (em.source_entity_id = m.id::text AND em.source_entity_type = 'message')\n JOIN comms_channels c ON m.channel_id = c.id\n JOIN comms_channel_participants cp ON cp.channel_id = c.id\n WHERE em.entity_type = ANY($1)\n AND em.entity_id = $2\n AND cp.user_id = $3\n AND cp.left_at IS NULL\n AND m.deleted_at IS NULL\n ORDER BY em.created_at DESC\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "channel_id: uuid::Uuid",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "channel_name?",
- "type_info": "Varchar"
- },
- {
- "ordinal": 2,
- "name": "message_id: uuid::Uuid",
- "type_info": "Uuid"
- },
- {
- "ordinal": 3,
- "name": "thread_id?: uuid::Uuid",
- "type_info": "Uuid"
- },
- {
- "ordinal": 4,
- "name": "sender_id!",
- "type_info": "Text"
- },
- {
- "ordinal": 5,
- "name": "message_content!",
- "type_info": "Text"
- },
- {
- "ordinal": 6,
- "name": "message_created_at!: chrono::DateTime<chrono::Utc>",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 7,
- "name": "attachment_created_at!: chrono::DateTime<chrono::Utc>",
- "type_info": "Timestamptz"
- }
- ],
- "parameters": {
- "Left": [
- "TextArray",
- "Text",
- "Text"
- ]
- },
- "nullable": [
- false,
- true,
- false,
- true,
- false,
- false,
- false,
- false
- ]
- },
- "hash": "83738f54a7c60ab63b5a34bbe91fbc9d5a6ae437ae40ce93363db4b96c0ff314"
-}
diff --git a/.sqlx/query-61246005cb69889a67ac46d2032f4fe9c1da7b19594f1b7dc59f2ed0a613dec1.json b/.sqlx/query-85ac0b659b023163bff3913e78ab26e5e78898f2020317b25ac7f61f8d11a31a.json
similarity index 77%
rename from .sqlx/query-61246005cb69889a67ac46d2032f4fe9c1da7b19594f1b7dc59f2ed0a613dec1.json
rename to .sqlx/query-85ac0b659b023163bff3913e78ab26e5e78898f2020317b25ac7f61f8d11a31a.json
index ccd0aadea5..8c8bd9b093 100644
--- a/.sqlx/query-61246005cb69889a67ac46d2032f4fe9c1da7b19594f1b7dc59f2ed0a613dec1.json
+++ b/.sqlx/query-85ac0b659b023163bff3913e78ab26e5e78898f2020317b25ac7f61f8d11a31a.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n INSERT INTO comms_messages (id, channel_id, sender_id, content, thread_id)\n VALUES ($1, $2, $3, $4, $5)\n RETURNING\n id,\n channel_id,\n sender_id,\n content,\n created_at,\n updated_at,\n thread_id,\n edited_at as \"edited_at: chrono::DateTime<chrono::Utc>\",\n deleted_at as \"deleted_at: chrono::DateTime<chrono::Utc>\"\n ",
+ "query": "\n INSERT INTO comms_messages (id, channel_id, sender_id, content, thread_id)\n VALUES ($1, $2, $3, $4, $5)\n RETURNING\n id,\n channel_id AS \"channel_id!\",\n sender_id,\n content,\n created_at,\n updated_at,\n thread_id,\n edited_at as \"edited_at: chrono::DateTime<chrono::Utc>\",\n deleted_at as \"deleted_at: chrono::DateTime<chrono::Utc>\"\n ",
"describe": {
"columns": [
{
@@ -10,7 +10,7 @@
},
{
"ordinal": 1,
- "name": "channel_id",
+ "name": "channel_id!",
"type_info": "Uuid"
},
{
@@ -60,7 +60,7 @@
},
"nullable": [
false,
- false,
+ true,
false,
false,
false,
@@ -70,5 +70,5 @@
true
]
},
- "hash": "61246005cb69889a67ac46d2032f4fe9c1da7b19594f1b7dc59f2ed0a613dec1"
+ "hash": "85ac0b659b023163bff3913e78ab26e5e78898f2020317b25ac7f61f8d11a31a"
}
diff --git a/.sqlx/query-4e6737a5a777280784037e88ccdda4e6e89dae702e5b50269db6e17265d14643.json b/.sqlx/query-8ab6d8f4fa26d7bed9a560f7d5ed3ba1f8f4fb86b54bb90788a25319fb3a411e.json
similarity index 80%
rename from .sqlx/query-4e6737a5a777280784037e88ccdda4e6e89dae702e5b50269db6e17265d14643.json
rename to .sqlx/query-8ab6d8f4fa26d7bed9a560f7d5ed3ba1f8f4fb86b54bb90788a25319fb3a411e.json
index df6dc3c263..fdeb1e86cb 100644
--- a/.sqlx/query-4e6737a5a777280784037e88ccdda4e6e89dae702e5b50269db6e17265d14643.json
+++ b/.sqlx/query-8ab6d8f4fa26d7bed9a560f7d5ed3ba1f8f4fb86b54bb90788a25319fb3a411e.json
@@ -1,11 +1,11 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT \n m.channel_id AS \"channel_id: uuid::Uuid\",\n c.name AS \"channel_name?\", -- Option<String>\n m.id AS \"message_id: uuid::Uuid\",\n m.thread_id AS \"thread_id?: uuid::Uuid\",\n m.sender_id AS \"sender_id!\", -- String\n m.content AS \"message_content!\", -- String\n m.created_at AS \"message_created_at!: chrono::DateTime<chrono::Utc>\",\n em.created_at AS \"attachment_created_at!: chrono::DateTime<chrono::Utc>\"\n FROM comms_entity_mentions em\n JOIN comms_messages m ON (em.source_entity_id = m.id::text AND em.source_entity_type = 'message')\n JOIN comms_channels c ON m.channel_id = c.id\n JOIN comms_channel_participants cp ON cp.channel_id = c.id\n WHERE em.entity_type = $1\n AND em.entity_id = $2\n AND cp.user_id = $3\n AND cp.left_at IS NULL\n AND m.deleted_at IS NULL\n ORDER BY em.created_at DESC\n ",
+ "query": "\n SELECT\n m.channel_id AS \"channel_id!: uuid::Uuid\",\n c.name AS \"channel_name?\", -- Option<String>\n m.id AS \"message_id: uuid::Uuid\",\n m.thread_id AS \"thread_id?: uuid::Uuid\",\n m.sender_id AS \"sender_id!\", -- String\n m.content AS \"message_content!\", -- String\n m.created_at AS \"message_created_at!: chrono::DateTime<chrono::Utc>\",\n em.created_at AS \"attachment_created_at!: chrono::DateTime<chrono::Utc>\"\n FROM comms_entity_mentions em\n JOIN comms_messages m ON (em.source_entity_id = m.id::text AND em.source_entity_type = 'message')\n JOIN comms_channels c ON m.channel_id = c.id\n JOIN comms_channel_participants cp ON cp.channel_id = c.id\n WHERE em.entity_type = ANY($1)\n AND em.entity_id = $2\n AND cp.user_id = $3\n AND cp.left_at IS NULL\n AND m.deleted_at IS NULL\n ORDER BY em.created_at DESC\n ",
"describe": {
"columns": [
{
"ordinal": 0,
- "name": "channel_id: uuid::Uuid",
+ "name": "channel_id!: uuid::Uuid",
"type_info": "Uuid"
},
{
@@ -46,13 +46,13 @@
],
"parameters": {
"Left": [
- "Text",
+ "TextArray",
"Text",
"Text"
]
},
"nullable": [
- false,
+ true,
true,
false,
true,
@@ -62,5 +62,5 @@
false
]
},
- "hash": "4e6737a5a777280784037e88ccdda4e6e89dae702e5b50269db6e17265d14643"
+ "hash": "8ab6d8f4fa26d7bed9a560f7d5ed3ba1f8f4fb86b54bb90788a25319fb3a411e"
}
diff --git a/.sqlx/query-6220297009a6be311ae935bd69ad9a2235af5d38bf380330fa1b2d40777a3b53.json b/.sqlx/query-8b0808ef30a05c617c739053849e86499d27a2cb5a794c320301f96fee22a755.json
similarity index 58%
rename from .sqlx/query-6220297009a6be311ae935bd69ad9a2235af5d38bf380330fa1b2d40777a3b53.json
rename to .sqlx/query-8b0808ef30a05c617c739053849e86499d27a2cb5a794c320301f96fee22a755.json
index 1626fcf87d..7b4d70d6df 100644
--- a/.sqlx/query-6220297009a6be311ae935bd69ad9a2235af5d38bf380330fa1b2d40777a3b53.json
+++ b/.sqlx/query-8b0808ef30a05c617c739053849e86499d27a2cb5a794c320301f96fee22a755.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n m.id,\n m.channel_id,\n m.sender_id,\n m.triggered_by_user_id,\n m.content,\n m.created_at,\n m.updated_at,\n m.edited_at::timestamptz AS \"edited_at?\",\n m.deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages m\n WHERE m.channel_id = $1\n AND m.thread_id IS NULL\n AND (m.deleted_at IS NULL OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id AND r.deleted_at IS NULL\n ))\n AND (m.created_at, m.id) > ($2, $3)\n ORDER BY m.created_at ASC, m.id ASC\n LIMIT $4\n ",
+ "query": "\n SELECT\n m.id,\n m.channel_id AS \"channel_id!\",\n m.sender_id,\n m.triggered_by_user_id,\n m.content,\n m.created_at,\n m.updated_at,\n m.edited_at::timestamptz AS \"edited_at?\",\n m.deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages m\n WHERE m.channel_id = $1\n AND m.thread_id IS NULL\n AND (m.deleted_at IS NULL OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id AND r.deleted_at IS NULL\n ))\n AND (m.created_at, m.id) < ($2, $3)\n ORDER BY m.created_at DESC, m.id DESC\n LIMIT $4\n ",
"describe": {
"columns": [
{
@@ -10,7 +10,7 @@
},
{
"ordinal": 1,
- "name": "channel_id",
+ "name": "channel_id!",
"type_info": "Uuid"
},
{
@@ -59,7 +59,7 @@
},
"nullable": [
false,
- false,
+ true,
false,
true,
false,
@@ -69,5 +69,5 @@
null
]
},
- "hash": "6220297009a6be311ae935bd69ad9a2235af5d38bf380330fa1b2d40777a3b53"
+ "hash": "8b0808ef30a05c617c739053849e86499d27a2cb5a794c320301f96fee22a755"
}
diff --git a/.sqlx/query-b002a549f1a74fd65b2a24564ef8e35fa56a985e1f8eef8265139dd4a2777440.json b/.sqlx/query-99b6d0252281ac21fb7e07ec2cd3ea76b3600ee18f37d2ab7085487c1831ebcb.json
similarity index 67%
rename from .sqlx/query-b002a549f1a74fd65b2a24564ef8e35fa56a985e1f8eef8265139dd4a2777440.json
rename to .sqlx/query-99b6d0252281ac21fb7e07ec2cd3ea76b3600ee18f37d2ab7085487c1831ebcb.json
index 58ebd52eaf..7ce3b44314 100644
--- a/.sqlx/query-b002a549f1a74fd65b2a24564ef8e35fa56a985e1f8eef8265139dd4a2777440.json
+++ b/.sqlx/query-99b6d0252281ac21fb7e07ec2cd3ea76b3600ee18f37d2ab7085487c1831ebcb.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n id,\n channel_id,\n thread_id,\n sender_id,\n triggered_by_user_id,\n content,\n created_at,\n updated_at,\n edited_at::timestamptz AS \"edited_at?\",\n deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages\n WHERE channel_id = $1\n AND (created_at, id) > ($2, $3)\n ORDER BY created_at ASC, id ASC\n LIMIT $4\n ",
+ "query": "\n SELECT\n id,\n channel_id AS \"channel_id!\",\n thread_id,\n sender_id,\n triggered_by_user_id,\n content,\n created_at,\n updated_at,\n edited_at::timestamptz AS \"edited_at?\",\n deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages\n WHERE channel_id = $1\n AND (created_at, id) > ($2, $3)\n ORDER BY created_at ASC, id ASC\n LIMIT $4\n ",
"describe": {
"columns": [
{
@@ -10,7 +10,7 @@
},
{
"ordinal": 1,
- "name": "channel_id",
+ "name": "channel_id!",
"type_info": "Uuid"
},
{
@@ -64,7 +64,7 @@
},
"nullable": [
false,
- false,
+ true,
true,
false,
true,
@@ -75,5 +75,5 @@
null
]
},
- "hash": "b002a549f1a74fd65b2a24564ef8e35fa56a985e1f8eef8265139dd4a2777440"
+ "hash": "99b6d0252281ac21fb7e07ec2cd3ea76b3600ee18f37d2ab7085487c1831ebcb"
}
diff --git a/.sqlx/query-17c240f6a460b4c63a40f0ab96f8d756f2001fb03c1e389dabb596d95359ed94.json b/.sqlx/query-9d3f29593fc1a7e4f5f2fab79f6c0e6b0e2a80e1a9ecb8fbde75d0f21985161f.json
similarity index 74%
rename from .sqlx/query-17c240f6a460b4c63a40f0ab96f8d756f2001fb03c1e389dabb596d95359ed94.json
rename to .sqlx/query-9d3f29593fc1a7e4f5f2fab79f6c0e6b0e2a80e1a9ecb8fbde75d0f21985161f.json
index 3c25cdad6e..8cfe8711c1 100644
--- a/.sqlx/query-17c240f6a460b4c63a40f0ab96f8d756f2001fb03c1e389dabb596d95359ed94.json
+++ b/.sqlx/query-9d3f29593fc1a7e4f5f2fab79f6c0e6b0e2a80e1a9ecb8fbde75d0f21985161f.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n UPDATE comms_messages\n SET content = $1, updated_at = NOW(), edited_at = NOW()\n WHERE id = $2 AND channel_id = $3\n RETURNING\n id,\n channel_id,\n sender_id,\n triggered_by_user_id,\n content,\n created_at,\n updated_at,\n thread_id,\n edited_at::timestamptz AS \"edited_at?\",\n deleted_at::timestamptz AS \"deleted_at?\"\n ",
+ "query": "\n UPDATE comms_messages\n SET content = $1, updated_at = NOW(), edited_at = NOW()\n WHERE id = $2 AND channel_id = $3\n RETURNING\n id,\n channel_id AS \"channel_id!\",\n sender_id,\n triggered_by_user_id,\n content,\n created_at,\n updated_at,\n thread_id,\n edited_at::timestamptz AS \"edited_at?\",\n deleted_at::timestamptz AS \"deleted_at?\"\n ",
"describe": {
"columns": [
{
@@ -10,7 +10,7 @@
},
{
"ordinal": 1,
- "name": "channel_id",
+ "name": "channel_id!",
"type_info": "Uuid"
},
{
@@ -63,7 +63,7 @@
},
"nullable": [
false,
- false,
+ true,
false,
true,
false,
@@ -74,5 +74,5 @@
null
]
},
- "hash": "17c240f6a460b4c63a40f0ab96f8d756f2001fb03c1e389dabb596d95359ed94"
+ "hash": "9d3f29593fc1a7e4f5f2fab79f6c0e6b0e2a80e1a9ecb8fbde75d0f21985161f"
}
diff --git a/.sqlx/query-fbee16f91442381c51b85d5e77042ba3787eec094e62746727249f3055ec2dbb.json b/.sqlx/query-a55b029a1193f4b77bf035e6791c28cf4ffe86e9ef0a29272a5030ea982757b3.json
similarity index 62%
rename from .sqlx/query-fbee16f91442381c51b85d5e77042ba3787eec094e62746727249f3055ec2dbb.json
rename to .sqlx/query-a55b029a1193f4b77bf035e6791c28cf4ffe86e9ef0a29272a5030ea982757b3.json
index b1688e3f54..8184c4e557 100644
--- a/.sqlx/query-fbee16f91442381c51b85d5e77042ba3787eec094e62746727249f3055ec2dbb.json
+++ b/.sqlx/query-a55b029a1193f4b77bf035e6791c28cf4ffe86e9ef0a29272a5030ea982757b3.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n m.id,\n m.channel_id,\n m.sender_id,\n m.triggered_by_user_id,\n m.content,\n m.created_at,\n m.updated_at,\n m.edited_at::timestamptz AS \"edited_at?\",\n m.deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages m\n WHERE m.id = COALESCE(\n (SELECT thread_id FROM comms_messages WHERE id = $1 AND channel_id = $2),\n $1\n )\n AND m.channel_id = $2\n AND m.thread_id IS NULL\n ",
+ "query": "\n SELECT\n m.id,\n m.channel_id AS \"channel_id!\",\n m.sender_id,\n m.triggered_by_user_id,\n m.content,\n m.created_at,\n m.updated_at,\n m.edited_at::timestamptz AS \"edited_at?\",\n m.deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages m\n WHERE m.id = COALESCE(\n (SELECT thread_id FROM comms_messages WHERE id = $1 AND channel_id = $2),\n $1\n )\n AND m.channel_id = $2\n AND m.thread_id IS NULL\n ",
"describe": {
"columns": [
{
@@ -10,7 +10,7 @@
},
{
"ordinal": 1,
- "name": "channel_id",
+ "name": "channel_id!",
"type_info": "Uuid"
},
{
@@ -57,7 +57,7 @@
},
"nullable": [
false,
- false,
+ true,
false,
true,
false,
@@ -67,5 +67,5 @@
null
]
},
- "hash": "fbee16f91442381c51b85d5e77042ba3787eec094e62746727249f3055ec2dbb"
+ "hash": "a55b029a1193f4b77bf035e6791c28cf4ffe86e9ef0a29272a5030ea982757b3"
}
diff --git a/.sqlx/query-e6f63b89995a909f72c7c4012ab340e40f13453f1bec6cf41cb202930c44e84a.json b/.sqlx/query-ad23f7bf93814f78d866ce255cc14a6d7477b66db3ee07f14f4a28f25c6d81d6.json
similarity index 64%
rename from .sqlx/query-e6f63b89995a909f72c7c4012ab340e40f13453f1bec6cf41cb202930c44e84a.json
rename to .sqlx/query-ad23f7bf93814f78d866ce255cc14a6d7477b66db3ee07f14f4a28f25c6d81d6.json
index 9c27aa5033..65d2fe4eee 100644
--- a/.sqlx/query-e6f63b89995a909f72c7c4012ab340e40f13453f1bec6cf41cb202930c44e84a.json
+++ b/.sqlx/query-ad23f7bf93814f78d866ce255cc14a6d7477b66db3ee07f14f4a28f25c6d81d6.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT id, channel_id, thread_id, created_at\n FROM comms_messages\n WHERE id = $1\n AND channel_id = $2\n ",
+ "query": "\n SELECT id, channel_id AS \"channel_id!\", thread_id, created_at\n FROM comms_messages\n WHERE id = $1\n AND channel_id = $2\n ",
"describe": {
"columns": [
{
@@ -10,7 +10,7 @@
},
{
"ordinal": 1,
- "name": "channel_id",
+ "name": "channel_id!",
"type_info": "Uuid"
},
{
@@ -32,10 +32,10 @@
},
"nullable": [
false,
- false,
+ true,
true,
false
]
},
- "hash": "e6f63b89995a909f72c7c4012ab340e40f13453f1bec6cf41cb202930c44e84a"
+ "hash": "ad23f7bf93814f78d866ce255cc14a6d7477b66db3ee07f14f4a28f25c6d81d6"
}
diff --git a/.sqlx/query-256ccfc280aaca7d1eff2bd993b29d29e94abe7ec9ad2574d53e138413e049cd.json b/.sqlx/query-ae3cbeee12dbf77c4e9deb43bea4ac6001828f0207e6571ce199b4e61d58d042.json
similarity index 76%
rename from .sqlx/query-256ccfc280aaca7d1eff2bd993b29d29e94abe7ec9ad2574d53e138413e049cd.json
rename to .sqlx/query-ae3cbeee12dbf77c4e9deb43bea4ac6001828f0207e6571ce199b4e61d58d042.json
index 17701131fd..5db8756d99 100644
--- a/.sqlx/query-256ccfc280aaca7d1eff2bd993b29d29e94abe7ec9ad2574d53e138413e049cd.json
+++ b/.sqlx/query-ae3cbeee12dbf77c4e9deb43bea4ac6001828f0207e6571ce199b4e61d58d042.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n UPDATE comms_messages\n SET\n updated_at = NOW(),\n edited_at = NOW(),\n deleted_at = CASE\n WHEN $2 = false AND (content IS NULL OR content ~ '^[\\s]*$') THEN NOW()\n ELSE deleted_at\n END\n WHERE id = $1\n RETURNING\n id,\n channel_id,\n sender_id,\n triggered_by_user_id,\n content,\n created_at,\n updated_at,\n thread_id,\n edited_at::timestamptz AS \"edited_at?\",\n deleted_at::timestamptz AS \"deleted_at?\"\n ",
+ "query": "\n UPDATE comms_messages\n SET\n updated_at = NOW(),\n edited_at = NOW(),\n deleted_at = CASE\n WHEN $2 = false AND (content IS NULL OR content ~ '^[\\s]*$') THEN NOW()\n ELSE deleted_at\n END\n WHERE id = $1\n RETURNING\n id,\n channel_id AS \"channel_id!\",\n sender_id,\n triggered_by_user_id,\n content,\n created_at,\n updated_at,\n thread_id,\n edited_at::timestamptz AS \"edited_at?\",\n deleted_at::timestamptz AS \"deleted_at?\"\n ",
"describe": {
"columns": [
{
@@ -10,7 +10,7 @@
},
{
"ordinal": 1,
- "name": "channel_id",
+ "name": "channel_id!",
"type_info": "Uuid"
},
{
@@ -62,7 +62,7 @@
},
"nullable": [
false,
- false,
+ true,
false,
true,
false,
@@ -73,5 +73,5 @@
null
]
},
- "hash": "256ccfc280aaca7d1eff2bd993b29d29e94abe7ec9ad2574d53e138413e049cd"
+ "hash": "ae3cbeee12dbf77c4e9deb43bea4ac6001828f0207e6571ce199b4e61d58d042"
}
diff --git a/.sqlx/query-811160740482a042237f6ffc433290e0e89ba9521fbba575634fd92a39433271.json b/.sqlx/query-c970de18cffdd356dd7d6ea165cd0053f3b385539d734626c16c108f292b56bc.json
similarity index 74%
rename from .sqlx/query-811160740482a042237f6ffc433290e0e89ba9521fbba575634fd92a39433271.json
rename to .sqlx/query-c970de18cffdd356dd7d6ea165cd0053f3b385539d734626c16c108f292b56bc.json
index 90d232e808..8f163de6b0 100644
--- a/.sqlx/query-811160740482a042237f6ffc433290e0e89ba9521fbba575634fd92a39433271.json
+++ b/.sqlx/query-c970de18cffdd356dd7d6ea165cd0053f3b385539d734626c16c108f292b56bc.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n UPDATE comms_messages\n SET content = '', updated_at = NOW(), deleted_at = NOW()\n WHERE id = $1 AND channel_id = $2\n RETURNING\n id,\n channel_id,\n sender_id,\n triggered_by_user_id,\n content,\n created_at,\n updated_at,\n thread_id,\n edited_at::timestamptz AS \"edited_at?\",\n deleted_at::timestamptz AS \"deleted_at?\"\n ",
+ "query": "\n UPDATE comms_messages\n SET content = '', updated_at = NOW(), deleted_at = NOW()\n WHERE id = $1 AND channel_id = $2\n RETURNING\n id,\n channel_id AS \"channel_id!\",\n sender_id,\n triggered_by_user_id,\n content,\n created_at,\n updated_at,\n thread_id,\n edited_at::timestamptz AS \"edited_at?\",\n deleted_at::timestamptz AS \"deleted_at?\"\n ",
"describe": {
"columns": [
{
@@ -10,7 +10,7 @@
},
{
"ordinal": 1,
- "name": "channel_id",
+ "name": "channel_id!",
"type_info": "Uuid"
},
{
@@ -62,7 +62,7 @@
},
"nullable": [
false,
- false,
+ true,
false,
true,
false,
@@ -73,5 +73,5 @@
null
]
},
- "hash": "811160740482a042237f6ffc433290e0e89ba9521fbba575634fd92a39433271"
+ "hash": "c970de18cffdd356dd7d6ea165cd0053f3b385539d734626c16c108f292b56bc"
}
diff --git a/.sqlx/query-cbb7c34ed6c79aebe0645f4be0a6ab4cd2e0be49816f9781821f60a983618648.json b/.sqlx/query-cbb7c34ed6c79aebe0645f4be0a6ab4cd2e0be49816f9781821f60a983618648.json
deleted file mode 100644
index 5443917355..0000000000
--- a/.sqlx/query-cbb7c34ed6c79aebe0645f4be0a6ab4cd2e0be49816f9781821f60a983618648.json
+++ /dev/null
@@ -1,100 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT\n m.id,\n m.channel_id,\n m.sender_id,\n m.triggered_by_user_id,\n m.content,\n m.created_at,\n m.updated_at,\n m.edited_at::timestamptz AS \"edited_at?\",\n m.deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages m\n WHERE m.channel_id = $1\n AND m.thread_id IS NULL\n AND (m.deleted_at IS NULL OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id AND r.deleted_at IS NULL\n ))\n AND ($2::timestamptz IS NULL OR (m.created_at, m.id) < ($2, $3))\n AND ($5::uuid[] IS NULL OR m.id = ANY($5))\n AND ($6::timestamptz IS NULL OR m.created_at >= $6)\n AND ($7::timestamptz IS NULL OR m.created_at < $7)\n AND ($13::timestamptz IS NULL OR m.created_at > $13)\n AND (\n ($8::timestamptz IS NULL AND $9::timestamptz IS NULL)\n OR (\n ($8::timestamptz IS NULL OR m.created_at >= $8)\n AND ($9::timestamptz IS NULL OR m.created_at < $9)\n )\n OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id\n AND r.deleted_at IS NULL\n AND ($8::timestamptz IS NULL OR r.created_at >= $8)\n AND ($9::timestamptz IS NULL OR r.created_at < $9)\n )\n )\n AND ($10::bool = FALSE OR EXISTS (\n SELECT 1\n FROM notification n\n JOIN user_notification un ON un.notification_id = n.id\n JOIN comms_messages msg ON msg.id = (n.metadata->>'messageId')::uuid\n WHERE un.user_id = $12::text\n AND un.deleted_at IS NULL\n AND un.state = ANY($11::notification_state[])\n AND n.event_item_type = 'channel'\n AND n.event_item_id = $1::uuid::text\n AND n.metadata->>'messageId' IS NOT NULL\n AND msg.channel_id = $1\n AND msg.deleted_at IS NULL\n AND COALESCE(msg.thread_id, msg.id) = m.id\n ))\n ORDER BY m.created_at DESC, m.id DESC\n LIMIT $4\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "id",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "channel_id",
- "type_info": "Uuid"
- },
- {
- "ordinal": 2,
- "name": "sender_id",
- "type_info": "Text"
- },
- {
- "ordinal": 3,
- "name": "triggered_by_user_id",
- "type_info": "Text"
- },
- {
- "ordinal": 4,
- "name": "content",
- "type_info": "Text"
- },
- {
- "ordinal": 5,
- "name": "created_at",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 6,
- "name": "updated_at",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 7,
- "name": "edited_at?",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 8,
- "name": "deleted_at?",
- "type_info": "Timestamptz"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid",
- "Timestamptz",
- "Uuid",
- "Int8",
- "UuidArray",
- "Timestamptz",
- "Timestamptz",
- "Timestamptz",
- "Timestamptz",
- "Bool",
- {
- "Custom": {
- "name": "notification_state[]",
- "kind": {
- "Array": {
- "Custom": {
- "name": "notification_state",
- "kind": {
- "Enum": [
- "unseen",
- "seen",
- "done"
- ]
- }
- }
- }
- }
- }
- },
- "Text",
- "Timestamptz"
- ]
- },
- "nullable": [
- false,
- false,
- false,
- true,
- false,
- false,
- false,
- null,
- null
- ]
- },
- "hash": "cbb7c34ed6c79aebe0645f4be0a6ab4cd2e0be49816f9781821f60a983618648"
-}
diff --git a/.sqlx/query-cc1f54832b14dfabeada87f07bedbce1d1f4bda4422d11ea57db0684b10b7413.json b/.sqlx/query-cc1f54832b14dfabeada87f07bedbce1d1f4bda4422d11ea57db0684b10b7413.json
new file mode 100644
index 0000000000..4f1222b4d5
--- /dev/null
+++ b/.sqlx/query-cc1f54832b14dfabeada87f07bedbce1d1f4bda4422d11ea57db0684b10b7413.json
@@ -0,0 +1,100 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n m.id,\n m.channel_id AS \"channel_id!\",\n m.sender_id,\n m.triggered_by_user_id,\n m.content,\n m.created_at,\n m.updated_at,\n m.edited_at::timestamptz AS \"edited_at?\",\n m.deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages m\n WHERE m.channel_id = $1\n AND m.thread_id IS NULL\n AND (m.deleted_at IS NULL OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id AND r.deleted_at IS NULL\n ))\n AND ($2::timestamptz IS NULL OR (m.created_at, m.id) < ($2, $3))\n AND ($5::uuid[] IS NULL OR m.id = ANY($5))\n AND ($6::timestamptz IS NULL OR m.created_at >= $6)\n AND ($7::timestamptz IS NULL OR m.created_at < $7)\n AND ($13::timestamptz IS NULL OR m.created_at > $13)\n AND (\n ($8::timestamptz IS NULL AND $9::timestamptz IS NULL)\n OR (\n ($8::timestamptz IS NULL OR m.created_at >= $8)\n AND ($9::timestamptz IS NULL OR m.created_at < $9)\n )\n OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id\n AND r.deleted_at IS NULL\n AND ($8::timestamptz IS NULL OR r.created_at >= $8)\n AND ($9::timestamptz IS NULL OR r.created_at < $9)\n )\n )\n AND ($10::bool = FALSE OR EXISTS (\n SELECT 1\n FROM notification n\n JOIN user_notification un ON un.notification_id = n.id\n JOIN comms_messages msg ON msg.id = (n.metadata->>'messageId')::uuid\n WHERE un.user_id = $12::text\n AND un.deleted_at IS NULL\n AND un.state = ANY($11::notification_state[])\n AND n.event_item_type = 'channel'\n AND n.event_item_id = $1::uuid::text\n AND n.metadata->>'messageId' IS NOT NULL\n AND msg.channel_id = $1\n AND msg.deleted_at IS NULL\n AND COALESCE(msg.thread_id, msg.id) = m.id\n ))\n ORDER BY m.created_at DESC, m.id DESC\n LIMIT $4\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "channel_id!",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "sender_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "triggered_by_user_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "content",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 6,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 7,
+ "name": "edited_at?",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 8,
+ "name": "deleted_at?",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Timestamptz",
+ "Uuid",
+ "Int8",
+ "UuidArray",
+ "Timestamptz",
+ "Timestamptz",
+ "Timestamptz",
+ "Timestamptz",
+ "Bool",
+ {
+ "Custom": {
+ "name": "notification_state[]",
+ "kind": {
+ "Array": {
+ "Custom": {
+ "name": "notification_state",
+ "kind": {
+ "Enum": [
+ "unseen",
+ "seen",
+ "done"
+ ]
+ }
+ }
+ }
+ }
+ }
+ },
+ "Text",
+ "Timestamptz"
+ ]
+ },
+ "nullable": [
+ false,
+ true,
+ false,
+ true,
+ false,
+ false,
+ false,
+ null,
+ null
+ ]
+ },
+ "hash": "cc1f54832b14dfabeada87f07bedbce1d1f4bda4422d11ea57db0684b10b7413"
+}
diff --git a/.sqlx/query-ed2f2e964af409b89eceb93c1b4ee3364dbb75bdc22047789492b8f73b3e2491.json b/.sqlx/query-cd5c750c90b42d40403c9d5a2e6d7c851acbdc96745cf321657ac6365fe9bf62.json
similarity index 64%
rename from .sqlx/query-ed2f2e964af409b89eceb93c1b4ee3364dbb75bdc22047789492b8f73b3e2491.json
rename to .sqlx/query-cd5c750c90b42d40403c9d5a2e6d7c851acbdc96745cf321657ac6365fe9bf62.json
index 4c202caa69..80579ce721 100644
--- a/.sqlx/query-ed2f2e964af409b89eceb93c1b4ee3364dbb75bdc22047789492b8f73b3e2491.json
+++ b/.sqlx/query-cd5c750c90b42d40403c9d5a2e6d7c851acbdc96745cf321657ac6365fe9bf62.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n id,\n channel_id,\n sender_id,\n content,\n created_at,\n updated_at,\n thread_id,\n edited_at as \"edited_at: chrono::DateTime<chrono::Utc>\",\n deleted_at as \"deleted_at: chrono::DateTime<chrono::Utc>\"\n FROM (\n SELECT *\n FROM comms_messages\n WHERE channel_id = $1\n AND ($2::timestamptz IS NULL OR created_at >= $2)\n ORDER BY created_at DESC\n LIMIT $3\n ) AS latest_messages\n ORDER BY created_at ASC\n ",
+ "query": "\n SELECT\n id,\n channel_id AS \"channel_id!\",\n sender_id,\n content,\n created_at,\n updated_at,\n thread_id,\n edited_at as \"edited_at: chrono::DateTime<chrono::Utc>\",\n deleted_at as \"deleted_at: chrono::DateTime<chrono::Utc>\"\n FROM (\n SELECT *\n FROM comms_messages\n WHERE channel_id = $1\n AND ($2::timestamptz IS NULL OR created_at >= $2)\n ORDER BY created_at DESC\n LIMIT $3\n ) AS latest_messages\n ORDER BY created_at ASC\n ",
"describe": {
"columns": [
{
@@ -10,7 +10,7 @@
},
{
"ordinal": 1,
- "name": "channel_id",
+ "name": "channel_id!",
"type_info": "Uuid"
},
{
@@ -58,7 +58,7 @@
},
"nullable": [
false,
- false,
+ true,
false,
false,
false,
@@ -68,5 +68,5 @@
true
]
},
- "hash": "ed2f2e964af409b89eceb93c1b4ee3364dbb75bdc22047789492b8f73b3e2491"
+ "hash": "cd5c750c90b42d40403c9d5a2e6d7c851acbdc96745cf321657ac6365fe9bf62"
}
diff --git a/.sqlx/query-d82d977779927364228d9652cd1958df1fa31ba45ef06790f540139098e3d10f.json b/.sqlx/query-f0418b18b6ecd18b2177a774701b835ac7bdae8c6ca339502f1d85d71b02e8dd.json
similarity index 70%
rename from .sqlx/query-d82d977779927364228d9652cd1958df1fa31ba45ef06790f540139098e3d10f.json
rename to .sqlx/query-f0418b18b6ecd18b2177a774701b835ac7bdae8c6ca339502f1d85d71b02e8dd.json
index 2a7f8ba1fa..855ee9b8c8 100644
--- a/.sqlx/query-d82d977779927364228d9652cd1958df1fa31ba45ef06790f540139098e3d10f.json
+++ b/.sqlx/query-f0418b18b6ecd18b2177a774701b835ac7bdae8c6ca339502f1d85d71b02e8dd.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n id,\n channel_id,\n thread_id,\n sender_id,\n triggered_by_user_id,\n content,\n created_at,\n updated_at,\n edited_at::timestamptz AS \"edited_at?\",\n deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages\n WHERE id = $1 AND channel_id = $2\n ",
+ "query": "\n SELECT\n id,\n channel_id AS \"channel_id!\",\n thread_id,\n sender_id,\n triggered_by_user_id,\n content,\n created_at,\n updated_at,\n edited_at::timestamptz AS \"edited_at?\",\n deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages\n WHERE id = $1 AND channel_id = $2\n ",
"describe": {
"columns": [
{
@@ -10,7 +10,7 @@
},
{
"ordinal": 1,
- "name": "channel_id",
+ "name": "channel_id!",
"type_info": "Uuid"
},
{
@@ -62,7 +62,7 @@
},
"nullable": [
false,
- false,
+ true,
true,
false,
true,
@@ -73,5 +73,5 @@
null
]
},
- "hash": "d82d977779927364228d9652cd1958df1fa31ba45ef06790f540139098e3d10f"
+ "hash": "f0418b18b6ecd18b2177a774701b835ac7bdae8c6ca339502f1d85d71b02e8dd"
}
diff --git a/crates/agent_session/src/outbound/postgres.rs b/crates/agent_session/src/outbound/postgres.rs
index 716e2d127e..bf77a6ac29 100644
--- a/crates/agent_session/src/outbound/postgres.rs
+++ b/crates/agent_session/src/outbound/postgres.rs
@@ -371,7 +371,8 @@ impl AgentSessionRepo for PgAgentSessionRepo {
)
.fetch_optional(&mut *transaction)
.await
- .context("failed to read the originating message's channel")?,
+ .context("failed to read the originating message's channel")?
+ .flatten(),
None => None,
};
diff --git a/crates/channels/src/outbound/pg_channels_repo.rs b/crates/channels/src/outbound/pg_channels_repo.rs
index ee5018f78c..ed5eec2eb0 100644
--- a/crates/channels/src/outbound/pg_channels_repo.rs
+++ b/crates/channels/src/outbound/pg_channels_repo.rs
@@ -1755,7 +1755,7 @@ impl ChannelRepo for PgChannelsRepo {
r#"
SELECT
m.id,
- m.channel_id,
+ m.channel_id AS "channel_id!",
m.sender_id,
m.triggered_by_user_id,
m.content,
@@ -1832,7 +1832,7 @@ impl ChannelRepo for PgChannelsRepo {
r#"
SELECT
m.id,
- m.channel_id,
+ m.channel_id AS "channel_id!",
m.sender_id,
m.triggered_by_user_id,
m.content,
@@ -2237,7 +2237,7 @@ impl ChannelRepo for PgChannelsRepo {
r#"
SELECT
id,
- channel_id,
+ channel_id AS "channel_id!",
thread_id,
sender_id,
triggered_by_user_id,
@@ -2264,7 +2264,7 @@ impl ChannelRepo for PgChannelsRepo {
r#"
SELECT
id,
- channel_id,
+ channel_id AS "channel_id!",
thread_id,
sender_id,
triggered_by_user_id,
@@ -2293,7 +2293,7 @@ impl ChannelRepo for PgChannelsRepo {
r#"
SELECT
id,
- channel_id,
+ channel_id AS "channel_id!",
thread_id,
sender_id,
triggered_by_user_id,
@@ -2373,7 +2373,7 @@ impl ChannelRepo for PgChannelsRepo {
AttachmentChannelReference,
r#"
SELECT
- m.channel_id AS "channel_id: uuid::Uuid",
+ m.channel_id AS "channel_id!: uuid::Uuid",
c.name AS "channel_name?", -- Option<String>
m.id AS "message_id: uuid::Uuid",
m.thread_id AS "thread_id?: uuid::Uuid",
@@ -2484,7 +2484,7 @@ impl ChannelRepo for PgChannelsRepo {
r#"
SELECT
m.id,
- m.channel_id,
+ m.channel_id AS "channel_id!",
m.sender_id,
m.triggered_by_user_id,
m.content,
@@ -2528,7 +2528,7 @@ impl ChannelRepo for PgChannelsRepo {
let row = sqlx::query_as!(
ResolvedMessageRow,
r#"
- SELECT id, channel_id, thread_id, created_at
+ SELECT id, channel_id AS "channel_id!", thread_id, created_at
FROM comms_messages
WHERE id = $1
AND channel_id = $2
@@ -2570,7 +2570,7 @@ impl ChannelRepo for PgChannelsRepo {
r#"
SELECT
m.id,
- m.channel_id,
+ m.channel_id AS "channel_id!",
m.sender_id,
m.triggered_by_user_id,
m.content,
@@ -2601,7 +2601,7 @@ impl ChannelRepo for PgChannelsRepo {
r#"
SELECT
m.id,
- m.channel_id,
+ m.channel_id AS "channel_id!",
m.sender_id,
m.triggered_by_user_id,
m.content,
@@ -3263,7 +3263,7 @@ impl ChannelRepo for PgChannelsRepo {
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING
id,
- channel_id,
+ channel_id AS "channel_id!",
sender_id,
triggered_by_user_id,
content,
@@ -3508,7 +3508,7 @@ impl ChannelRepo for PgChannelsRepo {
WHERE id = $1
RETURNING
id,
- channel_id,
+ channel_id AS "channel_id!",
sender_id,
triggered_by_user_id,
content,
@@ -3541,7 +3541,7 @@ impl ChannelRepo for PgChannelsRepo {
WHERE id = $2 AND channel_id = $3
RETURNING
id,
- channel_id,
+ channel_id AS "channel_id!",
sender_id,
triggered_by_user_id,
content,
@@ -3573,7 +3573,7 @@ impl ChannelRepo for PgChannelsRepo {
WHERE id = $1 AND channel_id = $2
RETURNING
id,
- channel_id,
+ channel_id AS "channel_id!",
sender_id,
triggered_by_user_id,
content,
diff --git a/crates/comms_db_client/src/attachments/get_attachment_references.rs b/crates/comms_db_client/src/attachments/get_attachment_references.rs
index e90003111d..2d961f03f7 100644
--- a/crates/comms_db_client/src/attachments/get_attachment_references.rs
+++ b/crates/comms_db_client/src/attachments/get_attachment_references.rs
@@ -105,7 +105,7 @@ pub async fn get_attachment_references(
ChannelReference,
r#"
SELECT
- m.channel_id AS "channel_id: uuid::Uuid",
+ m.channel_id AS "channel_id!: uuid::Uuid",
c.name AS "channel_name?", -- Option<String>
m.id AS "message_id: uuid::Uuid",
m.thread_id AS "thread_id?: uuid::Uuid",
diff --git a/crates/comms_db_client/src/messages/create_message.rs b/crates/comms_db_client/src/messages/create_message.rs
index 219058ca04..463e5e8166 100644
--- a/crates/comms_db_client/src/messages/create_message.rs
+++ b/crates/comms_db_client/src/messages/create_message.rs
@@ -25,7 +25,7 @@ where
VALUES ($1, $2, $3, $4, $5)
RETURNING
id,
- channel_id,
+ channel_id AS "channel_id!",
sender_id,
content,
created_at,
diff --git a/crates/comms_db_client/src/messages/get_messages.rs b/crates/comms_db_client/src/messages/get_messages.rs
index b9e96926c1..ebb089d7cd 100644
--- a/crates/comms_db_client/src/messages/get_messages.rs
+++ b/crates/comms_db_client/src/messages/get_messages.rs
@@ -16,7 +16,7 @@ pub async fn get_messages(
r#"
SELECT
id,
- channel_id,
+ channel_id AS "channel_id!",
sender_id,
content,
created_at,
@@ -72,13 +72,15 @@ pub async fn get_channel_messages(
let messages = sqlx::query!(
r#"
SELECT
- channel_id,
+ channel_id AS "channel_id!",
id
FROM comms_messages
- WHERE
+ WHERE channel_id IS NOT NULL
+ AND (
$3::bool IS NULL
OR ($3 AND deleted_at IS NOT NULL)
OR (NOT $3 AND deleted_at IS NULL)
+ )
ORDER BY created_at ASC
LIMIT $1
OFFSET $2
diff --git a/crates/comms_db_client/src/messages/seed_message.rs b/crates/comms_db_client/src/messages/seed_message.rs
index be3eaded4d..878bd5e81d 100644
--- a/crates/comms_db_client/src/messages/seed_message.rs
+++ b/crates/comms_db_client/src/messages/seed_message.rs
@@ -34,7 +34,7 @@ where
VALUES ($1, $2, $3, $4, $5)
RETURNING
id,
- channel_id,
+ channel_id AS "channel_id!",
sender_id,
content,
created_at,
diff --git a/crates/comms_db_client/tests/messages_parent_schema.rs b/crates/comms_db_client/tests/messages_parent_schema.rs
new file mode 100644
index 0000000000..2f3190d70b
--- /dev/null
+++ b/crates/comms_db_client/tests/messages_parent_schema.rs
@@ -0,0 +1,486 @@
+use comms_db_client::messages::create_message::{CreateMessageOptions, create_message};
+use comms_db_client::messages::get_messages::get_channel_messages;
+use macro_db_migrator::MACRO_DB_MIGRATIONS;
+use sqlx::{Pool, Postgres};
+use uuid::Uuid;
+
+const CH1: Uuid = Uuid::from_u128(0x11111111_1111_1111_1111_111111111111);
+const CH3: Uuid = Uuid::from_u128(0x33333333_3333_3333_3333_333333333333);
+const USER1: &str = "macro|[email protected]";
+const USER2: &str = "macro|[email protected]";
+const DOC: &str = "doc-1";
+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(),
+ }
+}
+
+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",
+ )
+ .bind(id)
+ .fetch_one(pool)
+ .await?)
+}
+
+async fn thread_rows(pool: &Pool<Postgres>, root_id: Uuid) -> anyhow::Result<i64> {
+ Ok(
+ sqlx::query_scalar("SELECT count(*) FROM comms_message_threads WHERE root_id = $1")
+ .bind(root_id)
+ .fetch_one(pool)
+ .await?,
+ )
+}
+
+async 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')",
+ )
+ .bind(id)
+ .bind(channel_id)
+ .bind(USER1)
+ .execute(pool)
+ .await?;
+ Ok(id)
+}
+
+async fn insert_document_root(pool: &Pool<Postgres>, document_id: &str) -> anyhow::Result<Uuid> {
+ let id = macro_uuid::generate_uuid_v7();
+ sqlx::query(
+ "INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, sender_id, content)
+ VALUES ($1, 'document', $2, $3, 'root')",
+ )
+ .bind(id)
+ .bind(document_id)
+ .bind(USER1)
+ .execute(pool)
+ .await?;
+ Ok(id)
+}
+
+async fn set_markdown_anchor(
+ pool: &Pool<Postgres>,
+ root_id: Uuid,
+ mark_id: &str,
+) -> sqlx::Result<()> {
+ sqlx::query(
+ "UPDATE comms_message_threads
+ SET anchor = jsonb_build_object('type', 'markdown', 'mark_id', $2::text)
+ WHERE root_id = $1",
+ )
+ .bind(root_id)
+ .bind(mark_id)
+ .execute(pool)
+ .await?;
+ Ok(())
+}
+
+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(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ const _: &sqlx::migrate::Migrator = &MACRO_DB_MIGRATIONS; // Dummy reference for IDE
+ let id = insert_channel_root(&pool, CH1).await?;
+
+ assert_eq!(parent_columns(&pool, id).await?, channel_parent(CH1));
+
+ let (parent_type, parent_id, user_id, resolved, deleted_at): (
+ String,
+ String,
+ String,
+ bool,
+ Option<chrono::DateTime<chrono::Utc>>,
+ ) = sqlx::query_as(
+ "SELECT parent_entity_type, parent_entity_id, user_id, resolved, deleted_at
+ FROM comms_message_threads WHERE root_id = $1",
+ )
+ .bind(id)
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(parent_type, "channel");
+ assert_eq!(parent_id, CH1.to_string());
+ assert_eq!(user_id, USER1);
+ assert!(!resolved);
+ assert_eq!(deleted_at, None);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../fixtures", scripts("channels"))
+)]
+async fn parent_only_channel_insert_gets_channel_id(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)
+ VALUES ($1, 'channel', $2, $3, 'hi')",
+ )
+ .bind(id)
+ .bind(CH3.to_string())
+ .bind(USER1)
+ .execute(&pool)
+ .await?;
+
+ assert_eq!(parent_columns(&pool, id).await?, channel_parent(CH3));
+ assert_eq!(thread_rows(&pool, id).await?, 1);
+ Ok(())
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn document_root_without_channel_inserts_and_gets_thread_row(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let root_id = insert_document_root(&pool, DOC).await?;
+
+ assert_eq!(
+ parent_columns(&pool, root_id).await?,
+ ParentColumns {
+ channel_id: None,
+ parent_entity_type: "document".to_owned(),
+ parent_entity_id: DOC.to_owned(),
+ }
+ );
+ assert_eq!(thread_rows(&pool, root_id).await?, 1);
+
+ let reply_id = macro_uuid::generate_uuid_v7();
+ sqlx::query(
+ "INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, thread_id, sender_id, content)
+ VALUES ($1, 'document', $2, $3, $4, 'reply')",
+ )
+ .bind(reply_id)
+ .bind(DOC)
+ .bind(root_id)
+ .bind(USER2)
+ .execute(&pool)
+ .await?;
+ assert_eq!(thread_rows(&pool, reply_id).await?, 0);
+
+ sqlx::query("DELETE FROM comms_messages WHERE id = $1")
+ .bind(root_id)
+ .execute(&pool)
+ .await?;
+ let remaining: i64 =
+ sqlx::query_scalar("SELECT count(*) FROM comms_messages WHERE parent_entity_id = $1")
+ .bind(DOC)
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(remaining, 0);
+ assert_eq!(thread_rows(&pool, root_id).await?, 0);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../fixtures", scripts("channels"))
+)]
+async fn reply_with_different_parent_than_root_is_rejected(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let doc_root = insert_document_root(&pool, DOC).await?;
+ let err = sqlx::query(
+ "INSERT INTO comms_messages (id, parent_entity_type, parent_entity_id, thread_id, sender_id, content)
+ VALUES ($1, 'document', $2, $3, $4, 'reply')",
+ )
+ .bind(macro_uuid::generate_uuid_v7())
+ .bind(OTHER_DOC)
+ .bind(doc_root)
+ .bind(USER2)
+ .execute(&pool)
+ .await
+ .expect_err("reply on another document must not reference this root");
+ assert_eq!(
+ constraint_name(&err),
+ Some("comms_messages_thread_parent_fkey")
+ );
+
+ 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')",
+ )
+ .bind(macro_uuid::generate_uuid_v7())
+ .bind(CH3)
+ .bind(channel_root)
+ .bind(USER2)
+ .execute(&pool)
+ .await
+ .expect_err("reply in another channel must not reference this root");
+ assert_eq!(
+ constraint_name(&err),
+ Some("comms_messages_thread_parent_fkey")
+ );
+ 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"))
+)]
+async fn anchor_on_channel_root_is_rejected(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ let channel_root = insert_channel_root(&pool, CH1).await?;
+ let err = set_markdown_anchor(&pool, channel_root, "mark-1")
+ .await
+ .expect_err("channel threads cannot be anchored");
+ assert_eq!(
+ constraint_name(&err),
+ Some("comms_message_threads_anchor_parent_check")
+ );
+
+ let doc_root = insert_document_root(&pool, DOC).await?;
+ let err = sqlx::query(
+ r#"UPDATE comms_message_threads SET anchor = '{"type": "unknown"}' WHERE root_id = $1"#,
+ )
+ .bind(doc_root)
+ .execute(&pool)
+ .await
+ .expect_err("unknown anchor types are rejected");
+ assert_eq!(
+ constraint_name(&err),
+ Some("comms_message_threads_anchor_check")
+ );
+ Ok(())
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn duplicate_live_markdown_mark_in_one_document_is_rejected(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let first = insert_document_root(&pool, DOC).await?;
+ let second = insert_document_root(&pool, DOC).await?;
+ let other_document = insert_document_root(&pool, OTHER_DOC).await?;
+
+ set_markdown_anchor(&pool, first, "mark-1").await?;
+ let err = set_markdown_anchor(&pool, second, "mark-1")
+ .await
+ .expect_err("one live discussion per mark per document");
+ assert_eq!(
+ constraint_name(&err),
+ Some("idx_comms_message_threads_markdown_mark")
+ );
+
+ set_markdown_anchor(&pool, other_document, "mark-1").await?;
+
+ sqlx::query("UPDATE comms_message_threads SET deleted_at = now() WHERE root_id = $1")
+ .bind(first)
+ .execute(&pool)
+ .await?;
+ set_markdown_anchor(&pool, second, "mark-1").await?;
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../fixtures", scripts("channels"))
+)]
+async fn create_message_channel_reply_path_is_unchanged(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let root = create_message(
+ &pool,
+ CreateMessageOptions {
+ channel_id: CH1,
+ sender_id: USER1.to_owned(),
+ content: "root".to_owned(),
+ thread_id: None,
+ },
+ )
+ .await?;
+ let reply = create_message(
+ &pool,
+ CreateMessageOptions {
+ channel_id: CH1,
+ sender_id: USER2.to_owned(),
+ content: "reply".to_owned(),
+ thread_id: Some(root.id),
+ },
+ )
+ .await?;
+
+ assert_eq!(root.channel_id, CH1);
+ assert_eq!(root.thread_id, None);
+ assert_eq!(reply.channel_id, CH1);
+ assert_eq!(reply.thread_id, Some(root.id));
+ assert_eq!(reply.sender_id.as_ref(), USER2);
+
+ assert_eq!(parent_columns(&pool, reply.id).await?, channel_parent(CH1));
+ assert_eq!(thread_rows(&pool, root.id).await?, 1);
+ assert_eq!(thread_rows(&pool, reply.id).await?, 0);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../fixtures", scripts("channels"))
+)]
+async fn channel_message_backfill_scan_skips_document_rows(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let channel_root = insert_channel_root(&pool, CH1).await?;
+ insert_document_root(&pool, DOC).await?;
+
+ let messages = get_channel_messages(&pool, 10, 0, None).await?;
+ assert_eq!(messages, vec![(CH1, channel_root)]);
+ Ok(())
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn pdf_anchors_reference_document_thread_rows(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ sqlx::query(
+ "INSERT INTO macro_user (id, username, email, stripe_customer_id)
+ VALUES ('55555555-5555-5555-5555-555555555555', 'owner', '[email protected]', 'stripe-test')",
+ )
+ .execute(&pool)
+ .await?;
+ sqlx::query(
+ r#"INSERT INTO "User" (id, email, macro_user_id)
+ VALUES ($1, '[email protected]', '55555555-5555-5555-5555-555555555555')"#,
+ )
+ .bind(USER1)
+ .execute(&pool)
+ .await?;
+ sqlx::query(
+ r#"INSERT INTO "Document" (id, name, owner, "fileType") VALUES ($1, 'a.pdf', $2, 'pdf')"#,
+ )
+ .bind(DOC)
+ .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);
+ sqlx::query(
+ r#"INSERT INTO "PdfPlaceableCommentAnchor" (
+ uuid, "documentId", owner, page, "wasEdited", "wasDeleted", "shouldLockOnSave",
+ "originalPage", "originalIndex", "xPct", "yPct", "widthPct", "heightPct", rotation,
+ "threadId", root_id
+ )
+ VALUES ($1, $2, $3, 1, false, false, false, 1, 0, 0.1, 0.1, 0.2, 0.2, 0, 901, $4)"#,
+ )
+ .bind(placeable)
+ .bind(DOC)
+ .bind(USER1)
+ .bind(root_id)
+ .execute(&pool)
+ .await?;
+ sqlx::query(
+ r#"INSERT INTO "PdfHighlightAnchor" (
+ uuid, "documentId", owner, page, red, green, blue, alpha, type, text,
+ "pageViewportWidth", "pageViewportHeight", root_id
+ )
+ VALUES ($1, $2, $3, 1, 255, 0, 0, 1.0, 0, 'highlighted', 800, 600, $4)"#,
+ )
+ .bind(highlight)
+ .bind(DOC)
+ .bind(USER1)
+ .bind(root_id)
+ .execute(&pool)
+ .await?;
+
+ let err = sqlx::query(
+ r#"INSERT INTO "PdfHighlightAnchor" (
+ uuid, "documentId", owner, page, red, green, blue, alpha, type, text,
+ "pageViewportWidth", "pageViewportHeight", root_id
+ )
+ VALUES ($1, $2, $3, 1, 255, 0, 0, 1.0, 0, 'dangling', 800, 600, $4)"#,
+ )
+ .bind(Uuid::from_u128(0xf0000000_0000_0000_0000_000000000003))
+ .bind(DOC)
+ .bind(USER1)
+ .bind(macro_uuid::generate_uuid_v7())
+ .execute(&pool)
+ .await
+ .expect_err("root_id must reference a thread row");
+ assert_eq!(
+ constraint_name(&err),
+ Some("PdfHighlightAnchor_root_id_fkey")
+ );
+
+ sqlx::query("DELETE FROM comms_message_threads WHERE root_id = $1")
+ .bind(root_id)
+ .execute(&pool)
+ .await?;
+
+ let placeable_rows: i64 =
+ sqlx::query_scalar(r#"SELECT count(*) FROM "PdfPlaceableCommentAnchor" WHERE uuid = $1"#)
+ .bind(placeable)
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(placeable_rows, 0);
+ let highlight_root: Option<Uuid> =
+ sqlx::query_scalar(r#"SELECT root_id FROM "PdfHighlightAnchor" WHERE uuid = $1"#)
+ .bind(highlight)
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(highlight_root, None);
+ Ok(())
+}
diff --git a/crates/macro_db_client/migrations/20260910144348_messages_parent_aware_schema.sql b/crates/macro_db_client/migrations/20260910144348_messages_parent_aware_schema.sql
new file mode 100644
index 0000000000..1eb23c44fa
--- /dev/null
+++ b/crates/macro_db_client/migrations/20260910144348_messages_parent_aware_schema.sql
@@ -0,0 +1,172 @@
+-- Additive, parent-aware schema for comms_messages. Nothing is dropped or renamed;
+-- channel_id stays until every writer has moved to the parent columns.
+
+ALTER TABLE comms_messages
+ ADD COLUMN parent_entity_type text,
+ ADD COLUMN parent_entity_id text,
+ ADD COLUMN imported_author text,
+ ADD COLUMN import_metadata jsonb,
+ ADD COLUMN import_order bigint;
+
+UPDATE comms_messages
+SET parent_entity_type = 'channel',
+ parent_entity_id = channel_id::text;
+
+ALTER TABLE comms_messages
+ ALTER COLUMN parent_entity_type SET NOT NULL,
+ ALTER COLUMN parent_entity_id SET NOT NULL,
+ ALTER COLUMN channel_id DROP NOT NULL,
+ ADD CONSTRAINT comms_messages_parent_type_check
+ CHECK (parent_entity_type IN ('channel', 'document')),
+ ADD CONSTRAINT comms_messages_parent_id_check
+ CHECK (length(parent_entity_id) > 0),
+ ADD CONSTRAINT comms_messages_channel_parent_check
+ CHECK ((parent_entity_type = 'channel') = (channel_id IS NOT NULL)),
+ ADD CONSTRAINT comms_messages_parent_identity
+ UNIQUE (id, parent_entity_type, parent_entity_id);
+
+-- Transition shim. Deployed channel writers only set channel_id and the new
+-- messages crate only sets the parent columns; keep the two representations of
+-- a channel parent in step on insert. Delete this trigger together with
+-- channel_id in the last PR of the stack.
+CREATE FUNCTION sync_comms_message_parent() RETURNS trigger
+LANGUAGE plpgsql AS $$
+BEGIN
+ IF NEW.channel_id IS NOT NULL THEN
+ IF NEW.parent_entity_type IS NULL THEN
+ NEW.parent_entity_type := 'channel';
+ END IF;
+ IF NEW.parent_entity_type = 'channel' THEN
+ IF NEW.parent_entity_id IS NULL THEN
+ NEW.parent_entity_id := NEW.channel_id::text;
+ ELSIF NEW.parent_entity_id <> NEW.channel_id::text THEN
+ RAISE EXCEPTION 'comms_messages % has channel_id % but parent_entity_id %',
+ NEW.id, NEW.channel_id, NEW.parent_entity_id
+ USING ERRCODE = 'check_violation';
+ END IF;
+ END IF;
+ ELSIF NEW.parent_entity_type = 'channel' AND NEW.parent_entity_id IS NOT NULL THEN
+ NEW.channel_id := NEW.parent_entity_id::uuid;
+ END IF;
+ RETURN NEW;
+END;
+$$;
+
+CREATE TRIGGER trg_sync_comms_message_parent
+BEFORE INSERT ON comms_messages
+FOR EACH ROW
+EXECUTE FUNCTION sync_comms_message_parent();
+
+ALTER TABLE comms_messages
+ DROP CONSTRAINT comms_messages_thread_id_fkey,
+ ADD CONSTRAINT comms_messages_thread_parent_fkey
+ FOREIGN KEY (thread_id, parent_entity_type, parent_entity_id)
+ REFERENCES comms_messages (id, parent_entity_type, parent_entity_id)
+ ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED;
+
+CREATE TABLE comms_message_threads (
+ root_id uuid PRIMARY KEY,
+ parent_entity_type text NOT NULL,
+ parent_entity_id text NOT NULL,
+ user_id text NOT NULL,
+ resolved boolean NOT NULL DEFAULT false,
+ anchor jsonb,
+ import_metadata jsonb,
+ created_at timestamptz NOT NULL,
+ updated_at timestamptz NOT NULL,
+ deleted_at timestamptz,
+ CONSTRAINT comms_message_threads_root_fkey
+ FOREIGN KEY (root_id, parent_entity_type, parent_entity_id)
+ REFERENCES comms_messages (id, parent_entity_type, parent_entity_id)
+ ON DELETE CASCADE,
+ CONSTRAINT comms_message_threads_anchor_parent_check
+ CHECK (anchor IS NULL OR parent_entity_type = 'document'),
+ CONSTRAINT comms_message_threads_anchor_check CHECK (
+ anchor IS NULL OR (
+ jsonb_typeof(anchor) = 'object'
+ AND anchor->>'type' IN ('markdown', 'pdf_highlight', 'pdf_placeable')
+ )
+ )
+);
+
+CREATE UNIQUE INDEX idx_comms_message_threads_markdown_mark
+ ON comms_message_threads (parent_entity_id, (anchor->>'mark_id'))
+ WHERE anchor->>'type' = 'markdown' AND deleted_at IS NULL;
+
+CREATE INDEX idx_comms_message_threads_parent
+ ON comms_message_threads (parent_entity_type, parent_entity_id);
+
+INSERT INTO comms_message_threads (
+ root_id, parent_entity_type, parent_entity_id, user_id, created_at, updated_at
+)
+SELECT id, parent_entity_type, parent_entity_id, sender_id, created_at, updated_at
+FROM comms_messages
+WHERE thread_id IS NULL;
+
+-- Structural bookkeeping: every root message owns one thread row, and today's
+-- channel writers know nothing about the table.
+CREATE FUNCTION initialize_comms_message_thread() RETURNS trigger
+LANGUAGE plpgsql AS $$
+BEGIN
+ IF NEW.thread_id IS NULL THEN
+ INSERT INTO comms_message_threads (
+ root_id, parent_entity_type, parent_entity_id, user_id, created_at, updated_at
+ )
+ VALUES (
+ NEW.id, NEW.parent_entity_type, NEW.parent_entity_id,
+ NEW.sender_id, NEW.created_at, NEW.updated_at
+ );
+ END IF;
+ RETURN NEW;
+END;
+$$;
+
+CREATE TRIGGER trg_initialize_comms_message_thread
+AFTER INSERT ON comms_messages
+FOR EACH ROW
+EXECUTE FUNCTION initialize_comms_message_thread();
+
+-- Immutable legacy-id mappings for the comment import. Their FKs to the message
+-- tables are added once the import has written rows.
+CREATE TABLE migrated_comment_id (
+ comment_id bigint PRIMARY KEY,
+ message_id uuid NOT NULL UNIQUE,
+ document_id text NOT NULL REFERENCES "Document" (id) ON DELETE CASCADE
+);
+
+CREATE TABLE migrated_comment_thread_id (
+ thread_id bigint PRIMARY KEY,
+ root_id uuid NOT NULL UNIQUE,
+ document_id text NOT NULL REFERENCES "Document" (id) ON DELETE CASCADE
+);
+
+CREATE INDEX idx_comms_messages_parent_timeline
+ ON comms_messages (parent_entity_type, parent_entity_id, created_at, id)
+ WHERE thread_id IS NULL;
+
+CREATE INDEX idx_comms_messages_parent_history
+ ON comms_messages (parent_entity_type, parent_entity_id, created_at DESC, id DESC);
+
+CREATE INDEX idx_comms_messages_parent_activity
+ ON comms_messages (parent_entity_type, parent_entity_id, created_at DESC)
+ WHERE deleted_at IS NULL;
+
+CREATE INDEX idx_comms_messages_thread_order
+ ON comms_messages (thread_id, import_order, created_at, id)
+ WHERE thread_id IS NOT NULL;
+
+-- New document discussions attach to PDF anchors by root. Legacy anchors keep
+-- their "threadId" until the import fills root_id from the mapping tables.
+ALTER TABLE "PdfPlaceableCommentAnchor"
+ ADD COLUMN root_id uuid REFERENCES comms_message_threads (root_id) ON DELETE CASCADE;
+
+CREATE INDEX "PdfPlaceableCommentAnchor_root_id_idx"
+ ON "PdfPlaceableCommentAnchor" (root_id)
+ WHERE root_id IS NOT NULL;
+
+ALTER TABLE "PdfHighlightAnchor"
+ ADD COLUMN root_id uuid REFERENCES comms_message_threads (root_id) ON DELETE SET NULL;
+
+CREATE INDEX "PdfHighlightAnchor_root_id_idx"
+ ON "PdfHighlightAnchor" (root_id)
+ WHERE root_id IS NOT NULL;
diff --git a/crates/soup/fixtures/notified_at.sql b/crates/soup/fixtures/notified_at.sql
index 2a14f2a73f..077c3d5eca 100644
--- a/crates/soup/fixtures/notified_at.sql
+++ b/crates/soup/fixtures/notified_at.sql
@@ -76,9 +76,11 @@ INSERT INTO public.comms_channels ("id", "channel_type", "owner_id", "created_at
VALUES ('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');
-INSERT INTO public.comms_messages ("id", "channel_id", "thread_id", "sender_id", "content", "created_at", "updated_at")
+-- 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")
-- 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', 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', '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');
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),