78 lines
3.5 KiB
SQL
78 lines
3.5 KiB
SQL
-- +goose Up
|
|
CREATE SCHEMA memory;
|
|
|
|
-- An incident is one investigation of one target: what the event was, the cause
|
|
-- the investigation found and what was done about it. The row is created when
|
|
-- the cause is recorded, and its actions are added when the run settles them.
|
|
CREATE TABLE memory.incidents (
|
|
id uuid PRIMARY KEY,
|
|
org_id text NOT NULL,
|
|
run_id uuid NOT NULL,
|
|
target_kind text NOT NULL,
|
|
target_id text NOT NULL,
|
|
cause text NOT NULL,
|
|
summary text NOT NULL,
|
|
event jsonb NOT NULL,
|
|
actions jsonb NOT NULL DEFAULT '[]',
|
|
at timestamptz NOT NULL DEFAULT now(),
|
|
UNIQUE (org_id, run_id)
|
|
);
|
|
CREATE INDEX incidents_by_target ON memory.incidents (org_id, target_kind, target_id, at DESC);
|
|
|
|
-- A note is one item of Archie's memory. class is its kind: working context for
|
|
-- one chat session, episodic history or enterprise retrieval in a collection,
|
|
-- or a durable fact.
|
|
CREATE TABLE memory.notes (
|
|
id uuid PRIMARY KEY,
|
|
org_id text NOT NULL,
|
|
class text NOT NULL CHECK (class IN ('working_context', 'episodic_history', 'enterprise_retrieval', 'durable_fact')),
|
|
scope text NOT NULL CHECK (scope IN ('person', 'shared')),
|
|
state text NOT NULL CHECK (state IN ('pending', 'published')),
|
|
owner_id text NOT NULL,
|
|
shared_by text NOT NULL DEFAULT '',
|
|
purpose text NOT NULL CHECK (purpose <> ''),
|
|
text text NOT NULL CHECK (text <> ''),
|
|
session_id uuid,
|
|
collection text,
|
|
disabled_at timestamptz,
|
|
retain_until timestamptz,
|
|
version integer NOT NULL DEFAULT 0,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
CHECK ((scope <> 'person') OR (state = 'published')),
|
|
CHECK ((scope <> 'person') OR (owner_id <> '')),
|
|
CHECK ((scope <> 'shared') OR (shared_by <> '')),
|
|
CHECK (((state = 'pending') AND (version = 0)) OR ((state = 'published') AND (version > 0))),
|
|
CHECK ((class <> 'working_context') OR (session_id IS NOT NULL)),
|
|
CHECK ((class <> 'episodic_history') OR (collection IS NOT NULL)),
|
|
CHECK ((class <> 'enterprise_retrieval') OR (collection IS NOT NULL)),
|
|
CHECK ((class <> 'durable_fact') OR ((session_id IS NULL) AND (collection IS NULL)))
|
|
);
|
|
CREATE INDEX notes_by_person ON memory.notes (org_id, owner_id, class, created_at DESC) WHERE (scope = 'person');
|
|
CREATE INDEX notes_published ON memory.notes (org_id, class, created_at DESC) WHERE ((scope = 'shared') AND (state = 'published'));
|
|
CREATE INDEX notes_pending ON memory.notes (org_id, created_at) WHERE ((scope = 'shared') AND (state = 'pending'));
|
|
|
|
-- Every published version of a shared note, so a publication can be rolled
|
|
-- back and its publisher and purpose read.
|
|
CREATE TABLE memory.note_versions (
|
|
note_id uuid NOT NULL REFERENCES memory.notes (id) ON DELETE CASCADE,
|
|
version integer NOT NULL CHECK (version > 0),
|
|
text text NOT NULL,
|
|
purpose text NOT NULL,
|
|
publisher_id text NOT NULL,
|
|
published_at timestamptz NOT NULL DEFAULT now(),
|
|
PRIMARY KEY (note_id, version)
|
|
);
|
|
|
|
-- Each committed deletion is listed here. The deletion record kept outside the
|
|
-- database must contain every deletion listed here, so a restore detects a
|
|
-- missing or incomplete record instead of trusting it.
|
|
CREATE TABLE memory.deletion_witness (
|
|
note_id uuid PRIMARY KEY,
|
|
org_id text NOT NULL,
|
|
at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
|
|
-- +goose Down
|
|
DROP SCHEMA memory CASCADE;
|