Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

chore(db): remove UserItemAccess table (#4548)

Will Hutchinson50cdad0ef1f8
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index 0901d82f2b..e51c4b391d 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -423,14 +423,6 @@
"prompt",
"workspace-hack"
],
- "backfill_entity_access": [
- "backfill_entity_access",
- "macro_entrypoint",
- "macro_env",
- "macro_env_var",
- "macro_uuid",
- "workspace-hack"
- ],
"bot_id": [
"bot_id",
"cowlike",
diff --git a/rust/cloud-storage/.sqlx/query-f08b1a5fbea1fba7b91975fdcaa5c5adde2d9bc89eece2cbf17133b7e5f6d6d9.json b/rust/cloud-storage/.sqlx/query-4d75ecd9c8d6f98881d44d88d10ebc20de426c8aad90b680cc8ae351118e5495.json
similarity index 85%
rename from rust/cloud-storage/.sqlx/query-f08b1a5fbea1fba7b91975fdcaa5c5adde2d9bc89eece2cbf17133b7e5f6d6d9.json
rename to rust/cloud-storage/.sqlx/query-4d75ecd9c8d6f98881d44d88d10ebc20de426c8aad90b680cc8ae351118e5495.json
index 8372c988bd..067614a699 100644
--- a/rust/cloud-storage/.sqlx/query-f08b1a5fbea1fba7b91975fdcaa5c5adde2d9bc89eece2cbf17133b7e5f6d6d9.json
+++ b/rust/cloud-storage/.sqlx/query-4d75ecd9c8d6f98881d44d88d10ebc20de426c8aad90b680cc8ae351118e5495.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\nSELECT\n ep.id as entity_property_id,\n ep.entity_id,\n ep.entity_type as \"entity_type: EntityType\",\n ep.property_definition_id,\n ep.values as \"values: sqlx::types::JsonValue\",\n ep.created_at as entity_property_created_at,\n ep.updated_at as entity_property_updated_at,\n pd.team_id as definition_team_id,\n pd.user_id as definition_user_id,\n pd.display_name,\n pd.data_type as \"data_type: DataType\",\n pd.is_multi_select,\n pd.specific_entity_type as \"specific_entity_type: Option<EntityType>\",\n pd.created_at as definition_created_at,\n pd.updated_at as definition_updated_at,\n pd.is_system as definition_is_system\nFROM entity_properties ep\nINNER JOIN property_definitions pd ON ep.property_definition_id = pd.id\nWHERE (ep.entity_id, ep.entity_type) IN (\n SELECT * FROM UNNEST($1::TEXT[], $2::property_entity_type[])\n)\nAND pd.id = ANY($3::UUID[])\n ",
+ "query": "\nSELECT\n ep.id as entity_property_id,\n ep.entity_id,\n ep.entity_type as \"entity_type: EntityType\",\n ep.property_definition_id,\n ep.values as \"values: sqlx::types::JsonValue\",\n ep.created_at as entity_property_created_at,\n ep.updated_at as entity_property_updated_at,\n pd.team_id as definition_team_id,\n pd.user_id as definition_user_id,\n pd.display_name,\n pd.data_type as \"data_type: DataType\",\n pd.is_multi_select,\n pd.specific_entity_type as \"specific_entity_type: Option<EntityType>\",\n pd.created_at as definition_created_at,\n pd.updated_at as definition_updated_at,\n pd.is_system as definition_is_system\nFROM entity_properties ep\nINNER JOIN property_definitions pd ON ep.property_definition_id = pd.id\nWHERE (ep.entity_id, ep.entity_type) IN (\n SELECT * FROM UNNEST($1::TEXT[], $2::property_entity_type[])\n)\nAND (\n pd.id = ANY($3::UUID[])\n OR (\n $4::text IS NOT NULL\n AND pd.data_type = $5\n AND (\n pd.user_id = $4\n OR pd.team_id IN (SELECT tu.team_id FROM team_user tu WHERE tu.user_id = $4)\n )\n )\n)\n ",
"describe": {
"columns": [
{
@@ -160,7 +160,26 @@
}
}
},
- "UuidArray"
+ "UuidArray",
+ "Text",
+ {
+ "Custom": {
+ "name": "property_data_type",
+ "kind": {
+ "Enum": [
+ "BOOLEAN",
+ "DATE",
+ "NUMBER",
+ "STRING",
+ "SELECT_NUMBER",
+ "SELECT_STRING",
+ "ENTITY",
+ "LINK",
+ "TAG"
+ ]
+ }
+ }
+ }
]
},
"nullable": [
@@ -182,5 +201,5 @@
false
]
},
- "hash": "f08b1a5fbea1fba7b91975fdcaa5c5adde2d9bc89eece2cbf17133b7e5f6d6d9"
+ "hash": "4d75ecd9c8d6f98881d44d88d10ebc20de426c8aad90b680cc8ae351118e5495"
}
diff --git a/rust/cloud-storage/.sqlx/query-afff07a12eda767f4facb034315ce7e3d89db508f08afe5c547e981a875e9801.json b/rust/cloud-storage/.sqlx/query-afff07a12eda767f4facb034315ce7e3d89db508f08afe5c547e981a875e9801.json
deleted file mode 100644
index b56a60c64f..0000000000
--- a/rust/cloud-storage/.sqlx/query-afff07a12eda767f4facb034315ce7e3d89db508f08afe5c547e981a875e9801.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT cp.channel_id::text FROM comms_channel_participants cp\n WHERE cp.user_id = $1 AND cp.left_at IS NULL\n UNION ALL\n SELECT t.team_id::text FROM team_user t\n WHERE t.user_id = $1\n UNION ALL\n SELECT $1\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "channel_id",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Text"
- ]
- },
- "nullable": [
- null
- ]
- },
- "hash": "afff07a12eda767f4facb034315ce7e3d89db508f08afe5c547e981a875e9801"
-}
diff --git a/rust/cloud-storage/.sqlx/query-ba49dfcd0a3ab834f9e61f27a0a80174e549103ee63f03d30fe76857c8803b6a.json b/rust/cloud-storage/.sqlx/query-ba49dfcd0a3ab834f9e61f27a0a80174e549103ee63f03d30fe76857c8803b6a.json
deleted file mode 100644
index 6dd63ef404..0000000000
--- a/rust/cloud-storage/.sqlx/query-ba49dfcd0a3ab834f9e61f27a0a80174e549103ee63f03d30fe76857c8803b6a.json
+++ /dev/null
@@ -1,28 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH RECURSIVE ProjectHierarchy AS (\n -- Find direct user access to projects first as starting point\n SELECT\n p.id,\n uia.access_level\n FROM \"Project\" p\n JOIN \"UserItemAccess\" uia ON p.id = uia.item_id AND uia.item_type = 'project'\n WHERE uia.user_id = $1 AND p.\"deletedAt\" IS NULL\n\n UNION ALL\n\n -- Then walk down the project tree and grab child projects, keeping parent's access\n SELECT\n p.id,\n ph.access_level\n FROM \"Project\" p\n JOIN ProjectHierarchy ph ON p.\"parentId\" = ph.id\n WHERE p.\"deletedAt\" IS NULL\n ),\n -- Now build up all the ways a user can have access to stuff\n AllAccessGrants AS (\n -- Explicit access to items via UserItemAccess table\n SELECT uia.item_id, uia.item_type, uia.access_level\n FROM \"UserItemAccess\" uia\n -- We join to each table to check its \"deletedAt\" status.\n -- This is more explicit and robust than using subqueries.\n LEFT JOIN \"Document\" d ON uia.item_type = 'document' AND uia.item_id = d.id\n LEFT JOIN \"Chat\" c ON uia.item_type = 'chat' AND uia.item_id = c.id\n LEFT JOIN \"Project\" p ON uia.item_type = 'project' AND uia.item_id = p.id\n WHERE uia.user_id = $1\n -- Rule: The item must not be deleted.\n AND (\n (uia.item_type = 'document' AND d.\"deletedAt\" IS NULL) OR\n (uia.item_type = 'chat' AND c.\"deletedAt\" IS NULL) OR\n (uia.item_type = 'project' AND p.\"deletedAt\" IS NULL)\n )\n\n -- The rest of the unions are to get implicit access to items via project access\n UNION ALL\n\n -- Access to docs in visible projects\n SELECT\n d.id AS item_id,\n 'document' AS item_type,\n ph.access_level\n FROM \"Document\" d\n JOIN ProjectHierarchy ph ON d.\"projectId\" = ph.id\n WHERE d.\"projectId\" IS NOT NULL AND d.\"deletedAt\" IS NULL\n\n UNION ALL\n\n -- Access to chats in visible projects\n SELECT\n c.id AS item_id,\n 'chat' AS item_type,\n ph.access_level\n FROM \"Chat\" c\n JOIN ProjectHierarchy ph ON c.\"projectId\" = ph.id\n WHERE c.\"projectId\" IS NOT NULL AND c.\"deletedAt\" IS NULL\n\n UNION ALL\n\n -- Include the projects we found earlier\n SELECT\n ph.id AS item_id,\n 'project' AS item_type,\n ph.access_level\n FROM ProjectHierarchy ph\n ),\n UserAccessibleItems AS (\n SELECT\n item_id,\n item_type\n FROM AllAccessGrants\n GROUP BY item_id, item_type\n )\n SELECT item_id as \"item_id!\", item_type as \"item_type!\" FROM UserAccessibleItems\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "item_id!",
- "type_info": "Text"
- },
- {
- "ordinal": 1,
- "name": "item_type!",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Text"
- ]
- },
- "nullable": [
- null,
- null
- ]
- },
- "hash": "ba49dfcd0a3ab834f9e61f27a0a80174e549103ee63f03d30fe76857c8803b6a"
-}
diff --git a/rust/cloud-storage/.sqlx/query-f9f132f209b5a22522df5c6b9ae936efa63645abc9b36965f9e8daead43b7adc.json b/rust/cloud-storage/.sqlx/query-f9f132f209b5a22522df5c6b9ae936efa63645abc9b36965f9e8daead43b7adc.json
deleted file mode 100644
index dd2fb46466..0000000000
--- a/rust/cloud-storage/.sqlx/query-f9f132f209b5a22522df5c6b9ae936efa63645abc9b36965f9e8daead43b7adc.json
+++ /dev/null
@@ -1,28 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT DISTINCT\n ea.entity_id,\n ea.entity_type\n FROM entity_access ea\n WHERE ea.source_id = ANY($1)\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "entity_id",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "entity_type",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "TextArray"
- ]
- },
- "nullable": [
- false,
- false
- ]
- },
- "hash": "f9f132f209b5a22522df5c6b9ae936efa63645abc9b36965f9e8daead43b7adc"
-}
diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lock
index 71ce571198..e6cdc9cb9e 100644
--- a/rust/cloud-storage/Cargo.lock
+++ b/rust/cloud-storage/Cargo.lock
@@ -1847,20 +1847,6 @@ dependencies = [
"syn 2.0.117",
]
-[[package]]
-name = "backfill_entity_access"
-version = "0.1.0"
-dependencies = [
- "anyhow",
- "macro_entrypoint",
- "macro_env_var",
- "macro_uuid",
- "sqlx",
- "tokio",
- "tracing",
- "workspace-hack",
-]
-
[[package]]
name = "backoff"
version = "0.4.0"
diff --git a/rust/cloud-storage/Cargo.toml b/rust/cloud-storage/Cargo.toml
index e221ab1ae8..4d1739a548 100644
--- a/rust/cloud-storage/Cargo.toml
+++ b/rust/cloud-storage/Cargo.toml
@@ -7,7 +7,6 @@ members = [
"ai_projections_refresh_handler",
"ai_usage",
"authentication_service",
- "backfill_entity_access",
"bot_id",
"bots",
"call_recording_preview_handler",
diff --git a/rust/cloud-storage/backfill_entity_access/Cargo.toml b/rust/cloud-storage/backfill_entity_access/Cargo.toml
deleted file mode 100644
index 90bd4ae526..0000000000
--- a/rust/cloud-storage/backfill_entity_access/Cargo.toml
+++ /dev/null
@@ -1,15 +0,0 @@
-[package]
-edition = "2024"
-name = "backfill_entity_access"
-publish = false
-version = "0.1.0"
-
-[dependencies]
-anyhow = { workspace = true }
-macro_entrypoint = { path = "../macro_entrypoint" }
-macro_env_var = { path = "../macro_env_var" }
-macro_uuid = { path = "../macro_uuid" }
-sqlx = { workspace = true }
-tokio = { workspace = true }
-tracing = { workspace = true }
-workspace-hack = { version = "0.1", path = "../workspace-hack" }
diff --git a/rust/cloud-storage/backfill_entity_access/justfile b/rust/cloud-storage/backfill_entity_access/justfile
deleted file mode 100644
index 38c004ab6d..0000000000
--- a/rust/cloud-storage/backfill_entity_access/justfile
+++ /dev/null
@@ -1,9 +0,0 @@
-import '../database.just'
-
-mod sqlx '../sqlx.just'
-
-prepare_db:
- just sqlx::prepare_db {{ DATABASE_URL }}
-
-check:
- cargo check --all-features
diff --git a/rust/cloud-storage/backfill_entity_access/src/config.rs b/rust/cloud-storage/backfill_entity_access/src/config.rs
deleted file mode 100644
index c20dc85336..0000000000
--- a/rust/cloud-storage/backfill_entity_access/src/config.rs
+++ /dev/null
@@ -1,10 +0,0 @@
-use macro_env_var::env_var;
-
-env_var! {
- pub struct EnvVars {
- /// macrodb url
- pub DatabaseUrl,
- /// macro user id
- pub MacroUserId,
- }
-}
diff --git a/rust/cloud-storage/backfill_entity_access/src/main.rs b/rust/cloud-storage/backfill_entity_access/src/main.rs
deleted file mode 100644
index aee586cfca..0000000000
--- a/rust/cloud-storage/backfill_entity_access/src/main.rs
+++ /dev/null
@@ -1,493 +0,0 @@
-//! Backfill entity access records.
-
-mod config;
-
-use anyhow::Context;
-use config::EnvVars;
-use macro_entrypoint::MacroEntrypoint;
-use macro_uuid::Uuid;
-use sqlx::postgres::PgPoolOptions;
-use sqlx::{Pool, Postgres};
-
-/// Rows of `UserItemAccess` consumed per SQL statement for the 1:1 phases
-/// (owned items, channel/team shares on non-project items). Each batch is a
-/// single `INSERT ... SELECT` in its own autocommit transaction, so lock
-/// windows and WAL size stay bounded.
-const BATCH_SIZE: i64 = 5_000;
-
-/// Smaller batch for the project phase, because each `UserItemAccess` row
-/// fans out to every descendant of the shared project tree.
-const PROJECT_BATCH_SIZE: i64 = 500;
-
-#[tokio::main]
-async fn main() -> anyhow::Result<()> {
- MacroEntrypoint::default().init();
-
- let command = std::env::args()
- .nth(1)
- .context("usage: backfill_entity_access <backfill|verify>")?;
-
- let env_vars = EnvVars::new()?;
-
- let db = PgPoolOptions::new()
- .min_connections(1)
- .max_connections(4)
- .connect(&env_vars.database_url)
- .await
- .context("could not connect to db")?;
-
- match command.as_str() {
- "backfill" => {
- println!("Starting backfill...");
-
- backfill_non_project_channels_and_teams(&db).await?;
- backfill_owned_items(&db).await?;
- backfill_project_items(&db).await?;
-
- println!("All backfill phases COMPLETED");
- }
- "verify" => {
- let _user_access_items =
- get_legacy_user_items(&db, &env_vars.macro_user_id, false).await?;
-
- let _entity_access_items =
- get_entity_access_items(&db, &env_vars.macro_user_id, false).await?;
- }
- other => anyhow::bail!(
- "unknown command: {other}. usage: backfill_entity_access <backfill|verify>"
- ),
- }
-
- Ok(())
-}
-
-/// Drives a keyset-paginated loop. `sql` must bind `$1 = cursor uuid` and
-/// `$2 = batch_size bigint`, and must return exactly one row
-/// `(Option<Uuid>, i64)` — the maximum `UserItemAccess.id` processed in this
-/// batch (NULL when empty = termination), and the number of source rows
-/// considered in the batch.
-#[allow(clippy::disallowed_methods, reason = "legacy code. fix later")]
-async fn run_keyset(
- db: &Pool<Postgres>,
- label: &str,
- sql: &str,
- batch_size: i64,
-) -> anyhow::Result<()> {
- let start = std::time::Instant::now();
- let mut cursor: Uuid = Uuid::nil();
- let mut batches: u64 = 0;
- let mut total_rows: i64 = 0;
- loop {
- let (next_cursor, batch_rows): (Option<Uuid>, i64) = sqlx::query_as(sql)
- .bind(cursor)
- .bind(batch_size)
- .fetch_one(db)
- .await
- .with_context(|| format!("[{label}] batch {batches} failed at cursor={cursor}"))?;
-
- match next_cursor {
- Some(max_id) => {
- batches += 1;
- total_rows += batch_rows;
- cursor = max_id;
- if batches.is_multiple_of(50) {
- println!(
- "[{label}] {batches} batches, {total_rows} rows, cursor={cursor}, elapsed={:?}",
- start.elapsed()
- );
- }
- }
- None => {
- println!(
- "[{label}] DONE: {batches} batches, {total_rows} source rows in {:?}",
- start.elapsed()
- );
- return Ok(());
- }
- }
- }
-}
-
-/// Phase 1: user-owned items — direct grants with `source_type = 'user'`
-/// and `granted_from_project_id IS NULL`.
-async fn backfill_owned_items(db: &Pool<Postgres>) -> anyhow::Result<()> {
- println!("[owned_items] STARTING");
- run_keyset(
- db,
- "owned_items",
- r#"
- WITH batch AS (
- SELECT uia.id, uia.item_id, uia.item_type, uia.user_id, uia.access_level
- FROM "UserItemAccess" uia
- WHERE uia.id > $1
- AND uia.granted_from_channel_id IS NULL
- AND uia.granted_from_team_id IS NULL
- ORDER BY uia.id
- LIMIT $2
- ),
- ins AS (
- INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level)
- SELECT DISTINCT ON (entity_id, entity_type, source_id, source_type)
- b.item_id::uuid AS entity_id,
- CASE WHEN b.item_type = 'thread' THEN 'email_thread' ELSE b.item_type END AS entity_type,
- b.user_id AS source_id,
- 'user'::entity_access_source_type AS source_type,
- b.access_level
- FROM batch b
- ORDER BY entity_id, entity_type, source_id, source_type, b.access_level DESC
- ON CONFLICT (entity_id, entity_type, source_id, source_type)
- WHERE granted_from_project_id IS NULL
- DO NOTHING
- RETURNING 1
- )
- SELECT
- (SELECT id FROM batch ORDER BY id DESC LIMIT 1),
- (SELECT COUNT(*)::bigint FROM batch)
- "#,
- BATCH_SIZE,
- )
- .await?;
- println!("[owned_items] COMPLETED");
- Ok(())
-}
-
-/// Phase 2: non-project items shared via a channel or a team — direct grants
-/// with `source_type = 'channel'` or `'team'` and
-/// `granted_from_project_id IS NULL`.
-async fn backfill_non_project_channels_and_teams(db: &Pool<Postgres>) -> anyhow::Result<()> {
- println!("[channels_and_teams] STARTING");
-
- run_keyset(
- db,
- "channels_and_teams:channels",
- r#"
- WITH batch AS (
- SELECT uia.id, uia.item_id, uia.item_type, uia.granted_from_channel_id, uia.access_level
- FROM "UserItemAccess" uia
- WHERE uia.id > $1
- AND uia.item_type <> 'project'
- AND uia.granted_from_channel_id IS NOT NULL
- ORDER BY uia.id
- LIMIT $2
- ),
- ins AS (
- INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level)
- SELECT DISTINCT ON (entity_id, entity_type, source_id, source_type)
- b.item_id::uuid AS entity_id,
- CASE WHEN b.item_type = 'thread' THEN 'email_thread' ELSE b.item_type END AS entity_type,
- b.granted_from_channel_id::text AS source_id,
- 'channel'::entity_access_source_type AS source_type,
- b.access_level
- FROM batch b
- ORDER BY entity_id, entity_type, source_id, source_type, b.access_level DESC
- ON CONFLICT (entity_id, entity_type, source_id, source_type)
- WHERE granted_from_project_id IS NULL
- DO NOTHING
- RETURNING 1
- )
- SELECT
- (SELECT id FROM batch ORDER BY id DESC LIMIT 1),
- (SELECT COUNT(*)::bigint FROM batch)
- "#,
- BATCH_SIZE,
- )
- .await?;
-
- run_keyset(
- db,
- "channels_and_teams:teams",
- r#"
- WITH batch AS (
- SELECT uia.id, uia.item_id, uia.item_type, uia.granted_from_team_id, uia.access_level
- FROM "UserItemAccess" uia
- WHERE uia.id > $1
- AND uia.item_type <> 'project'
- AND uia.granted_from_team_id IS NOT NULL
- ORDER BY uia.id
- LIMIT $2
- ),
- ins AS (
- INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level)
- SELECT DISTINCT ON (entity_id, entity_type, source_id, source_type)
- b.item_id::uuid AS entity_id,
- CASE WHEN b.item_type = 'thread' THEN 'email_thread' ELSE b.item_type END AS entity_type,
- b.granted_from_team_id::text AS source_id,
- 'team'::entity_access_source_type AS source_type,
- b.access_level
- FROM batch b
- ORDER BY entity_id, entity_type, source_id, source_type, b.access_level DESC
- ON CONFLICT (entity_id, entity_type, source_id, source_type)
- WHERE granted_from_project_id IS NULL
- DO NOTHING
- RETURNING 1
- )
- SELECT
- (SELECT id FROM batch ORDER BY id DESC LIMIT 1),
- (SELECT COUNT(*)::bigint FROM batch)
- "#,
- BATCH_SIZE,
- )
- .await?;
-
- println!("[channels_and_teams] COMPLETED");
- Ok(())
-}
-
-/// Phase 3: project shares — direct grant on the shared project itself, plus
-/// inherited grants on every descendant (child projects, docs, chats, email
-/// threads) keyed to the shared project via `granted_from_project_id`.
-/// Mirrors `entity_access_db_utils::update_entity_access_channel_share_permissions`.
-///
-/// Team-shared projects are intentionally skipped: the product does not
-/// support sharing a project directly with a team.
-///
-/// Both direct and inherited inserts happen in one statement per batch via
-/// two data-modifying CTEs; they target different partial unique indexes, so
-/// they do not compete for locks.
-async fn backfill_project_items(db: &Pool<Postgres>) -> anyhow::Result<()> {
- println!("[project_items] STARTING");
- run_keyset(
- db,
- "project_items",
- r#"
- WITH RECURSIVE batch AS (
- SELECT uia.id, uia.item_id, uia.granted_from_channel_id, uia.access_level
- FROM "UserItemAccess" uia
- WHERE uia.id > $1
- AND uia.item_type = 'project'
- AND uia.granted_from_team_id IS NULL
- ORDER BY uia.id
- LIMIT $2
- ),
- share_sources AS (
- SELECT DISTINCT ON (project_id, source_id, source_type)
- b.item_id AS project_id,
- COALESCE(b.granted_from_channel_id::text, p."userId") AS source_id,
- CASE WHEN b.granted_from_channel_id IS NOT NULL
- THEN 'channel' ELSE 'user' END::entity_access_source_type AS source_type,
- b.access_level
- FROM batch b
- JOIN "Project" p ON p.id = b.item_id
- ORDER BY project_id, source_id, source_type, b.access_level DESC
- ),
- direct_ins AS (
- INSERT INTO entity_access (entity_id, entity_type, source_id, source_type, access_level)
- SELECT DISTINCT ON (entity_id, source_id, source_type)
- ss.project_id::uuid AS entity_id,
- 'project' AS entity_type,
- ss.source_id,
- ss.source_type,
- ss.access_level
- FROM share_sources ss
- ORDER BY entity_id, source_id, source_type, ss.access_level DESC
- ON CONFLICT (entity_id, entity_type, source_id, source_type)
- WHERE granted_from_project_id IS NULL
- DO NOTHING
- RETURNING 1
- ),
- descendant_projects AS (
- SELECT ss.project_id AS root_project_id, ss.project_id AS id
- FROM share_sources ss
- UNION ALL
- SELECT dp.root_project_id, p.id
- FROM "Project" p
- JOIN descendant_projects dp ON p."parentId" = dp.id
- ),
- descendants AS (
- SELECT dp.root_project_id, dp.id::uuid AS entity_id, 'project' AS entity_type
- FROM descendant_projects dp
- WHERE dp.id <> dp.root_project_id
-
- UNION ALL
- SELECT dp.root_project_id, d.id::uuid, 'document'
- FROM "Document" d
- JOIN descendant_projects dp ON d."projectId" = dp.id
-
- UNION ALL
- SELECT dp.root_project_id, c.id::uuid, 'chat'
- FROM "Chat" c
- JOIN descendant_projects dp ON c."projectId" = dp.id
-
- UNION ALL
- SELECT dp.root_project_id, et.id::uuid, 'email_thread'
- FROM email_threads et
- JOIN descendant_projects dp ON et.project_id = dp.id
- ),
- inherited_ins AS (
- INSERT INTO entity_access
- (entity_id, entity_type, source_id, source_type, access_level, granted_from_project_id)
- SELECT DISTINCT ON (entity_id, entity_type, source_id, source_type, granted_from_project_id)
- d.entity_id,
- d.entity_type,
- ss.source_id,
- ss.source_type,
- ss.access_level,
- d.root_project_id AS granted_from_project_id
- FROM descendants d
- JOIN share_sources ss ON ss.project_id = d.root_project_id
- ORDER BY entity_id, entity_type, source_id, source_type, granted_from_project_id, ss.access_level DESC
- ON CONFLICT (entity_id, entity_type, source_id, source_type, granted_from_project_id)
- WHERE granted_from_project_id IS NOT NULL
- DO NOTHING
- RETURNING 1
- )
- SELECT
- (SELECT id FROM batch ORDER BY id DESC LIMIT 1),
- (SELECT COUNT(*)::bigint FROM batch)
- "#,
- PROJECT_BATCH_SIZE,
- )
- .await?;
- println!("[project_items] COMPLETED");
- Ok(())
-}
-
-/// Copied from `get_user_accessible_items` in macro_db_client crate
-pub async fn get_legacy_user_items(
- db: &Pool<Postgres>,
- user_id: &str,
- _exclude_owned: bool,
-) -> anyhow::Result<Vec<(String, String)>> {
- let start = std::time::Instant::now();
- let results = sqlx::query!(
- r#"
- WITH RECURSIVE ProjectHierarchy AS (
- -- Find direct user access to projects first as starting point
- SELECT
- p.id,
- uia.access_level
- FROM "Project" p
- JOIN "UserItemAccess" uia ON p.id = uia.item_id AND uia.item_type = 'project'
- WHERE uia.user_id = $1 AND p."deletedAt" IS NULL
-
- UNION ALL
-
- -- Then walk down the project tree and grab child projects, keeping parent's access
- SELECT
- p.id,
- ph.access_level
- FROM "Project" p
- JOIN ProjectHierarchy ph ON p."parentId" = ph.id
- WHERE p."deletedAt" IS NULL
- ),
- -- Now build up all the ways a user can have access to stuff
- AllAccessGrants AS (
- -- Explicit access to items via UserItemAccess table
- SELECT uia.item_id, uia.item_type, uia.access_level
- FROM "UserItemAccess" uia
- -- We join to each table to check its "deletedAt" status.
- -- This is more explicit and robust than using subqueries.
- LEFT JOIN "Document" d ON uia.item_type = 'document' AND uia.item_id = d.id
- LEFT JOIN "Chat" c ON uia.item_type = 'chat' AND uia.item_id = c.id
- LEFT JOIN "Project" p ON uia.item_type = 'project' AND uia.item_id = p.id
- WHERE uia.user_id = $1
- -- Rule: The item must not be deleted.
- AND (
- (uia.item_type = 'document' AND d."deletedAt" IS NULL) OR
- (uia.item_type = 'chat' AND c."deletedAt" IS NULL) OR
- (uia.item_type = 'project' AND p."deletedAt" IS NULL)
- )
-
- -- The rest of the unions are to get implicit access to items via project access
- UNION ALL
-
- -- Access to docs in visible projects
- SELECT
- d.id AS item_id,
- 'document' AS item_type,
- ph.access_level
- FROM "Document" d
- JOIN ProjectHierarchy ph ON d."projectId" = ph.id
- WHERE d."projectId" IS NOT NULL AND d."deletedAt" IS NULL
-
- UNION ALL
-
- -- Access to chats in visible projects
- SELECT
- c.id AS item_id,
- 'chat' AS item_type,
- ph.access_level
- FROM "Chat" c
- JOIN ProjectHierarchy ph ON c."projectId" = ph.id
- WHERE c."projectId" IS NOT NULL AND c."deletedAt" IS NULL
-
- UNION ALL
-
- -- Include the projects we found earlier
- SELECT
- ph.id AS item_id,
- 'project' AS item_type,
- ph.access_level
- FROM ProjectHierarchy ph
- ),
- UserAccessibleItems AS (
- SELECT
- item_id,
- item_type
- FROM AllAccessGrants
- GROUP BY item_id, item_type
- )
- SELECT item_id as "item_id!", item_type as "item_type!" FROM UserAccessibleItems
- "#,
- user_id,
- )
- .map(|r| (r.item_id, r.item_type))
- .fetch_all(db)
- .await?;
-
- let elapsed = start.elapsed();
- println!(
- "[get_legacy_user_items] user={user_id} returned {} items in {elapsed:.2?}",
- results.len()
- );
-
- Ok(results)
-}
-
-pub async fn get_entity_access_items(
- db: &Pool<Postgres>,
- user_id: &str,
- _exclude_owned: bool,
-) -> anyhow::Result<Vec<(macro_uuid::Uuid, String)>> {
- let start = std::time::Instant::now();
- // Fetch source IDs first
- let source_ids = sqlx::query_scalar!(
- r#"
- SELECT cp.channel_id::text FROM comms_channel_participants cp
- WHERE cp.user_id = $1 AND cp.left_at IS NULL
- UNION ALL
- SELECT t.team_id::text FROM team_user t
- WHERE t.user_id = $1
- UNION ALL
- SELECT $1
- "#,
- user_id
- )
- .fetch_all(db)
- .await?;
-
- let source_ids: Vec<String> = source_ids.into_iter().flatten().collect();
-
- // Then use ANY($2) which plays nicely with the source_id index
- let results = sqlx::query!(
- r#"
- SELECT DISTINCT
- ea.entity_id,
- ea.entity_type
- FROM entity_access ea
- WHERE ea.source_id = ANY($1)
- "#,
- &source_ids,
- )
- .map(|r| (r.entity_id, r.entity_type))
- .fetch_all(db)
- .await?;
-
- let elapsed = start.elapsed();
- println!(
- "[get_entity_access_items] user={user_id} returned {} items in {elapsed:.2?}",
- results.len()
- );
-
- Ok(results)
-}
diff --git a/rust/cloud-storage/justfile b/rust/cloud-storage/justfile
index bf6eadb50e..df15c21433 100644
--- a/rust/cloud-storage/justfile
+++ b/rust/cloud-storage/justfile
@@ -104,7 +104,6 @@ setup_test_envs:
echo "DATABASE_URL={{ DATABASE_URL }}" >> ./memory/.env
echo "DATABASE_URL={{ DATABASE_URL }}" >> ./ai_usage/.env
echo "DATABASE_URL={{ DATABASE_URL }}" >> ./call/.env
- echo "DATABASE_URL={{ DATABASE_URL }}" >> ./backfill_entity_access/.env
echo "DATABASE_URL={{ DATABASE_URL }}" >> ./entity_access_management/.env
echo "DATABASE_URL={{ DATABASE_URL }}" >> ./mobile_welcome_email_db_client/.env
echo "DATABASE_URL={{ DATABASE_URL }}" >> ./entity_access_db_utils/.env
diff --git a/rust/cloud-storage/macro_db_client/fixtures/mixed_items.sql b/rust/cloud-storage/macro_db_client/fixtures/mixed_items.sql
index 427fbdd59d..efeec5ee36 100644
--- a/rust/cloud-storage/macro_db_client/fixtures/mixed_items.sql
+++ b/rust/cloud-storage/macro_db_client/fixtures/mixed_items.sql
@@ -88,55 +88,5 @@ INSERT INTO public."Chat" ("id",
true,
'test-project');
--- Add user access to all items with different access levels to test DISTINCT ON
-INSERT INTO public."UserItemAccess" ("id",
- "user_id",
- "item_id",
- "item_type",
- "access_level",
- "created_at") (SELECT gen_random_uuid(),
- 'test-document',
- 'document',
- 'owner',
- '2023-01-15 10:00:00');
-
--- Add another access level for document to ensure we get the highest one
-INSERT INTO public."UserItemAccess" ("id",
- "user_id",
- "item_id",
- "item_type",
- "access_level",
- "created_at") (SELECT gen_random_uuid(),
- 'test-document',
- 'document',
- 'view',
- '2023-01-15 10:30:00');
-
-INSERT INTO public."UserItemAccess" ("id",
- "user_id",
- "item_id",
- "item_type",
- "access_level",
- "created_at") (SELECT gen_random_uuid(),
- 'test-chat',
- 'chat',
- 'owner',
- '2023-01-16 11:00:00');
-
-INSERT INTO public."UserItemAccess" ("id",
- "user_id",
- "item_id",
- "item_type",
- "access_level",
- "created_at") (SELECT gen_random_uuid(),
- 'test-project',
- 'project',
- 'owner',
- '2023-01-17 12:00:00');
-
-- Re-enable foreign key constraints
-SET session_replication_role = 'origin';
\ No newline at end of file
+SET session_replication_role = 'origin';
diff --git a/rust/cloud-storage/macro_db_client/migrations/20260707154258_remove_legacy_user_item_access.sql b/rust/cloud-storage/macro_db_client/migrations/20260707154258_remove_legacy_user_item_access.sql
new file mode 100644
index 0000000000..24c2209301
--- /dev/null
+++ b/rust/cloud-storage/macro_db_client/migrations/20260707154258_remove_legacy_user_item_access.sql
@@ -0,0 +1,2 @@
+-- Add migration script here
+DROP TABLE IF EXISTS "UserItemAccess";