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(favorites): use uuid comparison for channel/message/email joins (#4534)
teo9c20617210a7
diff --git a/rust/cloud-storage/.sqlx/query-125c32f6efd3f81dbd4a01e20aeb668399f153c44c0ac147af1612cac86f1360.json b/rust/cloud-storage/.sqlx/query-125c32f6efd3f81dbd4a01e20aeb668399f153c44c0ac147af1612cac86f1360.jsonnew file mode 100644index 0000000000..318a70d683--- /dev/null+++ b/rust/cloud-storage/.sqlx/query-125c32f6efd3f81dbd4a01e20aeb668399f153c44c0ac147af1612cac86f1360.json@@ -0,0 +1,70 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT\n f.entity_type as \"entity_type!\",\n f.entity_id as \"entity_id!\",\n f.sort_order as \"sort_order!\",\n f.created_at as \"created_at!\",\n CASE f.entity_type\n WHEN 'document' THEN d.name\n WHEN 'chat' THEN c.name\n WHEN 'project' THEN p.name\n WHEN 'channel' THEN ch.name::text\n WHEN 'email_thread' THEN em.subject\n END as \"name?\",\n d.\"fileType\" as \"file_type?\",\n dt.sub_type::text as \"document_sub_type?\",\n ch.channel_type::text as \"channel_type?\",\n cm.channel_id::text as \"channel_id?\"\n FROM favorite f\n -- The comms/email tables key on uuid while favorite.entity_id is\n -- text. Compare in uuid (casting the favorite side) so their\n -- primary-key indexes are usable; casting the table side to text\n -- forced a full scan of each table on every listing. The regex\n -- guard keeps non-uuid entity_ids from failing the cast: they\n -- yield NULL and simply don't hydrate.\n CROSS JOIN LATERAL (\n SELECT CASE\n WHEN f.entity_id ~ '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$'\n THEN f.entity_id::uuid\n END AS entity_uuid\n ) fid\n LEFT JOIN \"Document\" d ON f.entity_type = 'document' AND d.id = f.entity_id\n LEFT JOIN document_sub_type dt ON f.entity_type = 'document' AND dt.document_id = f.entity_id\n LEFT JOIN \"Chat\" c ON f.entity_type = 'chat' AND c.id = f.entity_id\n LEFT JOIN \"Project\" p ON f.entity_type = 'project' AND p.id = f.entity_id\n LEFT JOIN comms_channels ch ON f.entity_type = 'channel' AND ch.id = fid.entity_uuid\n LEFT JOIN comms_messages cm ON f.entity_type = 'channel_message' AND cm.id = fid.entity_uuid\n LEFT JOIN email_threads et ON f.entity_type = 'email_thread' AND et.id = fid.entity_uuid\n LEFT JOIN LATERAL (\n SELECT m.subject\n FROM email_messages m\n WHERE m.thread_id = et.id AND m.is_draft = false\n ORDER BY m.internal_date_ts DESC NULLS LAST\n LIMIT 1\n ) em ON f.entity_type = 'email_thread'\n WHERE f.user_id = $1\n AND (f.entity_type <> 'document' OR (d.id IS NOT NULL AND d.\"deletedAt\" IS NULL))\n AND (f.entity_type <> 'chat' OR (c.id IS NOT NULL AND c.\"deletedAt\" IS NULL))\n AND (f.entity_type <> 'project' OR (p.id IS NOT NULL AND p.\"deletedAt\" IS NULL))\n ORDER BY f.sort_order ASC, f.created_at ASC\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "entity_type!",+ "type_info": "Text"+ },+ {+ "ordinal": 1,+ "name": "entity_id!",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "sort_order!",+ "type_info": "Float8"+ },+ {+ "ordinal": 3,+ "name": "created_at!",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 4,+ "name": "name?",+ "type_info": "Text"+ },+ {+ "ordinal": 5,+ "name": "file_type?",+ "type_info": "Text"+ },+ {+ "ordinal": 6,+ "name": "document_sub_type?",+ "type_info": "Text"+ },+ {+ "ordinal": 7,+ "name": "channel_type?",+ "type_info": "Text"+ },+ {+ "ordinal": 8,+ "name": "channel_id?",+ "type_info": "Text"+ }+ ],+ "parameters": {+ "Left": [+ "Text"+ ]+ },+ "nullable": [+ false,+ false,+ false,+ false,+ null,+ true,+ null,+ null,+ null+ ]+ },+ "hash": "125c32f6efd3f81dbd4a01e20aeb668399f153c44c0ac147af1612cac86f1360"+}diff --git a/rust/cloud-storage/.sqlx/query-36a185a68b67c421880ace1b043b023106f3890102b5a71a5172d8fd401fba9c.json b/rust/cloud-storage/.sqlx/query-36a185a68b67c421880ace1b043b023106f3890102b5a71a5172d8fd401fba9c.jsondeleted file mode 100644index 131bd65561..0000000000--- a/rust/cloud-storage/.sqlx/query-36a185a68b67c421880ace1b043b023106f3890102b5a71a5172d8fd401fba9c.json+++ /dev/null@@ -1,70 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT\n f.entity_type as \"entity_type!\",\n f.entity_id as \"entity_id!\",\n f.sort_order as \"sort_order!\",\n f.created_at as \"created_at!\",\n CASE f.entity_type\n WHEN 'document' THEN d.name\n WHEN 'chat' THEN c.name\n WHEN 'project' THEN p.name\n WHEN 'channel' THEN ch.name::text\n WHEN 'email_thread' THEN em.subject\n END as \"name?\",\n d.\"fileType\" as \"file_type?\",\n dt.sub_type::text as \"document_sub_type?\",\n ch.channel_type::text as \"channel_type?\",\n cm.channel_id::text as \"channel_id?\"\n FROM favorite f\n LEFT JOIN \"Document\" d ON f.entity_type = 'document' AND d.id = f.entity_id\n LEFT JOIN document_sub_type dt ON f.entity_type = 'document' AND dt.document_id = f.entity_id\n LEFT JOIN \"Chat\" c ON f.entity_type = 'chat' AND c.id = f.entity_id\n LEFT JOIN \"Project\" p ON f.entity_type = 'project' AND p.id = f.entity_id\n LEFT JOIN comms_channels ch ON f.entity_type = 'channel' AND ch.id::text = f.entity_id\n LEFT JOIN comms_messages cm ON f.entity_type = 'channel_message' AND cm.id::text = f.entity_id\n LEFT JOIN email_threads et ON f.entity_type = 'email_thread' AND et.id::text = f.entity_id\n LEFT JOIN LATERAL (\n SELECT m.subject\n FROM email_messages m\n WHERE m.thread_id = et.id AND m.is_draft = false\n ORDER BY m.internal_date_ts DESC NULLS LAST\n LIMIT 1\n ) em ON f.entity_type = 'email_thread'\n WHERE f.user_id = $1\n AND (f.entity_type <> 'document' OR (d.id IS NOT NULL AND d.\"deletedAt\" IS NULL))\n AND (f.entity_type <> 'chat' OR (c.id IS NOT NULL AND c.\"deletedAt\" IS NULL))\n AND (f.entity_type <> 'project' OR (p.id IS NOT NULL AND p.\"deletedAt\" IS NULL))\n ORDER BY f.sort_order ASC, f.created_at ASC\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "entity_type!",- "type_info": "Text"- },- {- "ordinal": 1,- "name": "entity_id!",- "type_info": "Text"- },- {- "ordinal": 2,- "name": "sort_order!",- "type_info": "Float8"- },- {- "ordinal": 3,- "name": "created_at!",- "type_info": "Timestamptz"- },- {- "ordinal": 4,- "name": "name?",- "type_info": "Text"- },- {- "ordinal": 5,- "name": "file_type?",- "type_info": "Text"- },- {- "ordinal": 6,- "name": "document_sub_type?",- "type_info": "Text"- },- {- "ordinal": 7,- "name": "channel_type?",- "type_info": "Text"- },- {- "ordinal": 8,- "name": "channel_id?",- "type_info": "Text"- }- ],- "parameters": {- "Left": [- "Text"- ]- },- "nullable": [- false,- false,- false,- false,- null,- true,- null,- null,- null- ]- },- "hash": "36a185a68b67c421880ace1b043b023106f3890102b5a71a5172d8fd401fba9c"-}diff --git a/rust/cloud-storage/favorites/src/outbound/pg_favorites_repo.rs b/rust/cloud-storage/favorites/src/outbound/pg_favorites_repo.rsindex 6aeeab3c78..242032dfe3 100644--- a/rust/cloud-storage/favorites/src/outbound/pg_favorites_repo.rs+++ b/rust/cloud-storage/favorites/src/outbound/pg_favorites_repo.rs@@ -147,13 +147,25 @@ impl FavoritesRepo for PgFavoritesRepo {ch.channel_type::text as "channel_type?",cm.channel_id::text as "channel_id?"FROM favorite f+ -- The comms/email tables key on uuid while favorite.entity_id is+ -- text. Compare in uuid (casting the favorite side) so their+ -- primary-key indexes are usable; casting the table side to text+ -- forced a full scan of each table on every listing. The regex+ -- guard keeps non-uuid entity_ids from failing the cast: they+ -- yield NULL and simply don't hydrate.+ CROSS JOIN LATERAL (+ SELECT CASE+ WHEN f.entity_id ~ '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$'+ THEN f.entity_id::uuid+ END AS entity_uuid+ ) fidLEFT JOIN "Document" d ON f.entity_type = 'document' AND d.id = f.entity_idLEFT JOIN document_sub_type dt ON f.entity_type = 'document' AND dt.document_id = f.entity_idLEFT JOIN "Chat" c ON f.entity_type = 'chat' AND c.id = f.entity_idLEFT JOIN "Project" p ON f.entity_type = 'project' AND p.id = f.entity_id- LEFT JOIN comms_channels ch ON f.entity_type = 'channel' AND ch.id::text = f.entity_id- LEFT JOIN comms_messages cm ON f.entity_type = 'channel_message' AND cm.id::text = f.entity_id- LEFT JOIN email_threads et ON f.entity_type = 'email_thread' AND et.id::text = f.entity_id+ LEFT JOIN comms_channels ch ON f.entity_type = 'channel' AND ch.id = fid.entity_uuid+ LEFT JOIN comms_messages cm ON f.entity_type = 'channel_message' AND cm.id = fid.entity_uuid+ LEFT JOIN email_threads et ON f.entity_type = 'email_thread' AND et.id = fid.entity_uuidLEFT JOIN LATERAL (SELECT m.subjectFROM email_messages mdiff --git a/rust/cloud-storage/favorites/src/outbound/pg_favorites_repo/tests.rs b/rust/cloud-storage/favorites/src/outbound/pg_favorites_repo/tests.rsindex fa67a4637c..3802aab16d 100644--- a/rust/cloud-storage/favorites/src/outbound/pg_favorites_repo/tests.rs+++ b/rust/cloud-storage/favorites/src/outbound/pg_favorites_repo/tests.rs@@ -155,6 +155,49 @@ async fn list_favorites_hydrates_names_and_skips_deleted(pool: PgPool) {assert_eq!(favorites[1].name, None);}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn list_favorites_hydrates_uuid_keyed_entities(pool: PgPool) {+ insert_user(&pool, USER_A).await;+ let channel_id = Uuid::now_v7().to_string();+ sqlx::query(+ r#"INSERT INTO comms_channels (id, name, channel_type, owner_id) VALUES ($1::uuid, 'Eng', 'public', $2)"#,+ )+ .bind(&channel_id)+ .bind(USER_A)+ .execute(&pool)+ .await+ .expect("channel should insert");++ let repo = PgFavoritesRepo::new(pool);+ repo.add_favorite(+ &user(USER_A),+ &EntityType::Channel.with_entity_str(&channel_id),+ )+ .await+ .expect("channel favorite should insert");+ // A favorite whose entity_id is not a valid uuid must not break the+ // listing (the uuid tables are joined on a casted entity_id); it lists+ // unhydrated.+ repo.add_favorite(+ &user(USER_A),+ &EntityType::Channel.with_entity_str("not-a-uuid"),+ )+ .await+ .expect("non-uuid channel favorite should insert");++ let favorites = repo+ .list_favorites(&user(USER_A))+ .await+ .expect("favorites should list");+ assert_eq!(favorites.len(), 2);+ assert_eq!(favorites[0].entity_id, channel_id);+ assert_eq!(favorites[0].name.as_deref(), Some("Eng"));+ assert_eq!(favorites[0].channel_type.as_deref(), Some("public"));+ assert_eq!(favorites[1].entity_id, "not-a-uuid");+ assert_eq!(favorites[1].name, None);+ assert_eq!(favorites[1].channel_type, None);+}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]async fn remove_favorite_by_entity_scopes_to_user(pool: PgPool) {insert_user(&pool, USER_A).await;