153 lines
5.4 KiB
PL/PgSQL
153 lines
5.4 KiB
PL/PgSQL
-- +goose Up
|
|
-- The pending limit per need, the pause after a decline, and the expiry at the
|
|
-- response deadline. A delivered or viewed invitation whose deadline has passed
|
|
-- moves to `expired` with the reason `invitation_timeout`. Nothing else changes
|
|
-- with it: no consent, no contact grant, and no organization quota is returned.
|
|
-- The expiry is written by a scheduled job, which the send schedules for every
|
|
-- delivery at its deadline, and by the candidate commands that find the
|
|
-- deadline passed. The system performs the expiry, so its audit event names no
|
|
-- actor.
|
|
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', 'viewed', 'declined',
|
|
'accepted', 'expired'
|
|
)
|
|
);
|
|
|
|
ALTER TABLE invitation_events
|
|
ADD CONSTRAINT invitation_events_expiry_changes_no_field CHECK (
|
|
kind <> 'expired' OR cardinality(changed_fields) = 0
|
|
);
|
|
|
|
-- Only the expiry is a server change, so only its event may lack an actor.
|
|
ALTER TABLE invitation_events
|
|
ALTER COLUMN actor_id DROP NOT NULL;
|
|
|
|
ALTER TABLE invitation_events
|
|
ADD CONSTRAINT invitation_events_system_actor CHECK (
|
|
(kind = 'expired') = (actor_id IS NULL)
|
|
);
|
|
|
|
-- Only a delivered or viewed invitation expires, and the expiry is not
|
|
-- recorded before the deadline it passed. The application compares the clock
|
|
-- with the deadline; this trigger keeps a row changed outside the application
|
|
-- from skipping either rule. An expiry without its reason is left to the
|
|
-- check constraint invitations_expiry_reason, which runs after this trigger.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION invitations_guard_expiry() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF NEW.state = 'expired' AND NEW.expiry_reason IS NOT NULL
|
|
AND OLD.state IS DISTINCT FROM NEW.state THEN
|
|
IF OLD.state NOT IN ('delivered', 'viewed') THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'invitations_expiry_from_pending',
|
|
TABLE = 'invitations',
|
|
MESSAGE = 'only a delivered or viewed invitation expires';
|
|
END IF;
|
|
IF NEW.updated_at < OLD.response_deadline_at THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'invitations_expiry_after_deadline',
|
|
TABLE = 'invitations',
|
|
MESSAGE = 'an invitation expires at or after its response deadline';
|
|
END IF;
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER invitations_guard_expiry
|
|
BEFORE UPDATE ON invitations
|
|
FOR EACH ROW EXECUTE FUNCTION invitations_guard_expiry();
|
|
|
|
-- The invitations that wait for an answer under one need of an organization:
|
|
-- the pending limit counts them.
|
|
CREATE INDEX invitations_pending_need_idx
|
|
ON invitations (organization_id, need_id)
|
|
WHERE need_id IS NOT NULL AND state IN ('delivered', 'viewed', 'counter');
|
|
|
|
-- The latest decline of one candidate by one organization, which starts the
|
|
-- pause before the next invitation of that pair.
|
|
CREATE INDEX invitations_declined_pair_idx
|
|
ON invitations (organization_id, candidate_id, responded_at DESC)
|
|
WHERE state = 'declined';
|
|
|
|
-- Invitations delivered before this migration have no deadline job yet. The
|
|
-- job's key is the invitation identifier, so the schedule is idempotent, and
|
|
-- its due time is the deadline that the invitation already stores.
|
|
INSERT INTO scheduled_jobs (
|
|
kind,
|
|
dedup_key,
|
|
payload,
|
|
due_at,
|
|
available_at,
|
|
status,
|
|
attempts
|
|
)
|
|
SELECT
|
|
'invitation.response_deadline',
|
|
invitations.id::text,
|
|
jsonb_build_object('invitation_id', invitations.id::text),
|
|
invitations.response_deadline_at AS due_at,
|
|
invitations.response_deadline_at AS available_at,
|
|
'pending',
|
|
0
|
|
FROM invitations
|
|
WHERE
|
|
invitations.state IN ('delivered', 'viewed')
|
|
AND invitations.response_deadline_at IS NOT NULL
|
|
ON CONFLICT (kind, dedup_key) DO NOTHING;
|
|
|
|
-- +goose Down
|
|
-- A rollback must not hide an expiry that already happened, and it must not
|
|
-- leave the deadline jobs without a handler.
|
|
-- +goose StatementBegin
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (SELECT 1 FROM invitation_events WHERE kind = 'expired')
|
|
OR EXISTS (SELECT 1 FROM invitations WHERE state = 'expired') THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'invitation_expiries_retained',
|
|
TABLE = 'invitation_events',
|
|
MESSAGE = 'expired invitations must be resolved before rollback';
|
|
END IF;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
DELETE FROM scheduled_jobs WHERE kind = 'invitation.response_deadline';
|
|
|
|
DROP INDEX invitations_declined_pair_idx;
|
|
DROP INDEX invitations_pending_need_idx;
|
|
|
|
DROP TRIGGER invitations_guard_expiry ON invitations;
|
|
DROP FUNCTION invitations_guard_expiry();
|
|
|
|
ALTER TABLE invitation_events
|
|
DROP CONSTRAINT invitation_events_system_actor;
|
|
|
|
ALTER TABLE invitation_events
|
|
ALTER COLUMN actor_id SET NOT NULL;
|
|
|
|
ALTER TABLE invitation_events
|
|
DROP CONSTRAINT invitation_events_expiry_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', 'delivered', 'viewed', 'declined',
|
|
'accepted'
|
|
)
|
|
);
|