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.

fix: migration attachment -> entity (#2997)

Eric Hayesd44ba0dbd802
diff --git a/rust/cloud-storage/macro_db_client/migrations/20260430120001_chat_attachment_to_entity.down.sql b/rust/cloud-storage/macro_db_client/migrations/20260430120001_chat_attachment_to_entity.down.sql
index 0ab5f8a959..b13ea2a866 100644
--- a/rust/cloud-storage/macro_db_client/migrations/20260430120001_chat_attachment_to_entity.down.sql
+++ b/rust/cloud-storage/macro_db_client/migrations/20260430120001_chat_attachment_to_entity.down.sql
@@ -2,6 +2,20 @@
BEGIN;
+-- Backfill old columns for rows inserted after the up migration
+UPDATE "ChatAttachment"
+SET
+ "old_attachmentType" = CASE "entity_type"
+ WHEN 'document' THEN 'document'
+ WHEN 'static_file' THEN 'image'
+ WHEN 'channel' THEN 'channel'
+ WHEN 'email_thread' THEN 'email'
+ WHEN 'project' THEN 'project'
+ ELSE 'document'
+ END,
+ "old_attachmentId" = "entity_id"::TEXT
+WHERE "old_attachmentType" IS NULL;
+
-- Restore NOT NULL on old columns
ALTER TABLE "ChatAttachment"
ALTER COLUMN "old_attachmentType" SET NOT NULL;
diff --git a/rust/cloud-storage/macro_db_client/migrations/20260430120001_chat_attachment_to_entity.up.sql b/rust/cloud-storage/macro_db_client/migrations/20260430120001_chat_attachment_to_entity.up.sql
index 900173c57b..29f5fa61b0 100644
--- a/rust/cloud-storage/macro_db_client/migrations/20260430120001_chat_attachment_to_entity.up.sql
+++ b/rust/cloud-storage/macro_db_client/migrations/20260430120001_chat_attachment_to_entity.up.sql
@@ -9,6 +9,10 @@ ALTER TABLE "ChatAttachment"
ADD COLUMN "entity_type" TEXT,
ADD COLUMN "entity_id" UUID;
+-- Drop rows with invalid UUIDs
+DELETE FROM "ChatAttachment"
+WHERE "attachmentId" !~ '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$';
+
-- Migrate data: map old attachmentType values to EntityType values, cast id to UUID
UPDATE "ChatAttachment"
SET