116 lines
5.7 KiB
PL/PgSQL
116 lines
5.7 KiB
PL/PgSQL
-- +goose Up
|
|
-- Resend may accept a request even when the caller loses the response. Keep
|
|
-- that delivery unknown, but schedule bounded retries while its provider key
|
|
-- is still inside the deduplication window. An operator decision after the
|
|
-- automatic cutoff is append-only and single-use; manual send markers never
|
|
-- reset the provider's idempotency window.
|
|
|
|
ALTER TABLE deliveries
|
|
ADD COLUMN manual_retry_authorized_at timestamptz,
|
|
ADD COLUMN manual_retry_started_at timestamptz,
|
|
ADD COLUMN send_started_attempt_count integer;
|
|
|
|
COMMENT ON COLUMN deliveries.manual_retry_authorized_at IS
|
|
'Database time of the latest audited operator authorization to retry a terminal unknown delivery; it does not replace the original send history.';
|
|
COMMENT ON COLUMN deliveries.manual_retry_started_at IS
|
|
'Database time when the first provider call of the latest manual authorization started; this is a single-use/recovery marker and never resets the provider deduplication window.';
|
|
COMMENT ON COLUMN deliveries.send_started_attempt_count IS
|
|
'Attempt count when the latest provider call started. Equality with attempt_count means its result was not recorded; a greater attempt_count means a provider result was stored after that send-start.';
|
|
|
|
ALTER TABLE deliveries ADD CONSTRAINT deliveries_manual_retry_cycle_complete CHECK (
|
|
manual_retry_started_at IS NULL OR manual_retry_authorized_at IS NOT NULL
|
|
);
|
|
ALTER TABLE deliveries ADD CONSTRAINT deliveries_send_started_attempt_count_valid CHECK (
|
|
send_started_attempt_count IS NULL
|
|
OR (send_started_attempt_count >= 0 AND send_started_attempt_count <= attempt_count)
|
|
);
|
|
|
|
-- Keep the decision after a delivery or account is deleted. This table holds
|
|
-- only an opaque delivery ID, attempt number, operator handle, acknowledgement
|
|
-- and decision time; it never stores recipient data or message content.
|
|
CREATE TABLE delivery_retry_decisions (
|
|
decision_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
delivery_id uuid NOT NULL,
|
|
attempt_count integer NOT NULL CHECK (attempt_count > 0),
|
|
actor text NOT NULL CHECK (
|
|
length(actor) <= 128
|
|
AND actor ~ '^operator:[a-z0-9][a-z0-9._-]*$'
|
|
),
|
|
duplicate_risk_acknowledged boolean NOT NULL CHECK (duplicate_risk_acknowledged),
|
|
previous_lease_expired boolean NOT NULL,
|
|
decided_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
|
);
|
|
|
|
CREATE INDEX delivery_retry_decisions_delivery_idx
|
|
ON delivery_retry_decisions (delivery_id, decision_id);
|
|
|
|
COMMENT ON TABLE delivery_retry_decisions IS
|
|
'Append-only record of an operator decision to retry a terminal unknown mail delivery after its provider deduplication window. No foreign key so history survives account deletion.';
|
|
COMMENT ON COLUMN delivery_retry_decisions.delivery_id IS
|
|
'Opaque ID of the delivery whose current unknown attempt was authorized for another send cycle.';
|
|
COMMENT ON COLUMN delivery_retry_decisions.actor IS
|
|
'Operator handle in the form operator:<handle>; never an email address or credential.';
|
|
COMMENT ON COLUMN delivery_retry_decisions.duplicate_risk_acknowledged IS
|
|
'Must be true: after the provider key expires, reusing it does not guarantee deduplication.';
|
|
COMMENT ON COLUMN delivery_retry_decisions.previous_lease_expired IS
|
|
'True when this decision also cleared an expired worker lease after a manual send began but its result was not recorded.';
|
|
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION delivery_retry_decisions_refuse_change() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF TG_OP = 'TRUNCATE' THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'delivery_retry_decisions_immutable',
|
|
TABLE = 'delivery_retry_decisions',
|
|
MESSAGE = 'mail retry decisions are append-only';
|
|
END IF;
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'delivery_retry_decisions_immutable',
|
|
TABLE = 'delivery_retry_decisions',
|
|
MESSAGE = 'mail retry decisions are append-only';
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER delivery_retry_decisions_refuse_update_delete
|
|
BEFORE UPDATE OR DELETE ON delivery_retry_decisions
|
|
FOR EACH ROW EXECUTE FUNCTION delivery_retry_decisions_refuse_change();
|
|
|
|
CREATE TRIGGER delivery_retry_decisions_refuse_truncate
|
|
BEFORE TRUNCATE ON delivery_retry_decisions
|
|
FOR EACH STATEMENT EXECUTE FUNCTION delivery_retry_decisions_refuse_change();
|
|
|
|
-- A due unknown row is claimable only when the retry policy gave it a due
|
|
-- time. Keep the worker's single indexed queue broad enough for all due
|
|
-- service-mail statuses.
|
|
DROP INDEX deliveries_due_idx;
|
|
CREATE INDEX deliveries_due_idx
|
|
ON deliveries (next_attempt_at, id)
|
|
WHERE
|
|
status IN ('pending', 'waiting_quota', 'unknown')
|
|
AND next_attempt_at IS NOT NULL
|
|
AND kind IN ('verification', 'password_reset');
|
|
|
|
-- +goose Down
|
|
-- Rolling back this migration removes its append-only operator decision
|
|
-- history. Export or preserve that audit data before an intentional rollback.
|
|
DROP INDEX deliveries_due_idx;
|
|
CREATE INDEX deliveries_due_idx
|
|
ON deliveries (next_attempt_at, id)
|
|
WHERE
|
|
status IN ('pending', 'waiting_quota')
|
|
AND next_attempt_at IS NOT NULL
|
|
AND kind IN ('verification', 'password_reset');
|
|
|
|
DROP TRIGGER delivery_retry_decisions_refuse_update_delete ON delivery_retry_decisions;
|
|
DROP TRIGGER delivery_retry_decisions_refuse_truncate ON delivery_retry_decisions;
|
|
DROP FUNCTION delivery_retry_decisions_refuse_change();
|
|
DROP INDEX delivery_retry_decisions_delivery_idx;
|
|
DROP TABLE delivery_retry_decisions;
|
|
ALTER TABLE deliveries DROP CONSTRAINT deliveries_manual_retry_cycle_complete;
|
|
ALTER TABLE deliveries DROP CONSTRAINT deliveries_send_started_attempt_count_valid;
|
|
ALTER TABLE deliveries DROP COLUMN send_started_attempt_count, DROP COLUMN manual_retry_started_at, DROP COLUMN manual_retry_authorized_at;
|