Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(email): purge deleted mailboxes in bounded batches (#7914)
teo3eaa6a86b90e
diff --git a/.sqlx/query-0fb4770d4efebb0884648f05d96d18b872a3f2d9bb9282605d1a42232ad66f25.json b/.sqlx/query-0fb4770d4efebb0884648f05d96d18b872a3f2d9bb9282605d1a42232ad66f25.jsonnew file mode 100644index 0000000000..3095609134--- /dev/null+++ b/.sqlx/query-0fb4770d4efebb0884648f05d96d18b872a3f2d9bb9282605d1a42232ad66f25.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "DELETE FROM email_message_labels WHERE message_id = ANY($1)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "UuidArray"+ ]+ },+ "nullable": []+ },+ "hash": "0fb4770d4efebb0884648f05d96d18b872a3f2d9bb9282605d1a42232ad66f25"+}diff --git a/.sqlx/query-1737b467f32485d1c1bbb1bc9e4bec8d2e8d0f4df724c29e243281f376d40c3d.json b/.sqlx/query-1737b467f32485d1c1bbb1bc9e4bec8d2e8d0f4df724c29e243281f376d40c3d.jsonnew file mode 100644index 0000000000..598eb43605--- /dev/null+++ b/.sqlx/query-1737b467f32485d1c1bbb1bc9e4bec8d2e8d0f4df724c29e243281f376d40c3d.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "DELETE FROM email_threads WHERE link_id = $1 AND id = ANY($2)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "UuidArray"+ ]+ },+ "nullable": []+ },+ "hash": "1737b467f32485d1c1bbb1bc9e4bec8d2e8d0f4df724c29e243281f376d40c3d"+}diff --git a/.sqlx/query-18f484857d5b6d140bfe2e78fce5632c78c630fddf249f0d53261e601969889b.json b/.sqlx/query-18f484857d5b6d140bfe2e78fce5632c78c630fddf249f0d53261e601969889b.jsondeleted file mode 100644index e742261f23..0000000000--- a/.sqlx/query-18f484857d5b6d140bfe2e78fce5632c78c630fddf249f0d53261e601969889b.json+++ /dev/null@@ -1,14 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n DELETE FROM email_links\n WHERE id = $1\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Uuid"- ]- },- "nullable": []- },- "hash": "18f484857d5b6d140bfe2e78fce5632c78c630fddf249f0d53261e601969889b"-}diff --git a/.sqlx/query-2c338c2ef57859ba1e52826a8f3a6f88eb6a033d8a654cae8d2d8c6171caa92f.json b/.sqlx/query-2c338c2ef57859ba1e52826a8f3a6f88eb6a033d8a654cae8d2d8c6171caa92f.jsonnew file mode 100644index 0000000000..646c87dd01--- /dev/null+++ b/.sqlx/query-2c338c2ef57859ba1e52826a8f3a6f88eb6a033d8a654cae8d2d8c6171caa92f.json@@ -0,0 +1,23 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT id FROM email_threads WHERE link_id = $1 LIMIT $2",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid",+ "Int8"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "2c338c2ef57859ba1e52826a8f3a6f88eb6a033d8a654cae8d2d8c6171caa92f"+}diff --git a/.sqlx/query-34c23bace03122ae7b210604dd7b780a517764af6add91391add20a506ae8df3.json b/.sqlx/query-34c23bace03122ae7b210604dd7b780a517764af6add91391add20a506ae8df3.jsonnew file mode 100644index 0000000000..0651955d9c--- /dev/null+++ b/.sqlx/query-34c23bace03122ae7b210604dd7b780a517764af6add91391add20a506ae8df3.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "DELETE FROM email_attachments WHERE message_id = ANY($1)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "UuidArray"+ ]+ },+ "nullable": []+ },+ "hash": "34c23bace03122ae7b210604dd7b780a517764af6add91391add20a506ae8df3"+}diff --git a/.sqlx/query-74e2214e68d34a0c65aab38926518e3c0b8905a79eeb38ed7f84258b00a1b991.json b/.sqlx/query-4ed916398df41da55ee3c7f8f3c2bef8873244b3ec8719db7c6f5ca6884497bf.jsonsimilarity index 83%rename from .sqlx/query-74e2214e68d34a0c65aab38926518e3c0b8905a79eeb38ed7f84258b00a1b991.jsonrename to .sqlx/query-4ed916398df41da55ee3c7f8f3c2bef8873244b3ec8719db7c6f5ca6884497bf.jsonindex e84c3d95cc..613b75651d 100644--- a/.sqlx/query-74e2214e68d34a0c65aab38926518e3c0b8905a79eeb38ed7f84258b00a1b991.json+++ b/.sqlx/query-4ed916398df41da55ee3c7f8f3c2bef8873244b3ec8719db7c6f5ca6884497bf.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active)\n VALUES ($1, $2, $3, $4, $5, $6)\n ON CONFLICT (fusionauth_user_id, email_address, provider)\n DO UPDATE SET\n is_sync_active = EXCLUDED.is_sync_active,\n needs_reauth = false,\n last_sync_error_at = NULL,\n updated_at = NOW()\n RETURNING id, is_primary\n ",+ "query": "\n INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active)\n VALUES ($1, $2, $3, $4, $5, $6)\n ON CONFLICT (fusionauth_user_id, email_address, provider)\n DO UPDATE SET\n is_sync_active = EXCLUDED.is_sync_active,\n needs_reauth = false,\n last_sync_error_at = NULL,\n updated_at = NOW()\n WHERE email_links.deleting_at IS NULL\n RETURNING id, is_primary\n ","describe": {"columns": [{@@ -38,5 +38,5 @@false]},- "hash": "74e2214e68d34a0c65aab38926518e3c0b8905a79eeb38ed7f84258b00a1b991"+ "hash": "4ed916398df41da55ee3c7f8f3c2bef8873244b3ec8719db7c6f5ca6884497bf"}diff --git a/.sqlx/query-5ac51156caa0acb7799e831ee5bd997fba3d4931b47d83a3568fc2ae6cb6c72f.json b/.sqlx/query-5ac51156caa0acb7799e831ee5bd997fba3d4931b47d83a3568fc2ae6cb6c72f.jsonnew file mode 100644index 0000000000..d703499c9c--- /dev/null+++ b/.sqlx/query-5ac51156caa0acb7799e831ee5bd997fba3d4931b47d83a3568fc2ae6cb6c72f.json@@ -0,0 +1,30 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT id, email_address FROM email_contacts\n WHERE link_id = $1 AND email_address >= $2\n ORDER BY email_address\n LIMIT $3\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "email_address",+ "type_info": "Varchar"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid",+ "Text",+ "Int8"+ ]+ },+ "nullable": [+ false,+ false+ ]+ },+ "hash": "5ac51156caa0acb7799e831ee5bd997fba3d4931b47d83a3568fc2ae6cb6c72f"+}diff --git a/.sqlx/query-5daaed9025e5104c8d6888ce6892f5b170025d006a5d2c965b73760825a9d7cf.json b/.sqlx/query-5daaed9025e5104c8d6888ce6892f5b170025d006a5d2c965b73760825a9d7cf.jsonnew file mode 100644index 0000000000..fc442fa865--- /dev/null+++ b/.sqlx/query-5daaed9025e5104c8d6888ce6892f5b170025d006a5d2c965b73760825a9d7cf.json@@ -0,0 +1,28 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT is_sync_active, deleting_at FROM email_links WHERE id = $1",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "is_sync_active",+ "type_info": "Bool"+ },+ {+ "ordinal": 1,+ "name": "deleting_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ false,+ true+ ]+ },+ "hash": "5daaed9025e5104c8d6888ce6892f5b170025d006a5d2c965b73760825a9d7cf"+}diff --git a/.sqlx/query-6292ff5634441f63f6211453d0243ace417ab20d63c98c2c2dc1f24c4ae44c25.json b/.sqlx/query-6292ff5634441f63f6211453d0243ace417ab20d63c98c2c2dc1f24c4ae44c25.jsonnew file mode 100644index 0000000000..2157be6083--- /dev/null+++ b/.sqlx/query-6292ff5634441f63f6211453d0243ace417ab20d63c98c2c2dc1f24c4ae44c25.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE email_attachments_sfs\n SET attachment_id = NULL\n WHERE attachment_id IN (SELECT id FROM email_attachments WHERE message_id = ANY($1))\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "UuidArray"+ ]+ },+ "nullable": []+ },+ "hash": "6292ff5634441f63f6211453d0243ace417ab20d63c98c2c2dc1f24c4ae44c25"+}diff --git a/.sqlx/query-642ef618920627da606566be75196fde0dcc1fbd79953eb9792a7e0de4af83f7.json b/.sqlx/query-642ef618920627da606566be75196fde0dcc1fbd79953eb9792a7e0de4af83f7.jsonnew file mode 100644index 0000000000..2da6c973ae--- /dev/null+++ b/.sqlx/query-642ef618920627da606566be75196fde0dcc1fbd79953eb9792a7e0de4af83f7.json@@ -0,0 +1,20 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT COUNT(*) AS \"count!\" FROM email_attachments_sfs WHERE attachment_id IS NULL",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "count!",+ "type_info": "Int8"+ }+ ],+ "parameters": {+ "Left": []+ },+ "nullable": [+ null+ ]+ },+ "hash": "642ef618920627da606566be75196fde0dcc1fbd79953eb9792a7e0de4af83f7"+}diff --git a/.sqlx/query-6755a6c598094aa312f912730751afa0e82b15ec2d74bca911e8e617a51c1534.json b/.sqlx/query-6755a6c598094aa312f912730751afa0e82b15ec2d74bca911e8e617a51c1534.jsonnew file mode 100644index 0000000000..4c3b8b1d7c--- /dev/null+++ b/.sqlx/query-6755a6c598094aa312f912730751afa0e82b15ec2d74bca911e8e617a51c1534.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE email_links\n SET deleting_at = COALESCE(deleting_at, NOW()),\n is_sync_active = false,\n updated_at = NOW()\n WHERE id = $1 AND (deleting_at IS NULL OR is_sync_active)\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "6755a6c598094aa312f912730751afa0e82b15ec2d74bca911e8e617a51c1534"+}diff --git a/.sqlx/query-9ffed25f4e006eb1bd06d834743632fc08829762b7105fdadd7268b32a11c19b.json b/.sqlx/query-9ffed25f4e006eb1bd06d834743632fc08829762b7105fdadd7268b32a11c19b.jsondeleted file mode 100644index 1b1b610823..0000000000--- a/.sqlx/query-9ffed25f4e006eb1bd06d834743632fc08829762b7105fdadd7268b32a11c19b.json+++ /dev/null@@ -1,15 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n UPDATE email_links\n SET is_sync_active = $2, updated_at = NOW()\n WHERE id = $1 AND is_sync_active IS DISTINCT FROM $2\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Uuid",- "Bool"- ]- },- "nullable": []- },- "hash": "9ffed25f4e006eb1bd06d834743632fc08829762b7105fdadd7268b32a11c19b"-}diff --git a/.sqlx/query-aa2d7beef1886fc3dd36f7af190686722b0a43bce9e9c47e1b8d96c7ecc22a66.json b/.sqlx/query-aa2d7beef1886fc3dd36f7af190686722b0a43bce9e9c47e1b8d96c7ecc22a66.jsonnew file mode 100644index 0000000000..164b0251e1--- /dev/null+++ b/.sqlx/query-aa2d7beef1886fc3dd36f7af190686722b0a43bce9e9c47e1b8d96c7ecc22a66.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "DELETE FROM email_contacts WHERE id = ANY($1)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "UuidArray"+ ]+ },+ "nullable": []+ },+ "hash": "aa2d7beef1886fc3dd36f7af190686722b0a43bce9e9c47e1b8d96c7ecc22a66"+}diff --git a/.sqlx/query-b030f985dbefdf6ad8028094394968842d9cfcc9401ac8f915564b531bef29c5.json b/.sqlx/query-b030f985dbefdf6ad8028094394968842d9cfcc9401ac8f915564b531bef29c5.jsonnew file mode 100644index 0000000000..37f06a2484--- /dev/null+++ b/.sqlx/query-b030f985dbefdf6ad8028094394968842d9cfcc9401ac8f915564b531bef29c5.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "DELETE FROM email_scheduled_messages WHERE message_id = ANY($1)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "UuidArray"+ ]+ },+ "nullable": []+ },+ "hash": "b030f985dbefdf6ad8028094394968842d9cfcc9401ac8f915564b531bef29c5"+}diff --git a/.sqlx/query-b122972e4f1bc3551f1a6e430087d03eae249966b4fb5727a12510d03ae4bbdf.json b/.sqlx/query-b122972e4f1bc3551f1a6e430087d03eae249966b4fb5727a12510d03ae4bbdf.jsonnew file mode 100644index 0000000000..b2c1da44bd--- /dev/null+++ b/.sqlx/query-b122972e4f1bc3551f1a6e430087d03eae249966b4fb5727a12510d03ae4bbdf.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "DELETE FROM email_messages WHERE id = ANY($1)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "UuidArray"+ ]+ },+ "nullable": []+ },+ "hash": "b122972e4f1bc3551f1a6e430087d03eae249966b4fb5727a12510d03ae4bbdf"+}diff --git a/.sqlx/query-bf5169741be9bd205bf19efbd3065fd19067315a06be6a199db041f748e41c5d.json b/.sqlx/query-bf5169741be9bd205bf19efbd3065fd19067315a06be6a199db041f748e41c5d.jsonnew file mode 100644index 0000000000..87923d15a2--- /dev/null+++ b/.sqlx/query-bf5169741be9bd205bf19efbd3065fd19067315a06be6a199db041f748e41c5d.json@@ -0,0 +1,100 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT\n (SELECT COUNT(*) FROM email_links WHERE id = $1) AS \"links!\",\n (SELECT COUNT(*) FROM email_settings WHERE link_id = $1) AS \"settings!\",\n (SELECT COUNT(*) FROM email_gmail_histories WHERE link_id = $1) AS \"gmail_histories!\",\n (SELECT COUNT(*) FROM email_sync_tokens WHERE link_id = $1) AS \"sync_tokens!\",\n (SELECT COUNT(*) FROM email_contacts WHERE link_id = $1) AS \"contacts!\",\n (SELECT COUNT(*) FROM email_labels WHERE link_id = $1) AS \"labels!\",\n (SELECT COUNT(*) FROM email_threads WHERE link_id = $1) AS \"threads!\",\n (SELECT COUNT(*) FROM email_messages WHERE link_id = $1) AS \"messages!\",\n (SELECT COUNT(*) FROM email_message_labels ml\n JOIN email_labels l ON l.id = ml.label_id WHERE l.link_id = $1) AS \"message_labels!\",\n (SELECT COUNT(*) FROM email_message_recipients r\n JOIN email_contacts c ON c.id = r.contact_id WHERE c.link_id = $1) AS \"recipients!\",\n (SELECT COUNT(*) FROM email_attachments a\n JOIN email_messages m ON m.id = a.message_id WHERE m.link_id = $1) AS \"attachments!\",\n (SELECT COUNT(*) FROM email_attachments_sfs s\n JOIN email_attachments a ON a.id = s.attachment_id\n JOIN email_messages m ON m.id = a.message_id WHERE m.link_id = $1) AS \"attached_sfs!\",\n (SELECT COUNT(*) FROM email_user_history WHERE link_id = $1) AS \"user_history!\",\n (SELECT COUNT(*) FROM email_scheduled_messages WHERE link_id = $1) AS \"scheduled!\"\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "links!",+ "type_info": "Int8"+ },+ {+ "ordinal": 1,+ "name": "settings!",+ "type_info": "Int8"+ },+ {+ "ordinal": 2,+ "name": "gmail_histories!",+ "type_info": "Int8"+ },+ {+ "ordinal": 3,+ "name": "sync_tokens!",+ "type_info": "Int8"+ },+ {+ "ordinal": 4,+ "name": "contacts!",+ "type_info": "Int8"+ },+ {+ "ordinal": 5,+ "name": "labels!",+ "type_info": "Int8"+ },+ {+ "ordinal": 6,+ "name": "threads!",+ "type_info": "Int8"+ },+ {+ "ordinal": 7,+ "name": "messages!",+ "type_info": "Int8"+ },+ {+ "ordinal": 8,+ "name": "message_labels!",+ "type_info": "Int8"+ },+ {+ "ordinal": 9,+ "name": "recipients!",+ "type_info": "Int8"+ },+ {+ "ordinal": 10,+ "name": "attachments!",+ "type_info": "Int8"+ },+ {+ "ordinal": 11,+ "name": "attached_sfs!",+ "type_info": "Int8"+ },+ {+ "ordinal": 12,+ "name": "user_history!",+ "type_info": "Int8"+ },+ {+ "ordinal": 13,+ "name": "scheduled!",+ "type_info": "Int8"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ null,+ null,+ null,+ null,+ null,+ null,+ null,+ null,+ null,+ null,+ null,+ null,+ null,+ null+ ]+ },+ "hash": "bf5169741be9bd205bf19efbd3065fd19067315a06be6a199db041f748e41c5d"+}diff --git a/.sqlx/query-c38ec3118465613ec5abbcbad8bb372a53489255d52806c857dd0a707f057749.json b/.sqlx/query-c38ec3118465613ec5abbcbad8bb372a53489255d52806c857dd0a707f057749.jsonnew file mode 100644index 0000000000..ee65b5085f--- /dev/null+++ b/.sqlx/query-c38ec3118465613ec5abbcbad8bb372a53489255d52806c857dd0a707f057749.json@@ -0,0 +1,30 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT id, thread_id FROM email_messages\n WHERE link_id = $1 AND thread_id >= $2\n ORDER BY thread_id\n LIMIT $3\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "thread_id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid",+ "Uuid",+ "Int8"+ ]+ },+ "nullable": [+ false,+ false+ ]+ },+ "hash": "c38ec3118465613ec5abbcbad8bb372a53489255d52806c857dd0a707f057749"+}diff --git a/.sqlx/query-d28d9b6369040ce97cb2f8a6111eb4c90e27c898275a2407bdc86096067f7670.json b/.sqlx/query-d28d9b6369040ce97cb2f8a6111eb4c90e27c898275a2407bdc86096067f7670.jsonnew file mode 100644index 0000000000..1ccdcb4ab7--- /dev/null+++ b/.sqlx/query-d28d9b6369040ce97cb2f8a6111eb4c90e27c898275a2407bdc86096067f7670.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE email_links\n SET is_sync_active = $2, updated_at = NOW()\n WHERE id = $1\n AND is_sync_active IS DISTINCT FROM $2\n AND (NOT $2 OR deleting_at IS NULL)\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Bool"+ ]+ },+ "nullable": []+ },+ "hash": "d28d9b6369040ce97cb2f8a6111eb4c90e27c898275a2407bdc86096067f7670"+}diff --git a/.sqlx/query-d350f57aa94a8db9b4db04d51cd4e350e919ee5484e2f6a7270366e79a336afc.json b/.sqlx/query-d350f57aa94a8db9b4db04d51cd4e350e919ee5484e2f6a7270366e79a336afc.jsonnew file mode 100644index 0000000000..16258dccbc--- /dev/null+++ b/.sqlx/query-d350f57aa94a8db9b4db04d51cd4e350e919ee5484e2f6a7270366e79a336afc.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT pg_advisory_xact_lock(hashtextextended('email_link_purge:' || $1::uuid::text, 0))",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "pg_advisory_xact_lock",+ "type_info": "Void"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ null+ ]+ },+ "hash": "d350f57aa94a8db9b4db04d51cd4e350e919ee5484e2f6a7270366e79a336afc"+}diff --git a/.sqlx/query-d5425557288adba8f620aa402056f9642ca989044c436936e6f75d16231b97d2.json b/.sqlx/query-d5425557288adba8f620aa402056f9642ca989044c436936e6f75d16231b97d2.jsonnew file mode 100644index 0000000000..ce30b2cf1f--- /dev/null+++ b/.sqlx/query-d5425557288adba8f620aa402056f9642ca989044c436936e6f75d16231b97d2.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "DELETE FROM email_user_history WHERE link_id = $1 AND thread_id = ANY($2)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "UuidArray"+ ]+ },+ "nullable": []+ },+ "hash": "d5425557288adba8f620aa402056f9642ca989044c436936e6f75d16231b97d2"+}diff --git a/.sqlx/query-e7f15ca0310d43cb4d85b86c938d19c10e04d8f8624ab1f7651f448ea2e6fa81.json b/.sqlx/query-e7f15ca0310d43cb4d85b86c938d19c10e04d8f8624ab1f7651f448ea2e6fa81.jsonnew file mode 100644index 0000000000..ac22d419a3--- /dev/null+++ b/.sqlx/query-e7f15ca0310d43cb4d85b86c938d19c10e04d8f8624ab1f7651f448ea2e6fa81.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "DELETE FROM email_message_recipients WHERE message_id = ANY($1)",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "UuidArray"+ ]+ },+ "nullable": []+ },+ "hash": "e7f15ca0310d43cb4d85b86c938d19c10e04d8f8624ab1f7651f448ea2e6fa81"+}diff --git a/.sqlx/query-f5cb86aaf6e3eda9e35c3733206072b3dd1dd26718305bec2e6888634458f206.json b/.sqlx/query-f5cb86aaf6e3eda9e35c3733206072b3dd1dd26718305bec2e6888634458f206.jsonnew file mode 100644index 0000000000..6ad6f6f2fb--- /dev/null+++ b/.sqlx/query-f5cb86aaf6e3eda9e35c3733206072b3dd1dd26718305bec2e6888634458f206.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "DELETE FROM email_links WHERE id = $1 AND deleting_at IS NOT NULL",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "f5cb86aaf6e3eda9e35c3733206072b3dd1dd26718305bec2e6888634458f206"+}diff --git a/crates/email_db_client/fixtures/purge_link.sql b/crates/email_db_client/fixtures/purge_link.sqlnew file mode 100644index 0000000000..945b5c5e89--- /dev/null+++ b/crates/email_db_client/fixtures/purge_link.sql@@ -0,0 +1,85 @@+-- SQL fixture for purge_link tests.+-- Two mailboxes with identical shapes: link a001 is purged, link b001 must be left intact.+-- Each has 4 contacts, 3 labels, 10 threads of 3 messages and 1 empty thread. Every message replies to the+-- previous one in its thread, carries 2 labels and 2 recipients; the first message of each+-- thread has an attachment stored in SFS; each thread has user history; one message is a+-- scheduled send.++CREATE TEMP TABLE purge_fixture_links (tag TEXT, id UUID);+INSERT INTO purge_fixture_links (tag, id)+VALUES ('a', '00000000-0000-0000-0000-00000000a001'),+ ('b', '00000000-0000-0000-0000-00000000b001');++INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active)+SELECT id, 'macro|purge_' || tag || '@example.com', id::text, 'purge_' || tag || '@example.com', 'GMAIL', true+FROM purge_fixture_links;++INSERT INTO email_settings (link_id)+SELECT id FROM purge_fixture_links;++INSERT INTO email_gmail_histories (link_id, history_id)+SELECT id, 'history-' || tag FROM purge_fixture_links;++INSERT INTO email_sync_tokens (link_id, contacts_sync_token)+SELECT id, 'sync-' || tag FROM purge_fixture_links;++INSERT INTO email_contacts (id, link_id, email_address)+SELECT md5(l.tag || '-contact-' || c)::uuid, l.id, 'contact' || c || '_' || l.tag || '@example.com'+FROM purge_fixture_links l, generate_series(1, 4) AS c;++INSERT INTO email_labels (id, link_id, provider_label_id, name)+SELECT md5(l.tag || '-label-' || n)::uuid, l.id, 'Label_' || n, 'Label ' || n+FROM purge_fixture_links l, generate_series(1, 3) AS n;++INSERT INTO email_threads (id, link_id, provider_id, inbox_visible)+SELECT md5(l.tag || '-thread-' || t)::uuid, l.id, l.tag || '-thread-' || t, true+FROM purge_fixture_links l, generate_series(1, 10) AS t;++-- A thread with no messages, which only the thread phase reaches.+INSERT INTO email_threads (id, link_id, provider_id)+SELECT md5(tag || '-thread-empty')::uuid, id, tag || '-thread-empty'+FROM purge_fixture_links;++INSERT INTO email_messages (id, thread_id, link_id, provider_id, from_contact_id, replying_to_id,+ internal_date_ts, body_text)+SELECT md5(l.tag || '-msg-' || t || '-' || n)::uuid,+ md5(l.tag || '-thread-' || t)::uuid,+ l.id,+ l.tag || '-msg-' || t || '-' || n,+ md5(l.tag || '-contact-' || (1 + n % 4))::uuid,+ CASE WHEN n > 1 THEN md5(l.tag || '-msg-' || t || '-' || (n - 1))::uuid END,+ TIMESTAMPTZ '2025-01-01 00:00:00+00' + make_interval(hours => t * 10 + n),+ repeat('body ', 200)+FROM purge_fixture_links l, generate_series(1, 10) AS t, generate_series(1, 3) AS n;++INSERT INTO email_message_labels (message_id, label_id)+SELECT m.id, md5(l.tag || '-label-' || n)::uuid+FROM purge_fixture_links l+JOIN email_messages m ON m.link_id = l.id,+generate_series(1, 2) AS n;++INSERT INTO email_message_recipients (message_id, contact_id, recipient_type)+SELECT m.id, md5(l.tag || '-contact-' || r.contact)::uuid, r.kind::email_recipient_type+FROM purge_fixture_links l+JOIN email_messages m ON m.link_id = l.id,+(VALUES (2, 'TO'), (3, 'CC')) AS r(contact, kind);++INSERT INTO email_attachments (id, message_id, provider_attachment_id, filename, mime_type, size_bytes)+SELECT md5(l.tag || '-attachment-' || t)::uuid, md5(l.tag || '-msg-' || t || '-1')::uuid,+ l.tag || '-attachment-' || t, 'file' || t || '.pdf', 'application/pdf', 1024+FROM purge_fixture_links l, generate_series(1, 10) AS t;++INSERT INTO email_attachments_sfs (id, attachment_id, sfs_id)+SELECT md5(l.tag || '-sfs-' || t)::uuid, md5(l.tag || '-attachment-' || t)::uuid,+ md5(l.tag || '-sfs-file-' || t)::uuid+FROM purge_fixture_links l, generate_series(1, 10) AS t;++INSERT INTO email_user_history (link_id, thread_id)+SELECT l.id, md5(l.tag || '-thread-' || t)::uuid+FROM purge_fixture_links l, generate_series(1, 10) AS t;++INSERT INTO email_scheduled_messages (link_id, message_id, send_time)+SELECT id, md5(tag || '-msg-1-3')::uuid, TIMESTAMPTZ '2025-02-01 00:00:00+00'+FROM purge_fixture_links;++DROP TABLE purge_fixture_links;diff --git a/crates/email_db_client/src/links/delete.rs b/crates/email_db_client/src/links/delete.rsindex 99f129e548..58286c3840 100644--- a/crates/email_db_client/src/links/delete.rs+++ b/crates/email_db_client/src/links/delete.rs@@ -1,20 +1,382 @@-use sqlx::PgPool;use sqlx::types::Uuid;+use sqlx::{PgPool, Postgres, Transaction};+use std::num::NonZeroU32;-/// Deletes a link by its ID.-/// Will cascade to delete threads, messages, attachments, and labels for the user.-/// Returns the number of rows affected (should be 1 if successful, 0 if the link didn't exist).+#[cfg(test)]+mod test;++/// How many rows each [`purge_link`] transaction removes. Every batch commits on its own,+/// so no single transaction holds locks, WAL or the xmin horizon for a whole mailbox.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub struct PurgeBatchSizes {+ /// Messages per transaction, deleted together with their labels, recipients,+ /// attachments and scheduled sends, and with the threads the batch finishes. Message+ /// bodies are most of a mailbox's bytes (TOAST), so this is the knob that bounds a+ /// transaction's WAL.+ pub messages: NonZeroU32,+ /// Threads per transaction for threads left once every message is gone.+ pub threads: NonZeroU32,+ /// Contacts per transaction.+ pub contacts: NonZeroU32,+}++impl Default for PurgeBatchSizes {+ fn default() -> Self {+ Self {+ messages: NonZeroU32::new(1_000).expect("non-zero"),+ threads: NonZeroU32::new(2_000).expect("non-zero"),+ contacts: NonZeroU32::new(5_000).expect("non-zero"),+ }+ }+}++/// Rows removed by one [`purge_link`] call. A rerun reports only what it removed itself.+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]+pub struct PurgeSummary {+ /// Messages deleted.+ pub messages: u64,+ /// Threads deleted.+ pub threads: u64,+ /// Contacts deleted.+ pub contacts: u64,+ /// Whether this call deleted the `email_links` row; `false` when it was already gone.+ pub link_deleted: bool,+}++/// Marks a link as being deleted and stops its sync. A marked link never resumes syncing:+/// re-enabling sync and reconnecting the same mailbox both refuse it until the purge has+/// removed the row.+///+/// Idempotent and cheap to repeat: an already-marked link keeps its original `deleting_at`+/// and the row is not rewritten. A no-op for a missing link.#[tracing::instrument(skip(pool), err)]-pub async fn delete_link_by_id(pool: &PgPool, link_id: Uuid) -> anyhow::Result<u64> {- let result = sqlx::query!(+pub async fn mark_link_deleting(pool: &PgPool, link_id: Uuid) -> anyhow::Result<()> {+ sqlx::query!(r#"- DELETE FROM email_links- WHERE id = $1+ UPDATE email_links+ SET deleting_at = COALESCE(deleting_at, NOW()),+ is_sync_active = false,+ updated_at = NOW()+ WHERE id = $1 AND (deleting_at IS NULL OR is_sync_active)"#,link_id).execute(pool).await?;- Ok(result.rows_affected())+ Ok(())+}++/// Deletes a link and all of its email data incrementally: messages (deepest children+/// first) with the threads they belong to, then any remaining threads, then contacts,+/// each in bounded short transactions, and finally the `email_links` row, whose cascade+/// only has small per-link tables left to clear.+///+/// Marks the link via [`mark_link_deleting`] first, so it can be called on its own. Safe to+/// rerun after a crash (it resumes from whatever is left) and to run concurrently for the+/// same link: batches for one link are serialized by an advisory lock.+#[tracing::instrument(skip(pool), err)]+pub async fn purge_link(+ pool: &PgPool,+ link_id: Uuid,+ batch_sizes: PurgeBatchSizes,+) -> anyhow::Result<PurgeSummary> {+ mark_link_deleting(pool, link_id).await?;++ let mut summary = PurgeSummary::default();++ let mut thread_cursor = Uuid::nil();+ loop {+ let batch = purge_message_batch(pool, link_id, thread_cursor, batch_sizes.messages).await?;+ summary.messages += batch.messages;+ summary.threads += batch.threads;+ let Some(next_cursor) = batch.next_cursor else {+ break;+ };+ thread_cursor = next_cursor;+ }++ loop {+ let batch = purge_thread_batch(pool, link_id, batch_sizes.threads).await?;+ summary.threads += batch.deleted;+ if !batch.full {+ break;+ }+ }++ let mut contact_cursor = String::new();+ loop {+ let batch =+ purge_contact_batch(pool, link_id, &contact_cursor, batch_sizes.contacts).await?;+ summary.contacts += batch.deleted;+ let Some(next_cursor) = batch.next_cursor else {+ break;+ };+ contact_cursor = next_cursor;+ }++ summary.link_deleted = delete_marked_link(pool, link_id).await?;++ tracing::info!(?summary, "Purged email link");+ Ok(summary)+}++/// Opens a purge batch transaction holding the link's purge lock until commit, so a+/// redelivered delete running alongside the original takes turns batch by batch instead+/// of deadlocking on the rows both are deleting.+async fn begin_purge_batch(+ pool: &PgPool,+ link_id: Uuid,+) -> anyhow::Result<Transaction<'static, Postgres>> {+ let mut tx = pool.begin().await?;+ sqlx::query!(+ "SELECT pg_advisory_xact_lock(hashtextextended('email_link_purge:' || $1::uuid::text, 0))",+ link_id+ )+ .execute(&mut *tx)+ .await?;+ Ok(tx)+}++/// Outcome of one [`purge_message_batch`].+struct MessageBatch {+ messages: u64,+ threads: u64,+ /// Where the next batch starts; `None` once the batch came back short.+ next_cursor: Option<Uuid>,+}++/// Deletes up to `limit` of the link's messages with their children, deepest first,+/// walking messages in thread order from `thread_cursor`. Each batch starts at the last+/// thread the previous one touched, so it never rescans index entries of rows earlier+/// batches deleted (which stay in the index until vacuum, and stay unkillable while any+/// older snapshot is open). Threads whose messages the batch exhausted are deleted with it.+async fn purge_message_batch(+ pool: &PgPool,+ link_id: Uuid,+ thread_cursor: Uuid,+ limit: NonZeroU32,+) -> anyhow::Result<MessageBatch> {+ let mut tx = begin_purge_batch(pool, link_id).await?;++ let rows = sqlx::query!(+ r#"+ SELECT id, thread_id FROM email_messages+ WHERE link_id = $1 AND thread_id >= $2+ ORDER BY thread_id+ LIMIT $3+ "#,+ link_id,+ thread_cursor,+ i64::from(limit.get())+ )+ .fetch_all(&mut *tx)+ .await?;++ let Some(last_thread_id) = rows.last().map(|row| row.thread_id) else {+ tx.commit().await?;+ return Ok(MessageBatch {+ messages: 0,+ threads: 0,+ next_cursor: None,+ });+ };+ let full = rows.len() as u64 == u64::from(limit.get());++ let message_ids: Vec<Uuid> = rows.iter().map(|row| row.id).collect();+ // Rows come back in thread order, so every thread before the last one is exhausted.+ // The last thread may continue past this batch unless the batch came back short.+ let mut finished_thread_ids: Vec<Uuid> = rows+ .iter()+ .map(|row| row.thread_id)+ .filter(|thread_id| !full || *thread_id != last_thread_id)+ .collect();+ finished_thread_ids.dedup();++ sqlx::query!(+ "DELETE FROM email_message_labels WHERE message_id = ANY($1)",+ &message_ids+ )+ .execute(&mut *tx)+ .await?;++ sqlx::query!(+ "DELETE FROM email_message_recipients WHERE message_id = ANY($1)",+ &message_ids+ )+ .execute(&mut *tx)+ .await?;++ // Detached rather than deleted: email_sfs_delete_handler removes the stored file for+ // every row whose attachment_id is NULL, then deletes the row itself.+ sqlx::query!(+ r#"+ UPDATE email_attachments_sfs+ SET attachment_id = NULL+ WHERE attachment_id IN (SELECT id FROM email_attachments WHERE message_id = ANY($1))+ "#,+ &message_ids+ )+ .execute(&mut *tx)+ .await?;++ sqlx::query!(+ "DELETE FROM email_attachments WHERE message_id = ANY($1)",+ &message_ids+ )+ .execute(&mut *tx)+ .await?;++ sqlx::query!(+ "DELETE FROM email_scheduled_messages WHERE message_id = ANY($1)",+ &message_ids+ )+ .execute(&mut *tx)+ .await?;++ let messages = sqlx::query!(+ "DELETE FROM email_messages WHERE id = ANY($1)",+ &message_ids+ )+ .execute(&mut *tx)+ .await?+ .rows_affected();++ let threads = delete_threads(&mut tx, link_id, &finished_thread_ids).await?;++ tx.commit().await?;+ Ok(MessageBatch {+ messages,+ threads,+ next_cursor: full.then_some(last_thread_id),+ })+}++/// Deletes the given threads of the link with their user history. Returns the number of+/// threads deleted.+async fn delete_threads(+ tx: &mut Transaction<'static, Postgres>,+ link_id: Uuid,+ thread_ids: &[Uuid],+) -> anyhow::Result<u64> {+ if thread_ids.is_empty() {+ return Ok(0);+ }++ sqlx::query!(+ "DELETE FROM email_user_history WHERE link_id = $1 AND thread_id = ANY($2)",+ link_id,+ thread_ids+ )+ .execute(&mut **tx)+ .await?;++ Ok(sqlx::query!(+ "DELETE FROM email_threads WHERE link_id = $1 AND id = ANY($2)",+ link_id,+ thread_ids+ )+ .execute(&mut **tx)+ .await?+ .rows_affected())+}++/// Outcome of one [`purge_thread_batch`].+struct ThreadBatch {+ deleted: u64,+ /// Whether the batch came back full, so more threads may remain.+ full: bool,+}++/// Deletes up to `limit` of the link's remaining threads. The message phase already+/// deleted every thread that had messages, so this only meets threads that never had any+/// and the last ones a straggling sync wrote mid-purge.+async fn purge_thread_batch(+ pool: &PgPool,+ link_id: Uuid,+ limit: NonZeroU32,+) -> anyhow::Result<ThreadBatch> {+ let mut tx = begin_purge_batch(pool, link_id).await?;++ let thread_ids = sqlx::query_scalar!(+ "SELECT id FROM email_threads WHERE link_id = $1 LIMIT $2",+ link_id,+ i64::from(limit.get())+ )+ .fetch_all(&mut *tx)+ .await?;++ let full = thread_ids.len() as u64 == u64::from(limit.get());+ let deleted = delete_threads(&mut tx, link_id, &thread_ids).await?;++ tx.commit().await?;+ Ok(ThreadBatch { deleted, full })+}++/// Outcome of one [`purge_contact_batch`].+struct ContactBatch {+ deleted: u64,+ /// Where the next batch starts; `None` once the batch came back short.+ next_cursor: Option<String>,+}++/// Deletes up to `limit` of the link's contacts, walking them in email address order from+/// `email_cursor` so no batch rescans rows an earlier one deleted.+async fn purge_contact_batch(+ pool: &PgPool,+ link_id: Uuid,+ email_cursor: &str,+ limit: NonZeroU32,+) -> anyhow::Result<ContactBatch> {+ let mut tx = begin_purge_batch(pool, link_id).await?;++ let rows = sqlx::query!(+ r#"+ SELECT id, email_address FROM email_contacts+ WHERE link_id = $1 AND email_address >= $2+ ORDER BY email_address+ LIMIT $3+ "#,+ link_id,+ email_cursor,+ i64::from(limit.get())+ )+ .fetch_all(&mut *tx)+ .await?;++ let full = rows.len() as u64 == u64::from(limit.get());+ let contact_ids: Vec<Uuid> = rows.iter().map(|row| row.id).collect();++ let deleted = sqlx::query!(+ "DELETE FROM email_contacts WHERE id = ANY($1)",+ &contact_ids+ )+ .execute(&mut *tx)+ .await?+ .rows_affected();++ tx.commit().await?;+ Ok(ContactBatch {+ deleted,+ next_cursor: full+ .then(|| rows.last().map(|row| row.email_address.clone()))+ .flatten(),+ })+}++/// Deletes the link row once its bulk data is gone. Only a link marked by+/// [`mark_link_deleting`] is deleted. Returns whether a row was deleted.+async fn delete_marked_link(pool: &PgPool, link_id: Uuid) -> anyhow::Result<bool> {+ let mut tx = begin_purge_batch(pool, link_id).await?;++ let deleted = sqlx::query!(+ "DELETE FROM email_links WHERE id = $1 AND deleting_at IS NOT NULL",+ link_id+ )+ .execute(&mut *tx)+ .await?+ .rows_affected();++ tx.commit().await?;+ Ok(deleted > 0)}diff --git a/crates/email_db_client/src/links/delete/test.rs b/crates/email_db_client/src/links/delete/test.rsnew file mode 100644index 0000000000..7e3ddaefd5--- /dev/null+++ b/crates/email_db_client/src/links/delete/test.rs@@ -0,0 +1,328 @@+use super::*;+use crate::links::insert::upsert_link;+use crate::links::update::update_link_sync_status;+use macro_db_migrator::MACRO_DB_MIGRATIONS;+use macro_user_id::email::EmailStr;+use macro_user_id::user_id::MacroUserIdStr;+use models_email::email::service::link::{Link, UserProvider};+use sqlx::{Pool, Postgres};++/// The fixture link that tests purge.+const PURGED: Uuid = Uuid::from_u128(0xa001);+/// The fixture link that must survive every purge of [`PURGED`].+const KEPT: Uuid = Uuid::from_u128(0xb001);++/// Rows the `purge_link` fixture stores for one link.+#[derive(Debug, PartialEq, Eq)]+struct LinkRows {+ links: i64,+ settings: i64,+ gmail_histories: i64,+ sync_tokens: i64,+ contacts: i64,+ labels: i64,+ threads: i64,+ messages: i64,+ message_labels: i64,+ recipients: i64,+ attachments: i64,+ attached_sfs: i64,+ user_history: i64,+ scheduled: i64,+}++impl LinkRows {+ const EMPTY: Self = Self {+ links: 0,+ settings: 0,+ gmail_histories: 0,+ sync_tokens: 0,+ contacts: 0,+ labels: 0,+ threads: 0,+ messages: 0,+ message_labels: 0,+ recipients: 0,+ attachments: 0,+ attached_sfs: 0,+ user_history: 0,+ scheduled: 0,+ };++ const FULL: Self = Self {+ links: 1,+ settings: 1,+ gmail_histories: 1,+ sync_tokens: 1,+ contacts: 4,+ labels: 3,+ threads: 11,+ messages: 30,+ message_labels: 60,+ recipients: 60,+ attachments: 10,+ attached_sfs: 10,+ user_history: 10,+ scheduled: 1,+ };+}++async fn link_rows(pool: &Pool<Postgres>, link_id: Uuid) -> anyhow::Result<LinkRows> {+ Ok(sqlx::query_as!(+ LinkRows,+ r#"+ SELECT+ (SELECT COUNT(*) FROM email_links WHERE id = $1) AS "links!",+ (SELECT COUNT(*) FROM email_settings WHERE link_id = $1) AS "settings!",+ (SELECT COUNT(*) FROM email_gmail_histories WHERE link_id = $1) AS "gmail_histories!",+ (SELECT COUNT(*) FROM email_sync_tokens WHERE link_id = $1) AS "sync_tokens!",+ (SELECT COUNT(*) FROM email_contacts WHERE link_id = $1) AS "contacts!",+ (SELECT COUNT(*) FROM email_labels WHERE link_id = $1) AS "labels!",+ (SELECT COUNT(*) FROM email_threads WHERE link_id = $1) AS "threads!",+ (SELECT COUNT(*) FROM email_messages WHERE link_id = $1) AS "messages!",+ (SELECT COUNT(*) FROM email_message_labels ml+ JOIN email_labels l ON l.id = ml.label_id WHERE l.link_id = $1) AS "message_labels!",+ (SELECT COUNT(*) FROM email_message_recipients r+ JOIN email_contacts c ON c.id = r.contact_id WHERE c.link_id = $1) AS "recipients!",+ (SELECT COUNT(*) FROM email_attachments a+ JOIN email_messages m ON m.id = a.message_id WHERE m.link_id = $1) AS "attachments!",+ (SELECT COUNT(*) FROM email_attachments_sfs s+ JOIN email_attachments a ON a.id = s.attachment_id+ JOIN email_messages m ON m.id = a.message_id WHERE m.link_id = $1) AS "attached_sfs!",+ (SELECT COUNT(*) FROM email_user_history WHERE link_id = $1) AS "user_history!",+ (SELECT COUNT(*) FROM email_scheduled_messages WHERE link_id = $1) AS "scheduled!"+ "#,+ link_id+ )+ .fetch_one(pool)+ .await?)+}++/// SFS rows left for `email_sfs_delete_handler` to clean up.+async fn detached_sfs_rows(pool: &Pool<Postgres>) -> anyhow::Result<i64> {+ Ok(sqlx::query_scalar!(+ r#"SELECT COUNT(*) AS "count!" FROM email_attachments_sfs WHERE attachment_id IS NULL"#+ )+ .fetch_one(pool)+ .await?)+}++struct DeletionState {+ is_sync_active: bool,+ deleting_at: Option<chrono::DateTime<chrono::Utc>>,+}++async fn deletion_state(pool: &Pool<Postgres>, link_id: Uuid) -> anyhow::Result<DeletionState> {+ Ok(sqlx::query_as!(+ DeletionState,+ "SELECT is_sync_active, deleting_at FROM email_links WHERE id = $1",+ link_id+ )+ .fetch_one(pool)+ .await?)+}++fn batch_sizes(messages: u32, threads: u32, contacts: u32) -> PurgeBatchSizes {+ PurgeBatchSizes {+ messages: NonZeroU32::new(messages).expect("non-zero"),+ threads: NonZeroU32::new(threads).expect("non-zero"),+ contacts: NonZeroU32::new(contacts).expect("non-zero"),+ }+}++/// Small enough that every phase of the fixture takes several batches, with a partial last one.+fn tiny_batches() -> PurgeBatchSizes {+ batch_sizes(4, 3, 3)+}++fn gmail_link(email: &str) -> anyhow::Result<Link> {+ Ok(Link {+ id: macro_uuid::generate_uuid_v7(),+ macro_id: MacroUserIdStr::try_from(format!("macro|{email}"))?,+ fusionauth_user_id: "33333333-3333-3333-3333-333333333333".to_string(),+ email_address: EmailStr::try_from(email.to_string())?,+ provider: UserProvider::Gmail,+ is_sync_active: true,+ is_primary: true,+ needs_reauth: false,+ last_sync_error_at: None,+ created_at: Default::default(),+ updated_at: Default::default(),+ })+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("purge_link"))+)]+async fn purge_removes_all_link_data_in_batches(pool: Pool<Postgres>) -> anyhow::Result<()> {+ assert_eq!(link_rows(&pool, PURGED).await?, LinkRows::FULL);++ let summary = purge_link(&pool, PURGED, tiny_batches()).await?;++ assert_eq!(+ summary,+ PurgeSummary {+ messages: 30,+ threads: 11,+ contacts: 4,+ link_deleted: true,+ }+ );+ assert_eq!(link_rows(&pool, PURGED).await?, LinkRows::EMPTY);+ assert_eq!(link_rows(&pool, KEPT).await?, LinkRows::FULL);+ // The purged link's stored files are handed to the SFS cleanup instead of leaking.+ assert_eq!(detached_sfs_rows(&pool).await?, 10);++ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("purge_link"))+)]+async fn purge_resumes_after_an_interrupted_run(pool: Pool<Postgres>) -> anyhow::Result<()> {+ // A worker that crashed partway through the message phase. Threads hold 3 messages, so+ // two batches of 7 finish 4 threads and stop partway into a fifth.+ mark_link_deleting(&pool, PURGED).await?;+ let limit = NonZeroU32::new(7).expect("non-zero");+ let first = purge_message_batch(&pool, PURGED, Uuid::nil(), limit).await?;+ assert_eq!((first.messages, first.threads), (7, 2));+ let cursor = first.next_cursor.expect("more messages remain");+ let second = purge_message_batch(&pool, PURGED, cursor, limit).await?;+ assert_eq!((second.messages, second.threads), (7, 2));++ let partial = link_rows(&pool, PURGED).await?;+ assert_eq!(partial.links, 1);+ assert_eq!(partial.messages, 16);+ assert_eq!(partial.threads, 7);+ let state = deletion_state(&pool, PURGED).await?;+ assert!(!state.is_sync_active);+ assert!(state.deleting_at.is_some());++ // The redelivered message resumes from what is left.+ let resumed = purge_link(&pool, PURGED, tiny_batches()).await?;+ assert_eq!(+ resumed,+ PurgeSummary {+ messages: 16,+ threads: 7,+ contacts: 4,+ link_deleted: true,+ }+ );++ // A duplicate delivery after completion finds nothing left to do.+ let duplicate = purge_link(&pool, PURGED, tiny_batches()).await?;+ assert_eq!(duplicate, PurgeSummary::default());++ assert_eq!(link_rows(&pool, PURGED).await?, LinkRows::EMPTY);+ assert_eq!(link_rows(&pool, KEPT).await?, LinkRows::FULL);+ assert_eq!(detached_sfs_rows(&pool).await?, 10);++ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("purge_link"))+)]+async fn message_batches_keep_a_thread_until_its_last_message_is_gone(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ // Threads hold 3 messages, so a batch of 2 ends inside a thread every time.+ mark_link_deleting(&pool, PURGED).await?;+ let limit = NonZeroU32::new(2).expect("non-zero");++ let first = purge_message_batch(&pool, PURGED, Uuid::nil(), limit).await?;+ assert_eq!((first.messages, first.threads), (2, 0));+ let partial = link_rows(&pool, PURGED).await?;+ assert_eq!((partial.messages, partial.threads), (28, 11));++ let cursor = first.next_cursor.expect("more messages remain");+ let second = purge_message_batch(&pool, PURGED, cursor, limit).await?;+ assert_eq!((second.messages, second.threads), (2, 1));+ let partial = link_rows(&pool, PURGED).await?;+ assert_eq!((partial.messages, partial.threads), (26, 10));+ assert_eq!(partial.user_history, 9);++ assert_eq!(link_rows(&pool, KEPT).await?, LinkRows::FULL);++ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("purge_link"))+)]+async fn concurrent_purges_of_one_link_both_succeed(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let (first, second) = tokio::try_join!(+ purge_link(&pool, PURGED, batch_sizes(2, 2, 1)),+ purge_link(&pool, PURGED, batch_sizes(3, 1, 2)),+ )?;++ assert_eq!(first.messages + second.messages, 30);+ assert_eq!(first.threads + second.threads, 11);+ assert_eq!(first.contacts + second.contacts, 4);+ assert!(first.link_deleted != second.link_deleted);++ assert_eq!(link_rows(&pool, PURGED).await?, LinkRows::EMPTY);+ assert_eq!(link_rows(&pool, KEPT).await?, LinkRows::FULL);++ Ok(())+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn sync_does_not_resume_on_a_link_being_deleted(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let mut conn = pool.acquire().await?;+ let link = upsert_link(&mut conn, gmail_link("[email protected]")?).await?;++ mark_link_deleting(&pool, link.id).await?;+ let marked = deletion_state(&pool, link.id).await?;+ assert!(!marked.is_sync_active);+ let deleting_at = marked.deleting_at.expect("deleting_at is set");++ update_link_sync_status(&pool, link.id, true).await?;+ assert!(!deletion_state(&pool, link.id).await?.is_sync_active);++ // Reconnecting the same mailbox mid-purge must not revive the doomed row.+ assert!(+ upsert_link(&mut conn, gmail_link("[email protected]")?)+ .await+ .is_err()+ );++ // Marking again (a redelivered delete) keeps the original mark.+ mark_link_deleting(&pool, link.id).await?;+ let state = deletion_state(&pool, link.id).await?;+ assert!(!state.is_sync_active);+ assert_eq!(state.deleting_at, Some(deleting_at));++ assert!(+ purge_link(&pool, link.id, PurgeBatchSizes::default())+ .await?+ .link_deleted+ );++ // Once the purge is done, reconnecting creates a fresh, syncing link.+ let reconnected = upsert_link(&mut conn, gmail_link("[email protected]")?).await?;+ assert_ne!(reconnected.id, link.id);+ let state = deletion_state(&pool, reconnected.id).await?;+ assert!(state.is_sync_active);+ assert!(state.deleting_at.is_none());++ Ok(())+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn purge_of_a_missing_link_is_a_no_op(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let missing = macro_uuid::generate_uuid_v7();++ mark_link_deleting(&pool, missing).await?;+ assert_eq!(+ purge_link(&pool, missing, PurgeBatchSizes::default()).await?,+ PurgeSummary::default()+ );++ Ok(())+}diff --git a/crates/email_db_client/src/links/insert.rs b/crates/email_db_client/src/links/insert.rsindex 2c5e9cc472..09f7a1738b 100644--- a/crates/email_db_client/src/links/insert.rs+++ b/crates/email_db_client/src/links/insert.rs@@ -16,6 +16,9 @@ struct LinkId {/// If a record with matching fusionauth_user_id, email_address, and provider already exists,/// updates the existing record with values from the provided Link./// Returns the ID of the inserted or updated link and a boolean indicating if a new record was created.+///+/// Fails when the existing record is being deleted: reviving it would resume sync on a link+/// whose data is mid-purge. Once the purge removes the row, a retry inserts a fresh link.#[tracing::instrument(skip(conn), err)]pub async fn upsert_link(conn: &mut sqlx::PgConnection,@@ -52,6 +55,7 @@ pub async fn upsert_link(needs_reauth = false,last_sync_error_at = NULL,updated_at = NOW()+ WHERE email_links.deleting_at IS NULLRETURNING id, is_primary"#,id,@@ -61,9 +65,15 @@ pub async fn upsert_link(db_provider as _,is_sync_active)- .fetch_one(&mut *conn)+ .fetch_optional(&mut *conn).await?;+ let Some(result) = result else {+ anyhow::bail!(+ "email link for {fusionauth_user_id} is being deleted; retry once it is gone"+ );+ };+let service_link = service::link::Link {id: result.id,macro_id,diff --git a/crates/email_db_client/src/links/update.rs b/crates/email_db_client/src/links/update.rsindex 1ac11ca3de..573ee1ce89 100644--- a/crates/email_db_client/src/links/update.rs+++ b/crates/email_db_client/src/links/update.rs@@ -81,9 +81,8 @@ pub async fn clear_link_needs_reauth(pool: &PgPool, link_id: Uuid) -> anyhow::Re/// Updates the sync active status for a link by its ID./// Also updates the updated_at timestamp to the current time.///-/// A no-op when the status already matches, so a redelivered delete message-/// returns immediately instead of queueing behind the row lock held by the-/// first delivery's cascading `DELETE FROM email_links`.+/// A no-op when the status already matches, so a repeat call never rewrites+/// the row. Never re-enables sync on a link that is being deleted.#[tracing::instrument(skip(pool), err)]pub async fn update_link_sync_status(pool: &PgPool,@@ -94,7 +93,9 @@ pub async fn update_link_sync_status(r#"UPDATE email_linksSET is_sync_active = $2, updated_at = NOW()- WHERE id = $1 AND is_sync_active IS DISTINCT FROM $2+ WHERE id = $1+ AND is_sync_active IS DISTINCT FROM $2+ AND (NOT $2 OR deleting_at IS NULL)"#,link_id,is_sync_activediff --git a/crates/macro_db_client/migrations/20261012153820_email_links_deleting_at.down.sql b/crates/macro_db_client/migrations/20261012153820_email_links_deleting_at.down.sqlnew file mode 100644index 0000000000..e61b1f348a--- /dev/null+++ b/crates/macro_db_client/migrations/20261012153820_email_links_deleting_at.down.sql@@ -0,0 +1,3 @@+SET LOCAL lock_timeout = '5s';++ALTER TABLE email_links DROP COLUMN deleting_at;diff --git a/crates/macro_db_client/migrations/20261012153820_email_links_deleting_at.up.sql b/crates/macro_db_client/migrations/20261012153820_email_links_deleting_at.up.sqlnew file mode 100644index 0000000000..4490eab39e--- /dev/null+++ b/crates/macro_db_client/migrations/20261012153820_email_links_deleting_at.up.sql@@ -0,0 +1,5 @@+-- Set when a link's teardown starts. A marked link never resumes syncing, and its+-- data is purged in bounded batches before the row itself is deleted.+SET LOCAL lock_timeout = '5s';++ALTER TABLE email_links ADD COLUMN deleting_at TIMESTAMPTZ;diff --git a/services/email_service/src/pubsub/link_manager/process.rs b/services/email_service/src/pubsub/link_manager/process.rsindex 4b69c7fbd1..1af9b3bd89 100644--- a/services/email_service/src/pubsub/link_manager/process.rs+++ b/services/email_service/src/pubsub/link_manager/process.rs@@ -7,6 +7,7 @@ use email::domain::events::{EmailMacroEvent, LinkDisconnectReason, LinkDisconnectedMetadata, LinkReauthRequiredMetadata,};use email_api_client::domain::models::{AccessToken, EmailApiError, TokenFreshness};+use email_db_client::links::delete::PurgeBatchSizes;use model_entity::EntityType;use model_notifications::InboxReauthRequiredMetadata;use models_email::api::refresh::RefreshEmailEvent;@@ -283,10 +284,10 @@ async fn handle_delete(deletion_reason: &DeletionReason,) -> anyhow::Result<()> {tracing::info!("Deleting link");- // set sync status to false so any future inbox updates get ignored- email_db_client::links::update::update_link_sync_status(&ctx.db, link.id, false)+ // mark the link as being deleted, which stops sync so any future inbox updates get ignored+ email_db_client::links::delete::mark_link_deleting(&ctx.db, link.id).await- .context("Failed to update link sync status")?;+ .context("Failed to mark link as deleting")?;// cancel any running backfill jobsemail_db_client::backfill::job::update::cancel_active_jobs_by_link_id(&ctx.db, link.id)@@ -336,10 +337,10 @@ async fn handle_delete(.ok();// Tear down CRM rows this link contributed to the user's team before- // the big cascading link delete fires. Best-effort: a failure here+ // the link is purged. Best-effort: a failure here// would only leave orphan `crm_contacts`/`crm_companies` rows behind- // (the `crm_contact_sources` FK to `email_links` cascades on the- // upcoming `delete_link_by_id`, so the link-scoped source rows go+ // (the `crm_contact_sources` FK to `email_links` cascades when the+ // upcoming `purge_link` deletes the link row, so the link-scoped source rows go// away regardless), so we log and continue rather than bailing.let macro_id_str = link.macro_id.to_string();match ctx.crm_service.get_team_id_for_user(¯o_id_str).await {@@ -360,10 +361,11 @@ async fn handle_delete(}}- // finally, delete all the user's link as well as all of their email data in a big cascading delete- email_db_client::links::delete::delete_link_by_id(&ctx.db, link.id)+ // finally, delete the link and all of its email data in bounded batches; a redelivered+ // message resumes the purge where an interrupted one stopped+ email_db_client::links::delete::purge_link(&ctx.db, link.id, PurgeBatchSizes::default()).await- .context("Failed to delete link in background task")?;+ .context("Failed to purge link in background task")?;// The teardown is async relative to the delete request, so signal completion// now that the rows are gone — a client showing this inbox can drop its data.