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(granola): delete Pipedream connections with their user (#9219)
Eric Hayes92db8ff99b28
diff --git a/crates/macro_db_client/migrations/20261030154833_pipedream_connections_cascade_with_user.sql b/crates/macro_db_client/migrations/20261030154833_pipedream_connections_cascade_with_user.sqlnew file mode 100644index 0000000000..731402a01c--- /dev/null+++ b/crates/macro_db_client/migrations/20261030154833_pipedream_connections_cascade_with_user.sql@@ -0,0 +1,11 @@+-- Deleted users left enabled Pipedream connections behind, and Granola sync+-- retried setting them up forever. Connections now follow their user, like+-- granola_sync_connections. The table is small; the lock on "User" is brief.+SET LOCAL lock_timeout = '1s';++DELETE FROM pipedream_mcp_connections c+WHERE NOT EXISTS (SELECT 1 FROM "User" u WHERE u.id = c.user_id);++ALTER TABLE pipedream_mcp_connections+ ADD CONSTRAINT pipedream_mcp_connections_user_id_fkey+ FOREIGN KEY (user_id) REFERENCES "User"(id) ON UPDATE CASCADE ON DELETE CASCADE;diff --git a/crates/pipedream_mcp/fixtures/users.sql b/crates/pipedream_mcp/fixtures/users.sqlnew file mode 100644index 0000000000..5e489859f3--- /dev/null+++ b/crates/pipedream_mcp/fixtures/users.sql@@ -0,0 +1,4 @@+INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES+('019a0000-0000-7000-8000-000000000011', 'pipedream-owner', '[email protected]', 'pipedream-owner');+INSERT INTO "User" (id, email, macro_user_id) VALUES+('macro|[email protected]', '[email protected]', '019a0000-0000-7000-8000-000000000011');diff --git a/crates/pipedream_mcp/src/outbound/pg_connection_repo/test.rs b/crates/pipedream_mcp/src/outbound/pg_connection_repo/test.rsindex 4a78c74629..1fe4b269c9 100644--- a/crates/pipedream_mcp/src/outbound/pg_connection_repo/test.rs+++ b/crates/pipedream_mcp/src/outbound/pg_connection_repo/test.rs@@ -1,11 +1,11 @@use super::*;use macro_db_migrator::MACRO_DB_MIGRATIONS;+const USER: &str = "macro|[email protected]";+fn connection(app_slug: &str) -> PipedreamConnection {PipedreamConnection {- user_id: MacroUserIdStr::parse_from_str("macro|[email protected]")- .unwrap()- .into_owned(),+ user_id: MacroUserIdStr::parse_from_str(USER).unwrap().into_owned(),app_slug: app_slug.to_owned(),server_name: app_slug.to_owned(),account_id: format!("apn_{app_slug}"),@@ -16,7 +16,10 @@ fn connection(app_slug: &str) -> PipedreamConnection {/// `sentry` is overlaid by the seed and `google_sheets` is not: a Pipedream/// row for an overlaid app is invisible to every read, so no consumer can/// reach the app through Pipedream.-#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("users"))+)]async fn reads_never_return_connections_to_overlaid_apps(pool: PgPool) {let repo = PgConnectionRepo::new(pool);let user = connection("sentry").user_id;@@ -53,3 +56,46 @@ async fn reads_never_return_connections_to_overlaid_apps(pool: PgPool) {.any(|o| o.app_slug == "sentry"));}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("users"))+)]+async fn connections_are_deleted_with_their_user(pool: PgPool) {+ let repo = PgConnectionRepo::new(pool.clone());+ repo.save(&connection("granola")).await.unwrap();+ sqlx::query!(r#"DELETE FROM "User" WHERE id = $1"#, USER)+ .execute(&pool)+ .await+ .unwrap();+ // Background sync lists enabled connections; an orphan would be set up forever.+ assert!(+ repo.list_enabled_for_app("granola")+ .await+ .unwrap()+ .is_empty()+ );+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("users"))+)]+async fn connections_follow_a_renamed_user(pool: PgPool) {+ let repo = PgConnectionRepo::new(pool.clone());+ repo.save(&connection("granola")).await.unwrap();+ let renamed = "macro|[email protected]";+ sqlx::query!(r#"UPDATE "User" SET id = $1 WHERE id = $2"#, renamed, USER)+ .execute(&pool)+ .await+ .unwrap();+ let listed = repo.list_enabled_for_app("granola").await.unwrap();+ assert_eq!(listed.len(), 1);+ assert_eq!(listed[0].user_id.as_ref(), renamed);+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn connections_require_an_existing_user(pool: PgPool) {+ let repo = PgConnectionRepo::new(pool);+ assert!(repo.save(&connection("granola")).await.is_err());+}