-- +goose Up -- Skill evidence is a projection of immutable assessment results. One fact is -- one source event; the skills of that event are tags of the fact, so several -- tags never multiply the fact. A fact is written by the database in the same -- transaction as the result that qualifies it, and migration backfills the -- results that already exist. The projection holds no answer, score, item, -- failure, or anti-cheat data: reads of the evidence never touch the result, -- attempt, or review tables. CREATE TABLE skill_evidence_facts ( id uuid NOT NULL DEFAULT gen_random_uuid(), candidate_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, -- A claim is a statement that nothing confirmed separately (trust 0); a -- measurement is an observed result (trust 1 to 3). Only assessments feed -- this table today, so every stored fact is a measurement. kind text NOT NULL, source_type text NOT NULL, -- The source event and the immutable result of that event. For an -- assessment both are the attempt, because a result is keyed by its attempt. event_id uuid NOT NULL, result_id uuid NOT NULL, attribution text NOT NULL, trust smallint NOT NULL, -- The grade that was passed, the basis of the measurement, and its -- calibration, copied from the category decision of the result. grade text NOT NULL, basis text NOT NULL, calibration_status text NOT NULL, occurred_at timestamptz NOT NULL, -- The candidate may hide a fact; hiding never edits the result. visibility text NOT NULL DEFAULT 'visible', recorded_at timestamptz NOT NULL DEFAULT clock_timestamp(), -- The transaction that created the fact. Tags are added only in that -- transaction, so a stored fact is never widened afterwards. created_txid bigint NOT NULL DEFAULT (pg_current_xact_id()::text::bigint), CONSTRAINT skill_evidence_facts_pkey PRIMARY KEY (id), CONSTRAINT skill_evidence_facts_event_key UNIQUE (source_type, event_id), CONSTRAINT skill_evidence_facts_result_fk FOREIGN KEY (result_id, candidate_id) REFERENCES assessment_attempt_results (attempt_id, user_id) ON DELETE CASCADE, CONSTRAINT skill_evidence_facts_kind CHECK (kind IN ('claim', 'measurement')), CONSTRAINT skill_evidence_facts_source CHECK (source_type IN ('assessment_result')), CONSTRAINT skill_evidence_facts_attribution CHECK (attribution IN ('personal', 'team_role', 'team_unattributed')), CONSTRAINT skill_evidence_facts_trust CHECK ( (kind = 'claim' AND trust = 0) OR (kind = 'measurement' AND trust BETWEEN 1 AND 3) ), -- An online assessment has no proctoring, so it never reaches the level of -- an in-person check. Level 1 is the level without a reliable identity -- confirmation; level 2 is reserved for a result tied to a confirmed identity. CONSTRAINT skill_evidence_facts_assessment_shape CHECK ( source_type <> 'assessment_result' OR (kind = 'measurement' AND trust BETWEEN 1 AND 2 AND attribution = 'personal' AND event_id = result_id) ), CONSTRAINT skill_evidence_facts_grade CHECK (grade IN ('intern', 'junior', 'middle', 'senior')), CONSTRAINT skill_evidence_facts_basis CHECK (basis IN ('synthetic_demo')), CONSTRAINT skill_evidence_facts_calibration CHECK (calibration_status IN ('uncalibrated')), CONSTRAINT skill_evidence_facts_visibility CHECK (visibility IN ('visible', 'hidden')) ); CREATE INDEX skill_evidence_facts_candidate_idx ON skill_evidence_facts (candidate_id, occurred_at DESC, id DESC); CREATE TABLE skill_evidence_skills ( fact_id uuid NOT NULL REFERENCES skill_evidence_facts (id) ON DELETE CASCADE, skill_code text NOT NULL, CONSTRAINT skill_evidence_skills_pkey PRIMARY KEY (fact_id, skill_code), -- The specialization codes of the catalog and the closed technology -- taxonomy of the anonymous card. CONSTRAINT skill_evidence_skills_code CHECK ( skill_code IN ( 'backend', 'frontend', 'mobile', 'network_engineer', 'devops_sre', 'data_analyst', 'ds_ml', 'information_security', 'qa', 'go', 'postgresql', 'kafka', 'docker' ) ) ); -- The single rule that decides which result is qualifying evidence: a real -- (root or corrective) result of the basic mode, confirmed, and carried by a -- reviewed category decision that either assigned the grade or left a higher -- one in force. An express training run, a failed attempt, and a result without -- a reviewed source never qualify. -- +goose StatementBegin CREATE FUNCTION skill_evidence_result_qualifies(source_result assessment_attempt_results) RETURNS boolean LANGUAGE sql IMMUTABLE AS $$ SELECT source_result.schema_version = 2 AND source_result.kind IN ('root', 'corrective') AND source_result.mode = 'demo_basic' AND source_result.outcome = 'confirmed' AND source_result.category_decision ->> 'status' IN ('assigned', 'unchanged') $$; -- +goose StatementEnd -- A fact must match the immutable result it names, so no write path can store -- evidence for an express run, a failed attempt, or another candidate. -- +goose StatementBegin CREATE FUNCTION skill_evidence_facts_guard_insert() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE source_result assessment_attempt_results%ROWTYPE; BEGIN SELECT * INTO source_result FROM assessment_attempt_results WHERE attempt_id = NEW.result_id AND user_id = NEW.candidate_id; IF NOT FOUND OR NOT skill_evidence_result_qualifies(source_result) OR source_result.category_decision ->> 'attempted_grade' IS DISTINCT FROM NEW.grade OR source_result.category_decision ->> 'source' IS DISTINCT FROM NEW.basis OR source_result.category_decision ->> 'calibration_status' IS DISTINCT FROM NEW.calibration_status OR source_result.decided_at <> NEW.occurred_at THEN RAISE EXCEPTION USING ERRCODE = '23514', CONSTRAINT = 'skill_evidence_facts_source_result', MESSAGE = 'skill evidence does not match a qualifying assessment result'; END IF; RETURN NEW; END; $$; -- +goose StatementEnd CREATE TRIGGER skill_evidence_facts_guard_insert BEFORE INSERT ON skill_evidence_facts FOR EACH ROW EXECUTE FUNCTION skill_evidence_facts_guard_insert(); -- A stored fact never changes except its visibility, and is removed only with -- its owner or its result. -- +goose StatementBegin CREATE FUNCTION skill_evidence_facts_refuse_change() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF TG_OP = 'DELETE' THEN IF pg_trigger_depth() > 1 THEN RETURN OLD; END IF; ELSIF ROW( NEW.id, NEW.candidate_id, NEW.kind, NEW.source_type, NEW.event_id, NEW.result_id, NEW.attribution, NEW.trust, NEW.grade, NEW.basis, NEW.calibration_status, NEW.occurred_at, NEW.recorded_at, NEW.created_txid ) IS NOT DISTINCT FROM ROW( OLD.id, OLD.candidate_id, OLD.kind, OLD.source_type, OLD.event_id, OLD.result_id, OLD.attribution, OLD.trust, OLD.grade, OLD.basis, OLD.calibration_status, OLD.occurred_at, OLD.recorded_at, OLD.created_txid ) THEN RETURN NEW; END IF; RAISE EXCEPTION USING ERRCODE = '23000', CONSTRAINT = 'skill_evidence_facts_immutable', MESSAGE = 'skill evidence facts are immutable except for their visibility'; END; $$; -- +goose StatementEnd CREATE TRIGGER skill_evidence_facts_refuse_change BEFORE UPDATE OR DELETE ON skill_evidence_facts FOR EACH ROW EXECUTE FUNCTION skill_evidence_facts_refuse_change(); -- A fact without a skill cannot be read or matched, so the check runs at -- commit, after the tags of the same transaction are in place. A fact that the -- same transaction already removed needs no tag. -- +goose StatementBegin CREATE FUNCTION skill_evidence_facts_require_skill() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF EXISTS (SELECT 1 FROM skill_evidence_facts WHERE id = NEW.id) AND NOT EXISTS (SELECT 1 FROM skill_evidence_skills WHERE fact_id = NEW.id) THEN RAISE EXCEPTION USING ERRCODE = '23514', CONSTRAINT = 'skill_evidence_facts_require_skill', MESSAGE = 'skill evidence fact has no skill'; END IF; RETURN NULL; END; $$; -- +goose StatementEnd CREATE CONSTRAINT TRIGGER skill_evidence_facts_require_skill AFTER INSERT ON skill_evidence_facts DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION skill_evidence_facts_require_skill(); -- +goose StatementBegin CREATE FUNCTION skill_evidence_skills_refuse_change() 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 = 'skill_evidence_skills_immutable', MESSAGE = 'skill evidence tags are immutable'; END; $$; -- +goose StatementEnd CREATE TRIGGER skill_evidence_skills_refuse_change BEFORE UPDATE OR DELETE ON skill_evidence_skills FOR EACH ROW EXECUTE FUNCTION skill_evidence_skills_refuse_change(); -- A tag joins a fact only in the transaction that created the fact: the -- skills of an event are fixed when the event is recorded, and a later write -- cannot widen what an organization may read about it. -- +goose StatementBegin CREATE FUNCTION skill_evidence_skills_require_new_fact() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NOT EXISTS ( SELECT 1 FROM skill_evidence_facts WHERE id = NEW.fact_id AND created_txid = pg_current_xact_id()::text::bigint ) THEN RAISE EXCEPTION USING ERRCODE = '23000', CONSTRAINT = 'skill_evidence_skills_fact_sealed', MESSAGE = 'skills can only be added in the transaction that created the fact'; END IF; RETURN NEW; END; $$; -- +goose StatementEnd CREATE TRIGGER skill_evidence_skills_require_new_fact BEFORE INSERT ON skill_evidence_skills FOR EACH ROW EXECUTE FUNCTION skill_evidence_skills_require_new_fact(); -- Projects one stored result. A result that does not qualify writes nothing, -- and a repeat writes nothing, so the function serves both the trigger and the -- backfill. Level 1 is the trust of an online result without a reliable -- identity confirmation. -- +goose StatementBegin CREATE FUNCTION skill_evidence_project_assessment_result(projected_attempt_id uuid) RETURNS void LANGUAGE plpgsql AS $$ DECLARE source_result assessment_attempt_results%ROWTYPE; new_fact_id uuid; BEGIN SELECT * INTO source_result FROM assessment_attempt_results WHERE attempt_id = projected_attempt_id; IF NOT FOUND OR NOT skill_evidence_result_qualifies(source_result) THEN RETURN; END IF; INSERT INTO skill_evidence_facts ( candidate_id, kind, source_type, event_id, result_id, attribution, trust, grade, basis, calibration_status, occurred_at ) VALUES ( source_result.user_id, 'measurement', 'assessment_result', source_result.attempt_id, source_result.attempt_id, 'personal', 1, source_result.category_decision ->> 'attempted_grade', source_result.category_decision ->> 'source', source_result.category_decision ->> 'calibration_status', source_result.decided_at ) ON CONFLICT (source_type, event_id) DO NOTHING RETURNING id INTO new_fact_id; IF new_fact_id IS NOT NULL THEN INSERT INTO skill_evidence_skills (fact_id, skill_code) VALUES (new_fact_id, source_result.category_decision ->> 'specialization'); END IF; END; $$; -- +goose StatementEnd -- +goose StatementBegin CREATE FUNCTION skill_evidence_project_result_trigger() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN PERFORM skill_evidence_project_assessment_result(NEW.attempt_id); RETURN NULL; END; $$; -- +goose StatementEnd CREATE TRIGGER skill_evidence_project_result AFTER INSERT ON assessment_attempt_results FOR EACH ROW EXECUTE FUNCTION skill_evidence_project_result_trigger(); SELECT skill_evidence_project_assessment_result(attempt_id) FROM assessment_attempt_results ORDER BY decided_at, attempt_id; -- +goose Down -- The facts are a projection: the results they were derived from stay, and the -- migration projects them again when it is applied again. Hiding a fact is the -- candidate's own decision and cannot be derived from a result, so a rollback -- refuses while any such decision, or any source other than an assessment -- result, would be lost. -- +goose StatementBegin DO $$ BEGIN IF EXISTS ( SELECT 1 FROM skill_evidence_facts WHERE visibility = 'hidden' OR source_type <> 'assessment_result' ) THEN RAISE EXCEPTION USING ERRCODE = '23000', CONSTRAINT = 'skill_evidence_retained', MESSAGE = 'skill evidence uses migration 00035 and must be retained'; END IF; END; $$; -- +goose StatementEnd DROP TRIGGER skill_evidence_project_result ON assessment_attempt_results; DROP FUNCTION skill_evidence_project_result_trigger(); DROP FUNCTION skill_evidence_project_assessment_result(uuid); DROP TRIGGER skill_evidence_skills_require_new_fact ON skill_evidence_skills; DROP FUNCTION skill_evidence_skills_require_new_fact(); DROP TRIGGER skill_evidence_skills_refuse_change ON skill_evidence_skills; DROP FUNCTION skill_evidence_skills_refuse_change(); DROP TRIGGER skill_evidence_facts_require_skill ON skill_evidence_facts; DROP FUNCTION skill_evidence_facts_require_skill(); DROP TRIGGER skill_evidence_facts_refuse_change ON skill_evidence_facts; DROP FUNCTION skill_evidence_facts_refuse_change(); DROP TRIGGER skill_evidence_facts_guard_insert ON skill_evidence_facts; DROP FUNCTION skill_evidence_facts_guard_insert(); DROP FUNCTION skill_evidence_result_qualifies(assessment_attempt_results); DROP TABLE skill_evidence_skills; DROP TABLE skill_evidence_facts;