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.

perf(email): make re-syncing an unchanged message a no-op (#7920)

teo39c3280890b2
diff --git a/.sqlx/query-1490ec3dcbefe6dff27992f93481ee30fef91724dadb7b7c1e461d8286235f60.json b/.sqlx/query-1490ec3dcbefe6dff27992f93481ee30fef91724dadb7b7c1e461d8286235f60.json
deleted file mode 100644
index 70047a0c81..0000000000
--- a/.sqlx/query-1490ec3dcbefe6dff27992f93481ee30fef91724dadb7b7c1e461d8286235f60.json
+++ /dev/null
@@ -1,44 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO email_messages (\n id, provider_id, link_id, global_id, thread_id, provider_thread_id, replying_to_id, provider_history_id, internal_date_ts,\n snippet, size_estimate, subject, from_name, from_contact_id, sent_at, has_attachments, is_read,\n is_starred, is_sent, is_draft, body_text, body_html_sanitized, headers_jsonb\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23)\n ON CONFLICT (link_id, provider_id) WHERE provider_id IS NOT NULL DO UPDATE SET\n global_id = EXCLUDED.global_id,\n provider_history_id = EXCLUDED.provider_history_id,\n provider_thread_id = EXCLUDED.provider_thread_id,\n replying_to_id = EXCLUDED.replying_to_id,\n from_name = EXCLUDED.from_name,\n from_contact_id = EXCLUDED.from_contact_id,\n internal_date_ts = EXCLUDED.internal_date_ts,\n snippet = EXCLUDED.snippet,\n size_estimate = EXCLUDED.size_estimate,\n subject = EXCLUDED.subject,\n sent_at = EXCLUDED.sent_at,\n has_attachments = EXCLUDED.has_attachments,\n is_read = EXCLUDED.is_read,\n is_starred = EXCLUDED.is_starred,\n is_sent = EXCLUDED.is_sent,\n is_draft = EXCLUDED.is_draft,\n headers_jsonb = EXCLUDED.headers_jsonb,\n body_text = EXCLUDED.body_text,\n body_html_sanitized = EXCLUDED.body_html_sanitized,\n updated_at = NOW()\n RETURNING id\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "id",
- "type_info": "Uuid"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid",
- "Text",
- "Uuid",
- "Text",
- "Uuid",
- "Text",
- "Uuid",
- "Text",
- "Timestamptz",
- "Text",
- "Int8",
- "Text",
- "Varchar",
- "Uuid",
- "Timestamptz",
- "Bool",
- "Bool",
- "Bool",
- "Bool",
- "Bool",
- "Text",
- "Text",
- "Jsonb"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "1490ec3dcbefe6dff27992f93481ee30fef91724dadb7b7c1e461d8286235f60"
-}
diff --git a/.sqlx/query-1b0915947c9a48cc032fef80600aade7a643a9222ec087dd94f3ca607a8e26d7.json b/.sqlx/query-1b0915947c9a48cc032fef80600aade7a643a9222ec087dd94f3ca607a8e26d7.json
new file mode 100644
index 0000000000..bab311708f
--- /dev/null
+++ b/.sqlx/query-1b0915947c9a48cc032fef80600aade7a643a9222ec087dd94f3ca607a8e26d7.json
@@ -0,0 +1,35 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH incoming AS (\n SELECT * FROM unnest($2::uuid[], $3::text[], $4::email_recipient_type[])\n AS t(contact_id, name, recipient_type)\n ), removed AS (\n DELETE FROM email_message_recipients\n WHERE message_id = $1::uuid\n AND (contact_id, recipient_type) NOT IN (\n SELECT contact_id, recipient_type FROM incoming\n )\n )\n INSERT INTO email_message_recipients (message_id, contact_id, name, recipient_type)\n SELECT $1, contact_id, name, recipient_type FROM incoming\n ON CONFLICT (message_id, contact_id, recipient_type) DO NOTHING\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "UuidArray",
+ "TextArray",
+ {
+ "Custom": {
+ "name": "email_recipient_type[]",
+ "kind": {
+ "Array": {
+ "Custom": {
+ "name": "email_recipient_type",
+ "kind": {
+ "Enum": [
+ "TO",
+ "CC",
+ "BCC"
+ ]
+ }
+ }
+ }
+ }
+ }
+ }
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "1b0915947c9a48cc032fef80600aade7a643a9222ec087dd94f3ca607a8e26d7"
+}
diff --git a/.sqlx/query-1cff65ed632a144c8ad44d01c4dc975bc0189d29c8c4407df52e0908d3a53059.json b/.sqlx/query-1cff65ed632a144c8ad44d01c4dc975bc0189d29c8c4407df52e0908d3a53059.json
new file mode 100644
index 0000000000..0d7692d82f
--- /dev/null
+++ b/.sqlx/query-1cff65ed632a144c8ad44d01c4dc975bc0189d29c8c4407df52e0908d3a53059.json
@@ -0,0 +1,24 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH updated AS (\n UPDATE email_messages\n SET\n replying_to_id = update_data.new_replying_to_id,\n updated_at = NOW()\n FROM\n UNNEST($1::uuid[], $2::uuid[])\n AS update_data(message_id, new_replying_to_id)\n WHERE\n email_messages.id = update_data.message_id\n AND email_messages.link_id = $3\n AND email_messages.replying_to_id IS DISTINCT FROM update_data.new_replying_to_id\n )\n SELECT COUNT(*) AS \"matched!\"\n FROM email_messages\n WHERE id = ANY($1) AND link_id = $3\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "matched!",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "UuidArray",
+ "UuidArray",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "1cff65ed632a144c8ad44d01c4dc975bc0189d29c8c4407df52e0908d3a53059"
+}
diff --git a/.sqlx/query-4d86a34e867a2f184c852a7a3040e4ea0cca6ff307640325618afdf5ba47fae9.json b/.sqlx/query-4d86a34e867a2f184c852a7a3040e4ea0cca6ff307640325618afdf5ba47fae9.json
new file mode 100644
index 0000000000..459f85a008
--- /dev/null
+++ b/.sqlx/query-4d86a34e867a2f184c852a7a3040e4ea0cca6ff307640325618afdf5ba47fae9.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH removed AS (\n DELETE FROM email_message_labels\n WHERE $3::bool\n AND message_id = $1::uuid\n AND label_id NOT IN (SELECT UNNEST($2::uuid[]))\n )\n INSERT INTO email_message_labels (message_id, label_id)\n SELECT $1, label_id FROM UNNEST($2::uuid[]) AS t(label_id)\n ON CONFLICT (message_id, label_id) DO NOTHING\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "UuidArray",
+ "Bool"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "4d86a34e867a2f184c852a7a3040e4ea0cca6ff307640325618afdf5ba47fae9"
+}
diff --git a/.sqlx/query-541d038f1693e63ee6d4626f58580bca194989d88fd991abe2efd2c66d5a5e8e.json b/.sqlx/query-541d038f1693e63ee6d4626f58580bca194989d88fd991abe2efd2c66d5a5e8e.json
deleted file mode 100644
index bc71f8c000..0000000000
--- a/.sqlx/query-541d038f1693e63ee6d4626f58580bca194989d88fd991abe2efd2c66d5a5e8e.json
+++ /dev/null
@@ -1,24 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n UPDATE email_messages\n SET\n replying_to_id = $1,\n updated_at = NOW()\n WHERE\n id = $2\n AND link_id = $3\n RETURNING id\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "id",
- "type_info": "Uuid"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid",
- "Uuid",
- "Uuid"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "541d038f1693e63ee6d4626f58580bca194989d88fd991abe2efd2c66d5a5e8e"
-}
diff --git a/.sqlx/query-6083ac1aac961924e1c6e545c28980fb5cfc48c1c2e409c12e4c98492e2a0f86.json b/.sqlx/query-6083ac1aac961924e1c6e545c28980fb5cfc48c1c2e409c12e4c98492e2a0f86.json
new file mode 100644
index 0000000000..e1bea5eec9
--- /dev/null
+++ b/.sqlx/query-6083ac1aac961924e1c6e545c28980fb5cfc48c1c2e409c12e4c98492e2a0f86.json
@@ -0,0 +1,24 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH updated AS (\n UPDATE email_messages\n SET\n replying_to_id = $1,\n updated_at = NOW()\n WHERE\n id = $2\n AND link_id = $3\n AND replying_to_id IS DISTINCT FROM $1\n )\n SELECT EXISTS (\n SELECT 1 FROM email_messages WHERE id = $2 AND link_id = $3\n ) AS \"found!\"\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "found!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "6083ac1aac961924e1c6e545c28980fb5cfc48c1c2e409c12e4c98492e2a0f86"
+}
diff --git a/.sqlx/query-6b857b8f38b989a421c17a92d2a97ffb17a235c4f8440fc192333cbe945e1422.json b/.sqlx/query-6b857b8f38b989a421c17a92d2a97ffb17a235c4f8440fc192333cbe945e1422.json
new file mode 100644
index 0000000000..ee97b7db9a
--- /dev/null
+++ b/.sqlx/query-6b857b8f38b989a421c17a92d2a97ffb17a235c4f8440fc192333cbe945e1422.json
@@ -0,0 +1,44 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO email_messages (\n id, provider_id, link_id, global_id, thread_id, provider_thread_id, replying_to_id, provider_history_id, internal_date_ts,\n snippet, size_estimate, subject, from_name, from_contact_id, sent_at, has_attachments, is_read,\n is_starred, is_sent, is_draft, body_text, body_html_sanitized, headers_jsonb\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23)\n ON CONFLICT (link_id, provider_id) WHERE provider_id IS NOT NULL DO UPDATE SET\n global_id = EXCLUDED.global_id,\n provider_history_id = EXCLUDED.provider_history_id,\n provider_thread_id = EXCLUDED.provider_thread_id,\n replying_to_id = COALESCE(EXCLUDED.replying_to_id, email_messages.replying_to_id),\n from_name = EXCLUDED.from_name,\n from_contact_id = EXCLUDED.from_contact_id,\n internal_date_ts = EXCLUDED.internal_date_ts,\n snippet = EXCLUDED.snippet,\n size_estimate = EXCLUDED.size_estimate,\n subject = EXCLUDED.subject,\n sent_at = EXCLUDED.sent_at,\n has_attachments = EXCLUDED.has_attachments,\n is_read = EXCLUDED.is_read,\n is_starred = EXCLUDED.is_starred,\n is_sent = EXCLUDED.is_sent,\n is_draft = EXCLUDED.is_draft,\n headers_jsonb = CASE\n WHEN email_messages.headers_jsonb IS DISTINCT FROM EXCLUDED.headers_jsonb\n THEN EXCLUDED.headers_jsonb ELSE email_messages.headers_jsonb END,\n body_text = CASE\n WHEN email_messages.body_text IS DISTINCT FROM EXCLUDED.body_text\n THEN EXCLUDED.body_text ELSE email_messages.body_text END,\n body_html_sanitized = CASE\n WHEN email_messages.body_html_sanitized IS DISTINCT FROM EXCLUDED.body_html_sanitized\n THEN EXCLUDED.body_html_sanitized ELSE email_messages.body_html_sanitized END,\n updated_at = NOW()\n -- Large columns last: the row comparison stops at the first difference.\n WHERE (\n email_messages.global_id, email_messages.provider_history_id,\n email_messages.provider_thread_id, email_messages.replying_to_id,\n email_messages.from_name, email_messages.from_contact_id,\n email_messages.internal_date_ts, email_messages.snippet,\n email_messages.size_estimate, email_messages.subject, email_messages.sent_at,\n email_messages.has_attachments, email_messages.is_read, email_messages.is_starred,\n email_messages.is_sent, email_messages.is_draft, email_messages.headers_jsonb,\n email_messages.body_text, email_messages.body_html_sanitized\n ) IS DISTINCT FROM (\n EXCLUDED.global_id, EXCLUDED.provider_history_id,\n EXCLUDED.provider_thread_id, COALESCE(EXCLUDED.replying_to_id, email_messages.replying_to_id),\n EXCLUDED.from_name, EXCLUDED.from_contact_id,\n EXCLUDED.internal_date_ts, EXCLUDED.snippet,\n EXCLUDED.size_estimate, EXCLUDED.subject, EXCLUDED.sent_at,\n EXCLUDED.has_attachments, EXCLUDED.is_read, EXCLUDED.is_starred,\n EXCLUDED.is_sent, EXCLUDED.is_draft, EXCLUDED.headers_jsonb,\n EXCLUDED.body_text, EXCLUDED.body_html_sanitized\n )\n RETURNING id\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Uuid",
+ "Text",
+ "Uuid",
+ "Text",
+ "Uuid",
+ "Text",
+ "Timestamptz",
+ "Text",
+ "Int8",
+ "Text",
+ "Varchar",
+ "Uuid",
+ "Timestamptz",
+ "Bool",
+ "Bool",
+ "Bool",
+ "Bool",
+ "Bool",
+ "Text",
+ "Text",
+ "Jsonb"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "6b857b8f38b989a421c17a92d2a97ffb17a235c4f8440fc192333cbe945e1422"
+}
diff --git a/.sqlx/query-88cb2c1126909dabe0cc1846ca2733ab98724d4b89d409fa770209d4d7627654.json b/.sqlx/query-88cb2c1126909dabe0cc1846ca2733ab98724d4b89d409fa770209d4d7627654.json
deleted file mode 100644
index 566b687721..0000000000
--- a/.sqlx/query-88cb2c1126909dabe0cc1846ca2733ab98724d4b89d409fa770209d4d7627654.json
+++ /dev/null
@@ -1,35 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO email_message_recipients (message_id, contact_id, name, recipient_type)\n SELECT * FROM unnest($1::uuid[], $2::uuid[], $3::text[], $4::email_recipient_type[])\n ON CONFLICT (message_id, contact_id, recipient_type) DO NOTHING\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "UuidArray",
- "UuidArray",
- "TextArray",
- {
- "Custom": {
- "name": "email_recipient_type[]",
- "kind": {
- "Array": {
- "Custom": {
- "name": "email_recipient_type",
- "kind": {
- "Enum": [
- "TO",
- "CC",
- "BCC"
- ]
- }
- }
- }
- }
- }
- }
- ]
- },
- "nullable": []
- },
- "hash": "88cb2c1126909dabe0cc1846ca2733ab98724d4b89d409fa770209d4d7627654"
-}
diff --git a/.sqlx/query-8fdd4afe0b260c02acea8a052a6fe39d6ad21150fc2f1049548c724516b9210b.json b/.sqlx/query-8fdd4afe0b260c02acea8a052a6fe39d6ad21150fc2f1049548c724516b9210b.json
deleted file mode 100644
index 0305c903f5..0000000000
--- a/.sqlx/query-8fdd4afe0b260c02acea8a052a6fe39d6ad21150fc2f1049548c724516b9210b.json
+++ /dev/null
@@ -1,15 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n DELETE FROM email_message_labels\n WHERE message_id = $1\n AND label_id NOT IN (\n SELECT UNNEST($2::uuid[])\n )\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "UuidArray"
- ]
- },
- "nullable": []
- },
- "hash": "8fdd4afe0b260c02acea8a052a6fe39d6ad21150fc2f1049548c724516b9210b"
-}
diff --git a/.sqlx/query-9cf47f4d88c29f80e8698b91319d81031064da7fa08afec7364df40546b1ac0c.json b/.sqlx/query-9cf47f4d88c29f80e8698b91319d81031064da7fa08afec7364df40546b1ac0c.json
deleted file mode 100644
index befc98605f..0000000000
--- a/.sqlx/query-9cf47f4d88c29f80e8698b91319d81031064da7fa08afec7364df40546b1ac0c.json
+++ /dev/null
@@ -1,16 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n UPDATE email_messages\n SET\n replying_to_id = update_data.new_replying_to_id,\n updated_at = NOW()\n FROM\n UNNEST($1::uuid[], $2::uuid[])\n AS update_data(message_id, new_replying_to_id)\n WHERE\n email_messages.id = update_data.message_id\n AND email_messages.link_id = $3\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "UuidArray",
- "UuidArray",
- "Uuid"
- ]
- },
- "nullable": []
- },
- "hash": "9cf47f4d88c29f80e8698b91319d81031064da7fa08afec7364df40546b1ac0c"
-}
diff --git a/.sqlx/query-a7ae627a68e68b0f097070774b430443e00c442f641df35da2be7a15311b0793.json b/.sqlx/query-a7ae627a68e68b0f097070774b430443e00c442f641df35da2be7a15311b0793.json
new file mode 100644
index 0000000000..f7d93ed394
--- /dev/null
+++ b/.sqlx/query-a7ae627a68e68b0f097070774b430443e00c442f641df35da2be7a15311b0793.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT id FROM email_messages\n WHERE link_id = $1 AND provider_id = $2\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "a7ae627a68e68b0f097070774b430443e00c442f641df35da2be7a15311b0793"
+}
diff --git a/.sqlx/query-b8eb30360d1232b36214b17758d8c7d897251a93c7d56b310a2fc6f026fdbea9.json b/.sqlx/query-b8eb30360d1232b36214b17758d8c7d897251a93c7d56b310a2fc6f026fdbea9.json
deleted file mode 100644
index 331e15bd4e..0000000000
--- a/.sqlx/query-b8eb30360d1232b36214b17758d8c7d897251a93c7d56b310a2fc6f026fdbea9.json
+++ /dev/null
@@ -1,34 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n DELETE FROM email_message_recipients\n WHERE message_id = $1\n AND (contact_id, recipient_type) NOT IN (\n SELECT contact_id, recipient_type\n FROM unnest($2::uuid[], $3::email_recipient_type[])\n AS t(contact_id, recipient_type)\n )\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "UuidArray",
- {
- "Custom": {
- "name": "email_recipient_type[]",
- "kind": {
- "Array": {
- "Custom": {
- "name": "email_recipient_type",
- "kind": {
- "Enum": [
- "TO",
- "CC",
- "BCC"
- ]
- }
- }
- }
- }
- }
- }
- ]
- },
- "nullable": []
- },
- "hash": "b8eb30360d1232b36214b17758d8c7d897251a93c7d56b310a2fc6f026fdbea9"
-}
diff --git a/crates/email/src/outbound/email_pg_repo/message.rs b/crates/email/src/outbound/email_pg_repo/message.rs
index ec50dee71e..e00f6f80e6 100644
--- a/crates/email/src/outbound/email_pg_repo/message.rs
+++ b/crates/email/src/outbound/email_pg_repo/message.rs
@@ -491,13 +491,11 @@ pub(super) async fn upsert_recipients(
}
let n = contacts.recipients.len();
- let mut message_ids: Vec<Uuid> = Vec::with_capacity(n);
let mut contact_ids: Vec<Uuid> = Vec::with_capacity(n);
let mut recipient_names: Vec<Option<String>> = Vec::with_capacity(n);
let mut recipient_types: Vec<DbRecipientType> = Vec::with_capacity(n);
for r in &contacts.recipients {
- message_ids.push(message_db_id);
contact_ids.push(r.contact_id);
recipient_names.push(r.name.clone());
recipient_types.push(match r.recipient_type {
@@ -507,32 +505,24 @@ pub(super) async fn upsert_recipients(
});
}
- // Delete stale recipients
- sqlx::query!(
- r#"
- DELETE FROM email_message_recipients
- WHERE message_id = $1
- AND (contact_id, recipient_type) NOT IN (
- SELECT contact_id, recipient_type
- FROM unnest($2::uuid[], $3::email_recipient_type[])
- AS t(contact_id, recipient_type)
- )
- "#,
- message_db_id,
- &contact_ids,
- &recipient_types as &[DbRecipientType],
- )
- .execute(&mut *tx)
- .await?;
-
- // Insert new recipients
+ // Delete stale recipients and insert new ones; an unchanged set deletes and inserts nothing.
sqlx::query!(
r#"
+ WITH incoming AS (
+ SELECT * FROM unnest($2::uuid[], $3::text[], $4::email_recipient_type[])
+ AS t(contact_id, name, recipient_type)
+ ), removed AS (
+ DELETE FROM email_message_recipients
+ WHERE message_id = $1::uuid
+ AND (contact_id, recipient_type) NOT IN (
+ SELECT contact_id, recipient_type FROM incoming
+ )
+ )
INSERT INTO email_message_recipients (message_id, contact_id, name, recipient_type)
- SELECT * FROM unnest($1::uuid[], $2::uuid[], $3::text[], $4::email_recipient_type[])
+ SELECT $1, contact_id, name, recipient_type FROM incoming
ON CONFLICT (message_id, contact_id, recipient_type) DO NOTHING
"#,
- &message_ids,
+ message_db_id,
&contact_ids,
&recipient_names as &[Option<String>],
&recipient_types as &[DbRecipientType],
diff --git a/crates/email/src/outbound/email_pg_repo/test/message.rs b/crates/email/src/outbound/email_pg_repo/test/message.rs
index 4d1d284ccd..80cc8814de 100644
--- a/crates/email/src/outbound/email_pg_repo/test/message.rs
+++ b/crates/email/src/outbound/email_pg_repo/test/message.rs
@@ -705,6 +705,50 @@ async fn test_upsert_recipients_removes_stale(pool: Pool<Postgres>) -> anyhow::R
Ok(())
}
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../../fixtures", scripts("email_draft"))
+)]
+async fn test_upsert_recipients_unchanged_set_writes_nothing(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ // msg1 has bob=TO in the fixture; upserting the same set must leave the row alone.
+ let msg_id = Uuid::parse_str("ee000001-0000-0000-0000-000000000001")?;
+ let bob_id = Uuid::parse_str("c0000002-0000-0000-0000-000000000002")?;
+
+ let row_versions = || {
+ sqlx::query_scalar!(
+ r#"
+ SELECT xmin::text AS "xmin!"
+ FROM email_message_recipients
+ WHERE message_id = $1
+ ORDER BY contact_id, recipient_type
+ "#,
+ msg_id
+ )
+ .fetch_all(&pool)
+ };
+ let before = row_versions().await?;
+ assert_eq!(before.len(), 1);
+
+ let contacts = UpsertedContacts {
+ from_contact_id: None,
+ recipients: vec![UpsertedRecipient {
+ contact_id: bob_id,
+ name: Some("Bob Jones".to_string()),
+ recipient_type: RecipientType::To,
+ }],
+ };
+
+ let mut tx = pool.begin().await?;
+ super::super::message::upsert_recipients(&mut tx, msg_id, &contacts).await?;
+ tx.commit().await?;
+
+ assert_eq!(row_versions().await?, before);
+
+ Ok(())
+}
+
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../../fixtures", scripts("email_draft"))
diff --git a/crates/email_db_client/src/contacts/upsert_message.rs b/crates/email_db_client/src/contacts/upsert_message.rs
index 5b42003e41..53d8f11fb4 100644
--- a/crates/email_db_client/src/contacts/upsert_message.rs
+++ b/crates/email_db_client/src/contacts/upsert_message.rs
@@ -278,44 +278,36 @@ pub async fn upsert_message_recipients(
}
let n = upserted_recipients.recipients.len();
- let mut message_ids_to_insert: Vec<Uuid> = Vec::with_capacity(n);
let mut contact_ids_to_insert: Vec<Uuid> = Vec::with_capacity(n);
let mut names_to_insert: Vec<Option<String>> = Vec::with_capacity(n);
let mut recipient_types_to_insert: Vec<address::EmailRecipientType> = Vec::with_capacity(n);
for recipient in &upserted_recipients.recipients {
- message_ids_to_insert.push(message_id);
contact_ids_to_insert.push(recipient.contact_id);
names_to_insert.push(recipient.name.clone());
recipient_types_to_insert.push(recipient.recipient_type.clone());
}
// Delete existing recipients for the message_id that don't match the values we are about to
- // insert, in case this is an upsert and some values got removed since the last insert (think drafts)
- sqlx::query!(
- r#"
- DELETE FROM email_message_recipients
- WHERE message_id = $1
- AND (contact_id, recipient_type) NOT IN (
- SELECT contact_id, recipient_type
- FROM unnest($2::uuid[], $3::email_recipient_type[])
- AS t(contact_id, recipient_type)
- )
- "#,
- message_id,
- &contact_ids_to_insert,
- &recipient_types_to_insert as &[db::address::EmailRecipientType]
- )
- .execute(&mut *tx)
- .await?;
-
+ // insert, in case this is an upsert and some values got removed since the last insert (think
+ // drafts). An unchanged set deletes and inserts nothing.
sqlx::query!(
r#"
+ WITH incoming AS (
+ SELECT * FROM unnest($2::uuid[], $3::text[], $4::email_recipient_type[])
+ AS t(contact_id, name, recipient_type)
+ ), removed AS (
+ DELETE FROM email_message_recipients
+ WHERE message_id = $1::uuid
+ AND (contact_id, recipient_type) NOT IN (
+ SELECT contact_id, recipient_type FROM incoming
+ )
+ )
INSERT INTO email_message_recipients (message_id, contact_id, name, recipient_type)
- SELECT * FROM unnest($1::uuid[], $2::uuid[], $3::text[], $4::email_recipient_type[])
+ SELECT $1, contact_id, name, recipient_type FROM incoming
ON CONFLICT (message_id, contact_id, recipient_type) DO NOTHING
"#,
- &message_ids_to_insert,
+ message_id,
&contact_ids_to_insert,
&names_to_insert as &[Option<String>],
&recipient_types_to_insert as &[db::address::EmailRecipientType]
diff --git a/crates/email_db_client/src/labels/insert.rs b/crates/email_db_client/src/labels/insert.rs
index 056a45c178..38c70c551f 100644
--- a/crates/email_db_client/src/labels/insert.rs
+++ b/crates/email_db_client/src/labels/insert.rs
@@ -259,7 +259,6 @@ pub async fn insert_label(
}
/// inserts the labels of an email into the database in a batch
-#[allow(clippy::disallowed_methods, reason = "legacy code. fix later")]
pub async fn insert_message_labels(
tx: &mut sqlx::PgConnection,
link_id: Uuid,
@@ -309,38 +308,27 @@ pub async fn insert_message_labels(
);
}
- // insert into junction table
- let message_ids_repeated: Vec<Uuid> =
- std::iter::repeat_n(message_id, label_mappings.len()).collect();
let label_db_ids: Vec<Uuid> = label_mappings.iter().map(|m| m.id).collect();
- // deleting records that don't match the ones to insert first in case we are doing an
- // upsert and some of the old ones got removed
- if delete_old {
- sqlx::query!(
- r#"
- DELETE FROM email_message_labels
- WHERE message_id = $1
- AND label_id NOT IN (
- SELECT UNNEST($2::uuid[])
- )
- "#,
- message_id,
- &label_db_ids
- )
- .execute(&mut *tx)
- .await?;
- }
-
- sqlx::query(
+ // with delete_old, records that don't match the ones to insert are removed in case we are
+ // doing an upsert and some of the old ones got removed. An unchanged set deletes and inserts
+ // nothing.
+ sqlx::query!(
r#"
+ WITH removed AS (
+ DELETE FROM email_message_labels
+ WHERE $3::bool
+ AND message_id = $1::uuid
+ AND label_id NOT IN (SELECT UNNEST($2::uuid[]))
+ )
INSERT INTO email_message_labels (message_id, label_id)
- SELECT * FROM unnest($1::uuid[], $2::uuid[])
+ SELECT $1, label_id FROM UNNEST($2::uuid[]) AS t(label_id)
ON CONFLICT (message_id, label_id) DO NOTHING
"#,
+ message_id,
+ &label_db_ids,
+ delete_old
)
- .bind(&message_ids_repeated)
- .bind(&label_db_ids)
.execute(&mut *tx)
.await?;
diff --git a/crates/email_db_client/src/messages/insert.rs b/crates/email_db_client/src/messages/insert.rs
index 2c9898033e..42ac85da68 100644
--- a/crates/email_db_client/src/messages/insert.rs
+++ b/crates/email_db_client/src/messages/insert.rs
@@ -1,5 +1,5 @@
#[cfg(test)]
-mod test;
+pub(crate) mod test;
use crate::attachments::provider;
use crate::messages::replying_to_id;
@@ -72,6 +72,13 @@ pub async fn insert_message_with_tx(
}
/// inserts message object into the database
+///
+/// On conflict the update is skipped entirely when the stored row already
+/// holds the incoming values, so a re-sync writes no new row version. When
+/// only small columns change, the large body/header columns keep their
+/// existing values, which lets Postgres reuse their TOAST chunks instead of
+/// rewriting them. Provider messages arrive without `replying_to_id` (it is
+/// resolved from headers afterwards), so a NULL keeps the stored value.
#[tracing::instrument(skip(tx, message), err)]
async fn insert_db_message(
tx: &mut sqlx::PgConnection,
@@ -83,7 +90,7 @@ async fn insert_db_message(
let db_message =
parse::service_to_db::map_service_message_to_db(message, thread_id, from_contact_id);
- let result = sqlx::query!(
+ let result = sqlx::query_scalar!(
r#"
INSERT INTO email_messages (
id, provider_id, link_id, global_id, thread_id, provider_thread_id, replying_to_id, provider_history_id, internal_date_ts,
@@ -95,7 +102,7 @@ async fn insert_db_message(
global_id = EXCLUDED.global_id,
provider_history_id = EXCLUDED.provider_history_id,
provider_thread_id = EXCLUDED.provider_thread_id,
- replying_to_id = EXCLUDED.replying_to_id,
+ replying_to_id = COALESCE(EXCLUDED.replying_to_id, email_messages.replying_to_id),
from_name = EXCLUDED.from_name,
from_contact_id = EXCLUDED.from_contact_id,
internal_date_ts = EXCLUDED.internal_date_ts,
@@ -108,10 +115,36 @@ async fn insert_db_message(
is_starred = EXCLUDED.is_starred,
is_sent = EXCLUDED.is_sent,
is_draft = EXCLUDED.is_draft,
- headers_jsonb = EXCLUDED.headers_jsonb,
- body_text = EXCLUDED.body_text,
- body_html_sanitized = EXCLUDED.body_html_sanitized,
+ headers_jsonb = CASE
+ WHEN email_messages.headers_jsonb IS DISTINCT FROM EXCLUDED.headers_jsonb
+ THEN EXCLUDED.headers_jsonb ELSE email_messages.headers_jsonb END,
+ body_text = CASE
+ WHEN email_messages.body_text IS DISTINCT FROM EXCLUDED.body_text
+ THEN EXCLUDED.body_text ELSE email_messages.body_text END,
+ body_html_sanitized = CASE
+ WHEN email_messages.body_html_sanitized IS DISTINCT FROM EXCLUDED.body_html_sanitized
+ THEN EXCLUDED.body_html_sanitized ELSE email_messages.body_html_sanitized END,
updated_at = NOW()
+ -- Large columns last: the row comparison stops at the first difference.
+ WHERE (
+ email_messages.global_id, email_messages.provider_history_id,
+ email_messages.provider_thread_id, email_messages.replying_to_id,
+ email_messages.from_name, email_messages.from_contact_id,
+ email_messages.internal_date_ts, email_messages.snippet,
+ email_messages.size_estimate, email_messages.subject, email_messages.sent_at,
+ email_messages.has_attachments, email_messages.is_read, email_messages.is_starred,
+ email_messages.is_sent, email_messages.is_draft, email_messages.headers_jsonb,
+ email_messages.body_text, email_messages.body_html_sanitized
+ ) IS DISTINCT FROM (
+ EXCLUDED.global_id, EXCLUDED.provider_history_id,
+ EXCLUDED.provider_thread_id, COALESCE(EXCLUDED.replying_to_id, email_messages.replying_to_id),
+ EXCLUDED.from_name, EXCLUDED.from_contact_id,
+ EXCLUDED.internal_date_ts, EXCLUDED.snippet,
+ EXCLUDED.size_estimate, EXCLUDED.subject, EXCLUDED.sent_at,
+ EXCLUDED.has_attachments, EXCLUDED.is_read, EXCLUDED.is_starred,
+ EXCLUDED.is_sent, EXCLUDED.is_draft, EXCLUDED.headers_jsonb,
+ EXCLUDED.body_text, EXCLUDED.body_html_sanitized
+ )
RETURNING id
"#,
db_message.id,
@@ -138,10 +171,26 @@ async fn insert_db_message(
db_message.body_html_sanitized,
db_message.headers_jsonb
)
- .fetch_one(&mut *tx)
+ .fetch_optional(&mut *tx)
.await?;
- Ok(result.id)
+ if let Some(id) = result {
+ return Ok(id);
+ }
+
+ // The conflicting row already holds this data; fetch its id.
+ let existing_id = sqlx::query_scalar!(
+ r#"
+ SELECT id FROM email_messages
+ WHERE link_id = $1 AND provider_id = $2
+ "#,
+ db_message.link_id,
+ db_message.provider_id,
+ )
+ .fetch_one(&mut *tx)
+ .await?;
+
+ Ok(existing_id)
}
/// Inserts a single message into the database with transaction handling
diff --git a/crates/email_db_client/src/messages/insert/test.rs b/crates/email_db_client/src/messages/insert/test.rs
index 5933b5f61d..421841d406 100644
--- a/crates/email_db_client/src/messages/insert/test.rs
+++ b/crates/email_db_client/src/messages/insert/test.rs
@@ -1,8 +1,9 @@
use super::*;
-use chrono::Utc;
+use chrono::{DateTime, Utc};
use macro_db_migrator::MACRO_DB_MIGRATIONS;
use models_email::email::service::address::ContactInfo;
use models_email::email::service::attachment::Attachment;
+use models_email::email::service::label::Label;
use sqlx::{Pool, Postgres};
const LINK_ID: &str = "00000000-0000-0000-0000-000000000001";
@@ -155,3 +156,418 @@ async fn oversized_provider_values_do_not_block_the_message_insert(
Ok(())
}
+
+pub(crate) const PARENT_GLOBAL_ID: &str = "<[email protected]>";
+
+fn contact(email: &str) -> ContactInfo {
+ ContactInfo {
+ email: email.to_string(),
+ name: None,
+ photo_url: None,
+ }
+}
+
+/// A message as the provider sync builds it: a fresh `db_id` on every sync and no
+/// `replying_to_id`, which is resolved from the headers after the insert.
+pub(crate) struct SyncedMessage<'a> {
+ pub(crate) provider_id: &'a str,
+ pub(crate) global_id: &'a str,
+ pub(crate) in_reply_to: Option<&'a str>,
+ pub(crate) to: &'a [&'a str],
+ pub(crate) cc: &'a [&'a str],
+ pub(crate) labels: &'a [&'a str],
+ pub(crate) body: &'a str,
+}
+
+impl SyncedMessage<'_> {
+ pub(crate) fn build(&self, link_id: Uuid, thread_db_id: Uuid) -> message::Message {
+ let ts = DateTime::<Utc>::from_timestamp(1_790_000_000, 0).expect("valid timestamp");
+ let headers = self.in_reply_to.map(|in_reply_to| {
+ serde_json::json!([
+ { "name": "Message-ID", "value": self.global_id },
+ { "name": "In-Reply-To", "value": in_reply_to },
+ ])
+ });
+ message::Message {
+ db_id: Uuid::now_v7(),
+ provider_id: Some(self.provider_id.to_string()),
+ thread_db_id,
+ provider_thread_id: Some("prov1".to_string()),
+ replying_to_id: None,
+ global_id: Some(self.global_id.to_string()),
+ link_id,
+ subject: Some("quarterly numbers".to_string()),
+ snippet: Some("snippet".to_string()),
+ provider_history_id: Some("1001".to_string()),
+ internal_date_ts: Some(ts),
+ sent_at: Some(ts),
+ size_estimate: Some(self.body.len() as i64),
+ is_read: false,
+ is_starred: false,
+ is_sent: false,
+ is_draft: false,
+ scheduled_send_time: None,
+ has_attachments: false,
+ from: Some(contact("[email protected]")),
+ to: self.to.iter().copied().map(contact).collect(),
+ cc: self.cc.iter().copied().map(contact).collect(),
+ bcc: vec![],
+ labels: self
+ .labels
+ .iter()
+ .map(|provider_label_id| Label {
+ id: None,
+ link_id,
+ provider_label_id: provider_label_id.to_string(),
+ name: None,
+ created_at: ts,
+ message_list_visibility: None,
+ label_list_visibility: None,
+ type_: None,
+ })
+ .collect(),
+ body_text: Some(self.body.to_string()),
+ body_html_sanitized: Some(format!("<p>{}</p>", self.body)),
+ body_macro: None,
+ attachments: vec![],
+ attachments_draft: vec![],
+ attachments_forwarded: vec![],
+ headers_json: headers,
+ created_at: ts,
+ updated_at: ts,
+ }
+ }
+}
+
+async fn seed_labels(pool: &PgPool, link_id: Uuid) -> anyhow::Result<()> {
+ sqlx::query!(
+ r#"
+ INSERT INTO email_labels (id, link_id, provider_label_id, name)
+ VALUES ($1, $3, 'INBOX', 'INBOX'), ($2, $3, 'UNREAD', 'UNREAD')
+ "#,
+ Uuid::now_v7(),
+ Uuid::now_v7(),
+ link_id,
+ )
+ .execute(pool)
+ .await?;
+ Ok(())
+}
+
+/// Row versions (`xmin`) of a message and its recipient and label rows. Any rewrite, or a
+/// delete followed by a re-insert, produces a new version.
+#[derive(Debug, PartialEq, Eq)]
+pub(crate) struct RowVersions {
+ pub(crate) message: String,
+ pub(crate) recipients: Vec<(String, String, String)>,
+ pub(crate) labels: Vec<(String, String)>,
+}
+
+pub(crate) async fn row_versions(pool: &PgPool, message_id: Uuid) -> anyhow::Result<RowVersions> {
+ let message = sqlx::query_scalar!(
+ r#"SELECT xmin::text AS "xmin!" FROM email_messages WHERE id = $1"#,
+ message_id
+ )
+ .fetch_one(pool)
+ .await?;
+
+ let recipients = sqlx::query!(
+ r#"
+ SELECT c.email_address, r.recipient_type::text AS "recipient_type!", r.xmin::text AS "xmin!"
+ FROM email_message_recipients r
+ JOIN email_contacts c ON c.id = r.contact_id
+ WHERE r.message_id = $1
+ ORDER BY c.email_address, r.recipient_type
+ "#,
+ message_id
+ )
+ .fetch_all(pool)
+ .await?
+ .into_iter()
+ .map(|r| (r.email_address, r.recipient_type, r.xmin))
+ .collect();
+
+ let labels = sqlx::query!(
+ r#"
+ SELECT l.provider_label_id, ml.xmin::text AS "xmin!"
+ FROM email_message_labels ml
+ JOIN email_labels l ON l.id = ml.label_id
+ WHERE ml.message_id = $1
+ ORDER BY l.provider_label_id
+ "#,
+ message_id
+ )
+ .fetch_all(pool)
+ .await?
+ .into_iter()
+ .map(|r| (r.provider_label_id, r.xmin))
+ .collect();
+
+ Ok(RowVersions {
+ message,
+ recipients,
+ labels,
+ })
+}
+
+/// `email_messages` row updates and TOAST chunk inserts. On Postgres 15+ the `xact` counters also
+/// include unflushed counts from earlier transactions on the same connection, so only compare
+/// against a baseline read in the same transaction.
+#[derive(Debug, PartialEq, Eq)]
+struct MessageWrites {
+ updated: i64,
+ toast_inserted: i64,
+}
+
+impl MessageWrites {
+ fn since(self, baseline: &MessageWrites) -> MessageWrites {
+ MessageWrites {
+ updated: self.updated - baseline.updated,
+ toast_inserted: self.toast_inserted - baseline.toast_inserted,
+ }
+ }
+}
+
+async fn message_writes(tx: &mut sqlx::PgConnection) -> anyhow::Result<MessageWrites> {
+ let row = sqlx::query!(
+ r#"
+ SELECT
+ pg_stat_get_xact_tuples_updated('email_messages'::regclass) AS "updated!",
+ pg_stat_get_xact_tuples_inserted(
+ (SELECT reltoastrelid FROM pg_class WHERE oid = 'email_messages'::regclass)
+ ) AS "toast_inserted!"
+ "#
+ )
+ .fetch_one(&mut *tx)
+ .await?;
+ Ok(MessageWrites {
+ updated: row.updated,
+ toast_inserted: row.toast_inserted,
+ })
+}
+
+async fn from_contact_id(pool: &PgPool, message_id: Uuid) -> anyhow::Result<Option<Uuid>> {
+ Ok(sqlx::query_scalar!(
+ r#"SELECT from_contact_id FROM email_messages WHERE id = $1"#,
+ message_id
+ )
+ .fetch_one(pool)
+ .await?)
+}
+
+/// Large enough that Postgres stores the bodies out of line in TOAST.
+fn large_body() -> String {
+ (0..2_000)
+ .map(|_| Uuid::now_v7().to_string())
+ .collect::<Vec<_>>()
+ .join(" ")
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("links", "threads"))
+)]
+async fn resyncing_an_unchanged_message_writes_nothing(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ let link_id = Uuid::parse_str(LINK_ID)?;
+ let thread_db_id = Uuid::parse_str(THREAD_ID)?;
+ seed_labels(&pool, link_id).await?;
+
+ let mut parent = SyncedMessage {
+ provider_id: "parent",
+ global_id: PARENT_GLOBAL_ID,
+ in_reply_to: None,
+ to: &["[email protected]"],
+ cc: &[],
+ labels: &["INBOX"],
+ body: "parent body",
+ }
+ .build(link_id, thread_db_id);
+ insert_message(&pool, thread_db_id, &mut parent, link_id, true).await?;
+
+ let body = large_body();
+ let reply = SyncedMessage {
+ provider_id: "reply",
+ global_id: "<[email protected]>",
+ in_reply_to: Some(PARENT_GLOBAL_ID),
+ to: &["[email protected]"],
+ cc: &["[email protected]"],
+ labels: &["INBOX", "UNREAD"],
+ body: &body,
+ };
+ let mut first_sync = reply.build(link_id, thread_db_id);
+ let reply_id = first_sync.db_id;
+ insert_message(&pool, thread_db_id, &mut first_sync, link_id, true).await?;
+
+ let replying_to = sqlx::query_scalar!(
+ r#"SELECT replying_to_id FROM email_messages WHERE id = $1"#,
+ reply_id
+ )
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(replying_to, Some(parent.db_id));
+
+ let before = row_versions(&pool, reply_id).await?;
+ assert_eq!(before.recipients.len(), 2);
+ assert_eq!(before.labels.len(), 2);
+
+ // The full ingest path: message upsert, recipients, labels and the replying_to update.
+ let mut resync = reply.build(link_id, thread_db_id);
+ insert_message(&pool, thread_db_id, &mut resync, link_id, true).await?;
+
+ assert_eq!(
+ row_versions(&pool, reply_id).await?,
+ before,
+ "re-syncing identical data must not write new row versions"
+ );
+ let replying_to = sqlx::query_scalar!(
+ r#"SELECT replying_to_id FROM email_messages WHERE id = $1"#,
+ reply_id
+ )
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(replying_to, Some(parent.db_id));
+
+ // The skipped upsert still resolves the stored row's id.
+ let from_contact_id = from_contact_id(&pool, reply_id).await?;
+ let mut tx = pool.begin().await?;
+ let baseline = message_writes(&mut tx).await?;
+ let mut resync = reply.build(link_id, thread_db_id);
+ let returned_id =
+ insert_db_message(&mut tx, &mut resync, thread_db_id, from_contact_id, true).await?;
+ assert_eq!(returned_id, reply_id);
+ assert_eq!(
+ message_writes(&mut tx).await?.since(&baseline),
+ MessageWrites {
+ updated: 0,
+ toast_inserted: 0
+ }
+ );
+ tx.rollback().await?;
+
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("links", "threads"))
+)]
+async fn changed_flag_updates_the_row_without_rewriting_body_toast(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let link_id = Uuid::parse_str(LINK_ID)?;
+ let thread_db_id = Uuid::parse_str(THREAD_ID)?;
+ let body = large_body();
+ let synced = SyncedMessage {
+ provider_id: "toasted",
+ global_id: "<[email protected]>",
+ in_reply_to: None,
+ to: &["[email protected]"],
+ cc: &[],
+ labels: &[],
+ body: &body,
+ };
+ let mut first_sync = synced.build(link_id, thread_db_id);
+ let message_id = first_sync.db_id;
+ insert_message(&pool, thread_db_id, &mut first_sync, link_id, false).await?;
+ let from_contact_id = from_contact_id(&pool, message_id).await?;
+
+ let mut tx = pool.begin().await?;
+ let baseline = message_writes(&mut tx).await?;
+ let mut read = synced.build(link_id, thread_db_id);
+ read.is_read = true;
+ let returned_id =
+ insert_db_message(&mut tx, &mut read, thread_db_id, from_contact_id, false).await?;
+ assert_eq!(returned_id, message_id);
+ assert_eq!(
+ message_writes(&mut tx).await?.since(&baseline),
+ MessageWrites {
+ updated: 1,
+ toast_inserted: 0
+ },
+ "an unchanged body must keep its TOAST chunks"
+ );
+
+ let edited_body = format!("{body} edited");
+ let edited = SyncedMessage {
+ body: &edited_body,
+ ..synced
+ };
+ let mut edited = edited.build(link_id, thread_db_id);
+ edited.is_read = true;
+ insert_db_message(&mut tx, &mut edited, thread_db_id, from_contact_id, false).await?;
+ let writes = message_writes(&mut tx).await?.since(&baseline);
+ assert_eq!(writes.updated, 2);
+ assert!(
+ writes.toast_inserted > 0,
+ "a changed body is written to TOAST"
+ );
+ tx.commit().await?;
+
+ let stored = sqlx::query!(
+ r#"SELECT is_read, body_text FROM email_messages WHERE id = $1"#,
+ message_id
+ )
+ .fetch_one(&pool)
+ .await?;
+ assert!(stored.is_read);
+ assert_eq!(stored.body_text.as_deref(), Some(edited_body.as_str()));
+
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("links", "threads"))
+)]
+async fn resync_with_changed_labels_and_recipients_replaces_only_the_changes(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let link_id = Uuid::parse_str(LINK_ID)?;
+ let thread_db_id = Uuid::parse_str(THREAD_ID)?;
+ seed_labels(&pool, link_id).await?;
+
+ let synced = SyncedMessage {
+ provider_id: "relabelled",
+ global_id: "<[email protected]>",
+ in_reply_to: None,
+ to: &["[email protected]"],
+ cc: &["[email protected]"],
+ labels: &["INBOX", "UNREAD"],
+ body: "body",
+ };
+ let mut first_sync = synced.build(link_id, thread_db_id);
+ let message_id = first_sync.db_id;
+ insert_message(&pool, thread_db_id, &mut first_sync, link_id, false).await?;
+ let before = row_versions(&pool, message_id).await?;
+
+ let mut resync = SyncedMessage {
+ cc: &["[email protected]"],
+ labels: &["INBOX"],
+ ..synced
+ }
+ .build(link_id, thread_db_id);
+ insert_message(&pool, thread_db_id, &mut resync, link_id, false).await?;
+ let after = row_versions(&pool, message_id).await?;
+
+ assert_eq!(after.message, before.message);
+ assert_eq!(
+ after
+ .recipients
+ .iter()
+ .map(|(email, kind, _)| (email.as_str(), kind.as_str()))
+ .collect::<Vec<_>>(),
+ vec![("[email protected]", "CC"), ("[email protected]", "TO")]
+ );
+ let kept_to = |versions: &RowVersions| {
+ versions
+ .recipients
+ .iter()
+ .find(|(email, _, _)| email == "[email protected]")
+ .cloned()
+ };
+ assert_eq!(kept_to(&after), kept_to(&before));
+ assert_eq!(after.labels, vec![before.labels[0].clone()]);
+ assert_eq!(after.labels[0].0, "INBOX");
+
+ Ok(())
+}
diff --git a/crates/email_db_client/src/messages/replying_to_id.rs b/crates/email_db_client/src/messages/replying_to_id.rs
index 6fb623c320..254416ad18 100644
--- a/crates/email_db_client/src/messages/replying_to_id.rs
+++ b/crates/email_db_client/src/messages/replying_to_id.rs
@@ -1,3 +1,6 @@
+#[cfg(test)]
+mod test;
+
use crate::messages;
use crate::messages::get;
@@ -5,7 +8,8 @@ use models_email::service::message;
use sqlx::types::Uuid;
use std::collections::HashMap;
-/// Updates a single message's replying_to_id field
+/// Updates a single message's replying_to_id field. The row is only rewritten when the value
+/// changes. Returns whether the message exists for the link.
#[tracing::instrument(skip(executor), err)]
pub async fn update_db_message_replying_to_id<'e, E>(
executor: E,
@@ -16,36 +20,38 @@ pub async fn update_db_message_replying_to_id<'e, E>(
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
- let result = sqlx::query!(
+ let found = sqlx::query_scalar!(
r#"
- UPDATE email_messages
- SET
- replying_to_id = $1,
- updated_at = NOW()
- WHERE
- id = $2
- AND link_id = $3
- RETURNING id
+ WITH updated AS (
+ UPDATE email_messages
+ SET
+ replying_to_id = $1,
+ updated_at = NOW()
+ WHERE
+ id = $2
+ AND link_id = $3
+ AND replying_to_id IS DISTINCT FROM $1
+ )
+ SELECT EXISTS (
+ SELECT 1 FROM email_messages WHERE id = $2 AND link_id = $3
+ ) AS "found!"
"#,
replying_to_id,
message_id,
link_id
)
- .fetch_optional(executor)
+ .fetch_one(executor)
.await?;
- // Return true if a message was updated, false otherwise
- let updated = result.is_some();
-
- if !updated {
+ if !found {
tracing::warn!(
message_id = %message_id,
link_id = %link_id,
- "No message was updated - message may not exist or doesn't belong to the link"
+ "Message not found - it may not exist or doesn't belong to the link"
);
}
- Ok(updated)
+ Ok(found)
}
/// Updates the replying_to_id for multiple messages belonging to a single link (typically part
@@ -65,34 +71,40 @@ where
let (message_ids, replying_to_ids): (Vec<Uuid>, Vec<Uuid>) = updates.iter().cloned().unzip();
- let result = sqlx::query!(
+ // Rows that already hold the value are left alone; the count covers every matching message
+ // so the warning below only fires for messages that are missing.
+ let matched = sqlx::query_scalar!(
r#"
- UPDATE email_messages
- SET
- replying_to_id = update_data.new_replying_to_id,
- updated_at = NOW()
- FROM
- UNNEST($1::uuid[], $2::uuid[])
- AS update_data(message_id, new_replying_to_id)
- WHERE
- email_messages.id = update_data.message_id
- AND email_messages.link_id = $3
+ WITH updated AS (
+ UPDATE email_messages
+ SET
+ replying_to_id = update_data.new_replying_to_id,
+ updated_at = NOW()
+ FROM
+ UNNEST($1::uuid[], $2::uuid[])
+ AS update_data(message_id, new_replying_to_id)
+ WHERE
+ email_messages.id = update_data.message_id
+ AND email_messages.link_id = $3
+ AND email_messages.replying_to_id IS DISTINCT FROM update_data.new_replying_to_id
+ )
+ SELECT COUNT(*) AS "matched!"
+ FROM email_messages
+ WHERE id = ANY($1) AND link_id = $3
"#,
&message_ids,
&replying_to_ids,
link_id
)
- .execute(executor)
+ .fetch_one(executor)
.await?;
- let rows_affected = result.rows_affected();
-
- if rows_affected < updates.len() as u64 {
+ if matched < updates.len() as i64 {
tracing::warn!(
link_id = %link_id,
- expected_updates = updates.len(),
- actual_updates = rows_affected,
- "Fewer messages were updated than expected. Some messages may not exist or do not belong to the link."
+ expected_messages = updates.len(),
+ found_messages = matched,
+ "Fewer messages found than expected. Some messages may not exist or do not belong to the link."
);
}
diff --git a/crates/email_db_client/src/messages/replying_to_id/test.rs b/crates/email_db_client/src/messages/replying_to_id/test.rs
new file mode 100644
index 0000000000..fb127c207d
--- /dev/null
+++ b/crates/email_db_client/src/messages/replying_to_id/test.rs
@@ -0,0 +1,88 @@
+use super::*;
+use macro_db_migrator::MACRO_DB_MIGRATIONS;
+use sqlx::{PgPool, Pool, Postgres};
+
+const LINK_ID: &str = "00000000-0000-0000-0000-000000000001";
+const THREAD_ID: &str = "10000000-0000-0000-0000-000000000001";
+
+async fn insert_message(pool: &PgPool, replying_to_id: Option<Uuid>) -> anyhow::Result<Uuid> {
+ let id = Uuid::now_v7();
+ sqlx::query!(
+ r#"
+ INSERT INTO email_messages (id, link_id, thread_id, replying_to_id)
+ VALUES ($1, $2, $3, $4)
+ "#,
+ id,
+ Uuid::parse_str(LINK_ID)?,
+ Uuid::parse_str(THREAD_ID)?,
+ replying_to_id,
+ )
+ .execute(pool)
+ .await?;
+ Ok(id)
+}
+
+async fn row_version(pool: &PgPool, id: Uuid) -> anyhow::Result<String> {
+ Ok(sqlx::query_scalar!(
+ r#"SELECT xmin::text AS "xmin!" FROM email_messages WHERE id = $1"#,
+ id
+ )
+ .fetch_one(pool)
+ .await?)
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("links", "threads"))
+)]
+async fn unchanged_replying_to_id_is_found_without_a_rewrite(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let link_id = Uuid::parse_str(LINK_ID)?;
+ let parent = insert_message(&pool, None).await?;
+ let reply = insert_message(&pool, Some(parent)).await?;
+ let before = row_version(&pool, reply).await?;
+
+ let found = update_db_message_replying_to_id(&pool, link_id, reply, Some(parent)).await?;
+
+ assert!(found);
+ assert_eq!(row_version(&pool, reply).await?, before);
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("links", "threads"))
+)]
+async fn changed_replying_to_id_is_written(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ let link_id = Uuid::parse_str(LINK_ID)?;
+ let parent = insert_message(&pool, None).await?;
+ let reply = insert_message(&pool, None).await?;
+
+ let found = update_db_message_replying_to_id(&pool, link_id, reply, Some(parent)).await?;
+
+ assert!(found);
+ let stored = sqlx::query_scalar!(
+ r#"SELECT replying_to_id FROM email_messages WHERE id = $1"#,
+ reply
+ )
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(stored, Some(parent));
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("links", "threads"))
+)]
+async fn missing_message_is_not_found(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ let link_id = Uuid::parse_str(LINK_ID)?;
+ let parent = insert_message(&pool, None).await?;
+
+ let found =
+ update_db_message_replying_to_id(&pool, link_id, Uuid::now_v7(), Some(parent)).await?;
+
+ assert!(!found);
+ Ok(())
+}
diff --git a/crates/email_db_client/src/threads/insert/test.rs b/crates/email_db_client/src/threads/insert/test.rs
index 7932bf3b43..2746eaadd6 100644
--- a/crates/email_db_client/src/threads/insert/test.rs
+++ b/crates/email_db_client/src/threads/insert/test.rs
@@ -148,6 +148,84 @@ async fn blank_insert_creates_new_thread(pool: Pool<Postgres>) -> anyhow::Result
Ok(())
}
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("links"))
+)]
+async fn resyncing_an_unchanged_thread_rewrites_no_messages(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ use crate::messages::insert::test::{PARENT_GLOBAL_ID, SyncedMessage, row_versions};
+
+ let link_id = Uuid::parse_str("00000000-0000-0000-0000-000000000001")?;
+ let parent = SyncedMessage {
+ provider_id: "thread-parent",
+ global_id: PARENT_GLOBAL_ID,
+ in_reply_to: None,
+ to: &["[email protected]"],
+ cc: &[],
+ labels: &[],
+ body: "parent body",
+ };
+ let reply = SyncedMessage {
+ provider_id: "thread-reply",
+ global_id: "<[email protected]>",
+ in_reply_to: Some(PARENT_GLOBAL_ID),
+ to: &["[email protected]"],
+ cc: &["[email protected]"],
+ labels: &[],
+ body: "reply body",
+ };
+ let synced_thread = || {
+ let thread_db_id = Uuid::now_v7();
+ let ts =
+ chrono::DateTime::<Utc>::from_timestamp(1_790_000_000, 0).expect("valid timestamp");
+ thread::Thread {
+ db_id: thread_db_id,
+ provider_id: Some("resync-thread".to_string()),
+ link_id,
+ inbox_visible: true,
+ is_read: false,
+ latest_inbound_message_ts: Some(ts),
+ latest_outbound_message_ts: None,
+ latest_non_spam_message_ts: Some(ts),
+ created_at: ts,
+ updated_at: ts,
+ messages: vec![
+ parent.build(link_id, thread_db_id),
+ reply.build(link_id, thread_db_id),
+ ],
+ }
+ };
+
+ let first_sync = synced_thread();
+ let parent_id = first_sync.messages[0].db_id;
+ let reply_id = first_sync.messages[1].db_id;
+ let thread_id = insert_thread_and_messages(&pool, first_sync, link_id).await?;
+
+ let replying_to = sqlx::query_scalar!(
+ r#"SELECT replying_to_id FROM email_messages WHERE id = $1"#,
+ reply_id
+ )
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(replying_to, Some(parent_id));
+ let parent_before = row_versions(&pool, parent_id).await?;
+ let reply_before = row_versions(&pool, reply_id).await?;
+
+ let resynced_thread_id = insert_thread_and_messages(&pool, synced_thread(), link_id).await?;
+
+ assert_eq!(resynced_thread_id, thread_id);
+ assert_eq!(row_versions(&pool, parent_id).await?, parent_before);
+ assert_eq!(
+ row_versions(&pool, reply_id).await?,
+ reply_before,
+ "re-syncing an unchanged thread must not rewrite its messages"
+ );
+
+ Ok(())
+}
+
/// The live inbox-sync path funnels through here, so it needs the same
/// protection from unstorable provider values as the backfill path.
#[sqlx::test(