189 lines
8.2 KiB
PL/PgSQL
189 lines
8.2 KiB
PL/PgSQL
-- +goose Up
|
|
-- A notification names the record it is about (its subject), so the sending
|
|
-- worker can re-check the rights the notice depends on before it calls the
|
|
-- transport. The subject is fixed when the request is stored and is copied to
|
|
-- the delivery, where the content trigger keeps it unchanged. The three
|
|
-- columns are set together. Only the kind invitation exists so far. A
|
|
-- notification without a subject stays valid: the sending worker cancels it
|
|
-- with source_closed, because it has no right to check.
|
|
--
|
|
-- Everything here is additive: existing rows keep NULL subjects.
|
|
ALTER TABLE mail_requests
|
|
ADD COLUMN subject_kind text,
|
|
ADD COLUMN subject_id uuid,
|
|
ADD COLUMN subject_organization_id uuid,
|
|
ADD CONSTRAINT mail_requests_subject_complete CHECK (
|
|
(subject_kind IS NULL) = (subject_id IS NULL)
|
|
AND (subject_kind IS NULL) = (subject_organization_id IS NULL)
|
|
AND (subject_kind IS NULL OR subject_kind IN ('invitation'))
|
|
);
|
|
|
|
COMMENT ON COLUMN mail_requests.subject_kind IS
|
|
'The kind of record the notification is about, NULL when it names none; the sending worker checks the rights of that kind.';
|
|
COMMENT ON COLUMN mail_requests.subject_id IS
|
|
'The record the notification is about, for example the invitation; an opaque ID, never an address or a contact.';
|
|
COMMENT ON COLUMN mail_requests.subject_organization_id IS
|
|
'The organization the subject record belongs to, for example the organization that sent the invitation.';
|
|
|
|
ALTER TABLE deliveries
|
|
ADD COLUMN subject_kind text,
|
|
ADD COLUMN subject_id uuid,
|
|
ADD COLUMN subject_organization_id uuid,
|
|
ADD CONSTRAINT deliveries_subject_complete CHECK (
|
|
(subject_kind IS NULL) = (subject_id IS NULL)
|
|
AND (subject_kind IS NULL) = (subject_organization_id IS NULL)
|
|
AND (subject_kind IS NULL OR subject_kind IN ('invitation'))
|
|
AND (subject_kind IS NULL OR kind = 'notification')
|
|
);
|
|
|
|
COMMENT ON COLUMN deliveries.subject_kind IS
|
|
'The kind of record a notification is about; set only for a notification, NULL when it names none. The product sending worker re-checks its rights before the send.';
|
|
COMMENT ON COLUMN deliveries.subject_id IS
|
|
'The record a notification is about; an opaque ID, never an address or a contact.';
|
|
COMMENT ON COLUMN deliveries.subject_organization_id IS
|
|
'The organization the subject record belongs to.';
|
|
|
|
-- The subject is part of what the delivery sends and is fixed, like the
|
|
-- recipient and the content. This is the function of migration 00012 with the
|
|
-- three subject columns added to the unchanged tuple.
|
|
-- +goose StatementBegin
|
|
CREATE OR REPLACE FUNCTION deliveries_refuse_content_change() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF (
|
|
NEW.id, NEW.source_event_id, NEW.recipient_user_id,
|
|
NEW.recipient_email, NEW.kind, NEW.template_version, NEW.subject,
|
|
NEW.content_sha256,
|
|
NEW.idempotency_key, NEW.created_at, NEW.email_token_id,
|
|
NEW.subject_kind, NEW.subject_id, NEW.subject_organization_id
|
|
) IS DISTINCT FROM (
|
|
OLD.id, OLD.source_event_id, OLD.recipient_user_id,
|
|
OLD.recipient_email, OLD.kind, OLD.template_version, OLD.subject,
|
|
OLD.content_sha256,
|
|
OLD.idempotency_key, OLD.created_at, OLD.email_token_id,
|
|
OLD.subject_kind, OLD.subject_id, OLD.subject_organization_id
|
|
) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'deliveries_content_immutable',
|
|
TABLE = 'deliveries',
|
|
MESSAGE = 'delivery content and recipient are immutable';
|
|
END IF;
|
|
IF (NEW.text_body, NEW.html_body) IS DISTINCT FROM (OLD.text_body, OLD.html_body) THEN
|
|
IF NOT (
|
|
OLD.body_redacted_at IS NULL AND NEW.body_redacted_at IS NOT NULL
|
|
AND NEW.text_body = '' AND NEW.html_body = ''
|
|
AND NEW.status IN ('accepted', 'rejected', 'cancelled')
|
|
) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'deliveries_content_immutable',
|
|
TABLE = 'deliveries',
|
|
MESSAGE = 'delivery content and recipient are immutable';
|
|
END IF;
|
|
ELSIF OLD.body_redacted_at IS DISTINCT FROM NEW.body_redacted_at THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'deliveries_content_immutable',
|
|
TABLE = 'deliveries',
|
|
MESSAGE = 'delivery content and recipient are immutable';
|
|
END IF;
|
|
IF (
|
|
(NEW.provider, NEW.provider_account)
|
|
IS DISTINCT FROM (OLD.provider, OLD.provider_account)
|
|
) AND (OLD.attempt_count <> 0 OR NEW.attempt_count <> 0) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'deliveries_provider_pinned',
|
|
TABLE = 'deliveries',
|
|
MESSAGE = 'the provider account of a delivery is fixed after its first attempt';
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
-- +goose Down
|
|
-- Subjects name records whose rights a later version checks; dropping them
|
|
-- would send notices that were not checked. Refuse while any row names one.
|
|
-- +goose StatementBegin
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (SELECT 1 FROM deliveries WHERE subject_kind IS NOT NULL)
|
|
OR EXISTS (SELECT 1 FROM mail_requests WHERE subject_kind IS NOT NULL) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'feature_not_supported',
|
|
MESSAGE = 'notification subjects are stored; cancel or export the notifications that name one before this rollback';
|
|
END IF;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
-- The function of migration 00012 without the subject columns.
|
|
-- +goose StatementBegin
|
|
CREATE OR REPLACE FUNCTION deliveries_refuse_content_change() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF (
|
|
NEW.id, NEW.source_event_id, NEW.recipient_user_id,
|
|
NEW.recipient_email, NEW.kind, NEW.template_version, NEW.subject,
|
|
NEW.content_sha256,
|
|
NEW.idempotency_key, NEW.created_at, NEW.email_token_id
|
|
) IS DISTINCT FROM (
|
|
OLD.id, OLD.source_event_id, OLD.recipient_user_id,
|
|
OLD.recipient_email, OLD.kind, OLD.template_version, OLD.subject,
|
|
OLD.content_sha256,
|
|
OLD.idempotency_key, OLD.created_at, OLD.email_token_id
|
|
) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'deliveries_content_immutable',
|
|
TABLE = 'deliveries',
|
|
MESSAGE = 'delivery content and recipient are immutable';
|
|
END IF;
|
|
IF (NEW.text_body, NEW.html_body) IS DISTINCT FROM (OLD.text_body, OLD.html_body) THEN
|
|
IF NOT (
|
|
OLD.body_redacted_at IS NULL AND NEW.body_redacted_at IS NOT NULL
|
|
AND NEW.text_body = '' AND NEW.html_body = ''
|
|
AND NEW.status IN ('accepted', 'rejected', 'cancelled')
|
|
) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'deliveries_content_immutable',
|
|
TABLE = 'deliveries',
|
|
MESSAGE = 'delivery content and recipient are immutable';
|
|
END IF;
|
|
ELSIF OLD.body_redacted_at IS DISTINCT FROM NEW.body_redacted_at THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'deliveries_content_immutable',
|
|
TABLE = 'deliveries',
|
|
MESSAGE = 'delivery content and recipient are immutable';
|
|
END IF;
|
|
IF (
|
|
(NEW.provider, NEW.provider_account)
|
|
IS DISTINCT FROM (OLD.provider, OLD.provider_account)
|
|
) AND (OLD.attempt_count <> 0 OR NEW.attempt_count <> 0) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'deliveries_provider_pinned',
|
|
TABLE = 'deliveries',
|
|
MESSAGE = 'the provider account of a delivery is fixed after its first attempt';
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
ALTER TABLE deliveries DROP CONSTRAINT deliveries_subject_complete;
|
|
ALTER TABLE deliveries
|
|
DROP COLUMN subject_organization_id,
|
|
DROP COLUMN subject_id,
|
|
DROP COLUMN subject_kind;
|
|
|
|
ALTER TABLE mail_requests DROP CONSTRAINT mail_requests_subject_complete;
|
|
ALTER TABLE mail_requests
|
|
DROP COLUMN subject_organization_id,
|
|
DROP COLUMN subject_id,
|
|
DROP COLUMN subject_kind;
|