30 lines
1.4 KiB
PL/PgSQL
30 lines
1.4 KiB
PL/PgSQL
CREATE TABLE bell_events (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
producer_id text NOT NULL,
|
|
source_event_id text NOT NULL,
|
|
event_type text NOT NULL,
|
|
occurred_at timestamptz NOT NULL,
|
|
location text NOT NULL,
|
|
severity text NOT NULL CHECK (severity IN ('low','medium','high','critical')),
|
|
evidence_ref text,
|
|
normalized_payload jsonb NOT NULL,
|
|
payload_sha256 bytea NOT NULL,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
UNIQUE (producer_id, source_event_id)
|
|
);
|
|
CREATE INDEX bell_events_created_page_idx ON bell_events(created_at DESC,id DESC);
|
|
CREATE TABLE bell_event_receipts (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
event_id uuid NOT NULL UNIQUE REFERENCES bell_events(id),
|
|
producer_id text NOT NULL,
|
|
source_event_id text NOT NULL,
|
|
payload_sha256 bytea NOT NULL,
|
|
accepted_at timestamptz NOT NULL DEFAULT now(),
|
|
UNIQUE (producer_id, source_event_id)
|
|
);
|
|
|
|
CREATE OR REPLACE FUNCTION bell_reject_event_mutation() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
BEGIN RAISE EXCEPTION 'Bell Event and Receipt facts are immutable'; END $$;
|
|
CREATE TRIGGER bell_event_no_update BEFORE UPDATE OR DELETE ON bell_events FOR EACH ROW EXECUTE FUNCTION bell_reject_event_mutation();
|
|
CREATE TRIGGER bell_receipt_no_update BEFORE UPDATE OR DELETE ON bell_event_receipts FOR EACH ROW EXECUTE FUNCTION bell_reject_event_mutation();
|