Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(email): generify email links table (#5405)
Will Hutchinson2a639ed24ca9
diff --git a/.sqlx/query-a67a9d4ba9add6dbedaff410fba31c7c2320baefd11df560e9c4ef0e1160c20e.json b/.sqlx/query-123b1f5886fafe4a9fc3e1507a799c9ce88d7d3ae43fe2ecf4ed99eaa41cdfd0.jsonsimilarity index 51%rename from .sqlx/query-a67a9d4ba9add6dbedaff410fba31c7c2320baefd11df560e9c4ef0e1160c20e.jsonrename to .sqlx/query-123b1f5886fafe4a9fc3e1507a799c9ce88d7d3ae43fe2ecf4ed99eaa41cdfd0.jsonindex d24209d33c..a5e040649a 100644--- a/.sqlx/query-a67a9d4ba9add6dbedaff410fba31c7c2320baefd11df560e9c4ef0e1160c20e.json+++ b/.sqlx/query-123b1f5886fafe4a9fc3e1507a799c9ce88d7d3ae43fe2ecf4ed99eaa41cdfd0.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT job.id\n FROM calendar_backfill_jobs job\n WHERE job.id = $1\n AND job.email_link_id = $2\n AND job.kind = 'google_calendar'\n AND job.status = 'running'\n AND job.lease_token = $3\n AND job.lease_expires_at > now()\n AND EXISTS (\n SELECT 1\n FROM email_links link\n WHERE link.id = job.email_link_id\n AND link.google_grant_version = job.grant_version\n AND link.google_granted_scopes @> $5::text[]\n )\n AND EXISTS (\n SELECT 1\n FROM calendar_accounts account\n WHERE account.id = job.account_id\n AND account.email_link_id = job.email_link_id\n AND account.sync_status <> 'disabled'\n AND ($4::uuid IS NULL OR account.id = $4)\n )\n FOR UPDATE OF job\n ",+ "query": "\n SELECT job.id\n FROM calendar_backfill_jobs job\n WHERE job.id = $1\n AND job.email_link_id = $2\n AND job.kind = 'google_calendar'\n AND job.status = 'running'\n AND job.lease_token = $3\n AND job.lease_expires_at > now()\n AND EXISTS (\n SELECT 1\n FROM email_link_google_scopes scopes\n WHERE scopes.link_id = job.email_link_id\n AND scopes.grant_version = job.grant_version\n AND scopes.granted_scopes @> $5::text[]\n )\n AND EXISTS (\n SELECT 1\n FROM calendar_accounts account\n WHERE account.id = job.account_id\n AND account.email_link_id = job.email_link_id\n AND account.sync_status <> 'disabled'\n AND ($4::uuid IS NULL OR account.id = $4)\n )\n FOR UPDATE OF job\n ","describe": {"columns": [{@@ -22,5 +22,5 @@false]},- "hash": "a67a9d4ba9add6dbedaff410fba31c7c2320baefd11df560e9c4ef0e1160c20e"+ "hash": "123b1f5886fafe4a9fc3e1507a799c9ce88d7d3ae43fe2ecf4ed99eaa41cdfd0"}diff --git a/.sqlx/query-1c26edf29d47ee2ca10b21f5ce7ffe278ba07f69aa005d64c3a7c51a57b7242b.json b/.sqlx/query-1c26edf29d47ee2ca10b21f5ce7ffe278ba07f69aa005d64c3a7c51a57b7242b.jsonnew file mode 100644index 0000000000..b3fe176e4a--- /dev/null+++ b/.sqlx/query-1c26edf29d47ee2ca10b21f5ce7ffe278ba07f69aa005d64c3a7c51a57b7242b.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO email_link_google_scopes (link_id, granted_scopes, grant_version)\n VALUES ($1, $2, $3)\n ON CONFLICT (link_id) DO UPDATE\n SET granted_scopes = EXCLUDED.granted_scopes,\n grant_version = EXCLUDED.grant_version,\n updated_at = now()\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "TextArray",+ "Int8"+ ]+ },+ "nullable": []+ },+ "hash": "1c26edf29d47ee2ca10b21f5ce7ffe278ba07f69aa005d64c3a7c51a57b7242b"+}diff --git a/.sqlx/query-3d30393b7a33dc18e4de1e08a59bcd83110ca2ef5c786c62e3600bc666d3798f.json b/.sqlx/query-3d30393b7a33dc18e4de1e08a59bcd83110ca2ef5c786c62e3600bc666d3798f.jsondeleted file mode 100644index 6bc26a9b85..0000000000--- a/.sqlx/query-3d30393b7a33dc18e4de1e08a59bcd83110ca2ef5c786c62e3600bc666d3798f.json+++ /dev/null@@ -1,23 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n WITH due AS (\n UPDATE calendar_backfill_jobs job\n SET status = 'pending',\n cursor = '{}',\n scanned_count = 0,\n extracted_count = 0,\n last_error = NULL,\n started_at = NULL,\n completed_at = NULL,\n lease_token = NULL,\n lease_expires_at = NULL,\n updated_at = now()\n FROM calendar_accounts account, email_links link\n WHERE job.email_link_id = account.email_link_id\n AND job.account_id = account.id\n AND link.id = job.email_link_id\n AND job.kind = 'google_calendar'\n AND job.status = 'complete'\n AND job.grant_version = link.google_grant_version\n AND link.google_granted_scopes @> $2::text[]\n AND account.sync_status = 'ready'\n AND account.last_synced_at <= $1\n RETURNING job.id\n ),\n republished AS (\n UPDATE calendar_sync_outbox outbox\n SET published_at = NULL\n FROM due\n WHERE outbox.backfill_job_id = due.id\n )\n SELECT count(*) AS \"due_count!\" FROM due\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "due_count!",- "type_info": "Int8"- }- ],- "parameters": {- "Left": [- "Timestamptz",- "TextArray"- ]- },- "nullable": [- null- ]- },- "hash": "3d30393b7a33dc18e4de1e08a59bcd83110ca2ef5c786c62e3600bc666d3798f"-}diff --git a/.sqlx/query-417ce1abfdf12bd1ef05c7c5b67158fa676d4a08db1f56c4c8b1923771e723ef.json b/.sqlx/query-417ce1abfdf12bd1ef05c7c5b67158fa676d4a08db1f56c4c8b1923771e723ef.jsondeleted file mode 100644index 7f9db7ed85..0000000000--- a/.sqlx/query-417ce1abfdf12bd1ef05c7c5b67158fa676d4a08db1f56c4c8b1923771e723ef.json+++ /dev/null@@ -1,22 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "SELECT google_grant_version = 0 AS \"unrecorded!\" FROM email_links WHERE id = $1",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "unrecorded!",- "type_info": "Bool"- }- ],- "parameters": {- "Left": [- "Uuid"- ]- },- "nullable": [- null- ]- },- "hash": "417ce1abfdf12bd1ef05c7c5b67158fa676d4a08db1f56c4c8b1923771e723ef"-}diff --git a/.sqlx/query-58590b2ea830ad78615b548409a61101cf215e4f3871293c46d5af67886bf483.json b/.sqlx/query-4676a06d6944ba710c8d3750b0c8dd134aa199242588f7c11b2a118427235568.jsonsimilarity index 65%rename from .sqlx/query-58590b2ea830ad78615b548409a61101cf215e4f3871293c46d5af67886bf483.jsonrename to .sqlx/query-4676a06d6944ba710c8d3750b0c8dd134aa199242588f7c11b2a118427235568.jsonindex e9d24e9734..0768065287 100644--- a/.sqlx/query-58590b2ea830ad78615b548409a61101cf215e4f3871293c46d5af67886bf483.json+++ b/.sqlx/query-4676a06d6944ba710c8d3750b0c8dd134aa199242588f7c11b2a118427235568.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n UPDATE calendar_backfill_jobs\n SET status = $4,\n lease_expires_at = NULL,\n lease_token = NULL,\n last_error = $2,\n completed_at = $5,\n updated_at = now()\n WHERE id = $1\n AND email_link_id = $3\n AND kind = 'google_calendar'\n AND status NOT IN ('complete', 'failed')\n AND lease_token IS NULL\n AND EXISTS (\n SELECT 1\n FROM email_links link\n WHERE link.id = calendar_backfill_jobs.email_link_id\n AND link.google_grant_version = calendar_backfill_jobs.grant_version\n AND link.google_granted_scopes @> $6::text[]\n )\n ",+ "query": "\n UPDATE calendar_backfill_jobs\n SET status = $4,\n lease_expires_at = NULL,\n lease_token = NULL,\n last_error = $2,\n completed_at = $5,\n updated_at = now()\n WHERE id = $1\n AND email_link_id = $3\n AND kind = 'google_calendar'\n AND status NOT IN ('complete', 'failed')\n AND lease_token IS NULL\n AND EXISTS (\n SELECT 1\n FROM email_link_google_scopes scopes\n WHERE scopes.link_id = calendar_backfill_jobs.email_link_id\n AND scopes.grant_version = calendar_backfill_jobs.grant_version\n AND scopes.granted_scopes @> $6::text[]\n )\n ","describe": {"columns": [],"parameters": {@@ -15,5 +15,5 @@},"nullable": []},- "hash": "58590b2ea830ad78615b548409a61101cf215e4f3871293c46d5af67886bf483"+ "hash": "4676a06d6944ba710c8d3750b0c8dd134aa199242588f7c11b2a118427235568"}diff --git a/.sqlx/query-7dac4a96f590bb91f1837710fff321dc690c7fb0173bacee985d494fdfcded43.json b/.sqlx/query-7dac4a96f590bb91f1837710fff321dc690c7fb0173bacee985d494fdfcded43.jsonnew file mode 100644index 0000000000..18b5e0d21d--- /dev/null+++ b/.sqlx/query-7dac4a96f590bb91f1837710fff321dc690c7fb0173bacee985d494fdfcded43.json@@ -0,0 +1,23 @@+{+ "db_name": "PostgreSQL",+ "query": "\n WITH due AS (\n UPDATE calendar_backfill_jobs job\n SET status = 'pending',\n cursor = '{}',\n last_error = NULL,\n started_at = NULL,\n completed_at = NULL,\n lease_token = NULL,\n lease_expires_at = NULL,\n updated_at = now()\n FROM calendar_accounts account, email_link_google_scopes scopes\n WHERE job.email_link_id = $1\n AND job.email_link_id = account.email_link_id\n AND job.account_id = account.id\n AND scopes.link_id = job.email_link_id\n AND job.kind = 'google_calendar'\n AND job.status = 'complete'\n AND job.grant_version = scopes.grant_version\n AND scopes.granted_scopes @> $2::text[]\n AND account.sync_status = 'ready'\n RETURNING job.id\n ),\n republished AS (\n UPDATE calendar_sync_outbox outbox\n SET published_at = NULL\n FROM due\n WHERE outbox.backfill_job_id = due.id\n )\n SELECT count(*) > 0 AS \"scheduled!\" FROM due\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "scheduled!",+ "type_info": "Bool"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid",+ "TextArray"+ ]+ },+ "nullable": [+ null+ ]+ },+ "hash": "7dac4a96f590bb91f1837710fff321dc690c7fb0173bacee985d494fdfcded43"+}diff --git a/.sqlx/query-911ea39abb05eabc583ba6e345c941c6a57192bc933063dd48778359c6e5f083.json b/.sqlx/query-911ea39abb05eabc583ba6e345c941c6a57192bc933063dd48778359c6e5f083.jsonnew file mode 100644index 0000000000..bbbe0d3569--- /dev/null+++ b/.sqlx/query-911ea39abb05eabc583ba6e345c941c6a57192bc933063dd48778359c6e5f083.json@@ -0,0 +1,23 @@+{+ "db_name": "PostgreSQL",+ "query": "\n WITH due AS (\n UPDATE calendar_backfill_jobs job\n SET status = 'pending',\n cursor = '{}',\n scanned_count = 0,\n extracted_count = 0,\n last_error = NULL,\n started_at = NULL,\n completed_at = NULL,\n lease_token = NULL,\n lease_expires_at = NULL,\n updated_at = now()\n FROM calendar_accounts account, email_link_google_scopes scopes\n WHERE job.email_link_id = account.email_link_id\n AND job.account_id = account.id\n AND scopes.link_id = job.email_link_id\n AND job.kind = 'google_calendar'\n AND job.status = 'complete'\n AND job.grant_version = scopes.grant_version\n AND scopes.granted_scopes @> $2::text[]\n AND account.sync_status = 'ready'\n AND account.last_synced_at <= $1\n RETURNING job.id\n ),\n republished AS (\n UPDATE calendar_sync_outbox outbox\n SET published_at = NULL\n FROM due\n WHERE outbox.backfill_job_id = due.id\n )\n SELECT count(*) AS \"due_count!\" FROM due\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "due_count!",+ "type_info": "Int8"+ }+ ],+ "parameters": {+ "Left": [+ "Timestamptz",+ "TextArray"+ ]+ },+ "nullable": [+ null+ ]+ },+ "hash": "911ea39abb05eabc583ba6e345c941c6a57192bc933063dd48778359c6e5f083"+}diff --git a/.sqlx/query-14a4283e305f1b0edf4005a06973106e61acc84ec8662b7930a126aea5c514de.json b/.sqlx/query-91343113c1f5bb42291d27bb2f2b16ab9b43f63f768ea3dbc7d61c16452a9a92.jsonsimilarity index 70%rename from .sqlx/query-14a4283e305f1b0edf4005a06973106e61acc84ec8662b7930a126aea5c514de.jsonrename to .sqlx/query-91343113c1f5bb42291d27bb2f2b16ab9b43f63f768ea3dbc7d61c16452a9a92.jsonindex 797f64ef14..a99290a150 100644--- a/.sqlx/query-14a4283e305f1b0edf4005a06973106e61acc84ec8662b7930a126aea5c514de.json+++ b/.sqlx/query-91343113c1f5bb42291d27bb2f2b16ab9b43f63f768ea3dbc7d61c16452a9a92.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT l.id as \"id!\", l.macro_id as \"macro_id!\",\n l.fusionauth_user_id as \"fusionauth_user_id!\",\n l.email_address as \"email_address!\",\n l.provider as \"provider!: _\",\n l.is_sync_active as \"is_sync_active!\",\n l.is_primary as \"is_primary!\",\n l.needs_reauth as \"needs_reauth!\",\n l.last_sync_error_at,\n l.created_at as \"created_at!\",\n l.updated_at as \"updated_at!\",\n s.signature_on_replies_forwards as \"signature_on_replies_forwards?\",\n s.signature,\n bj.status as \"latest_backfill_status?: _\",\n c.sfs_photo_url as \"photo_url?\",\n l.google_granted_scopes as \"google_granted_scopes!\"\n FROM (\n SELECT el.id, el.macro_id, el.fusionauth_user_id, el.email_address,\n el.provider, el.is_sync_active, el.is_primary, el.needs_reauth,\n el.last_sync_error_at, el.created_at, el.updated_at,\n el.google_granted_scopes\n FROM email_links el\n WHERE el.macro_id = $1\n UNION\n SELECT el.id, el.macro_id, el.fusionauth_user_id, el.email_address,\n el.provider, el.is_sync_active, el.is_primary, el.needs_reauth,\n el.last_sync_error_at, el.created_at, el.updated_at,\n el.google_granted_scopes\n FROM email_links el\n JOIN macro_user_links mul ON el.id = mul.link_id\n WHERE mul.primary_macro_id = $1\n ) l\n LEFT JOIN email_settings s ON s.link_id = l.id\n LEFT JOIN LATERAL (\n SELECT status FROM email_backfill_jobs\n WHERE link_id = l.id\n ORDER BY created_at DESC\n LIMIT 1\n ) bj ON true\n LEFT JOIN email_contacts c\n ON c.link_id = l.id AND LOWER(c.email_address) = LOWER(l.email_address)\n ORDER BY l.created_at DESC\n ",+ "query": "\n SELECT l.id as \"id!\", l.macro_id as \"macro_id!\",\n l.fusionauth_user_id as \"fusionauth_user_id!\",\n l.email_address as \"email_address!\",\n l.provider as \"provider!: _\",\n l.is_sync_active as \"is_sync_active!\",\n l.is_primary as \"is_primary!\",\n l.needs_reauth as \"needs_reauth!\",\n l.last_sync_error_at,\n l.created_at as \"created_at!\",\n l.updated_at as \"updated_at!\",\n s.signature_on_replies_forwards as \"signature_on_replies_forwards?\",\n s.signature,\n bj.status as \"latest_backfill_status?: _\",\n c.sfs_photo_url as \"photo_url?\",\n COALESCE(g.granted_scopes, '{}') AS \"google_granted_scopes!\"\n FROM (\n SELECT el.id, el.macro_id, el.fusionauth_user_id, el.email_address,\n el.provider, el.is_sync_active, el.is_primary, el.needs_reauth,\n el.last_sync_error_at, el.created_at, el.updated_at\n FROM email_links el\n WHERE el.macro_id = $1\n UNION\n SELECT el.id, el.macro_id, el.fusionauth_user_id, el.email_address,\n el.provider, el.is_sync_active, el.is_primary, el.needs_reauth,\n el.last_sync_error_at, el.created_at, el.updated_at\n FROM email_links el\n JOIN macro_user_links mul ON el.id = mul.link_id\n WHERE mul.primary_macro_id = $1\n ) l\n LEFT JOIN email_link_google_scopes g ON g.link_id = l.id\n LEFT JOIN email_settings s ON s.link_id = l.id\n LEFT JOIN LATERAL (\n SELECT status FROM email_backfill_jobs\n WHERE link_id = l.id\n ORDER BY created_at DESC\n LIMIT 1\n ) bj ON true\n LEFT JOIN email_contacts c\n ON c.link_id = l.id AND LOWER(c.email_address) = LOWER(l.email_address)\n ORDER BY l.created_at DESC\n ","describe": {"columns": [{@@ -130,5 +130,5 @@null]},- "hash": "14a4283e305f1b0edf4005a06973106e61acc84ec8662b7930a126aea5c514de"+ "hash": "91343113c1f5bb42291d27bb2f2b16ab9b43f63f768ea3dbc7d61c16452a9a92"}diff --git a/.sqlx/query-a26e204c2c74cf9608191908dbcd0cbda042bd30e1600dd39baa1ab888f2ebea.json b/.sqlx/query-a26e204c2c74cf9608191908dbcd0cbda042bd30e1600dd39baa1ab888f2ebea.jsondeleted file mode 100644index 2bb2fcb98a..0000000000--- a/.sqlx/query-a26e204c2c74cf9608191908dbcd0cbda042bd30e1600dd39baa1ab888f2ebea.json+++ /dev/null@@ -1,23 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n WITH due AS (\n UPDATE calendar_backfill_jobs job\n SET status = 'pending',\n cursor = '{}',\n last_error = NULL,\n started_at = NULL,\n completed_at = NULL,\n lease_token = NULL,\n lease_expires_at = NULL,\n updated_at = now()\n FROM calendar_accounts account, email_links link\n WHERE job.email_link_id = $1\n AND job.email_link_id = account.email_link_id\n AND job.account_id = account.id\n AND link.id = job.email_link_id\n AND job.kind = 'google_calendar'\n AND job.status = 'complete'\n AND job.grant_version = link.google_grant_version\n AND link.google_granted_scopes @> $2::text[]\n AND account.sync_status = 'ready'\n RETURNING job.id\n ),\n republished AS (\n UPDATE calendar_sync_outbox outbox\n SET published_at = NULL\n FROM due\n WHERE outbox.backfill_job_id = due.id\n )\n SELECT count(*) > 0 AS \"scheduled!\" FROM due\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "scheduled!",- "type_info": "Bool"- }- ],- "parameters": {- "Left": [- "Uuid",- "TextArray"- ]- },- "nullable": [- null- ]- },- "hash": "a26e204c2c74cf9608191908dbcd0cbda042bd30e1600dd39baa1ab888f2ebea"-}diff --git a/.sqlx/query-a51b52b72fd89dec92598f619084c222946d986fcac1e402d4585481691faa4c.json b/.sqlx/query-a51b52b72fd89dec92598f619084c222946d986fcac1e402d4585481691faa4c.jsonnew file mode 100644index 0000000000..e936711c05--- /dev/null+++ b/.sqlx/query-a51b52b72fd89dec92598f619084c222946d986fcac1e402d4585481691faa4c.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT COALESCE(g.grant_version, 0) = 0 AS \"unrecorded!\"\n FROM email_links l\n LEFT JOIN email_link_google_scopes g ON g.link_id = l.id\n WHERE l.id = $1\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "unrecorded!",+ "type_info": "Bool"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ null+ ]+ },+ "hash": "a51b52b72fd89dec92598f619084c222946d986fcac1e402d4585481691faa4c"+}diff --git a/.sqlx/query-b6e979bed8e80a7ca84683f2e88b210d5076f7dbdcb0047e6ab5bdbdfafab839.json b/.sqlx/query-b6e979bed8e80a7ca84683f2e88b210d5076f7dbdcb0047e6ab5bdbdfafab839.jsondeleted file mode 100644index 6a4ec09096..0000000000--- a/.sqlx/query-b6e979bed8e80a7ca84683f2e88b210d5076f7dbdcb0047e6ab5bdbdfafab839.json+++ /dev/null@@ -1,25 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n UPDATE calendar_backfill_jobs\n SET status = 'running',\n started_at = COALESCE(started_at, now()),\n lease_token = $3,\n lease_expires_at = now() + interval '2 minutes',\n last_error = NULL,\n updated_at = now()\n WHERE id = $1\n AND email_link_id = $2\n AND kind = 'google_calendar'\n AND EXISTS (\n SELECT 1\n FROM email_links link\n WHERE link.id = calendar_backfill_jobs.email_link_id\n AND link.google_grant_version = calendar_backfill_jobs.grant_version\n AND link.google_granted_scopes @> $4::text[]\n )\n AND EXISTS (\n SELECT 1\n FROM calendar_accounts account\n WHERE account.id = calendar_backfill_jobs.account_id\n AND account.email_link_id = calendar_backfill_jobs.email_link_id\n AND account.sync_status <> 'disabled'\n )\n AND (\n status = 'pending'\n OR (\n status = 'running'\n AND lease_expires_at < now()\n )\n )\n RETURNING account_id\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "account_id",- "type_info": "Uuid"- }- ],- "parameters": {- "Left": [- "Uuid",- "Uuid",- "Uuid",- "TextArray"- ]- },- "nullable": [- true- ]- },- "hash": "b6e979bed8e80a7ca84683f2e88b210d5076f7dbdcb0047e6ab5bdbdfafab839"-}diff --git a/.sqlx/query-eb6dbbf51c037743d063b5b7a1002d9bf794f6907df2cc49584b741798ab508b.json b/.sqlx/query-c85904d992bea1f2620b61e237f8fc077d3eb406ade7d84e871e0846959c4520.jsonsimilarity index 69%rename from .sqlx/query-eb6dbbf51c037743d063b5b7a1002d9bf794f6907df2cc49584b741798ab508b.jsonrename to .sqlx/query-c85904d992bea1f2620b61e237f8fc077d3eb406ade7d84e871e0846959c4520.jsonindex 3e0eaee8fa..3b7501bc59 100644--- a/.sqlx/query-eb6dbbf51c037743d063b5b7a1002d9bf794f6907df2cc49584b741798ab508b.json+++ b/.sqlx/query-c85904d992bea1f2620b61e237f8fc077d3eb406ade7d84e871e0846959c4520.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n UPDATE calendar_backfill_jobs\n SET status = $4,\n lease_expires_at = NULL,\n lease_token = NULL,\n last_error = $2,\n completed_at = $5,\n updated_at = now()\n WHERE id = $1\n AND email_link_id = $3\n AND kind = 'google_calendar'\n AND status = 'running'\n AND lease_token = $6\n AND lease_expires_at > now()\n AND EXISTS (\n SELECT 1\n FROM email_links link\n WHERE link.id = calendar_backfill_jobs.email_link_id\n AND link.google_grant_version = calendar_backfill_jobs.grant_version\n AND link.google_granted_scopes @> $7::text[]\n )\n ",+ "query": "\n UPDATE calendar_backfill_jobs\n SET status = $4,\n lease_expires_at = NULL,\n lease_token = NULL,\n last_error = $2,\n completed_at = $5,\n updated_at = now()\n WHERE id = $1\n AND email_link_id = $3\n AND kind = 'google_calendar'\n AND status = 'running'\n AND lease_token = $6\n AND lease_expires_at > now()\n AND EXISTS (\n SELECT 1\n FROM email_link_google_scopes scopes\n WHERE scopes.link_id = calendar_backfill_jobs.email_link_id\n AND scopes.grant_version = calendar_backfill_jobs.grant_version\n AND scopes.granted_scopes @> $7::text[]\n )\n ","describe": {"columns": [],"parameters": {@@ -16,5 +16,5 @@},"nullable": []},- "hash": "eb6dbbf51c037743d063b5b7a1002d9bf794f6907df2cc49584b741798ab508b"+ "hash": "c85904d992bea1f2620b61e237f8fc077d3eb406ade7d84e871e0846959c4520"}diff --git a/.sqlx/query-8a9ec56cfa14540348c10d84ca4dece2ebb721e91dda88b42162771b407d302d.json b/.sqlx/query-c87baddeae70242a9989cfdf1a3d43ed58119ce0149a654e02301e8bf980756d.jsonsimilarity index 61%rename from .sqlx/query-8a9ec56cfa14540348c10d84ca4dece2ebb721e91dda88b42162771b407d302d.jsonrename to .sqlx/query-c87baddeae70242a9989cfdf1a3d43ed58119ce0149a654e02301e8bf980756d.jsonindex d6b8d6537c..a1b90ff1fb 100644--- a/.sqlx/query-8a9ec56cfa14540348c10d84ca4dece2ebb721e91dda88b42162771b407d302d.json+++ b/.sqlx/query-c87baddeae70242a9989cfdf1a3d43ed58119ce0149a654e02301e8bf980756d.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n UPDATE calendar_backfill_jobs\n SET lease_expires_at = now() + interval '2 minutes',\n updated_at = now()\n WHERE id = $1\n AND email_link_id = $2\n AND status = 'running'\n AND lease_token = $3\n AND lease_expires_at > now()\n AND EXISTS (\n SELECT 1\n FROM email_links link\n WHERE link.id = calendar_backfill_jobs.email_link_id\n AND link.google_grant_version = calendar_backfill_jobs.grant_version\n AND link.google_granted_scopes @> $4::text[]\n )\n ",+ "query": "\n UPDATE calendar_backfill_jobs\n SET lease_expires_at = now() + interval '2 minutes',\n updated_at = now()\n WHERE id = $1\n AND email_link_id = $2\n AND status = 'running'\n AND lease_token = $3\n AND lease_expires_at > now()\n AND EXISTS (\n SELECT 1\n FROM email_link_google_scopes scopes\n WHERE scopes.link_id = calendar_backfill_jobs.email_link_id\n AND scopes.grant_version = calendar_backfill_jobs.grant_version\n AND scopes.granted_scopes @> $4::text[]\n )\n ","describe": {"columns": [],"parameters": {@@ -13,5 +13,5 @@},"nullable": []},- "hash": "8a9ec56cfa14540348c10d84ca4dece2ebb721e91dda88b42162771b407d302d"+ "hash": "c87baddeae70242a9989cfdf1a3d43ed58119ce0149a654e02301e8bf980756d"}diff --git a/.sqlx/query-d46629520beb24f9ce40480fbaf29efe6e0a14d9d508f5697ccdb70fe42a0488.json b/.sqlx/query-d46629520beb24f9ce40480fbaf29efe6e0a14d9d508f5697ccdb70fe42a0488.jsonnew file mode 100644index 0000000000..8373aa1728--- /dev/null+++ b/.sqlx/query-d46629520beb24f9ce40480fbaf29efe6e0a14d9d508f5697ccdb70fe42a0488.json@@ -0,0 +1,40 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT\n l.macro_id,\n l.email_address::text AS \"email_address!\",\n COALESCE(g.granted_scopes, '{}') AS \"granted_scopes!\",\n COALESCE(g.grant_version, 0) AS \"grant_version!\"\n FROM email_links l\n LEFT JOIN email_link_google_scopes g ON g.link_id = l.id\n WHERE l.id = $1\n FOR UPDATE OF l\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "macro_id",+ "type_info": "Text"+ },+ {+ "ordinal": 1,+ "name": "email_address!",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "granted_scopes!",+ "type_info": "TextArray"+ },+ {+ "ordinal": 3,+ "name": "grant_version!",+ "type_info": "Int8"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ false,+ null,+ null,+ null+ ]+ },+ "hash": "d46629520beb24f9ce40480fbaf29efe6e0a14d9d508f5697ccdb70fe42a0488"+}diff --git a/.sqlx/query-e05feaac668af3aeba4463d38e8b453dadb4a7563a2a1a65778fdf3364d85361.json b/.sqlx/query-e05feaac668af3aeba4463d38e8b453dadb4a7563a2a1a65778fdf3364d85361.jsondeleted file mode 100644index 845af0c791..0000000000--- a/.sqlx/query-e05feaac668af3aeba4463d38e8b453dadb4a7563a2a1a65778fdf3364d85361.json+++ /dev/null@@ -1,40 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT\n macro_id,\n email_address::text AS \"email_address!\",\n google_granted_scopes,\n google_grant_version\n FROM email_links\n WHERE id = $1\n FOR UPDATE\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "macro_id",- "type_info": "Text"- },- {- "ordinal": 1,- "name": "email_address!",- "type_info": "Text"- },- {- "ordinal": 2,- "name": "google_granted_scopes",- "type_info": "TextArray"- },- {- "ordinal": 3,- "name": "google_grant_version",- "type_info": "Int8"- }- ],- "parameters": {- "Left": [- "Uuid"- ]- },- "nullable": [- false,- null,- false,- false- ]- },- "hash": "e05feaac668af3aeba4463d38e8b453dadb4a7563a2a1a65778fdf3364d85361"-}diff --git a/.sqlx/query-86e304676292a96720d06f53478ab0dcb9e6c0bef75322619ef71389188b3d38.json b/.sqlx/query-e10fba756a67ef10a1246426a39b99d2aabeb58798f2f56d0fab7c9ec6e330c3.jsonsimilarity index 63%rename from .sqlx/query-86e304676292a96720d06f53478ab0dcb9e6c0bef75322619ef71389188b3d38.jsonrename to .sqlx/query-e10fba756a67ef10a1246426a39b99d2aabeb58798f2f56d0fab7c9ec6e330c3.jsonindex 1a26dd2bf2..1588e286f1 100644--- a/.sqlx/query-86e304676292a96720d06f53478ab0dcb9e6c0bef75322619ef71389188b3d38.json+++ b/.sqlx/query-e10fba756a67ef10a1246426a39b99d2aabeb58798f2f56d0fab7c9ec6e330c3.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n UPDATE calendar_backfill_jobs\n SET status = 'complete',\n completed_at = now(),\n lease_token = NULL,\n lease_expires_at = NULL,\n updated_at = now()\n WHERE id = $1\n AND email_link_id = $2\n AND kind = 'google_calendar'\n AND status = 'running'\n AND lease_token = $3\n AND lease_expires_at > now()\n AND EXISTS (\n SELECT 1\n FROM email_links link\n WHERE link.id = calendar_backfill_jobs.email_link_id\n AND link.google_grant_version = calendar_backfill_jobs.grant_version\n AND link.google_granted_scopes @> $4::text[]\n )\n ",+ "query": "\n UPDATE calendar_backfill_jobs\n SET status = 'complete',\n completed_at = now(),\n lease_token = NULL,\n lease_expires_at = NULL,\n updated_at = now()\n WHERE id = $1\n AND email_link_id = $2\n AND kind = 'google_calendar'\n AND status = 'running'\n AND lease_token = $3\n AND lease_expires_at > now()\n AND EXISTS (\n SELECT 1\n FROM email_link_google_scopes scopes\n WHERE scopes.link_id = calendar_backfill_jobs.email_link_id\n AND scopes.grant_version = calendar_backfill_jobs.grant_version\n AND scopes.granted_scopes @> $4::text[]\n )\n ","describe": {"columns": [],"parameters": {@@ -13,5 +13,5 @@},"nullable": []},- "hash": "86e304676292a96720d06f53478ab0dcb9e6c0bef75322619ef71389188b3d38"+ "hash": "e10fba756a67ef10a1246426a39b99d2aabeb58798f2f56d0fab7c9ec6e330c3"}diff --git a/.sqlx/query-e8d8c97e566cc64b3dc67e7b5d70c6870503185008f8ffb7b54408b1adef6075.json b/.sqlx/query-e8d8c97e566cc64b3dc67e7b5d70c6870503185008f8ffb7b54408b1adef6075.jsonnew file mode 100644index 0000000000..a7de3dd552--- /dev/null+++ b/.sqlx/query-e8d8c97e566cc64b3dc67e7b5d70c6870503185008f8ffb7b54408b1adef6075.json@@ -0,0 +1,25 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE calendar_backfill_jobs\n SET status = 'running',\n started_at = COALESCE(started_at, now()),\n lease_token = $3,\n lease_expires_at = now() + interval '2 minutes',\n last_error = NULL,\n updated_at = now()\n WHERE id = $1\n AND email_link_id = $2\n AND kind = 'google_calendar'\n AND EXISTS (\n SELECT 1\n FROM email_link_google_scopes scopes\n WHERE scopes.link_id = calendar_backfill_jobs.email_link_id\n AND scopes.grant_version = calendar_backfill_jobs.grant_version\n AND scopes.granted_scopes @> $4::text[]\n )\n AND EXISTS (\n SELECT 1\n FROM calendar_accounts account\n WHERE account.id = calendar_backfill_jobs.account_id\n AND account.email_link_id = calendar_backfill_jobs.email_link_id\n AND account.sync_status <> 'disabled'\n )\n AND (\n status = 'pending'\n OR (\n status = 'running'\n AND lease_expires_at < now()\n )\n )\n RETURNING account_id\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "account_id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid",+ "Uuid",+ "Uuid",+ "TextArray"+ ]+ },+ "nullable": [+ true+ ]+ },+ "hash": "e8d8c97e566cc64b3dc67e7b5d70c6870503185008f8ffb7b54408b1adef6075"+}diff --git a/.sqlx/query-ff945bb9f03892418e5c80fa89048f731bbbe7ea1eef8dcbad0cc1eaf365cf75.json b/.sqlx/query-ff945bb9f03892418e5c80fa89048f731bbbe7ea1eef8dcbad0cc1eaf365cf75.jsondeleted file mode 100644index dd0f89c32b..0000000000--- a/.sqlx/query-ff945bb9f03892418e5c80fa89048f731bbbe7ea1eef8dcbad0cc1eaf365cf75.json+++ /dev/null@@ -1,16 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n UPDATE email_links\n SET google_granted_scopes = $2,\n google_grant_version = $3,\n updated_at = now()\n WHERE id = $1\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Uuid",- "TextArray",- "Int8"- ]- },- "nullable": []- },- "hash": "ff945bb9f03892418e5c80fa89048f731bbbe7ea1eef8dcbad0cc1eaf365cf75"-}diff --git a/Cargo.lock b/Cargo.lockindex 34d2995d8d..d5da8d53a8 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -5128,6 +5128,7 @@ dependencies = ["macro_config","macro_cors","macro_db_client",+ "macro_db_migrator","macro_entrypoint","macro_env","macro_env_var",diff --git a/crates/calendar_events/src/outbound/pg.rs b/crates/calendar_events/src/outbound/pg.rsindex d1e7fc761f..0c758140f2 100644--- a/crates/calendar_events/src/outbound/pg.rs+++ b/crates/calendar_events/src/outbound/pg.rs@@ -98,10 +98,10 @@ impl PgCalendarRepository {AND lease_expires_at > now()AND EXISTS (SELECT 1- FROM email_links link- WHERE link.id = calendar_backfill_jobs.email_link_id- AND link.google_grant_version = calendar_backfill_jobs.grant_version- AND link.google_granted_scopes @> $7::text[]+ FROM email_link_google_scopes scopes+ WHERE scopes.link_id = calendar_backfill_jobs.email_link_id+ AND scopes.grant_version = calendar_backfill_jobs.grant_version+ AND scopes.granted_scopes @> $7::text[])"#,key.job_id,@@ -132,10 +132,10 @@ impl PgCalendarRepository {AND lease_token IS NULLAND EXISTS (SELECT 1- FROM email_links link- WHERE link.id = calendar_backfill_jobs.email_link_id- AND link.google_grant_version = calendar_backfill_jobs.grant_version- AND link.google_granted_scopes @> $6::text[]+ FROM email_link_google_scopes scopes+ WHERE scopes.link_id = calendar_backfill_jobs.email_link_id+ AND scopes.grant_version = calendar_backfill_jobs.grant_version+ AND scopes.granted_scopes @> $6::text[])"#,key.job_id,@@ -224,12 +224,13 @@ impl PgCalendarRepository {GrantRow,r#"SELECT- macro_id,- email_address::text AS "email_address!",- google_granted_scopes,- google_grant_version- FROM email_links- WHERE id = $1+ l.macro_id,+ l.email_address::text AS "email_address!",+ COALESCE(g.granted_scopes, '{}') AS "granted_scopes!",+ COALESCE(g.grant_version, 0) AS "grant_version!"+ FROM email_links l+ LEFT JOIN email_link_google_scopes g ON g.link_id = l.id+ WHERE l.id = $1"#,email_link_id,)@@ -247,8 +248,8 @@ impl PgCalendarRepository {struct GrantRow {macro_id: String,email_address: String,- google_granted_scopes: Vec<String>,- google_grant_version: i64,+ granted_scopes: Vec<String>,+ grant_version: i64,}struct StoredCalendarRow {@@ -331,17 +332,20 @@ impl CalendarRepository for PgCalendarRepository {scopes: GoogleScopeSet,) -> Result<AppliedGoogleGrant, Report> {let mut tx = self.pool.begin().await.map_err(report)?;+ // The email_links row remains the grant serialization point. Every+ // production side-table write below occurs while holding this lock.let row = sqlx::query_as!(GrantRow,r#"SELECT- macro_id,- email_address::text AS "email_address!",- google_granted_scopes,- google_grant_version- FROM email_links- WHERE id = $1- FOR UPDATE+ l.macro_id,+ l.email_address::text AS "email_address!",+ COALESCE(g.granted_scopes, '{}') AS "granted_scopes!",+ COALESCE(g.grant_version, 0) AS "grant_version!"+ FROM email_links l+ LEFT JOIN email_link_google_scopes g ON g.link_id = l.id+ WHERE l.id = $1+ FOR UPDATE OF l"#,email_link_id,)@@ -349,32 +353,33 @@ impl CalendarRepository for PgCalendarRepository {.await.map_err(report)?;- let old_scopes = GoogleScopeSet::from_scopes(row.google_granted_scopes);+ let old_scopes = GoogleScopeSet::from_scopes(row.granted_scopes);let had_calendar_capability = old_scopes.has_calendar_capability();let changed = old_scopes != scopes;if !changed {let jobs = if scopes.has_calendar_capability() {- retry_failed_backfills_tx(&mut tx, email_link_id, row.google_grant_version).await?+ retry_failed_backfills_tx(&mut tx, email_link_id, row.grant_version).await?} else {Vec::new()};tx.commit().await.map_err(report)?;return Ok(AppliedGoogleGrant {- grant_version: row.google_grant_version,+ grant_version: row.grant_version,changed: false,jobs,});}- let grant_version = row.google_grant_version + 1;+ let grant_version = row.grant_version + 1;let granted_scopes = scopes.clone().into_vec();sqlx::query!(r#"- UPDATE email_links- SET google_granted_scopes = $2,- google_grant_version = $3,+ INSERT INTO email_link_google_scopes (link_id, granted_scopes, grant_version)+ VALUES ($1, $2, $3)+ ON CONFLICT (link_id) DO UPDATE+ SET granted_scopes = EXCLUDED.granted_scopes,+ grant_version = EXCLUDED.grant_version,updated_at = now()- WHERE id = $1"#,email_link_id,&granted_scopes,@@ -1049,15 +1054,15 @@ impl CalendarRepository for PgCalendarRepository {lease_token = NULL,lease_expires_at = NULL,updated_at = now()- FROM calendar_accounts account, email_links link+ FROM calendar_accounts account, email_link_google_scopes scopesWHERE job.email_link_id = $1AND job.email_link_id = account.email_link_idAND job.account_id = account.id- AND link.id = job.email_link_id+ AND scopes.link_id = job.email_link_idAND job.kind = 'google_calendar'AND job.status = 'complete'- AND job.grant_version = link.google_grant_version- AND link.google_granted_scopes @> $2::text[]+ AND job.grant_version = scopes.grant_version+ AND scopes.granted_scopes @> $2::text[]AND account.sync_status = 'ready'RETURNING job.id),@@ -1171,14 +1176,14 @@ impl GoogleCalendarSyncRepository for PgCalendarRepository {lease_token = NULL,lease_expires_at = NULL,updated_at = now()- FROM calendar_accounts account, email_links link+ FROM calendar_accounts account, email_link_google_scopes scopesWHERE job.email_link_id = account.email_link_idAND job.account_id = account.id- AND link.id = job.email_link_id+ AND scopes.link_id = job.email_link_idAND job.kind = 'google_calendar'AND job.status = 'complete'- AND job.grant_version = link.google_grant_version- AND link.google_granted_scopes @> $2::text[]+ AND job.grant_version = scopes.grant_version+ AND scopes.granted_scopes @> $2::text[]AND account.sync_status = 'ready'AND account.last_synced_at <= $1RETURNING job.id@@ -1220,10 +1225,10 @@ async fn fence_google_mutation_tx(AND job.lease_expires_at > now()AND EXISTS (SELECT 1- FROM email_links link- WHERE link.id = job.email_link_id- AND link.google_grant_version = job.grant_version- AND link.google_granted_scopes @> $5::text[]+ FROM email_link_google_scopes scopes+ WHERE scopes.link_id = job.email_link_id+ AND scopes.grant_version = job.grant_version+ AND scopes.granted_scopes @> $5::text[])AND EXISTS (SELECT 1@@ -1366,10 +1371,10 @@ impl CalendarBackfillRepository for PgCalendarRepository {AND kind = 'google_calendar'AND EXISTS (SELECT 1- FROM email_links link- WHERE link.id = calendar_backfill_jobs.email_link_id- AND link.google_grant_version = calendar_backfill_jobs.grant_version- AND link.google_granted_scopes @> $4::text[]+ FROM email_link_google_scopes scopes+ WHERE scopes.link_id = calendar_backfill_jobs.email_link_id+ AND scopes.grant_version = calendar_backfill_jobs.grant_version+ AND scopes.granted_scopes @> $4::text[])AND EXISTS (SELECT 1@@ -1487,10 +1492,10 @@ impl CalendarBackfillRepository for PgCalendarRepository {AND lease_expires_at > now()AND EXISTS (SELECT 1- FROM email_links link- WHERE link.id = calendar_backfill_jobs.email_link_id- AND link.google_grant_version = calendar_backfill_jobs.grant_version- AND link.google_granted_scopes @> $4::text[]+ FROM email_link_google_scopes scopes+ WHERE scopes.link_id = calendar_backfill_jobs.email_link_id+ AND scopes.grant_version = calendar_backfill_jobs.grant_version+ AND scopes.granted_scopes @> $4::text[])"#,key.job_id,@@ -1533,10 +1538,10 @@ impl CalendarBackfillRepository for PgCalendarRepository {AND lease_expires_at > now()AND EXISTS (SELECT 1- FROM email_links link- WHERE link.id = calendar_backfill_jobs.email_link_id- AND link.google_grant_version = calendar_backfill_jobs.grant_version- AND link.google_granted_scopes @> $4::text[]+ FROM email_link_google_scopes scopes+ WHERE scopes.link_id = calendar_backfill_jobs.email_link_id+ AND scopes.grant_version = calendar_backfill_jobs.grant_version+ AND scopes.granted_scopes @> $4::text[])"#,key.job_id,diff --git a/crates/calendar_events/src/outbound/pg/test.rs b/crates/calendar_events/src/outbound/pg/test.rsindex cd7556ecc2..77f99e5a12 100644--- a/crates/calendar_events/src/outbound/pg/test.rs+++ b/crates/calendar_events/src/outbound/pg/test.rs@@ -16,10 +16,12 @@ async fn persist_complete_grant(pool: &PgPool, link_id: Uuid, grant_version: i64let scopes = GOOGLE_CALENDAR_SCOPES.map(str::to_owned);sqlx::query!(r#"- UPDATE email_links- SET google_granted_scopes = $2,- google_grant_version = $3- WHERE id = $1+ INSERT INTO email_link_google_scopes (link_id, granted_scopes, grant_version)+ VALUES ($1, $2, $3)+ ON CONFLICT (link_id) DO UPDATE+ SET granted_scopes = EXCLUDED.granted_scopes,+ grant_version = EXCLUDED.grant_version,+ updated_at = now()"#,link_id,&scopes,@@ -162,6 +164,56 @@ fn timed_upsert(}}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn applying_grant_recreates_missing_side_table_state_from_version_zero(pool: PgPool) {+ let link_id = insert_link(&pool, "macro|[email protected]").await;+ let sentinel_updated_at = Utc.with_ymd_and_hms(2020, 1, 2, 3, 4, 5).unwrap();+ sqlx::query!(+ "UPDATE email_links SET updated_at = $2 WHERE id = $1",+ link_id,+ sentinel_updated_at,+ )+ .execute(&pool)+ .await+ .unwrap();+ sqlx::query!(+ "DELETE FROM email_link_google_scopes WHERE link_id = $1",+ link_id,+ )+ .execute(&pool)+ .await+ .unwrap();++ let grant = PgCalendarRepository::new(pool.clone())+ .apply_google_grant(link_id, complete_grant())+ .await+ .unwrap();+ assert!(grant.changed);+ assert_eq!(grant.grant_version, 1);++ let persisted = sqlx::query!(+ r#"+ SELECT+ l.updated_at AS "link_updated_at!",+ g.granted_scopes AS "side_scopes!",+ g.grant_version AS "side_version!"+ FROM email_links l+ JOIN email_link_google_scopes g ON g.link_id = l.id+ WHERE l.id = $1+ "#,+ link_id,+ )+ .fetch_one(&pool)+ .await+ .unwrap();+ assert_eq!(persisted.link_updated_at, sentinel_updated_at);+ assert_eq!(+ GoogleScopeSet::from_scopes(persisted.side_scopes),+ complete_grant()+ );+ assert_eq!(persisted.side_version, 1);+}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]async fn calendar_capability_transition_schedules_once_and_failed_jobs_can_retry(pool: PgPool) {let owner_id = "macro|[email protected]";diff --git a/crates/email_db_client/src/links/get.rs b/crates/email_db_client/src/links/get.rsindex 92b20c9721..00b0636b55 100644--- a/crates/email_db_client/src/links/get.rs+++ b/crates/email_db_client/src/links/get.rs@@ -173,8 +173,8 @@ pub struct InboxDetails {pub settings: service::settings::Settings,pub latest_backfill_status: Option<service::backfill::BackfillJobStatus>,pub photo_url: Option<String>,- /// The Google OAuth scopes recorded for the link's grant. Empty for links- /// connected before scope tracking existed (`google_grant_version = 0`).+ /// The Google OAuth scopes recorded for the link's grant. An empty vector+ /// represents either an absent grant-state row or the initial version-0 state.pub google_granted_scopes: Vec<String>,}@@ -225,23 +225,22 @@ pub async fn fetch_inbox_details_for_macro_id(s.signature,bj.status as "latest_backfill_status?: _",c.sfs_photo_url as "photo_url?",- l.google_granted_scopes as "google_granted_scopes!"+ COALESCE(g.granted_scopes, '{}') AS "google_granted_scopes!"FROM (SELECT el.id, el.macro_id, el.fusionauth_user_id, el.email_address,el.provider, el.is_sync_active, el.is_primary, el.needs_reauth,- el.last_sync_error_at, el.created_at, el.updated_at,- el.google_granted_scopes+ el.last_sync_error_at, el.created_at, el.updated_atFROM email_links elWHERE el.macro_id = $1UNIONSELECT el.id, el.macro_id, el.fusionauth_user_id, el.email_address,el.provider, el.is_sync_active, el.is_primary, el.needs_reauth,- el.last_sync_error_at, el.created_at, el.updated_at,- el.google_granted_scopes+ el.last_sync_error_at, el.created_at, el.updated_atFROM email_links elJOIN macro_user_links mul ON el.id = mul.link_idWHERE mul.primary_macro_id = $1) l+ LEFT JOIN email_link_google_scopes g ON g.link_id = l.idLEFT JOIN email_settings s ON s.link_id = l.idLEFT JOIN LATERAL (SELECT status FROM email_backfill_jobsdiff --git a/crates/email_db_client/src/links/get/test.rs b/crates/email_db_client/src/links/get/test.rsindex 9292508c8e..c201093548 100644--- a/crates/email_db_client/src/links/get/test.rs+++ b/crates/email_db_client/src/links/get/test.rs@@ -340,6 +340,51 @@ async fn fetch_inbox_details_joins_settings_backfill_and_photo(Ok(())}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn fetch_inbox_details_reads_google_scopes_from_side_table(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ insert_user(&pool, CHILD, "[email protected]").await;+ let (link_id, _, _) =+ insert_inbox_with_thread_and_message(&pool, CHILD, "[email protected]").await;+ let granted_scopes = vec![+ "https://www.googleapis.com/auth/calendar.readonly".to_owned(),+ "https://www.googleapis.com/auth/calendar.events".to_owned(),+ ];++ sqlx::query!(+ r#"+ INSERT INTO email_link_google_scopes (link_id, granted_scopes, grant_version)+ VALUES ($1, $2, 1)+ ON CONFLICT (link_id) DO UPDATE+ SET granted_scopes = EXCLUDED.granted_scopes,+ grant_version = EXCLUDED.grant_version,+ updated_at = now()+ "#,+ link_id,+ &granted_scopes,+ )+ .execute(&pool)+ .await?;++ let details = fetch_inbox_details_for_macro_id(&pool, ¯o_id(CHILD)).await?;+ assert_eq!(details.len(), 1);+ assert_eq!(details[0].google_granted_scopes, granted_scopes);++ sqlx::query!(+ "DELETE FROM email_link_google_scopes WHERE link_id = $1",+ link_id,+ )+ .execute(&pool)+ .await?;++ let details = fetch_inbox_details_for_macro_id(&pool, ¯o_id(CHILD)).await?;+ assert_eq!(details.len(), 1);+ assert!(details[0].google_granted_scopes.is_empty());++ Ok(())+}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]async fn fetch_inbox_details_includes_delegated_inbox_with_optional_fields_absent(pool: Pool<Postgres>,diff --git a/crates/macro_db_client/migrations/20260804194138_move_google_scopes_to_side_table.sql b/crates/macro_db_client/migrations/20260804194138_move_google_scopes_to_side_table.sqlnew file mode 100644index 0000000000..3d14ebdee0--- /dev/null+++ b/crates/macro_db_client/migrations/20260804194138_move_google_scopes_to_side_table.sql@@ -0,0 +1,17 @@+-- This migration is intentionally irreversible: the legacy grant columns are+-- removed after their values are copied to the side table.+CREATE TABLE email_link_google_scopes (+ link_id uuid PRIMARY KEY REFERENCES email_links(id) ON DELETE CASCADE,+ granted_scopes text[] NOT NULL DEFAULT '{}',+ grant_version bigint NOT NULL DEFAULT 0 CHECK (grant_version >= 0),+ created_at timestamptz NOT NULL DEFAULT now(),+ updated_at timestamptz NOT NULL DEFAULT now()+);++INSERT INTO email_link_google_scopes (link_id, granted_scopes, grant_version)+SELECT id, google_granted_scopes, google_grant_version+FROM email_links;++ALTER TABLE email_links+ DROP COLUMN google_granted_scopes,+ DROP COLUMN google_grant_version;diff --git a/services/email_service/Cargo.toml b/services/email_service/Cargo.tomlindex 8b3f78c2b5..62b60ed9af 100644--- a/services/email_service/Cargo.toml+++ b/services/email_service/Cargo.toml@@ -185,3 +185,4 @@ workspace-hack = { version = "0.1", path = "../../crates/workspace-hack" }[dev-dependencies]macro_auth = { path = "../../crates/macro_auth", features = ["testing"] }+macro_db_migrator = { path = "../../crates/macro_db_migrator" }diff --git a/services/email_service/src/api/email/init.rs b/services/email_service/src/api/email/init.rsindex b78a71fae9..2352d0d4cc 100644--- a/services/email_service/src/api/email/init.rs+++ b/services/email_service/src/api/email/init.rs@@ -744,6 +744,25 @@ async fn init_user(.into_response())}+/// Returns false when the link is missing or the lookup fails so token discovery remains+/// best-effort on the authentication path.+async fn has_unrecorded_google_grant(db: &sqlx::PgPool, link_id: Uuid) -> bool {+ sqlx::query_scalar!(+ r#"+ SELECT COALESCE(g.grant_version, 0) = 0 AS "unrecorded!"+ FROM email_links l+ LEFT JOIN email_link_google_scopes g ON g.link_id = l.id+ WHERE l.id = $1+ "#,+ link_id,+ )+ .fetch_optional(db)+ .await+ .ok()+ .flatten()+ .unwrap_or(false)+}+/// Init runs on every authentication, including first-login SSO where/// FusionAuth performs the token exchange and the granted scopes never reach/// the link flow. When the link has no recorded grant generation, discover@@ -751,16 +770,7 @@ async fn init_user(/// SSO-only users still receive calendar sync. Best-effort: a failure here/// must never fail authentication, and the next init retries it.async fn apply_grant_discovered_from_token(ctx: &ApiContext, link: &link::Link) {- let unrecorded = sqlx::query_scalar!(- r#"SELECT google_grant_version = 0 AS "unrecorded!" FROM email_links WHERE id = $1"#,- link.id,- )- .fetch_optional(&ctx.db)- .await- .ok()- .flatten()- .unwrap_or(false);- if !unrecorded {+ if !has_unrecorded_google_grant(&ctx.db, link.id).await {return;}let token = match fetch_gmail_token_for_email(diff --git a/services/email_service/src/api/email/init/test.rs b/services/email_service/src/api/email/init/test.rsindex ebcf2f1284..9903ffa605 100644--- a/services/email_service/src/api/email/init/test.rs+++ b/services/email_service/src/api/email/init/test.rs@@ -1,5 +1,27 @@use super::*;use anyhow::anyhow;+use macro_db_migrator::MACRO_DB_MIGRATIONS;+use sqlx::PgPool;++async fn insert_email_link(pool: &PgPool) -> Uuid {+ let link_id = Uuid::now_v7();+ let email_address = format!("sso-grant-{link_id}@example.com");+ sqlx::query!(+ r#"+ INSERT INTO email_links (+ id, macro_id, fusionauth_user_id, email_address, provider+ )+ VALUES ($1, $2, $2, $3, 'GMAIL')+ "#,+ link_id,+ "macro|[email protected]",+ email_address,+ )+ .execute(pool)+ .await+ .unwrap();+ link_id+}async fn body_json(response: Response) -> serde_json::Value {let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)@@ -8,6 +30,46 @@ async fn body_json(response: Response) -> serde_json::Value {serde_json::from_slice(&bytes).unwrap()}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn zero_grant_is_unrecorded(pool: PgPool) {+ let link_id = insert_email_link(&pool).await;+ sqlx::query!(+ "INSERT INTO email_link_google_scopes (link_id) VALUES ($1)",+ link_id,+ )+ .execute(&pool)+ .await+ .unwrap();++ assert!(has_unrecorded_google_grant(&pool, link_id).await);+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn missing_side_table_state_is_unrecorded(pool: PgPool) {+ let link_id = insert_email_link(&pool).await;++ assert!(has_unrecorded_google_grant(&pool, link_id).await);+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn recorded_side_table_grant_is_not_unrecorded(pool: PgPool) {+ let link_id = insert_email_link(&pool).await;+ sqlx::query!(+ "INSERT INTO email_link_google_scopes (link_id, grant_version) VALUES ($1, 1)",+ link_id,+ )+ .execute(&pool)+ .await+ .unwrap();++ assert!(!has_unrecorded_google_grant(&pool, link_id).await);+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn missing_email_link_is_not_unrecorded(pool: PgPool) {+ assert!(!has_unrecorded_google_grant(&pool, Uuid::now_v7()).await);+}+#[tokio::test]async fn no_gmail_grant_is_a_coded_400() {let response = InitError::NoGmailGrant.into_response();