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.

feat(projects): register projects in the entity table (#6448)

Will Hutchinson23a96943b3af
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index c31d8892fe..6a1bb61af1 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -6698,6 +6698,8 @@
"crates/entity_access_db_utils",
"crates/entity_access_management",
"crates/entity_mutation",
+ "crates/entity_registry/entity_registry_db_utils",
+ "crates/entity_registry/shared_entity_registry",
"crates/filter_ast",
"crates/foreign_entity",
"crates/frecency",
diff --git a/Cargo.lock b/Cargo.lock
index 23e4b995c6..4f3d71b415 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -12958,6 +12958,7 @@ dependencies = [
"entity_access_db_utils",
"entity_access_management",
"entity_mutation",
+ "entity_registry_db_utils",
"futures",
"hex",
"http-body-util",
@@ -12975,6 +12976,7 @@ dependencies = [
"model",
"model-entity",
"model-error-response",
+ "model_owner",
"model_user",
"models_bulk_upload",
"models_permissions",
diff --git a/crates/macro_db_client/src/document/initialize_onboarding_documents.rs b/crates/macro_db_client/src/document/initialize_onboarding_documents.rs
index 283bb2941e..1b4f6f1559 100644
--- a/crates/macro_db_client/src/document/initialize_onboarding_documents.rs
+++ b/crates/macro_db_client/src/document/initialize_onboarding_documents.rs
@@ -35,13 +35,13 @@ pub async fn create_project_transaction(
.fetch_one(transaction.as_mut())
.await?;
- // Create share permission
create_project_permission(transaction, &project.id, share_permission).await?;
upsert_user_history(transaction, user_id.copied(), &project.id, "project").await?;
+ let project_uuid = macro_uuid::string_to_uuid(&project.id).unwrap();
entity_access_db_utils::insert_entity_access_row(
transaction,
- &macro_uuid::string_to_uuid(&project.id).unwrap(),
+ &project_uuid,
EntityType::Project,
user_id.as_ref(),
entity_access_db_utils::EntityAccessSourceType::User,
@@ -49,6 +49,16 @@ pub async fn create_project_transaction(
)
.await?;
+ entity_registry_db_utils::insert_entity(
+ transaction,
+ entity_registry_db_utils::NewEntityRecord::new(
+ project_uuid,
+ entity_registry_db_utils::RegisteredEntityType::Project,
+ model_owner::Owner::User(user_id.copied().into_owned()),
+ ),
+ )
+ .await?;
+
Ok(project)
}
diff --git a/crates/macro_db_client/src/projects/delete.rs b/crates/macro_db_client/src/projects/delete.rs
index 6b7d416ba4..1ede1eda30 100644
--- a/crates/macro_db_client/src/projects/delete.rs
+++ b/crates/macro_db_client/src/projects/delete.rs
@@ -19,7 +19,6 @@ pub async fn delete_projects_bulk(
Ok(())
}
-// delete projects in bulk
#[tracing::instrument(skip(transaction))]
pub async fn delete_projects_bulk_tsx(
transaction: &mut Transaction<'_, Postgres>,
@@ -28,7 +27,6 @@ pub async fn delete_projects_bulk_tsx(
if project_ids.is_empty() {
return Ok(());
}
- // Delete pins
sqlx::query!(
r#"
DELETE FROM "Pin" WHERE "pinnedItemId" = ANY($1) AND "pinnedItemType" = $2
@@ -39,7 +37,6 @@ pub async fn delete_projects_bulk_tsx(
.execute(transaction.as_mut())
.await?;
- // Delete user history
sqlx::query!(
r#"
DELETE FROM "UserHistory" WHERE "itemId" = ANY($1) AND "itemType" = $2
@@ -65,16 +62,21 @@ pub async fn delete_projects_bulk_tsx(
.await
.context("unable to delete share permissions")?;
+ let project_uuids = project_ids
+ .iter()
+ .map(|p| macro_uuid::string_to_uuid(p).unwrap())
+ .collect::<Vec<uuid::Uuid>>();
crate::item_access::delete::delete_user_entity_access_bulk(
transaction,
- &project_ids
- .iter()
- .map(|p| macro_uuid::string_to_uuid(p).unwrap())
- .collect::<Vec<uuid::Uuid>>(),
+ &project_uuids,
EntityType::Project,
)
.await?;
+ for project_uuid in project_uuids {
+ entity_registry_db_utils::delete_entity(transaction, project_uuid).await?;
+ }
+
sqlx::query!(
r#"
DELETE FROM "Project"
diff --git a/crates/projects/Cargo.toml b/crates/projects/Cargo.toml
index 976284a471..481081702b 100644
--- a/crates/projects/Cargo.toml
+++ b/crates/projects/Cargo.toml
@@ -29,8 +29,10 @@ outbound = [
"dep:documents",
"dep:dynamodb_client",
"dep:entity_access_db_utils",
+ "dep:entity_registry_db_utils",
"dep:hex",
"dep:macro_sha_count_client",
+ "dep:model_owner",
"dep:share_permission_db_utils",
"dep:sqlx",
"dep:sqs_client",
@@ -99,8 +101,10 @@ document_sub_type = { path = "../document_sub_type", optional = true }
documents = { path = "../documents", default-features = false, optional = true }
dynamodb_client = { path = "../dynamodb_client", optional = true }
entity_access_db_utils = { path = "../entity_access_db_utils", optional = true }
+entity_registry_db_utils = { path = "../entity_registry/entity_registry_db_utils", optional = true }
hex = { workspace = true, optional = true }
macro_sha_count_client = { path = "../macro_sha_count_client", optional = true }
+model_owner = { path = "../model_owner", optional = true }
share_permission_db_utils = { path = "../share_permission_db_utils", optional = true }
sqlx = { workspace = true, optional = true }
sqs_client = { path = "../sqs_client", default-features = false, features = [
diff --git a/crates/projects/fixtures/projects_test_data.sql b/crates/projects/fixtures/projects_test_data.sql
index f8cfcd92e7..fc25fcf337 100644
--- a/crates/projects/fixtures/projects_test_data.sql
+++ b/crates/projects/fixtures/projects_test_data.sql
@@ -55,3 +55,19 @@ VALUES
('10000000-0000-0000-0000-000000000001', 'project', 'macro|[email protected]', 'user', 'view'),
('20000000-0000-0000-0000-000000000001', 'document', 'macro|[email protected]', 'user', 'owner'),
('30000000-0000-0000-0000-000000000001', 'chat', 'macro|[email protected]', 'user', 'owner');
+
+INSERT INTO public.entity (id, entity_type, owner_type, owner_id, created_at, updated_at, deleted_at)
+VALUES
+ ('10000000-0000-0000-0000-000000000001', 'project', 'user', 'macro|[email protected]', '2024-01-01', '2024-01-05', NULL),
+ ('10000000-0000-0000-0000-000000000002', 'project', 'user', 'macro|[email protected]', '2024-01-02', '2024-01-02', NULL),
+ ('10000000-0000-0000-0000-000000000003', 'project', 'user', 'macro|[email protected]', '2024-01-03', '2024-01-03', NULL),
+ ('10000000-0000-0000-0000-000000000004', 'project', 'user', 'macro|[email protected]', '2024-01-04', '2024-01-04', '2024-02-01'),
+ ('10000000-0000-0000-0000-000000000005', 'project', 'user', 'macro|[email protected]', '2024-01-05', '2024-01-06', NULL),
+ ('10000000-0000-0000-0000-000000000006', 'project', 'user', 'macro|[email protected]', '2024-01-06', '2024-01-07', NULL),
+ ('10000000-0000-0000-0000-000000000007', 'project', 'user', 'macro|[email protected]', '2024-01-07', '2024-01-08', NULL),
+ ('10000000-0000-0000-0000-000000000008', 'project', 'user', 'macro|[email protected]', '2024-01-08', '2024-01-09', NULL),
+ ('10000000-0000-0000-0000-000000000009', 'project', 'user', 'macro|[email protected]', '2024-01-09', '2024-01-10', '2024-02-02'),
+ ('20000000-0000-0000-0000-000000000001', 'document', 'user', 'macro|[email protected]', '2024-01-03', '2024-01-03', NULL),
+ ('20000000-0000-0000-0000-000000000002', 'document', 'user', 'macro|[email protected]', '2024-01-04', '2024-01-04', '2024-02-01'),
+ ('30000000-0000-0000-0000-000000000001', 'chat', 'user', 'macro|[email protected]', '2024-01-04', '2024-01-04', NULL),
+ ('30000000-0000-0000-0000-000000000002', 'chat', 'user', 'macro|[email protected]', '2024-01-05', '2024-01-05', '2024-02-01');
diff --git a/crates/projects/src/outbound/pg_project_repo/create.rs b/crates/projects/src/outbound/pg_project_repo/create.rs
index b1df0242e7..0247995583 100644
--- a/crates/projects/src/outbound/pg_project_repo/create.rs
+++ b/crates/projects/src/outbound/pg_project_repo/create.rs
@@ -1,6 +1,7 @@
use entity_access_db_utils::{
AccessLevel, EntityAccessSourceType, EntityType, insert_entity_access_row,
};
+use macro_user_id::user_id::MacroUserIdStr;
use model::project::Project;
use sqlx::{Postgres, Transaction};
@@ -63,5 +64,19 @@ pub(super) async fn create_project(
)
.await?;
+ entity_registry_db_utils::insert_entity(
+ transaction,
+ entity_registry_db_utils::NewEntityRecord::new(
+ project_id,
+ entity_registry_db_utils::RegisteredEntityType::Project,
+ model_owner::Owner::User(
+ MacroUserIdStr::try_from(args.user_id.clone())
+ .map_err(|error| sqlx::Error::Decode(Box::new(error)))?,
+ ),
+ ),
+ )
+ .await
+ .map_err(|error| sqlx::Error::Protocol(error.to_string()))?;
+
Ok(project)
}
diff --git a/crates/projects/src/outbound/pg_project_repo/delete.rs b/crates/projects/src/outbound/pg_project_repo/delete.rs
index 0458723d07..01a704f7f0 100644
--- a/crates/projects/src/outbound/pg_project_repo/delete.rs
+++ b/crates/projects/src/outbound/pg_project_repo/delete.rs
@@ -1,5 +1,6 @@
-use chrono::Utc;
+use chrono::{DateTime, Utc};
use sqlx::{Postgres, Transaction};
+use uuid::Uuid;
use crate::domain::models::{PurgedProjectTree, SoftDeleteResult};
@@ -137,6 +138,7 @@ async fn delete_items(
)
.execute(transaction.as_mut())
.await?;
+ delete_registered_entities(transaction, &all_ids).await?;
sqlx::query!(r#"DELETE FROM "Document" WHERE id = ANY($1)"#, document_ids)
.execute(transaction.as_mut())
.await?;
@@ -170,8 +172,6 @@ pub(super) async fn soft_delete_project(
.fetch_all(transaction.as_mut())
.await?;
- // Unlike the legacy implementation, child reads use the same transaction
- // as the writes. This intentionally removes read skew during concurrent moves.
let document_ids = sqlx::query_scalar!(
r#"SELECT id FROM "Document" WHERE "projectId" = ANY($1)"#,
&project_ids,
@@ -204,28 +204,29 @@ pub(super) async fn soft_delete_project(
.execute(transaction.as_mut())
.await?;
- let deleted_at = Utc::now().naive_utc();
+ let deleted_at = Utc::now();
sqlx::query!(
r#"UPDATE "Document" SET "deletedAt" = $2 WHERE id = ANY($1)"#,
&document_ids,
- deleted_at,
+ deleted_at.naive_utc(),
)
.execute(transaction.as_mut())
.await?;
sqlx::query!(
r#"UPDATE "Chat" SET "deletedAt" = $2 WHERE id = ANY($1)"#,
&chat_ids,
- deleted_at,
+ deleted_at.naive_utc(),
)
.execute(transaction.as_mut())
.await?;
sqlx::query!(
r#"UPDATE "Project" SET "deletedAt" = $2 WHERE id = ANY($1)"#,
&project_ids,
- deleted_at,
+ deleted_at.naive_utc(),
)
.execute(transaction.as_mut())
.await?;
+ mark_registered_entities_deleted(transaction, &all_ids, deleted_at).await?;
Ok(SoftDeleteResult {
project_ids,
@@ -233,3 +234,37 @@ pub(super) async fn soft_delete_project(
chat_ids,
})
}
+
+fn parse_entity_id(id: &str) -> Result<Uuid, sqlx::Error> {
+ id.parse()
+ .map_err(|error| sqlx::Error::Decode(Box::new(error)))
+}
+
+fn registry_protocol_error(error: impl ToString) -> sqlx::Error {
+ sqlx::Error::Protocol(error.to_string())
+}
+
+async fn mark_registered_entities_deleted(
+ transaction: &mut Transaction<'_, Postgres>,
+ ids: &[String],
+ deleted_at: DateTime<Utc>,
+) -> Result<(), sqlx::Error> {
+ for id in ids {
+ entity_registry_db_utils::mark_deleted(transaction, parse_entity_id(id)?, deleted_at)
+ .await
+ .map_err(registry_protocol_error)?;
+ }
+ Ok(())
+}
+
+async fn delete_registered_entities(
+ transaction: &mut Transaction<'_, Postgres>,
+ ids: &[String],
+) -> Result<(), sqlx::Error> {
+ for id in ids {
+ entity_registry_db_utils::delete_entity(transaction, parse_entity_id(id)?)
+ .await
+ .map_err(registry_protocol_error)?;
+ }
+ Ok(())
+}
diff --git a/crates/projects/src/outbound/pg_project_repo/revert_delete.rs b/crates/projects/src/outbound/pg_project_repo/revert_delete.rs
index f4dcc82618..e08202377c 100644
--- a/crates/projects/src/outbound/pg_project_repo/revert_delete.rs
+++ b/crates/projects/src/outbound/pg_project_repo/revert_delete.rs
@@ -33,8 +33,6 @@ pub(super) async fn revert_delete_project(
.map(|row| row.user_id.clone())
.collect::<Vec<_>>();
- // Child reads deliberately remain inside this transaction so restoration
- // observes one consistent subtree rather than the legacy read-skew window.
let documents = sqlx::query!(
r#"
SELECT id, owner FROM "Document"
@@ -70,6 +68,13 @@ pub(super) async fn revert_delete_project(
restore_items(transaction, "chat", &chat_ids, &chat_owner_ids).await?;
restore_items(transaction, "document", &document_ids, &document_owner_ids).await?;
restore_items(transaction, "project", &project_ids, &project_owner_ids).await?;
+ let restored_ids = project_ids
+ .iter()
+ .chain(&document_ids)
+ .chain(&chat_ids)
+ .cloned()
+ .collect::<Vec<_>>();
+ clear_registered_entities(transaction, &restored_ids).await?;
if let Some(parent_id) = previous_parent_id {
let parent_is_deleted = sqlx::query_scalar!(
@@ -145,3 +150,18 @@ async fn restore_items(
.await?;
Ok(())
}
+
+async fn clear_registered_entities(
+ transaction: &mut Transaction<'_, Postgres>,
+ ids: &[String],
+) -> Result<(), sqlx::Error> {
+ for id in ids {
+ let uuid = id
+ .parse()
+ .map_err(|error| sqlx::Error::Decode(Box::new(error)))?;
+ entity_registry_db_utils::clear_deleted(transaction, uuid)
+ .await
+ .map_err(|error| sqlx::Error::Protocol(error.to_string()))?;
+ }
+ Ok(())
+}
diff --git a/crates/projects/src/outbound/pg_project_repo/tests.rs b/crates/projects/src/outbound/pg_project_repo/tests.rs
index af7062eced..a8e0054ddf 100644
--- a/crates/projects/src/outbound/pg_project_repo/tests.rs
+++ b/crates/projects/src/outbound/pg_project_repo/tests.rs
@@ -9,7 +9,7 @@ use model::project::ProjectPreviewV2;
use models_permissions::share_permission::{
LinkShare, SharePermissionV2, UpdateSharePermissionRequestV2, access_level::AccessLevel,
};
-use sqlx::{Pool, Postgres};
+use sqlx::{Pool, Postgres, Row};
use super::PgProjectRepo;
use crate::domain::models::{CreateProjectArgs, EditProjectArgs, UploadFolderRepoArgs};
@@ -18,6 +18,10 @@ use crate::domain::ports::ProjectRepo;
const ROOT_ID: &str = "10000000-0000-0000-0000-000000000001";
const CHILD_ID: &str = "10000000-0000-0000-0000-000000000002";
const DELETED_ID: &str = "10000000-0000-0000-0000-000000000009";
+const DOCUMENT_ID: &str = "20000000-0000-0000-0000-000000000001";
+const DELETED_DOCUMENT_ID: &str = "20000000-0000-0000-0000-000000000002";
+const CHAT_ID: &str = "30000000-0000-0000-0000-000000000001";
+const DELETED_CHAT_ID: &str = "30000000-0000-0000-0000-000000000002";
#[derive(Debug, Eq, PartialEq)]
struct StoredSharePermission {
@@ -25,6 +29,60 @@ struct StoredSharePermission {
link_share_access_level: Option<String>,
}
+struct EntityRow {
+ owner_type: String,
+ owner_id: String,
+ entity_type: String,
+ deleted_at: Option<chrono::DateTime<chrono::Utc>>,
+}
+
+async fn fetch_entity_row(pool: &Pool<Postgres>, id: &str) -> EntityRow {
+ let row = sqlx::query(
+ r#"
+ SELECT
+ owner_type::text AS owner_type,
+ owner_id,
+ entity_type,
+ deleted_at
+ FROM entity
+ WHERE id = $1
+ "#,
+ )
+ .bind(uuid::Uuid::parse_str(id).unwrap())
+ .fetch_one(pool)
+ .await
+ .unwrap();
+ EntityRow {
+ owner_type: row.get("owner_type"),
+ owner_id: row.get("owner_id"),
+ entity_type: row.get("entity_type"),
+ deleted_at: row.get("deleted_at"),
+ }
+}
+
+async fn count_entity_rows(pool: &Pool<Postgres>) -> i64 {
+ sqlx::query_scalar("SELECT COUNT(*) FROM entity")
+ .fetch_one(pool)
+ .await
+ .unwrap()
+}
+
+async fn count_entity_rows_for_ids(pool: &Pool<Postgres>, ids: &[String]) -> i64 {
+ sqlx::query_scalar("SELECT COUNT(*) FROM entity WHERE id::text = ANY($1)")
+ .bind(ids)
+ .fetch_one(pool)
+ .await
+ .unwrap()
+}
+
+async fn count_entity_rows_for_id(pool: &Pool<Postgres>, id: &str) -> i64 {
+ sqlx::query_scalar("SELECT COUNT(*) FROM entity WHERE id::text = $1")
+ .bind(id)
+ .fetch_one(pool)
+ .await
+ .unwrap()
+}
+
async fn project_share_permission_columns(
pool: &Pool<Postgres>,
project_id: &str,
@@ -240,6 +298,14 @@ async fn create_is_atomic_and_inserts_all_metadata(pool: Pool<Postgres>) -> anyh
}
);
+ let entity = fetch_entity_row(&pool, &project.id).await;
+ assert_eq!(entity.owner_type, "user");
+ assert_eq!(entity.owner_id, "macro|[email protected]");
+ assert_eq!(entity.entity_type, "project");
+ assert_eq!(entity.deleted_at, None);
+
+ let entity_count_before_failed_create = count_entity_rows(&pool).await;
+
assert!(
repo.create_project(CreateProjectArgs {
user_id: "macro|[email protected]".to_owned(),
@@ -256,6 +322,21 @@ async fn create_is_atomic_and_inserts_all_metadata(pool: Pool<Postgres>) -> anyh
.fetch_one(&pool)
.await?;
assert_eq!(rolled_back, 0);
+ let rolled_back_entity: i64 = sqlx::query_scalar(
+ r#"
+ SELECT COUNT(*)
+ FROM entity
+ JOIN "Project" ON "Project".id = entity.id::text
+ WHERE "Project".name = 'Must roll back'
+ "#,
+ )
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(rolled_back_entity, 0);
+ assert_eq!(
+ count_entity_rows(&pool).await,
+ entity_count_before_failed_create
+ );
Ok(())
}
@@ -463,6 +544,27 @@ async fn recursive_detection_and_soft_delete_output(pool: Pool<Postgres>) -> any
.fetch_one(&pool)
.await?;
assert_eq!(remaining_history, 0);
+ let root_entity = fetch_entity_row(&pool, ROOT_ID).await;
+ assert!(root_entity.deleted_at.is_some());
+ assert!(
+ fetch_entity_row(&pool, DOCUMENT_ID)
+ .await
+ .deleted_at
+ .is_some()
+ );
+ assert!(
+ fetch_entity_row(&pool, DELETED_DOCUMENT_ID)
+ .await
+ .deleted_at
+ .is_some()
+ );
+ assert!(fetch_entity_row(&pool, CHAT_ID).await.deleted_at.is_some());
+ assert!(
+ fetch_entity_row(&pool, DELETED_CHAT_ID)
+ .await
+ .deleted_at
+ .is_some()
+ );
Ok(())
}
@@ -494,6 +596,7 @@ async fn revert_restores_subtree_and_handles_parent_state(
.is_none()
);
assert!(repo.get_project_by_id(CHILD_ID).await?.is_some());
+ assert_eq!(fetch_entity_row(&pool, ROOT_ID).await.deleted_at, None);
repo.soft_delete_project(CHILD_ID).await?;
repo.revert_delete_project(CHILD_ID, Some(ROOT_ID.to_owned()))
@@ -558,6 +661,7 @@ async fn purge_returns_outputs_and_removes_access_and_permissions(
.fetch_one(&pool)
.await?;
assert_eq!(remaining_access, 0);
+ assert_eq!(count_entity_rows_for_ids(&pool, &purged_ids).await, 0);
let remaining_permissions = sqlx::query_scalar!(
r#"SELECT COUNT(*) AS "count!" FROM "ProjectPermission" WHERE "projectId" = ANY($1)"#,
&result.project_ids,
@@ -589,6 +693,7 @@ async fn purge_rolls_back_all_deletions(pool: Pool<Postgres>) -> anyhow::Result<
.fetch_one(&pool)
.await?;
assert_eq!(access_count, 2);
+ assert_eq!(count_entity_rows_for_id(&pool, ROOT_ID).await, 1);
let permission_count = sqlx::query_scalar!(
r#"SELECT COUNT(*) AS "count!" FROM "ProjectPermission" WHERE "projectId" = $1"#,
ROOT_ID,
@@ -686,6 +791,13 @@ async fn upload_folder_preserves_tree_metadata_and_compensates(
.await?;
assert_eq!(created_permissions, 4);
+ for project_id in &result.project_ids {
+ let entity = fetch_entity_row(&pool, project_id).await;
+ assert_eq!(entity.owner_type, "user");
+ assert_eq!(entity.owner_id, "macro|[email protected]");
+ assert_eq!(entity.entity_type, "project");
+ }
+
repo.delete_uploaded_tree(&result.project_ids, &document_ids)
.await?;
let remaining = sqlx::query_scalar!(
@@ -702,6 +814,10 @@ async fn upload_folder_preserves_tree_metadata_and_compensates(
.fetch_one(&pool)
.await?;
assert_eq!(remaining_access, 0);
+ assert_eq!(
+ count_entity_rows_for_ids(&pool, &result.project_ids).await,
+ 0
+ );
Ok(())
}
diff --git a/crates/projects/src/outbound/pg_project_repo/upload_folder.rs b/crates/projects/src/outbound/pg_project_repo/upload_folder.rs
index c394a1e1a1..c865300037 100644
--- a/crates/projects/src/outbound/pg_project_repo/upload_folder.rs
+++ b/crates/projects/src/outbound/pg_project_repo/upload_folder.rs
@@ -172,6 +172,16 @@ async fn create_pending_project(
AccessLevel::Owner,
)
.await?;
+ entity_registry_db_utils::insert_entity(
+ transaction,
+ entity_registry_db_utils::NewEntityRecord::new(
+ entity_id,
+ entity_registry_db_utils::RegisteredEntityType::Project,
+ model_owner::Owner::User(user_id.clone()),
+ ),
+ )
+ .await
+ .map_err(|error| sqlx::Error::Protocol(error.to_string()))?;
Ok(project)
}