Files
lincodex/backend/db/migrations/00038_send_invitations.sql

118 lines
4.2 KiB
SQL

-- +goose Up
-- Sending an invitation. One transaction moves a draft to `delivered`, starts
-- the response deadline, charges the organization quota, appends the audit
-- event, and writes the outbox events of the notification. The send itself is
-- never a stored state: the audit event of the step is `delivered`, and it
-- changes none of the editable draft fields.
ALTER TABLE invitation_events
DROP CONSTRAINT invitation_events_kind_known;
ALTER TABLE invitation_events
ADD CONSTRAINT invitation_events_kind_known CHECK (
kind IN ('draft_created', 'draft_updated', 'delivered')
);
ALTER TABLE invitation_events
ADD CONSTRAINT invitation_events_delivery_changes_no_field CHECK (
kind <> 'delivered' OR cardinality(changed_fields) = 0
);
-- Invitation quota usage now names a stored invitation of the same
-- organization, so a charge cannot exist without the invitation it paid for.
-- Before sending existed nothing in the product wrote this table; a row that
-- names no invitation is reported instead of being dropped.
-- +goose StatementBegin
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM company_invitation_quota_usage AS usage
WHERE NOT EXISTS (
SELECT 1
FROM invitations
WHERE
invitations.organization_id = usage.organization_id
AND invitations.id = usage.invitation_id
)
) THEN
RAISE EXCEPTION USING
ERRCODE = 'foreign_key_violation',
CONSTRAINT = 'company_invitation_quota_usage_invitation_fkey',
TABLE = 'company_invitation_quota_usage',
MESSAGE = 'invitation quota usage must name stored invitations';
END IF;
END;
$$;
-- +goose StatementEnd
ALTER TABLE company_invitation_quota_usage
ADD CONSTRAINT company_invitation_quota_usage_invitation_fkey
FOREIGN KEY (organization_id, invitation_id)
REFERENCES invitations (organization_id, id) ON DELETE RESTRICT;
-- Outcome of an idempotent send, kept beside the generic idempotency record
-- and removed with it after the 24-hour replay window. A successful send
-- keeps the original response; a missing invitation and a state conflict
-- keep only the outcome, as the public contract stores them under the key.
-- The invitation is intentionally not a foreign key: the outcome of a missing
-- invitation names an identifier that does not exist.
CREATE TABLE invitation_send_results (
scope_hash bytea NOT NULL,
organization_id uuid NOT NULL,
invitation_id uuid NOT NULL,
outcome text NOT NULL,
snapshot jsonb,
CONSTRAINT invitation_send_results_pkey PRIMARY KEY (scope_hash),
CONSTRAINT invitation_send_results_idempotency_fkey FOREIGN KEY (
scope_hash
) REFERENCES idempotency_records (scope_hash) ON DELETE CASCADE,
CONSTRAINT invitation_send_results_scope_hash_sha256 CHECK (
octet_length(scope_hash) = 32
),
CONSTRAINT invitation_send_results_outcome CHECK (
(
outcome = 'sent'
AND snapshot IS NOT NULL
AND jsonb_typeof(snapshot) = 'object'
AND octet_length(snapshot::text) <= 196608
)
OR (
outcome IN ('not_found', 'state_conflict')
AND snapshot IS NULL
)
)
);
-- +goose Down
-- A rollback must not silently remove delivery history or a stored outcome.
-- +goose StatementBegin
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM invitation_events WHERE kind = 'delivered')
OR EXISTS (SELECT 1 FROM invitation_send_results) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'invitation_deliveries_retained',
TABLE = 'invitation_events',
MESSAGE = 'sent invitations must be resolved before rollback';
END IF;
END;
$$;
-- +goose StatementEnd
DROP TABLE invitation_send_results;
ALTER TABLE company_invitation_quota_usage
DROP CONSTRAINT company_invitation_quota_usage_invitation_fkey;
ALTER TABLE invitation_events
DROP CONSTRAINT invitation_events_delivery_changes_no_field;
ALTER TABLE invitation_events
DROP CONSTRAINT invitation_events_kind_known;
ALTER TABLE invitation_events
ADD CONSTRAINT invitation_events_kind_known CHECK (
kind IN ('draft_created', 'draft_updated')
);