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.

4.3 KB
Raw Blame
-- Reminders: a user's scheduled nudges, optionally attached to an entity.
--
-- Ids are generated by the application as uuid v7 (macro_uuid), not by the
-- database, so they are time-ordered and known before the insert returns.
CREATE TABLE IF NOT EXISTS reminder
(
    id           UUID        PRIMARY KEY NOT NULL,
    user_id      TEXT        NOT NULL REFERENCES "User" (id) ON DELETE CASCADE,
    description  TEXT        NOT NULL,
    -- Optional, polymorphic entity association. Every reminderable entity is
    -- keyed by a uuid — email_threads, comms_channels, calls, crm_companies
    -- and crm_contacts are uuid columns already, and "Document"/"Chat"/
    -- "Project" hold uuid values in text columns pending their own migration.
    -- Storing uuid here keeps this table correct now instead of inheriting a
    -- conversion later. Polymorphism still rules out an FK; readers filter
    -- dangling rows.
    entity_type  TEXT,
    entity_id    UUID,
    -- One-shot mode: the single instant to fire at.
    remind_at    TIMESTAMPTZ,
    -- Recurring mode: a 6-/7-field cron expression interpreted in `timezone`.
    cron         TEXT,
    timezone     TEXT,
    -- Next firing, derived from the schedule on write so the dispatcher polls
    -- an index instead of parsing cron, and clients can render "next run".
    next_run_at  TIMESTAMPTZ NOT NULL,
    enabled      BOOLEAN     NOT NULL DEFAULT TRUE,
    -- Set once a reminder has fired and will not fire again. Only the one-shot
    -- mode reaches that state today; the dispatcher will decide what, if
    -- anything, completion means for a recurring reminder, so nothing here
    -- prevents the column being set on one.
    completed_at TIMESTAMPTZ,
    created_at   TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at   TIMESTAMPTZ NOT NULL DEFAULT now(),
    -- An entity association is both columns or neither.
    CONSTRAINT reminder_entity_both_or_neither
        CHECK ((entity_type IS NULL) = (entity_id IS NULL)),
    -- Exactly one schedule mode, and a cron always carries its timezone.
    CONSTRAINT reminder_schedule_exactly_one
        CHECK (
            num_nonnulls(remind_at, cron) = 1
            AND (cron IS NULL) = (timezone IS NULL)
        ),
    -- The DB is the final guardrail for what the service already validates, so
    -- a direct SQL path cannot leave an unusable row behind. `length()` counts
    -- characters, matching the service's char-based limit.
    CONSTRAINT reminder_description_non_empty
        CHECK (length(btrim(description)) > 0),
    CONSTRAINT reminder_description_max_length
        CHECK (length(btrim(description)) <= 2000),
    -- No non-empty check on entity_id: a uuid cannot be blank.
    CONSTRAINT reminder_entity_type_non_empty
        CHECK (entity_type IS NULL OR length(btrim(entity_type)) > 0)
);

-- The caller's reminders, in firing order. Serves `includeCompleted=true`,
-- which the partial index below cannot.
CREATE INDEX IF NOT EXISTS reminder_user_next_run_idx
    ON reminder (user_id, next_run_at);

-- The default list query excludes completed reminders and pages on
-- (next_run_at, created_at, id), so index exactly that.
CREATE INDEX IF NOT EXISTS reminder_user_active_next_run_idx
    ON reminder (user_id, next_run_at, created_at, id)
    WHERE completed_at IS NULL;

-- Reverse lookup: "what reminders are set on this entity?"
CREATE INDEX IF NOT EXISTS reminder_entity_idx
    ON reminder (entity_type, entity_id)
    WHERE entity_id IS NOT NULL;

-- Dispatcher lookup: reminders that are due and still live.
CREATE INDEX IF NOT EXISTS reminder_due_idx
    ON reminder (next_run_at)
    WHERE enabled AND completed_at IS NULL;

-- One row per firing of a reminder. Written by the dispatcher, which does not
-- exist yet; CRUD leaves this table empty. Ids are application-generated uuid
-- v7, as on `reminder`.
CREATE TABLE IF NOT EXISTS reminder_occurrence
(
    id            UUID        PRIMARY KEY NOT NULL,
    reminder_id   UUID        NOT NULL REFERENCES reminder (id) ON DELETE CASCADE,
    scheduled_for TIMESTAMPTZ NOT NULL,
    -- NULL until delivery succeeds.
    sent_at       TIMESTAMPTZ,
    created_at    TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- Makes delivery idempotent: a firing can only be claimed once.
CREATE UNIQUE INDEX IF NOT EXISTS reminder_occurrence_firing_idx
    ON reminder_occurrence (reminder_id, scheduled_for);