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.

2.4 KB
Raw Blame
-- Collab surfaces: a stable id for an arbitrary collaborative markdown
-- surface (an input box, a message editor, a task field), backed by its own
-- Loro CRDT session in sync-service. The CRDT is the durable source of truth
-- for content; this table only records the surface's existence, its parent,
-- and its lifecycle state.
--
-- Ids are generated by the application as uuid v7 (macro_uuid), not by the
-- database. Surface ids are write-once by construction: sync-service
-- `initialize` is one-shot and the durable object has no delete route, so an
-- id is never recycled.
CREATE TABLE IF NOT EXISTS collab_surfaces
(
    id                 UUID        PRIMARY KEY NOT NULL,
    -- Required, polymorphic parent entity. All authorization derives from the
    -- caller's access to the parent (channel membership, document access,
    -- ...); a surface has no owner of its own because it is a shared fixture
    -- of the parent (e.g. a channel's shared input box). Every supported
    -- parent is keyed by a uuid (see the reminder table's entity_id note);
    -- polymorphism still rules out an FK, so deletion of the parent leaves
    -- dangling rows that readers must treat as inaccessible.
    parent_entity_type TEXT        NOT NULL,
    parent_entity_id   UUID        NOT NULL,
    -- pending → ready once the sync-service durable object has been
    -- initialized with the surface's first snapshot. Rows stuck in `pending`
    -- are failed creations (the service hard-deletes on init failure, so a
    -- surviving pending row means the service died mid-create).
    state              TEXT        NOT NULL DEFAULT 'pending'
        CHECK (state IN ('pending', 'ready')),
    created_at         TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at         TIMESTAMPTZ NOT NULL DEFAULT now(),
    -- Soft delete. The sync-service durable object is NOT reclaimed (same
    -- pre-existing gap as documents); a deleted row simply makes the surface
    -- unmintable, which cuts off all access.
    deleted_at         TIMESTAMPTZ,
    CONSTRAINT collab_surfaces_parent_type_non_empty
        CHECK (length(btrim(parent_entity_type)) > 0)
);

-- All live surfaces attached to a given parent entity (e.g. "the channel's
-- surfaces" at channel load).
CREATE INDEX IF NOT EXISTS collab_surfaces_parent_idx
    ON collab_surfaces (parent_entity_type, parent_entity_id)
    WHERE deleted_at IS NULL;