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(db): add entity_access table (#2296)

Will Hutchinson84b940cfe82e
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-1435acbba365796ddcb6ddbef86f4ebf664cffc44082996f6b60a60373c28f75.json b/rust/cloud-storage/macro_db_client/.sqlx/query-1435acbba365796ddcb6ddbef86f4ebf664cffc44082996f6b60a60373c28f75.json
deleted file mode 100644
index d2b084cf10..0000000000
--- a/rust/cloud-storage/macro_db_client/.sqlx/query-1435acbba365796ddcb6ddbef86f4ebf664cffc44082996f6b60a60373c28f75.json
+++ /dev/null
@@ -1,17 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)\n SELECT \n u.id,\n u.entity_id,\n 'TASK'::property_entity_type,\n u.property_definition_id,\n u.values\n FROM UNNEST(\n $1::UUID[],\n $2::TEXT[],\n $3::UUID[],\n $4::JSONB[]\n ) AS u(id, entity_id, property_definition_id, values)\n ON CONFLICT (entity_id, entity_type, property_definition_id)\n DO UPDATE SET \n values = EXCLUDED.values,\n updated_at = NOW()\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "UuidArray",
- "TextArray",
- "UuidArray",
- "JsonbArray"
- ]
- },
- "nullable": []
- },
- "hash": "1435acbba365796ddcb6ddbef86f4ebf664cffc44082996f6b60a60373c28f75"
-}
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-63492b018803ce26db5891964a0f52f37d2f54bd8243265c6561f4423af8617c.json b/rust/cloud-storage/macro_db_client/.sqlx/query-63492b018803ce26db5891964a0f52f37d2f54bd8243265c6561f4423af8617c.json
deleted file mode 100644
index 6d97183829..0000000000
--- a/rust/cloud-storage/macro_db_client/.sqlx/query-63492b018803ce26db5891964a0f52f37d2f54bd8243265c6561f4423af8617c.json
+++ /dev/null
@@ -1,16 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)\n SELECT \n u.id,\n u.entity_id,\n 'TASK'::property_entity_type,\n u.property_definition_id,\n NULL\n FROM UNNEST(\n $1::UUID[],\n $2::TEXT[],\n $3::UUID[]\n ) AS u(id, entity_id, property_definition_id)\n ON CONFLICT (entity_id, entity_type, property_definition_id)\n DO NOTHING\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "UuidArray",
- "TextArray",
- "UuidArray"
- ]
- },
- "nullable": []
- },
- "hash": "63492b018803ce26db5891964a0f52f37d2f54bd8243265c6561f4423af8617c"
-}
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-8d21bf0aa4c20589a55cf5777aa9c658fdacea4d0d0c7070aded12cccd373911.json b/rust/cloud-storage/macro_db_client/.sqlx/query-8d21bf0aa4c20589a55cf5777aa9c658fdacea4d0d0c7070aded12cccd373911.json
deleted file mode 100644
index 505a2bd912..0000000000
--- a/rust/cloud-storage/macro_db_client/.sqlx/query-8d21bf0aa4c20589a55cf5777aa9c658fdacea4d0d0c7070aded12cccd373911.json
+++ /dev/null
@@ -1,16 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n UPDATE entity_properties\n SET values = $2\n WHERE entity_id = $1\n AND property_definition_id = $3\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Text",
- "Jsonb",
- "Uuid"
- ]
- },
- "nullable": []
- },
- "hash": "8d21bf0aa4c20589a55cf5777aa9c658fdacea4d0d0c7070aded12cccd373911"
-}
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-9cd40fbb1ade51e9e0d49792839d1a363cada017252a6f653c2272608189bcc4.json b/rust/cloud-storage/macro_db_client/.sqlx/query-9cd40fbb1ade51e9e0d49792839d1a363cada017252a6f653c2272608189bcc4.json
deleted file mode 100644
index 9d717fa5f9..0000000000
--- a/rust/cloud-storage/macro_db_client/.sqlx/query-9cd40fbb1ade51e9e0d49792839d1a363cada017252a6f653c2272608189bcc4.json
+++ /dev/null
@@ -1,28 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT property_definition_id, values\n FROM entity_properties\n WHERE entity_id = $1\n AND entity_type = 'TASK'\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "property_definition_id",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "values",
- "type_info": "Jsonb"
- }
- ],
- "parameters": {
- "Left": [
- "Text"
- ]
- },
- "nullable": [
- false,
- true
- ]
- },
- "hash": "9cd40fbb1ade51e9e0d49792839d1a363cada017252a6f653c2272608189bcc4"
-}
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-f2d5d7aea9918978cd69c14f8afe24156c2fa387e001ee8eaf57e4a70a106069.json b/rust/cloud-storage/macro_db_client/.sqlx/query-f2d5d7aea9918978cd69c14f8afe24156c2fa387e001ee8eaf57e4a70a106069.json
deleted file mode 100644
index 182e0858ba..0000000000
--- a/rust/cloud-storage/macro_db_client/.sqlx/query-f2d5d7aea9918978cd69c14f8afe24156c2fa387e001ee8eaf57e4a70a106069.json
+++ /dev/null
@@ -1,18 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)\n SELECT \n u.id,\n u.entity_id,\n u.entity_type::property_entity_type,\n u.property_definition_id,\n u.values\n FROM UNNEST(\n $1::UUID[],\n $2::TEXT[],\n $3::TEXT[],\n $4::UUID[],\n $5::JSONB[]\n ) AS u(id, entity_id, entity_type, property_definition_id, values)\n ON CONFLICT (entity_id, entity_type, property_definition_id)\n DO UPDATE SET \n values = EXCLUDED.values,\n updated_at = NOW()\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "UuidArray",
- "TextArray",
- "TextArray",
- "UuidArray",
- "JsonbArray"
- ]
- },
- "nullable": []
- },
- "hash": "f2d5d7aea9918978cd69c14f8afe24156c2fa387e001ee8eaf57e4a70a106069"
-}
diff --git a/rust/cloud-storage/macro_db_client/migrations/20260331152752_add_entity_access_table.sql b/rust/cloud-storage/macro_db_client/migrations/20260331152752_add_entity_access_table.sql
new file mode 100644
index 0000000000..80fd6ccb1f
--- /dev/null
+++ b/rust/cloud-storage/macro_db_client/migrations/20260331152752_add_entity_access_table.sql
@@ -0,0 +1,41 @@
+CREATE TYPE entity_access_source_type AS ENUM ('channel', 'team', 'user');
+
+CREATE TABLE entity_access
+(
+ id BIGSERIAL PRIMARY KEY,
+ entity_id UUID NOT NULL,
+ entity_type TEXT NOT NULL,
+ source_id TEXT NOT NULL,
+ source_type entity_access_source_type NOT NULL,
+ access_level "AccessLevel" NOT NULL,
+ granted_from_project_id TEXT,
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
+);
+
+-- Ensure project deletion automatically cleans up all access granted from a given project
+ALTER TABLE "entity_access"
+ ADD CONSTRAINT "entity_access_granted_from_project_id_fkey"
+ FOREIGN KEY ("granted_from_project_id") REFERENCES "Project" ("id") ON DELETE CASCADE;
+
+-- Fast lookup on entity id + type
+CREATE INDEX "entity_access_entity_id_entity_type_idx"
+ ON entity_access ("entity_id", "entity_type");
+
+-- Fast lookup on source_id
+CREATE INDEX "entity_access_source_id_idx"
+ ON entity_access ("source_id");
+
+-- Fast lookup based on project_id
+CREATE INDEX "entity_access_granted_from_project_id_idx"
+ ON entity_access ("granted_from_project_id");
+
+-- Uniqueness for records with a granted_from_project_id
+CREATE UNIQUE INDEX "entity_access_unique_with_project"
+ ON entity_access ("entity_id", "entity_type", "source_id", "source_type", "granted_from_project_id")
+ WHERE "granted_from_project_id" IS NOT NULL;
+
+-- Uniqueness for records without a granted_from_project_id (e.g. creator/owner entries)
+CREATE UNIQUE INDEX "entity_access_unique_without_project"
+ ON entity_access ("entity_id", "entity_type", "source_id", "source_type")
+ WHERE "granted_from_project_id" IS NULL;