Files
lincodex/backend/db/migrations/00032_create_notification_inbox.sql

116 lines
4.5 KiB
PL/PgSQL

-- Inbox rows reference current product records instead of copying their
-- content. Recipient deletion removes the private inbox; organization context
-- remains retained while a source event refers to it.
-- +goose Up
CREATE TABLE notification_inbox_events (
recipient_user_id uuid NOT NULL,
event_id uuid NOT NULL,
organization_id uuid NOT NULL,
event_type text NOT NULL,
schema_version integer NOT NULL,
source_type text NOT NULL,
source_id uuid NOT NULL,
occurred_at timestamptz NOT NULL,
read_at timestamptz,
CONSTRAINT notification_inbox_events_pkey PRIMARY KEY (
recipient_user_id, event_id
),
CONSTRAINT notification_inbox_events_recipient_fkey FOREIGN KEY (
recipient_user_id
) REFERENCES users (id) ON DELETE CASCADE,
CONSTRAINT notification_inbox_events_organization_fkey FOREIGN KEY (
organization_id
) REFERENCES organizations (id) ON DELETE RESTRICT,
CONSTRAINT notification_inbox_events_type_known CHECK (
event_type = 'invitation.delivered'
AND schema_version = 1
AND source_type = 'invitation'
)
);
-- Supports a recipient's chronological inbox and exact organization filters.
CREATE INDEX notification_inbox_events_recipient_occurred_idx
ON notification_inbox_events (recipient_user_id, occurred_at DESC, event_id DESC);
CREATE INDEX notification_inbox_events_recipient_organization_occurred_idx
ON notification_inbox_events (
recipient_user_id, organization_id, occurred_at DESC, event_id DESC
);
-- The event reference is immutable. Only the first read timestamp may be
-- added; repeated mark-read calls may keep that timestamp unchanged.
-- +goose StatementBegin
CREATE FUNCTION notification_inbox_events_refuse_update() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF NEW.recipient_user_id IS NOT DISTINCT FROM OLD.recipient_user_id
AND NEW.event_id IS NOT DISTINCT FROM OLD.event_id
AND NEW.organization_id IS NOT DISTINCT FROM OLD.organization_id
AND NEW.event_type IS NOT DISTINCT FROM OLD.event_type
AND NEW.schema_version IS NOT DISTINCT FROM OLD.schema_version
AND NEW.source_type IS NOT DISTINCT FROM OLD.source_type
AND NEW.source_id IS NOT DISTINCT FROM OLD.source_id
AND NEW.occurred_at IS NOT DISTINCT FROM OLD.occurred_at
AND (
NEW.read_at IS NOT DISTINCT FROM OLD.read_at
OR (OLD.read_at IS NULL AND NEW.read_at IS NOT NULL)
) THEN
RETURN NEW;
END IF;
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'notification_inbox_events_immutable',
TABLE = 'notification_inbox_events',
MESSAGE = 'notification inbox event metadata is immutable';
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER notification_inbox_events_refuse_update
BEFORE UPDATE ON notification_inbox_events
FOR EACH ROW EXECUTE FUNCTION notification_inbox_events_refuse_update();
-- Prevent clearing a recipient's event marker while allowing account
-- deletion to cascade through private inbox rows.
-- +goose StatementBegin
CREATE FUNCTION notification_inbox_events_refuse_delete() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF pg_trigger_depth() > 1 THEN
RETURN OLD;
END IF;
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'notification_inbox_events_retained',
TABLE = 'notification_inbox_events',
MESSAGE = 'notification inbox events cannot be deleted directly';
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER notification_inbox_events_refuse_delete
BEFORE DELETE ON notification_inbox_events
FOR EACH ROW EXECUTE FUNCTION notification_inbox_events_refuse_delete();
-- A rollback must not silently remove recipient-visible history.
-- +goose Down
-- +goose StatementBegin
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM notification_inbox_events) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'notification_inbox_events_retained',
TABLE = 'notification_inbox_events',
MESSAGE = 'notification inbox records must be resolved before rollback';
END IF;
END;
$$;
-- +goose StatementEnd
DROP TRIGGER notification_inbox_events_refuse_update ON notification_inbox_events;
DROP FUNCTION notification_inbox_events_refuse_update();
DROP TRIGGER notification_inbox_events_refuse_delete ON notification_inbox_events;
DROP FUNCTION notification_inbox_events_refuse_delete();
DROP TABLE notification_inbox_events;