-- +goose Up -- Assessment completion supports the deliberately separate express outcome, -- and answers/finalization store immutable public responses for key replay. ALTER TABLE assessment_attempts DROP CONSTRAINT assessment_attempts_outcome, ADD CONSTRAINT assessment_attempts_outcome CHECK ( outcome IS NULL OR outcome IN ('confirmed', 'not_confirmed', 'not_determined', 'demonstration') ); ALTER TABLE assessment_attempt_idempotency_outcomes DROP CONSTRAINT assessment_attempt_idempotency_outcomes_operation, DROP CONSTRAINT assessment_attempt_idempotency_outcomes_status, ADD COLUMN pending_item_ids uuid[] NOT NULL DEFAULT '{}'::uuid[], ADD CONSTRAINT assessment_attempt_idempotency_outcomes_operation CHECK ( operation IN ('create', 'session', 'answer', 'finalize') ), ADD CONSTRAINT assessment_attempt_idempotency_outcomes_status CHECK ( (operation = 'create' AND status_code = 201 AND problem_code IS NULL AND response_snapshot IS NOT NULL) OR (operation = 'create' AND status_code = 409 AND problem_code = 'state_conflict' AND response_snapshot IS NULL) OR (operation = 'session' AND status_code = 200 AND problem_code IS NULL AND response_snapshot IS NOT NULL) OR (operation IN ('answer', 'finalize') AND status_code = 200 AND problem_code IS NULL AND response_snapshot IS NOT NULL) OR (operation IN ('answer', 'finalize') AND status_code = 404 AND problem_code = 'not_found' AND response_snapshot IS NULL) OR (operation = 'answer' AND status_code = 409 AND problem_code = 'state_conflict' AND response_snapshot IS NULL AND cardinality(pending_item_ids) = 0) OR (operation = 'finalize' AND status_code = 409 AND problem_code = 'state_conflict' AND response_snapshot IS NULL) ), ADD CONSTRAINT assessment_attempt_idempotency_outcomes_pending_items CHECK ( cardinality(pending_item_ids) <= 220 AND array_position(pending_item_ids, NULL) IS NULL AND (cardinality(pending_item_ids) = 0 OR (operation = 'finalize' AND status_code = 409 AND problem_code = 'state_conflict')) ); -- A completed result is stored once with its exact score, rationale, and -- pinned inputs. The public projection has no answer keys or submitted answers. CREATE TABLE assessment_attempt_results ( attempt_id uuid NOT NULL, user_id uuid NOT NULL, result_snapshot jsonb NOT NULL, schema_version smallint NOT NULL DEFAULT 1, kind text NOT NULL, mode text NOT NULL, outcome text NOT NULL, product_policy_version_id uuid NOT NULL REFERENCES policy_versions (id), product_policy_version integer NOT NULL, grading_policy_version_id uuid REFERENCES policy_versions (id), opening_id uuid REFERENCES assessment_category_openings (id), opening_manifest_sha256 bytea, training_binding_id uuid REFERENCES assessment_training_bank_bindings (id), training_manifest_sha256 bytea, bank_version text NOT NULL, algorithm_version text NOT NULL, rationale_rule text NOT NULL, pass_mark_basis_points integer, scored_items smallint, raw_score smallint, pass_points smallint, decided_at timestamptz NOT NULL, CONSTRAINT assessment_attempt_results_pkey PRIMARY KEY (attempt_id), CONSTRAINT assessment_attempt_results_owner_fk FOREIGN KEY (attempt_id, user_id) REFERENCES assessment_attempts (id, user_id) ON DELETE CASCADE, CONSTRAINT assessment_attempt_results_schema CHECK (schema_version = 1), CONSTRAINT assessment_attempt_results_snapshot CHECK ( jsonb_typeof(result_snapshot) = 'object' AND octet_length(result_snapshot::text) <= 65536 AND NOT (result_snapshot ?| ARRAY['answer', 'answers', 'answer_key', 'reference_answer', 'seed', 'variant', 'user_id']) AND NOT ((result_snapshot->'attempt') ?| ARRAY['answer', 'answers', 'answer_key', 'reference_answer', 'seed', 'variant', 'user_id']) ), CONSTRAINT assessment_attempt_results_versions CHECK ( product_policy_version > 0 AND bank_version ~ '^[A-Za-z0-9][A-Za-z0-9._-]{0,95}$' AND algorithm_version ~ '^[A-Za-z0-9][A-Za-z0-9._-]{0,95}$' ), CONSTRAINT assessment_attempt_results_decision CHECK ( (kind IN ('root', 'corrective') AND mode = 'demo_basic' AND outcome IN ('confirmed', 'not_confirmed') AND rationale_rule = 'demo_basic_pass_mark' AND grading_policy_version_id IS NOT NULL AND opening_id IS NOT NULL AND opening_manifest_sha256 IS NOT NULL AND octet_length(opening_manifest_sha256) = 32 AND training_binding_id IS NULL AND training_manifest_sha256 IS NULL AND pass_mark_basis_points BETWEEN 1 AND 10000 AND scored_items IS NOT NULL AND scored_items BETWEEN 1 AND 100 AND raw_score IS NOT NULL AND raw_score BETWEEN 0 AND scored_items AND pass_points IS NOT NULL AND pass_points BETWEEN 1 AND scored_items AND pass_points = ((scored_items * pass_mark_basis_points + 9999) / 10000) AND ((outcome = 'confirmed') = (raw_score >= pass_points))) OR (kind = 'demo_express' AND mode = 'demo_express' AND outcome = 'demonstration' AND rationale_rule = 'demonstration_only' AND grading_policy_version_id IS NULL AND opening_id IS NULL AND opening_manifest_sha256 IS NULL AND training_binding_id IS NOT NULL AND training_manifest_sha256 IS NOT NULL AND octet_length(training_manifest_sha256) = 32 AND pass_mark_basis_points IS NULL AND scored_items IS NULL AND raw_score IS NULL AND pass_points IS NULL) ) ); -- Keep the decision row bound to the completed attempt and exact pinned inputs. -- +goose StatementBegin CREATE FUNCTION assessment_attempt_result_guard() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE attempt assessment_attempts%ROWTYPE; BEGIN SELECT * INTO attempt FROM assessment_attempts WHERE id = NEW.attempt_id AND user_id = NEW.user_id FOR SHARE; IF NOT FOUND OR attempt.state <> 'completed' OR attempt.kind <> NEW.kind OR attempt.mode <> NEW.mode OR attempt.outcome <> NEW.outcome OR attempt.product_policy_version_id <> NEW.product_policy_version_id OR attempt.product_policy_version <> NEW.product_policy_version OR attempt.grading_policy_version_id IS DISTINCT FROM NEW.grading_policy_version_id OR attempt.opening_id IS DISTINCT FROM NEW.opening_id OR attempt.opening_manifest_sha256 IS DISTINCT FROM NEW.opening_manifest_sha256 OR attempt.training_binding_id IS DISTINCT FROM NEW.training_binding_id OR attempt.training_manifest_sha256 IS DISTINCT FROM NEW.training_manifest_sha256 OR attempt.bank_version <> NEW.bank_version OR attempt.algorithm_version <> NEW.algorithm_version OR attempt.ended_at IS DISTINCT FROM NEW.decided_at OR (NEW.result_snapshot->>'outcome') IS DISTINCT FROM NEW.outcome OR (NEW.result_snapshot->>'decided_at')::timestamptz IS DISTINCT FROM NEW.decided_at OR (NEW.result_snapshot->'attempt'->>'id')::uuid IS DISTINCT FROM NEW.attempt_id OR (NEW.result_snapshot->'attempt'->>'kind') IS DISTINCT FROM NEW.kind OR (NEW.result_snapshot->'attempt'->>'mode') IS DISTINCT FROM NEW.mode OR (NEW.result_snapshot->'pins'->>'product_policy_version_id')::uuid IS DISTINCT FROM NEW.product_policy_version_id OR (NEW.result_snapshot->'pins'->>'product_policy_version')::integer IS DISTINCT FROM NEW.product_policy_version OR (NEW.result_snapshot->'pins'->>'bank_version') IS DISTINCT FROM NEW.bank_version OR (NEW.result_snapshot->'pins'->>'algorithm_version') IS DISTINCT FROM NEW.algorithm_version OR (NEW.result_snapshot->'pins'->>'opening_id')::uuid IS DISTINCT FROM NEW.opening_id OR decode(NEW.result_snapshot->'pins'->>'opening_manifest_sha256', 'hex') IS DISTINCT FROM NEW.opening_manifest_sha256 OR (NEW.result_snapshot->'pins'->>'training_binding_id')::uuid IS DISTINCT FROM NEW.training_binding_id OR decode(NEW.result_snapshot->'pins'->>'training_manifest_sha256', 'hex') IS DISTINCT FROM NEW.training_manifest_sha256 OR (NEW.result_snapshot->'rationale'->>'rule') IS DISTINCT FROM NEW.rationale_rule OR (NEW.kind IN ('root', 'corrective') AND ( (NEW.result_snapshot->'basic_score'->>'points')::smallint IS DISTINCT FROM NEW.raw_score OR (NEW.result_snapshot->'basic_score'->>'max_points')::smallint IS DISTINCT FROM NEW.scored_items OR (NEW.result_snapshot->'basic_score'->>'pass_points')::smallint IS DISTINCT FROM NEW.pass_points OR (NEW.result_snapshot->'rationale'->>'pass_mark_basis_points')::integer IS DISTINCT FROM NEW.pass_mark_basis_points OR (NEW.result_snapshot->'rationale'->>'scored_items')::smallint IS DISTINCT FROM NEW.scored_items OR (NEW.result_snapshot->'rationale'->>'pass_points')::smallint IS DISTINCT FROM NEW.pass_points)) OR (NEW.kind = 'demo_express' AND NEW.result_snapshot ? 'basic_score') THEN RAISE EXCEPTION USING ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_result_snapshot_match', MESSAGE = 'assessment result does not match its completed attempt'; END IF; RETURN NEW; END; $$; -- +goose StatementEnd CREATE TRIGGER assessment_attempt_result_guard BEFORE INSERT ON assessment_attempt_results FOR EACH ROW EXECUTE FUNCTION assessment_attempt_result_guard(); -- Results cannot be overwritten or deleted independently of owner deletion. -- +goose StatementBegin CREATE FUNCTION assessment_attempt_result_refuse_mutation() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF TG_OP = 'DELETE' AND pg_trigger_depth() > 1 THEN RETURN OLD; END IF; RAISE EXCEPTION USING ERRCODE = '23000', CONSTRAINT = 'assessment_attempt_result_immutable', MESSAGE = 'assessment result snapshots are immutable'; END; $$; -- +goose StatementEnd CREATE TRIGGER assessment_attempt_result_refuse_mutation BEFORE UPDATE OR DELETE ON assessment_attempt_results FOR EACH ROW EXECUTE FUNCTION assessment_attempt_result_refuse_mutation(); -- +goose Down -- +goose StatementBegin DO $$ BEGIN IF EXISTS ( SELECT 1 FROM assessment_attempts WHERE outcome = 'demonstration' ) OR EXISTS ( SELECT 1 FROM assessment_attempt_idempotency_outcomes WHERE operation IN ('answer', 'finalize') ) OR EXISTS ( SELECT 1 FROM assessment_attempt_results ) THEN RAISE EXCEPTION 'assessment command data uses migration 00029 and must be retained'; END IF; END; $$; -- +goose StatementEnd DROP TRIGGER assessment_attempt_result_refuse_mutation ON assessment_attempt_results; DROP FUNCTION assessment_attempt_result_refuse_mutation(); DROP TRIGGER assessment_attempt_result_guard ON assessment_attempt_results; DROP FUNCTION assessment_attempt_result_guard(); DROP TABLE assessment_attempt_results; ALTER TABLE assessment_attempt_idempotency_outcomes DROP CONSTRAINT assessment_attempt_idempotency_outcomes_status, DROP CONSTRAINT assessment_attempt_idempotency_outcomes_operation, DROP CONSTRAINT assessment_attempt_idempotency_outcomes_pending_items, DROP COLUMN pending_item_ids, ADD CONSTRAINT assessment_attempt_idempotency_outcomes_operation CHECK (operation IN ('create', 'session')), ADD CONSTRAINT assessment_attempt_idempotency_outcomes_status CHECK ( (operation = 'create' AND status_code = 201 AND problem_code IS NULL AND response_snapshot IS NOT NULL) OR (operation = 'create' AND status_code = 409 AND problem_code IS NOT NULL AND response_snapshot IS NULL) OR (operation = 'session' AND status_code = 200 AND problem_code IS NULL AND response_snapshot IS NOT NULL) ); ALTER TABLE assessment_attempts DROP CONSTRAINT assessment_attempts_outcome, ADD CONSTRAINT assessment_attempts_outcome CHECK ( outcome IS NULL OR outcome IN ('confirmed', 'not_confirmed', 'not_determined') );