Files
lincodex/backend/db/migrations/00036_allow_synthetic_notification_event.sql
T

49 lines
1.6 KiB
SQL

-- The inbox allowlist gains a synthetic event type. It carries no product
-- meaning: it lets the consumer, the read-time visibility check, the list,
-- and the read state run end to end before real producers exist. Each event
-- type is bound to the one source type its content is resolved from.
-- +goose Up
ALTER TABLE notification_inbox_events
DROP CONSTRAINT notification_inbox_events_type_known;
ALTER TABLE notification_inbox_events
ADD CONSTRAINT notification_inbox_events_type_known CHECK (
schema_version = 1
AND (
(event_type = 'invitation.delivered' AND source_type = 'invitation')
OR (
event_type = 'notification.synthetic_delivered'
AND source_type = 'organization'
)
)
);
-- A rollback must not silently lose or orphan recipient-visible history.
-- +goose Down
-- +goose StatementBegin
DO $$
BEGIN
IF EXISTS (
SELECT 1 FROM notification_inbox_events
WHERE event_type <> 'invitation.delivered'
) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'notification_inbox_events_type_known',
TABLE = 'notification_inbox_events',
MESSAGE = 'synthetic notification inbox records must be resolved before rollback';
END IF;
END;
$$;
-- +goose StatementEnd
ALTER TABLE notification_inbox_events
DROP CONSTRAINT notification_inbox_events_type_known;
ALTER TABLE notification_inbox_events
ADD CONSTRAINT notification_inbox_events_type_known CHECK (
event_type = 'invitation.delivered'
AND schema_version = 1
AND source_type = 'invitation'
);