112 lines
4.0 KiB
SQL
112 lines
4.0 KiB
SQL
-- +goose Up
|
|
-- Product notifications get their own sending worker. It claims them through
|
|
-- deliveries_product_due_idx, re-checks the rights of their subject before
|
|
-- the transport is called, and cancels a notice whose basis no longer holds
|
|
-- with one of the reasons below. A cancelled notification loses its bodies,
|
|
-- as a token mail does, so a withdrawn notice keeps no text in the table.
|
|
-- The service worker and deliveries_due_idx do not change.
|
|
|
|
ALTER TABLE deliveries DROP CONSTRAINT deliveries_cancel_reason_matches_status;
|
|
ALTER TABLE deliveries ADD CONSTRAINT deliveries_cancel_reason_matches_status CHECK (
|
|
(status = 'cancelled') = (cancel_reason IS NOT NULL)
|
|
AND (
|
|
cancel_reason IS NULL
|
|
OR cancel_reason IN (
|
|
'token_missing',
|
|
'token_purpose_mismatch',
|
|
'token_owner_mismatch',
|
|
'token_consumed',
|
|
'token_revoked',
|
|
'token_expired',
|
|
'account_not_current',
|
|
'recipient_mismatch',
|
|
'content_invalid',
|
|
'processing_failed',
|
|
'rights_revoked',
|
|
'recipient_ineligible',
|
|
'source_closed'
|
|
)
|
|
)
|
|
);
|
|
|
|
-- A redacted mail has no bodies. A token mail may be redacted once it is
|
|
-- terminal; a notification once it is cancelled.
|
|
ALTER TABLE deliveries DROP CONSTRAINT deliveries_body_present;
|
|
ALTER TABLE deliveries ADD CONSTRAINT deliveries_body_present CHECK (
|
|
(body_redacted_at IS NULL AND (text_body <> '' OR html_body <> ''))
|
|
OR (
|
|
body_redacted_at IS NOT NULL
|
|
AND text_body = '' AND html_body = ''
|
|
AND (
|
|
(
|
|
kind IN ('verification', 'password_reset')
|
|
AND status IN ('accepted', 'rejected', 'cancelled')
|
|
)
|
|
OR (kind = 'notification' AND status = 'cancelled')
|
|
)
|
|
)
|
|
);
|
|
|
|
-- Serves the claim of due product notifications. The predicate names the
|
|
-- kind literal the product claim repeats, so an idle poll of either worker
|
|
-- reads only its own index and never the other lane's backlog.
|
|
CREATE INDEX deliveries_product_due_idx
|
|
ON deliveries (next_attempt_at, id)
|
|
WHERE
|
|
status IN ('pending', 'waiting_quota', 'unknown')
|
|
AND next_attempt_at IS NOT NULL
|
|
AND kind = 'notification';
|
|
|
|
-- +goose Down
|
|
DROP INDEX deliveries_product_due_idx;
|
|
|
|
-- Rows that name the reasons of this version, or redacted notifications,
|
|
-- cannot satisfy the checks of the earlier schema. Refuse before changing
|
|
-- them, so the rollback never rewrites a cancellation.
|
|
-- +goose StatementBegin
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (
|
|
SELECT 1 FROM deliveries
|
|
WHERE cancel_reason IN ('rights_revoked', 'recipient_ineligible', 'source_closed')
|
|
OR (kind = 'notification' AND body_redacted_at IS NOT NULL)
|
|
) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'feature_not_supported',
|
|
MESSAGE = 'notifications cancelled by a rights check are stored; export or resend them before this rollback';
|
|
END IF;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
ALTER TABLE deliveries DROP CONSTRAINT deliveries_body_present;
|
|
ALTER TABLE deliveries ADD CONSTRAINT deliveries_body_present CHECK (
|
|
(body_redacted_at IS NULL AND (text_body <> '' OR html_body <> ''))
|
|
OR (
|
|
body_redacted_at IS NOT NULL
|
|
AND text_body = '' AND html_body = ''
|
|
AND kind IN ('verification', 'password_reset')
|
|
AND status IN ('accepted', 'rejected', 'cancelled')
|
|
)
|
|
);
|
|
|
|
ALTER TABLE deliveries DROP CONSTRAINT deliveries_cancel_reason_matches_status;
|
|
ALTER TABLE deliveries ADD CONSTRAINT deliveries_cancel_reason_matches_status CHECK (
|
|
(status = 'cancelled') = (cancel_reason IS NOT NULL)
|
|
AND (
|
|
cancel_reason IS NULL
|
|
OR cancel_reason IN (
|
|
'token_missing',
|
|
'token_purpose_mismatch',
|
|
'token_owner_mismatch',
|
|
'token_consumed',
|
|
'token_revoked',
|
|
'token_expired',
|
|
'account_not_current',
|
|
'recipient_mismatch',
|
|
'content_invalid',
|
|
'processing_failed'
|
|
)
|
|
)
|
|
);
|