-- +goose Up -- An escalation step carries its own deadline. The path was a list of person -- ids; each entry becomes a person and the time that step waits, starting from -- the default approval deadline. UPDATE governance.settings s SET escalation = COALESCE(( SELECT jsonb_agg(jsonb_build_object('person', e.person, 'deadline_ms', s.approval_deadline_ms) ORDER BY e.n) FROM jsonb_array_elements_text(s.escalation) WITH ORDINALITY AS e (person, n) ), '[]'::jsonb); -- A request records who has been asked so far and when the current step runs -- out, so an answer is accepted only from someone the request has reached. A -- discarded approval is one the action no longer matched when it came to run. -- counted is set once, before the approval counts toward a raise, so a retry -- can't count it twice. ALTER TABLE approvals.requests DROP CONSTRAINT requests_state_check; ALTER TABLE approvals.requests ADD CONSTRAINT requests_state_check CHECK (state IN ('pending', 'approved', 'denied', 'expired', 'discarded')); ALTER TABLE approvals.requests ADD COLUMN step integer NOT NULL DEFAULT 0; ALTER TABLE approvals.requests ADD COLUMN step_deadline timestamptz; ALTER TABLE approvals.requests ADD COLUMN asked jsonb NOT NULL DEFAULT '[]'; ALTER TABLE approvals.requests ADD COLUMN change_request boolean NOT NULL DEFAULT false; ALTER TABLE approvals.requests ADD COLUMN counted boolean NOT NULL DEFAULT false; ALTER TABLE approvals.requests ADD COLUMN discarded_because text; -- +goose Down ALTER TABLE approvals.requests DROP COLUMN discarded_because; ALTER TABLE approvals.requests DROP COLUMN counted; ALTER TABLE approvals.requests DROP COLUMN change_request; ALTER TABLE approvals.requests DROP COLUMN asked; ALTER TABLE approvals.requests DROP COLUMN step_deadline; ALTER TABLE approvals.requests DROP COLUMN step; UPDATE approvals.requests SET state = 'denied' WHERE state = 'discarded'; ALTER TABLE approvals.requests DROP CONSTRAINT requests_state_check; ALTER TABLE approvals.requests ADD CONSTRAINT requests_state_check CHECK (state IN ('pending', 'approved', 'denied', 'expired')); UPDATE governance.settings s SET escalation = COALESCE(( SELECT jsonb_agg(e.step ->> 'person' ORDER BY e.n) FROM jsonb_array_elements(s.escalation) WITH ORDINALITY AS e (step, n) ), '[]'::jsonb);