Files
lincodex/backend/db/migrations/00007_create_mail_quota_usage.sql
T

111 lines
4.7 KiB
SQL

-- +goose Up
-- Mail quota usage: the recipients counted against a provider account per
-- UTC day and per UTC calendar month. The sending path reserves recipients
-- here before it calls the transport, in one transaction that locks the
-- window rows, so concurrent senders never exceed a configured limit
-- together. The limits themselves come from configuration
-- (EMAIL_QUOTA_DAILY, EMAIL_QUOTA_MONTHLY) and are not stored: lowering one
-- takes effect on the next reservation.
--
-- The table has no foreign key to users or deliveries on purpose. Deleting
-- an account deletes its deliveries and attempts, but the provider still
-- counted the messages already sent, so the usage must stay. The rows hold
-- no personal data: provider and account are configuration labels.
CREATE TABLE mail_quota_usage (
provider text NOT NULL,
provider_account text NOT NULL,
period_kind text NOT NULL,
-- The UTC date that starts the window: the day itself, or the first day
-- of the month.
period_start date NOT NULL,
used_recipients bigint NOT NULL,
-- Last time the provider itself refused a send for an exhausted quota in
-- this window; NULL when it never did. used_recipients was then raised
-- to the limit, and releases no longer lower it.
provider_exhausted_at timestamptz,
-- Application clock time of the last change.
updated_at timestamptz NOT NULL,
CONSTRAINT mail_quota_usage_pkey PRIMARY KEY (
provider, provider_account, period_kind, period_start
),
-- The same label rules as deliveries.provider and
-- deliveries.provider_account.
CONSTRAINT mail_quota_usage_provider_format CHECK (
provider ~ '^[a-z][a-z0-9_]{0,31}$'
),
CONSTRAINT mail_quota_usage_provider_account_format CHECK (
provider_account ~ '^[a-z0-9][a-z0-9._-]{0,63}$'
),
CONSTRAINT mail_quota_usage_period_kind_known CHECK (
period_kind IN ('day', 'month')
),
CONSTRAINT mail_quota_usage_month_starts_on_day_one CHECK (
period_kind <> 'month' OR extract(DAY FROM period_start) = 1
),
CONSTRAINT mail_quota_usage_used_not_negative CHECK (used_recipients >= 0)
);
-- Mail quota reservations: one row per admitted send, written in the
-- transaction that adds it to mail_quota_usage. The journal makes a release
-- happen at most once per reservation, whatever the caller retries, and lets
-- a retry of the same delivery inside the provider's idempotency window
-- reuse its open reservation instead of counting the message again.
--
-- Like the usage, it has no foreign key: a reservation outlives its delivery,
-- and delivery_id is an opaque ID without personal data. Every write that
-- also changes mail_quota_usage locks the usage rows first, in
-- (period_kind, period_start) order, and the journal row second.
CREATE TABLE mail_quota_reservations (
-- Generated in Go.
id uuid NOT NULL,
delivery_id uuid NOT NULL,
provider text NOT NULL,
provider_account text NOT NULL,
-- The windows the recipients were counted in.
day_start date NOT NULL,
month_start date NOT NULL,
recipients bigint NOT NULL,
reserved_at timestamptz NOT NULL,
-- Set once, when the recipients were given back.
released_at timestamptz,
-- The last settled outcome of a send under this reservation; NULL while
-- the send is in flight or its process stopped before settling.
settled_outcome text,
settled_at timestamptz,
CONSTRAINT mail_quota_reservations_pkey PRIMARY KEY (id),
CONSTRAINT mail_quota_reservations_provider_format CHECK (
provider ~ '^[a-z][a-z0-9_]{0,31}$'
),
CONSTRAINT mail_quota_reservations_provider_account_format CHECK (
provider_account ~ '^[a-z0-9][a-z0-9._-]{0,63}$'
),
CONSTRAINT mail_quota_reservations_windows_match CHECK (
extract(DAY FROM month_start) = 1
AND day_start >= month_start
AND day_start < month_start + INTERVAL '1 month'
),
CONSTRAINT mail_quota_reservations_recipients_positive CHECK (
recipients > 0
),
CONSTRAINT mail_quota_reservations_outcome_known CHECK (
(settled_outcome IS NULL) = (settled_at IS NULL)
AND (
settled_outcome IS NULL
OR settled_outcome IN ('accepted', 'unknown', 'rejected', 'not_sent')
)
),
CONSTRAINT mail_quota_reservations_times_ordered CHECK (
(released_at IS NULL OR released_at >= reserved_at)
AND (settled_at IS NULL OR settled_at >= reserved_at)
)
);
-- Serves the lookup of an open reservation of a delivery.
CREATE INDEX mail_quota_reservations_open_delivery_idx
ON mail_quota_reservations (delivery_id, reserved_at)
WHERE released_at IS NULL;
-- +goose Down
DROP TABLE mail_quota_reservations;
DROP TABLE mail_quota_usage;