720 lines
32 KiB
PL/PgSQL
720 lines
32 KiB
PL/PgSQL
-- +goose Up
|
|
-- Assessment attempts pin their product policy, category opening or separate
|
|
-- training source, and generated item variants. Candidate answers are stored
|
|
-- separately from the immutable variant and never appear in idempotency rows.
|
|
|
|
CREATE TABLE assessment_training_bank_bindings (
|
|
id uuid NOT NULL,
|
|
category text NOT NULL,
|
|
product_policy_version_id uuid NOT NULL REFERENCES policy_versions (id),
|
|
item_count integer NOT NULL,
|
|
manifest_sha256 bytea NOT NULL,
|
|
family_manifest jsonb NOT NULL,
|
|
reviewed_by text NOT NULL,
|
|
created_at timestamptz NOT NULL,
|
|
CONSTRAINT assessment_training_bank_bindings_pkey PRIMARY KEY (id),
|
|
CONSTRAINT assessment_training_bank_bindings_policy_manifest_key UNIQUE (
|
|
category, product_policy_version_id, manifest_sha256
|
|
),
|
|
CONSTRAINT assessment_training_bank_bindings_category_format CHECK (
|
|
category ~ '^[a-z][a-z0-9-]{1,47}$'
|
|
),
|
|
CONSTRAINT assessment_training_bank_bindings_item_count CHECK (
|
|
item_count BETWEEN 1 AND 20
|
|
),
|
|
CONSTRAINT assessment_training_bank_bindings_manifest_hash CHECK (
|
|
octet_length(manifest_sha256) = 32
|
|
),
|
|
CONSTRAINT assessment_training_bank_bindings_manifest_object CHECK (
|
|
jsonb_typeof(family_manifest) = 'array'
|
|
),
|
|
CONSTRAINT assessment_training_bank_bindings_reviewer CHECK (
|
|
char_length(reviewed_by) <= 128
|
|
AND reviewed_by ~ '^operator:[a-z0-9][a-z0-9._-]*$'
|
|
)
|
|
);
|
|
|
|
CREATE TABLE assessment_training_bank_binding_families (
|
|
binding_id uuid NOT NULL REFERENCES assessment_training_bank_bindings (id),
|
|
ordinal integer NOT NULL,
|
|
family_id uuid NOT NULL REFERENCES assessment_families (id),
|
|
family_key text NOT NULL,
|
|
family_version integer NOT NULL,
|
|
content_sha256 bytea NOT NULL,
|
|
CONSTRAINT assessment_training_bank_binding_families_pkey PRIMARY KEY (binding_id, ordinal),
|
|
CONSTRAINT assessment_training_bank_binding_families_family_key UNIQUE (binding_id, family_key),
|
|
CONSTRAINT assessment_training_bank_binding_families_ordinal CHECK (ordinal > 0),
|
|
CONSTRAINT assessment_training_bank_binding_families_version CHECK (family_version > 0),
|
|
CONSTRAINT assessment_training_bank_binding_families_hash CHECK (octet_length(content_sha256) = 32)
|
|
);
|
|
|
|
-- A binding is accepted only for an explicit training policy and a category
|
|
-- not already opened for real assessments. The category advisory lock is the
|
|
-- same lock taken by the bank's publish, retire, and opening operations.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_training_binding_guard() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
DECLARE
|
|
policy_item_count integer;
|
|
current_product_policy_id uuid;
|
|
BEGIN
|
|
PERFORM pg_advisory_xact_lock(
|
|
hashtextextended('lincodex.assessment.category:' || NEW.category, 0)
|
|
);
|
|
|
|
SELECT (document #>> '{assessment,training,demo_express_item_count}')::integer
|
|
INTO policy_item_count
|
|
FROM policy_versions
|
|
WHERE id = NEW.product_policy_version_id
|
|
AND policy_key = 'product';
|
|
|
|
IF policy_item_count IS NULL OR policy_item_count <> NEW.item_count THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_training_binding_policy',
|
|
MESSAGE = 'training binding does not match explicit product policy';
|
|
END IF;
|
|
|
|
SELECT id INTO current_product_policy_id
|
|
FROM policy_versions
|
|
WHERE policy_key = 'product' AND effective_at <= clock_timestamp()
|
|
ORDER BY version DESC
|
|
LIMIT 1;
|
|
IF current_product_policy_id IS DISTINCT FROM NEW.product_policy_version_id THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_training_binding_current_policy',
|
|
MESSAGE = 'training binding must pin the current product policy';
|
|
END IF;
|
|
|
|
IF EXISTS (
|
|
SELECT 1 FROM assessment_category_openings WHERE category = NEW.category
|
|
) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_training_category_is_disjoint',
|
|
MESSAGE = 'training category already has a real opening';
|
|
END IF;
|
|
|
|
NEW.created_at := clock_timestamp();
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_training_binding_guard
|
|
BEFORE INSERT ON assessment_training_bank_bindings
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_training_binding_guard();
|
|
|
|
-- A real opening cannot consume a category reserved for training. Taking the
|
|
-- same lock in both directions closes the race between binding and opening.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_real_opening_rejects_training_category() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
PERFORM pg_advisory_xact_lock(
|
|
hashtextextended('lincodex.assessment.category:' || NEW.category, 0)
|
|
);
|
|
|
|
IF EXISTS (
|
|
SELECT 1 FROM assessment_training_bank_bindings WHERE category = NEW.category
|
|
) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_training_category_is_disjoint',
|
|
MESSAGE = 'real opening cannot use a training category';
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_real_opening_rejects_training_category
|
|
BEFORE INSERT ON assessment_category_openings
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_real_opening_rejects_training_category();
|
|
|
|
-- Each pinned training family must be reviewed, published, and in the
|
|
-- category reserved by its binding. No family row can later be replaced.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_training_binding_family_guard() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
DECLARE
|
|
binding assessment_training_bank_bindings%ROWTYPE;
|
|
family assessment_families%ROWTYPE;
|
|
BEGIN
|
|
SELECT * INTO STRICT binding
|
|
FROM assessment_training_bank_bindings
|
|
WHERE id = NEW.binding_id;
|
|
|
|
SELECT * INTO STRICT family
|
|
FROM assessment_families
|
|
WHERE id = NEW.family_id;
|
|
|
|
IF family.category <> binding.category OR family.family_key <> NEW.family_key OR
|
|
family.version <> NEW.family_version OR family.content_sha256 <> NEW.content_sha256 OR
|
|
family.lifecycle <> 'published' OR family.review_status <> 'approved' OR
|
|
family.reviewer IS NULL OR family.author = family.reviewer THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_training_binding_family_reviewed',
|
|
MESSAGE = 'training family is not an approved published member of the binding';
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_training_binding_family_guard
|
|
BEFORE INSERT ON assessment_training_bank_binding_families
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_training_binding_family_guard();
|
|
|
|
-- The family rows and canonical manifest must agree when the transaction
|
|
-- commits; inserts may arrive after the binding header in the same unit.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_training_binding_complete() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
DECLARE
|
|
binding_id_value uuid;
|
|
BEGIN
|
|
binding_id_value := NEW.id;
|
|
PERFORM assessment_training_binding_assert_complete(binding_id_value);
|
|
|
|
RETURN NULL;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_training_binding_assert_complete(binding_id_value uuid) RETURNS void
|
|
LANGUAGE plpgsql AS $$
|
|
DECLARE
|
|
expected_count integer;
|
|
actual_count integer;
|
|
actual_manifest jsonb;
|
|
family_manifest_value jsonb;
|
|
BEGIN
|
|
SELECT item_count, family_manifest INTO expected_count, family_manifest_value
|
|
FROM assessment_training_bank_bindings
|
|
WHERE id = binding_id_value;
|
|
|
|
SELECT count(*)::integer INTO actual_count
|
|
FROM assessment_training_bank_binding_families
|
|
WHERE binding_id = binding_id_value;
|
|
|
|
IF actual_count <> expected_count OR jsonb_array_length(family_manifest_value) <> expected_count THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_training_binding_complete',
|
|
MESSAGE = 'training binding family count is incomplete';
|
|
END IF;
|
|
|
|
SELECT COALESCE(jsonb_agg(
|
|
jsonb_build_object(
|
|
'family_key', family_key,
|
|
'version', family_version,
|
|
'content_sha256', encode(content_sha256, 'hex')
|
|
) ORDER BY family_key, family_version
|
|
), '[]'::jsonb) INTO actual_manifest
|
|
FROM assessment_training_bank_binding_families
|
|
WHERE binding_id = binding_id_value;
|
|
|
|
IF actual_manifest <> family_manifest_value THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_training_binding_manifest',
|
|
MESSAGE = 'training binding manifest does not match its families';
|
|
END IF;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
-- The deferred child trigger prevents an extra family from being appended
|
|
-- after the header's own deferred completeness check was queued.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_training_binding_family_complete() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
PERFORM assessment_training_binding_assert_complete(NEW.binding_id);
|
|
RETURN NULL;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE CONSTRAINT TRIGGER assessment_training_binding_complete
|
|
AFTER INSERT ON assessment_training_bank_bindings
|
|
DEFERRABLE INITIALLY DEFERRED
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_training_binding_complete();
|
|
|
|
CREATE CONSTRAINT TRIGGER assessment_training_binding_family_complete
|
|
AFTER INSERT ON assessment_training_bank_binding_families
|
|
DEFERRABLE INITIALLY DEFERRED
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_training_binding_family_complete();
|
|
|
|
-- Bindings and their family manifest are immutable once stored.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_training_binding_refuse_change() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23000', CONSTRAINT = 'assessment_training_binding_immutable',
|
|
MESSAGE = 'training bank bindings are immutable';
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_training_binding_refuse_update_delete
|
|
BEFORE UPDATE OR DELETE ON assessment_training_bank_bindings
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_training_binding_refuse_change();
|
|
|
|
CREATE TRIGGER assessment_training_binding_family_refuse_update_delete
|
|
BEFORE UPDATE OR DELETE ON assessment_training_bank_binding_families
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_training_binding_refuse_change();
|
|
|
|
CREATE TABLE assessment_attempts (
|
|
id uuid NOT NULL,
|
|
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
|
kind text NOT NULL,
|
|
mode text NOT NULL,
|
|
state text NOT NULL,
|
|
outcome text,
|
|
bank_category text NOT NULL,
|
|
specialization text,
|
|
target_grade text,
|
|
task_language text,
|
|
root_attempt_id uuid 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,
|
|
created_at timestamptz NOT NULL,
|
|
updated_at timestamptz NOT NULL,
|
|
expires_at timestamptz NOT NULL,
|
|
chain_deadline timestamptz,
|
|
idle_timeout_seconds integer NOT NULL,
|
|
max_sessions integer NOT NULL,
|
|
sessions_used integer NOT NULL,
|
|
session_started_at timestamptz NOT NULL,
|
|
last_activity_at timestamptz NOT NULL,
|
|
version bigint NOT NULL,
|
|
ended_at timestamptz,
|
|
CONSTRAINT assessment_attempts_pkey PRIMARY KEY (id),
|
|
CONSTRAINT assessment_attempts_id_owner_key UNIQUE (id, user_id),
|
|
CONSTRAINT assessment_attempts_owner_root_fk FOREIGN KEY (root_attempt_id, user_id)
|
|
REFERENCES assessment_attempts (id, user_id) DEFERRABLE INITIALLY DEFERRED,
|
|
CONSTRAINT assessment_attempts_kind CHECK (kind IN ('root', 'corrective', 'demo_express')),
|
|
CONSTRAINT assessment_attempts_mode CHECK (mode IN ('demo_basic', 'adaptive', 'demo_express')),
|
|
CONSTRAINT assessment_attempts_state CHECK (
|
|
state IN ('in_progress', 'practice_pending', 'additional_practice_pending', 'ready_to_finalize',
|
|
'completed', 'expired', 'insufficient_bank')
|
|
),
|
|
CONSTRAINT assessment_attempts_outcome CHECK (
|
|
outcome IS NULL OR outcome IN ('confirmed', 'not_confirmed', 'not_determined')
|
|
),
|
|
CONSTRAINT assessment_attempts_track CHECK (
|
|
(
|
|
kind IN ('root', 'corrective')
|
|
AND mode IN ('demo_basic', 'adaptive')
|
|
AND specialization IS NOT NULL
|
|
AND specialization ~ '^[a-z][a-z0-9_-]{1,47}$'
|
|
AND target_grade IS NOT NULL
|
|
AND target_grade IN ('intern', 'junior', 'middle', 'senior')
|
|
AND (task_language IS NULL OR task_language IN ('go', 'python'))
|
|
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 chain_deadline IS NOT NULL
|
|
AND (
|
|
(kind = 'root' AND root_attempt_id = id)
|
|
OR (kind = 'corrective' AND root_attempt_id <> id)
|
|
)
|
|
) OR (
|
|
kind = 'demo_express'
|
|
AND mode = 'demo_express'
|
|
AND specialization IS NULL AND target_grade IS NULL AND task_language IS NULL
|
|
AND root_attempt_id = id
|
|
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 chain_deadline IS NULL
|
|
)
|
|
),
|
|
CONSTRAINT assessment_attempts_policy_version_positive CHECK (product_policy_version > 0),
|
|
CONSTRAINT assessment_attempts_version_positive CHECK (version > 0),
|
|
CONSTRAINT assessment_attempts_session_limits CHECK (
|
|
idle_timeout_seconds BETWEEN 60 AND 86400
|
|
AND max_sessions BETWEEN 1 AND 10
|
|
AND sessions_used BETWEEN 1 AND max_sessions
|
|
),
|
|
CONSTRAINT assessment_attempts_timestamps CHECK (
|
|
created_at <= updated_at
|
|
AND created_at <= session_started_at
|
|
AND session_started_at <= last_activity_at
|
|
AND last_activity_at <= updated_at
|
|
AND expires_at > created_at
|
|
AND (chain_deadline IS NULL OR (chain_deadline > created_at AND expires_at <= chain_deadline))
|
|
AND (ended_at IS NULL OR (ended_at >= created_at AND ended_at >= last_activity_at AND ended_at <= updated_at))
|
|
),
|
|
CONSTRAINT assessment_attempts_state_stamp CHECK (
|
|
(state IN ('in_progress', 'practice_pending', 'additional_practice_pending', 'ready_to_finalize')
|
|
AND outcome IS NULL AND ended_at IS NULL)
|
|
OR (state IN ('expired', 'insufficient_bank') AND outcome IS NULL AND ended_at IS NOT NULL)
|
|
OR (state = 'completed' AND outcome IS NOT NULL AND ended_at IS NOT NULL)
|
|
)
|
|
);
|
|
|
|
-- A corrective attempt can only attach to an eligible terminal root owned by
|
|
-- the same candidate and for the same specialization. The application still
|
|
-- validates the full chain order and descending grades inside its transaction.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_attempt_root_guard() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
DECLARE
|
|
root_attempt assessment_attempts%ROWTYPE;
|
|
BEGIN
|
|
IF NEW.kind <> 'corrective' THEN
|
|
RETURN NEW;
|
|
END IF;
|
|
|
|
SELECT * INTO root_attempt
|
|
FROM assessment_attempts
|
|
WHERE id = NEW.root_attempt_id
|
|
FOR SHARE;
|
|
|
|
IF NOT FOUND OR root_attempt.kind <> 'root' OR root_attempt.user_id <> NEW.user_id OR
|
|
root_attempt.specialization <> NEW.specialization OR
|
|
root_attempt.state NOT IN ('expired', 'completed') OR
|
|
(root_attempt.state = 'completed' AND root_attempt.outcome NOT IN ('not_confirmed', 'not_determined')) OR
|
|
root_attempt.chain_deadline IS NULL OR root_attempt.chain_deadline <= NEW.created_at OR
|
|
NEW.chain_deadline <> root_attempt.chain_deadline THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_root_eligibility',
|
|
MESSAGE = 'corrective attempt does not continue an eligible root';
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_attempt_root_guard
|
|
BEFORE INSERT OR UPDATE ON assessment_attempts
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_attempt_root_guard();
|
|
|
|
-- Verify the row refers to the exact current, reviewed opening or separately
|
|
-- reviewed training binding chosen by the transaction-bound bank service.
|
|
-- The caller holds the category advisory lock while it resolves and inserts.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_attempt_bank_pin_guard() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
DECLARE
|
|
pinned_category text;
|
|
pinned_product_policy_id uuid;
|
|
pinned_grading_policy_id uuid;
|
|
pinned_manifest_sha256 bytea;
|
|
current_product_policy_id uuid;
|
|
current_product_policy_version integer;
|
|
BEGIN
|
|
PERFORM pg_advisory_xact_lock(
|
|
hashtextextended('lincodex.assessment.category:' || NEW.bank_category, 0)
|
|
);
|
|
|
|
SELECT id, version INTO current_product_policy_id, current_product_policy_version
|
|
FROM policy_versions
|
|
WHERE policy_key = 'product' AND effective_at <= NEW.created_at
|
|
ORDER BY version DESC
|
|
LIMIT 1;
|
|
IF NOT FOUND OR current_product_policy_id <> NEW.product_policy_version_id OR
|
|
current_product_policy_version <> NEW.product_policy_version THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_product_policy_pin',
|
|
MESSAGE = 'attempt must pin the effective product policy';
|
|
END IF;
|
|
|
|
IF NEW.kind = 'demo_express' THEN
|
|
SELECT category, product_policy_version_id, manifest_sha256
|
|
INTO pinned_category, pinned_product_policy_id, pinned_manifest_sha256
|
|
FROM assessment_training_bank_bindings
|
|
WHERE id = NEW.training_binding_id;
|
|
|
|
IF NOT FOUND OR pinned_category <> NEW.bank_category OR
|
|
pinned_product_policy_id <> NEW.product_policy_version_id OR
|
|
pinned_manifest_sha256 <> NEW.training_manifest_sha256 THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_training_binding_pin',
|
|
MESSAGE = 'training attempt does not match its reviewed binding';
|
|
END IF;
|
|
ELSE
|
|
SELECT opening.category, opening.policy_version_id, catalog.published_manifest_sha256
|
|
INTO pinned_category, pinned_grading_policy_id, pinned_manifest_sha256
|
|
FROM assessment_category_openings AS opening
|
|
JOIN assessment_category_catalog_bindings AS catalog ON catalog.opening_id = opening.id
|
|
WHERE opening.id = NEW.opening_id
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM assessment_category_openings AS newer
|
|
WHERE newer.category = opening.category
|
|
AND (newer.opened_at, newer.id) > (opening.opened_at, opening.id)
|
|
);
|
|
|
|
IF NOT FOUND THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_opening_pin',
|
|
MESSAGE = 'real attempt opening is not current and reviewed';
|
|
END IF;
|
|
|
|
IF pinned_category <> NEW.bank_category OR
|
|
pinned_grading_policy_id <> NEW.grading_policy_version_id OR
|
|
pinned_manifest_sha256 <> NEW.opening_manifest_sha256 OR
|
|
current_product_policy_id IS DISTINCT FROM NEW.product_policy_version_id THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_opening_pin',
|
|
MESSAGE = 'real attempt does not match its current reviewed opening';
|
|
END IF;
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_attempt_bank_pin_guard
|
|
BEFORE INSERT ON assessment_attempts
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_attempt_bank_pin_guard();
|
|
|
|
-- Commands may update lifecycle and session fields, but an attempt's owner,
|
|
-- selection, policy/bank pins, and deadlines never change after creation.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_attempt_immutable_pins_guard() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF (to_jsonb(NEW) - 'state' - 'outcome' - 'updated_at' - 'sessions_used' -
|
|
'session_started_at' - 'last_activity_at' - 'version' - 'ended_at') IS DISTINCT FROM
|
|
(to_jsonb(OLD) - 'state' - 'outcome' - 'updated_at' - 'sessions_used' -
|
|
'session_started_at' - 'last_activity_at' - 'version' - 'ended_at') OR
|
|
NEW.version <> OLD.version + 1 OR NEW.updated_at < OLD.updated_at THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23000', CONSTRAINT = 'assessment_attempt_pins_immutable',
|
|
MESSAGE = 'assessment attempt pins are immutable and every transition increments version';
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_attempt_immutable_pins_guard
|
|
BEFORE UPDATE ON assessment_attempts
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_attempt_immutable_pins_guard();
|
|
|
|
CREATE INDEX assessment_attempts_candidate_created_idx
|
|
ON assessment_attempts (user_id, created_at DESC, id DESC);
|
|
|
|
-- Corrective attempts belong to their root chain and share its specialization.
|
|
CREATE INDEX assessment_attempts_root_chain_idx
|
|
ON assessment_attempts (root_attempt_id, created_at, id);
|
|
|
|
-- Active work is unique per candidate and real specialization. All nonterminal
|
|
-- lifecycle states stay in the index as later commands add them.
|
|
CREATE UNIQUE INDEX assessment_attempts_one_active_real_specialization
|
|
ON assessment_attempts (user_id, specialization)
|
|
WHERE kind IN ('root', 'corrective')
|
|
AND state IN ('in_progress', 'practice_pending', 'additional_practice_pending', 'ready_to_finalize');
|
|
|
|
CREATE UNIQUE INDEX assessment_attempts_one_active_training
|
|
ON assessment_attempts (user_id)
|
|
WHERE kind = 'demo_express'
|
|
AND state IN ('in_progress', 'practice_pending', 'additional_practice_pending', 'ready_to_finalize');
|
|
|
|
CREATE TABLE assessment_attempt_items (
|
|
id uuid NOT NULL,
|
|
attempt_id uuid NOT NULL REFERENCES assessment_attempts (id) ON DELETE CASCADE,
|
|
ordinal integer NOT NULL,
|
|
family_id uuid NOT NULL REFERENCES assessment_families (id),
|
|
family_key text NOT NULL,
|
|
family_version integer NOT NULL,
|
|
family_content_sha256 bytea NOT NULL,
|
|
variant jsonb NOT NULL,
|
|
-- Keep canonical UTF-8 JSON bytes so schema-valid strings containing NUL
|
|
-- survive PostgreSQL storage; jsonb rejects \u0000 values.
|
|
answer bytea,
|
|
answered_at timestamptz,
|
|
CONSTRAINT assessment_attempt_items_pkey PRIMARY KEY (id),
|
|
CONSTRAINT assessment_attempt_items_ordinal_key UNIQUE (attempt_id, ordinal),
|
|
CONSTRAINT assessment_attempt_items_family_key UNIQUE (attempt_id, family_key),
|
|
CONSTRAINT assessment_attempt_items_ordinal CHECK (ordinal BETWEEN 1 AND 220),
|
|
CONSTRAINT assessment_attempt_items_family_version CHECK (family_version > 0),
|
|
CONSTRAINT assessment_attempt_items_family_hash CHECK (octet_length(family_content_sha256) = 32),
|
|
CONSTRAINT assessment_attempt_items_variant_object CHECK (
|
|
jsonb_typeof(variant) = 'object' AND octet_length(variant::text) <= 1048576
|
|
),
|
|
CONSTRAINT assessment_attempt_items_answer_bound CHECK (
|
|
answer IS NULL OR octet_length(answer) <= 524288
|
|
),
|
|
CONSTRAINT assessment_attempt_items_answer_stamp CHECK (
|
|
(answer IS NULL) = (answered_at IS NULL)
|
|
)
|
|
);
|
|
|
|
CREATE INDEX assessment_attempt_items_family_usage_idx
|
|
ON assessment_attempt_items (family_id, attempt_id);
|
|
|
|
-- A variant's identity and content are fixed at creation. Answering is a
|
|
-- one-way NULL -> value transition; replay is handled by the attempt outcome.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_attempt_item_guard() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
DECLARE
|
|
attempt assessment_attempts%ROWTYPE;
|
|
family assessment_families%ROWTYPE;
|
|
BEGIN
|
|
IF TG_OP = 'DELETE' THEN
|
|
IF pg_trigger_depth() <= 1 THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23000', CONSTRAINT = 'assessment_attempt_item_delete_forbidden',
|
|
MESSAGE = 'assessment items can be removed only by account or attempt cascade';
|
|
END IF;
|
|
RETURN OLD;
|
|
END IF;
|
|
|
|
SELECT * INTO STRICT attempt
|
|
FROM assessment_attempts WHERE id = NEW.attempt_id;
|
|
SELECT * INTO STRICT family
|
|
FROM assessment_families WHERE id = NEW.family_id;
|
|
|
|
IF family.family_key <> NEW.family_key OR family.version <> NEW.family_version OR
|
|
family.content_sha256 <> NEW.family_content_sha256 OR family.category <> attempt.bank_category THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_item_family_pin',
|
|
MESSAGE = 'assessment item family pin is inconsistent';
|
|
END IF;
|
|
|
|
IF TG_OP = 'UPDATE' THEN
|
|
IF NEW.id <> OLD.id OR NEW.attempt_id <> OLD.attempt_id OR NEW.ordinal <> OLD.ordinal OR
|
|
NEW.family_id <> OLD.family_id OR NEW.family_key <> OLD.family_key OR
|
|
NEW.family_version <> OLD.family_version OR NEW.family_content_sha256 <> OLD.family_content_sha256 OR
|
|
NEW.variant <> OLD.variant OR OLD.answer IS NOT NULL OR NEW.answer IS NULL OR NEW.answered_at IS NULL THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23000', CONSTRAINT = 'assessment_attempt_item_immutable',
|
|
MESSAGE = 'assessment item identity and variant are immutable';
|
|
END IF;
|
|
IF NEW.answered_at < attempt.created_at OR NEW.answered_at >= attempt.expires_at THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_item_answer_time',
|
|
MESSAGE = 'assessment answer time is outside the attempt deadline';
|
|
END IF;
|
|
ELSIF TG_OP = 'INSERT' THEN
|
|
IF family.lifecycle <> 'published' OR family.review_status <> 'approved' OR
|
|
family.reviewer IS NULL OR family.author = family.reviewer THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_item_family_reviewed',
|
|
MESSAGE = 'assessment item family is not approved and published';
|
|
END IF;
|
|
IF attempt.kind = 'demo_express' AND NOT EXISTS (
|
|
SELECT 1
|
|
FROM assessment_training_bank_binding_families AS member
|
|
WHERE member.binding_id = attempt.training_binding_id
|
|
AND member.family_id = NEW.family_id
|
|
AND member.family_key = NEW.family_key
|
|
AND member.family_version = NEW.family_version
|
|
AND member.content_sha256 = NEW.family_content_sha256
|
|
) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_item_training_member',
|
|
MESSAGE = 'training attempt item is not in its pinned binding';
|
|
END IF;
|
|
IF NEW.answer IS NOT NULL OR NEW.answered_at IS NOT NULL THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_item_answer_initial',
|
|
MESSAGE = 'assessment items start without candidate answers';
|
|
END IF;
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_attempt_item_guard
|
|
BEFORE INSERT OR UPDATE OR DELETE ON assessment_attempt_items
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_attempt_item_guard();
|
|
|
|
-- Every idempotent response is a typed, allowlisted public read-model snapshot.
|
|
-- It never contains candidate answers, reference answers, seeds, family keys,
|
|
-- or private policy and opening identifiers.
|
|
CREATE TABLE assessment_attempt_idempotency_outcomes (
|
|
scope_hash bytea NOT NULL REFERENCES idempotency_records (scope_hash) ON DELETE CASCADE,
|
|
operation text NOT NULL,
|
|
status_code smallint NOT NULL,
|
|
problem_code text,
|
|
response_snapshot jsonb,
|
|
response_schema_version smallint NOT NULL DEFAULT 1,
|
|
created_at timestamptz NOT NULL,
|
|
CONSTRAINT assessment_attempt_idempotency_outcomes_pkey PRIMARY KEY (scope_hash),
|
|
CONSTRAINT assessment_attempt_idempotency_outcomes_scope_hash CHECK (octet_length(scope_hash) = 32),
|
|
CONSTRAINT assessment_attempt_idempotency_outcomes_operation CHECK (operation IN ('create', 'session')),
|
|
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)
|
|
),
|
|
CONSTRAINT assessment_attempt_idempotency_outcomes_response CHECK (
|
|
response_snapshot IS NULL OR (
|
|
jsonb_typeof(response_snapshot) = 'object'
|
|
AND octet_length(response_snapshot::text) <= 4194304
|
|
)
|
|
),
|
|
CONSTRAINT assessment_attempt_idempotency_outcomes_schema CHECK (response_schema_version = 1)
|
|
);
|
|
|
|
-- Keep committed command outcomes immutable; the generic 24-hour idempotency
|
|
-- purge may still delete them by cascading from idempotency_records.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_attempt_idempotency_outcome_refuse_update() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23000', CONSTRAINT = 'assessment_attempt_idempotency_outcome_immutable',
|
|
MESSAGE = 'assessment idempotency outcomes are immutable';
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_attempt_idempotency_outcome_refuse_update
|
|
BEFORE UPDATE ON assessment_attempt_idempotency_outcomes
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_attempt_idempotency_outcome_refuse_update();
|
|
|
|
-- +goose Down
|
|
DROP TRIGGER assessment_attempt_idempotency_outcome_refuse_update ON assessment_attempt_idempotency_outcomes;
|
|
DROP FUNCTION assessment_attempt_idempotency_outcome_refuse_update();
|
|
DROP TABLE assessment_attempt_idempotency_outcomes;
|
|
|
|
DROP TRIGGER assessment_attempt_item_guard ON assessment_attempt_items;
|
|
DROP FUNCTION assessment_attempt_item_guard();
|
|
DROP TABLE assessment_attempt_items;
|
|
DROP TRIGGER assessment_attempt_root_guard ON assessment_attempts;
|
|
DROP FUNCTION assessment_attempt_root_guard();
|
|
DROP TRIGGER assessment_attempt_bank_pin_guard ON assessment_attempts;
|
|
DROP FUNCTION assessment_attempt_bank_pin_guard();
|
|
DROP TRIGGER assessment_attempt_immutable_pins_guard ON assessment_attempts;
|
|
DROP FUNCTION assessment_attempt_immutable_pins_guard();
|
|
DROP INDEX assessment_attempts_one_active_training;
|
|
DROP INDEX assessment_attempts_one_active_real_specialization;
|
|
DROP INDEX assessment_attempts_root_chain_idx;
|
|
DROP INDEX assessment_attempts_candidate_created_idx;
|
|
DROP TABLE assessment_attempts;
|
|
|
|
DROP TRIGGER assessment_training_binding_family_refuse_update_delete ON assessment_training_bank_binding_families;
|
|
DROP TRIGGER assessment_training_binding_refuse_update_delete ON assessment_training_bank_bindings;
|
|
DROP FUNCTION assessment_training_binding_refuse_change();
|
|
DROP TRIGGER assessment_training_binding_complete ON assessment_training_bank_bindings;
|
|
DROP TRIGGER assessment_training_binding_family_complete ON assessment_training_bank_binding_families;
|
|
DROP FUNCTION assessment_training_binding_complete();
|
|
DROP FUNCTION assessment_training_binding_family_complete();
|
|
DROP FUNCTION assessment_training_binding_assert_complete(uuid);
|
|
DROP TRIGGER assessment_training_binding_family_guard ON assessment_training_bank_binding_families;
|
|
DROP FUNCTION assessment_training_binding_family_guard();
|
|
DROP TRIGGER assessment_real_opening_rejects_training_category ON assessment_category_openings;
|
|
DROP FUNCTION assessment_real_opening_rejects_training_category();
|
|
DROP TRIGGER assessment_training_binding_guard ON assessment_training_bank_bindings;
|
|
DROP FUNCTION assessment_training_binding_guard();
|
|
DROP TABLE assessment_training_bank_binding_families;
|
|
DROP TABLE assessment_training_bank_bindings;
|