380 lines
16 KiB
PL/PgSQL
380 lines
16 KiB
PL/PgSQL
-- +goose Up
|
|
-- Assessment bank: immutable versions of assessment task families and the
|
|
-- openings that make a category available.
|
|
--
|
|
-- One row of assessment_families is one version of one family. Its content
|
|
-- (key, rubric, seed generator, authorship, review) never changes: a change
|
|
-- is a new row with the next version number. The only columns that move are
|
|
-- lifecycle, published_at, published_by, retired_at, and retired_by, and only
|
|
-- forward (draft -> published -> retired), which the guard trigger enforces.
|
|
-- At most one version of a family is published at a time.
|
|
--
|
|
-- The tables hold task content and operator identities only, never personal
|
|
-- data of candidates.
|
|
CREATE TABLE assessment_families (
|
|
-- UUIDv7 generated in Go.
|
|
id uuid NOT NULL,
|
|
-- Stable name of the family across versions.
|
|
family_key text NOT NULL,
|
|
-- Numbered from 1 within the family key.
|
|
version integer NOT NULL,
|
|
category text NOT NULL,
|
|
-- Specialization or domain inside the category.
|
|
specialization text NOT NULL,
|
|
title text NOT NULL,
|
|
-- Name of the bundle the version was imported from. Provenance only; it
|
|
-- is not part of the content hash.
|
|
bundle_name text NOT NULL,
|
|
-- "ai:<name>" for a generated draft or "operator:<handle>" for a person.
|
|
author text NOT NULL,
|
|
-- The independent reviewer, "operator:<handle>"; NULL while the review
|
|
-- is pending.
|
|
reviewer text,
|
|
review_status text NOT NULL,
|
|
-- Seed generator: algorithm, parameter ranges, statement template.
|
|
seed_generator jsonb NOT NULL,
|
|
-- Answer key: answer format and the reference solution description.
|
|
answer_key jsonb NOT NULL,
|
|
-- Rubric: principle and criteria. It holds no points or thresholds;
|
|
-- grading rules come from the versioned policy.
|
|
rubric jsonb NOT NULL,
|
|
-- SHA-256 of the canonical content, 32 raw bytes. The domain recomputes
|
|
-- it when it reads a row, with the hash function that hash_scheme names.
|
|
content_sha256 bytea NOT NULL,
|
|
-- Version of the layout that content_sha256 hashes. A row keeps the
|
|
-- scheme it was stored with, so a later scheme verifies new rows without
|
|
-- invalidating old ones. Part of the immutable columns.
|
|
hash_scheme smallint NOT NULL,
|
|
lifecycle text NOT NULL,
|
|
-- Time from the database clock, set by the stamp trigger.
|
|
created_at timestamptz NOT NULL,
|
|
published_at timestamptz,
|
|
-- "operator:<handle>" that the caller declared when it published or
|
|
-- retired the version; not an authenticated identity until operator
|
|
-- accounts exist.
|
|
published_by text,
|
|
retired_at timestamptz,
|
|
retired_by text,
|
|
CONSTRAINT assessment_families_pkey PRIMARY KEY (id),
|
|
CONSTRAINT assessment_families_family_key_version_key UNIQUE (
|
|
family_key, version
|
|
),
|
|
CONSTRAINT assessment_families_family_key_format CHECK (
|
|
family_key ~ '^[a-z][a-z0-9-]{2,63}$'
|
|
),
|
|
CONSTRAINT assessment_families_version_positive CHECK (version > 0),
|
|
CONSTRAINT assessment_families_category_format CHECK (
|
|
category ~ '^[a-z][a-z0-9-]{1,47}$'
|
|
),
|
|
CONSTRAINT assessment_families_specialization_format CHECK (
|
|
specialization ~ '^[a-z][a-z0-9-]{1,63}$'
|
|
),
|
|
CONSTRAINT assessment_families_title_format CHECK (
|
|
char_length(title) BETWEEN 1 AND 200
|
|
AND title !~ '^\s|\s$'
|
|
AND title !~ '[[:cntrl:]]'
|
|
-- Invisible format characters: zero-width, bidirectional marks and
|
|
-- overrides, line and paragraph separators, word joiner and
|
|
-- invisible operators, and the BOM.
|
|
AND title !~ '[\u200B-\u200F\u202A-\u202E\u2028\u2029\u2060-\u2064\u2066-\u206F\uFEFF]'
|
|
),
|
|
CONSTRAINT assessment_families_bundle_name_format CHECK (
|
|
bundle_name ~ '^[a-z][a-z0-9._-]{2,63}$'
|
|
),
|
|
CONSTRAINT assessment_families_author_format CHECK (
|
|
char_length(author) <= 128
|
|
AND author ~ '^(ai|operator):[a-z0-9][a-z0-9._-]*$'
|
|
),
|
|
CONSTRAINT assessment_families_reviewer_format CHECK (
|
|
reviewer IS NULL OR (
|
|
char_length(reviewer) <= 128
|
|
AND reviewer ~ '^operator:[a-z0-9][a-z0-9._-]*$'
|
|
)
|
|
),
|
|
CONSTRAINT assessment_families_review_status_known CHECK (
|
|
review_status IN ('pending', 'approved', 'rejected')
|
|
),
|
|
-- A review is pending exactly while no reviewer is recorded.
|
|
CONSTRAINT assessment_families_review_has_reviewer CHECK (
|
|
(review_status = 'pending') = (reviewer IS NULL)
|
|
),
|
|
-- The review is independent: the reviewer is never the author.
|
|
CONSTRAINT assessment_families_reviewer_independent CHECK (
|
|
reviewer IS NULL OR reviewer <> author
|
|
),
|
|
CONSTRAINT assessment_families_documents_are_objects CHECK (
|
|
jsonb_typeof(seed_generator) = 'object'
|
|
AND jsonb_typeof(answer_key) = 'object'
|
|
AND jsonb_typeof(rubric) = 'object'
|
|
),
|
|
CONSTRAINT assessment_families_content_sha256_size CHECK (
|
|
octet_length(content_sha256) = 32
|
|
),
|
|
CONSTRAINT assessment_families_hash_scheme_known CHECK (
|
|
hash_scheme IN (1)
|
|
),
|
|
CONSTRAINT assessment_families_lifecycle_known CHECK (
|
|
lifecycle IN ('draft', 'published', 'retired')
|
|
),
|
|
CONSTRAINT assessment_families_lifecycle_stamps CHECK (
|
|
(
|
|
lifecycle = 'draft'
|
|
AND published_at IS NULL AND published_by IS NULL
|
|
AND retired_at IS NULL AND retired_by IS NULL
|
|
) OR (
|
|
lifecycle = 'published'
|
|
AND published_at IS NOT NULL AND published_by IS NOT NULL
|
|
AND retired_at IS NULL AND retired_by IS NULL
|
|
) OR (
|
|
lifecycle = 'retired'
|
|
AND published_at IS NOT NULL AND published_by IS NOT NULL
|
|
AND retired_at IS NOT NULL AND retired_by IS NOT NULL
|
|
)
|
|
),
|
|
CONSTRAINT assessment_families_published_by_format CHECK (
|
|
published_by IS NULL OR (
|
|
char_length(published_by) <= 128
|
|
AND published_by ~ '^operator:[a-z0-9][a-z0-9._-]*$'
|
|
)
|
|
),
|
|
CONSTRAINT assessment_families_retired_by_format CHECK (
|
|
retired_by IS NULL OR (
|
|
char_length(retired_by) <= 128
|
|
AND retired_by ~ '^operator:[a-z0-9][a-z0-9._-]*$'
|
|
)
|
|
),
|
|
-- A family is published only with an approved review by a reviewer.
|
|
CONSTRAINT assessment_families_published_requires_review CHECK (
|
|
lifecycle = 'draft' OR (
|
|
reviewer IS NOT NULL AND review_status = 'approved'
|
|
)
|
|
)
|
|
);
|
|
|
|
-- Coverage counts published families of a category.
|
|
CREATE INDEX assessment_families_category_lifecycle_idx
|
|
ON assessment_families (category, lifecycle);
|
|
|
|
-- At most one version of a family is published. Publishing a newer version
|
|
-- retires the older one first, in the same transaction.
|
|
CREATE UNIQUE INDEX assessment_families_one_published_key
|
|
ON assessment_families (family_key) WHERE lifecycle = 'published';
|
|
|
|
-- Every new version starts as a draft. The database clock sets created_at and
|
|
-- the lifecycle columns, whatever the caller sent.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_families_stamp_creation() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
NEW.created_at := clock_timestamp();
|
|
NEW.lifecycle := 'draft';
|
|
NEW.published_at := NULL;
|
|
NEW.published_by := NULL;
|
|
NEW.retired_at := NULL;
|
|
NEW.retired_by := NULL;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_families_stamp_creation
|
|
BEFORE INSERT ON assessment_families
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_families_stamp_creation();
|
|
|
|
-- Refuses every change except a forward lifecycle step. Any column other than
|
|
-- the lifecycle columns that differs between OLD and NEW is an edit of a
|
|
-- stored version. The caller names the operator of the step; the trigger sets
|
|
-- the time and keeps the publisher through a retirement. The errors use
|
|
-- SQLSTATE 23000 and the constraint names below, which the adapter maps to
|
|
-- port sentinels without reading the message.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_families_guard_update() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF (to_jsonb(NEW) - 'lifecycle' - 'published_at' - 'published_by' - 'retired_at' - 'retired_by')
|
|
IS DISTINCT FROM
|
|
(to_jsonb(OLD) - 'lifecycle' - 'published_at' - 'published_by' - 'retired_at' - 'retired_by') THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'assessment_families_immutable',
|
|
TABLE = 'assessment_families',
|
|
MESSAGE = 'assessment family versions are immutable: import a new version instead';
|
|
END IF;
|
|
IF OLD.lifecycle = 'draft' AND NEW.lifecycle = 'published' THEN
|
|
NEW.published_at := clock_timestamp();
|
|
NEW.retired_at := NULL;
|
|
NEW.retired_by := NULL;
|
|
ELSIF OLD.lifecycle = 'published' AND NEW.lifecycle = 'retired' THEN
|
|
NEW.published_at := OLD.published_at;
|
|
NEW.published_by := OLD.published_by;
|
|
NEW.retired_at := clock_timestamp();
|
|
ELSE
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'assessment_families_lifecycle_transition',
|
|
TABLE = 'assessment_families',
|
|
MESSAGE = 'assessment family lifecycle moves only draft to published to retired';
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_families_guard_update
|
|
BEFORE UPDATE ON assessment_families
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_families_guard_update();
|
|
|
|
-- Refuses DELETE and TRUNCATE. Row triggers do not fire for TRUNCATE, so a
|
|
-- statement trigger covers it.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_families_refuse_removal() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'assessment_families_immutable',
|
|
TABLE = 'assessment_families',
|
|
MESSAGE = 'assessment family versions are immutable: import a new version instead';
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_families_refuse_delete
|
|
BEFORE DELETE ON assessment_families
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_families_refuse_removal();
|
|
|
|
CREATE TRIGGER assessment_families_refuse_truncate
|
|
BEFORE TRUNCATE ON assessment_families
|
|
FOR EACH STATEMENT EXECUTE FUNCTION assessment_families_refuse_removal();
|
|
|
|
-- A category opening makes a category available. It records the versioned
|
|
-- policy the opening was made under, by reference, and the coverage the
|
|
-- database counted at that moment. An opening is never edited; the newest
|
|
-- opening of a category is the current one.
|
|
CREATE TABLE assessment_category_openings (
|
|
-- UUIDv7 generated in Go.
|
|
id uuid NOT NULL,
|
|
category text NOT NULL,
|
|
-- The agreed basic grading policy: a policy_versions.id. Product
|
|
-- thresholds are read from this version; the bank stores no thresholds
|
|
-- of its own. The insert trigger checks that the version exists instead
|
|
-- of a FOREIGN KEY, because PostgreSQL refuses TRUNCATE of a referenced
|
|
-- table before the policy_versions trigger can refuse it with its own
|
|
-- error. Versions are never deleted, so the check cannot go stale.
|
|
policy_version_id uuid NOT NULL,
|
|
-- "operator:<handle>" of the person who agreed to open the category.
|
|
opened_by text NOT NULL,
|
|
-- Distinct published families the policy requires: the insert trigger
|
|
-- derives it from the referenced policy document, so a value from the
|
|
-- caller is ignored.
|
|
required_families integer NOT NULL,
|
|
-- Distinct published families counted by the insert trigger.
|
|
published_families integer NOT NULL,
|
|
-- Time from the database clock, set by the stamp trigger.
|
|
opened_at timestamptz NOT NULL,
|
|
CONSTRAINT assessment_category_openings_pkey PRIMARY KEY (id),
|
|
CONSTRAINT assessment_category_openings_category_format CHECK (
|
|
category ~ '^[a-z][a-z0-9-]{1,47}$'
|
|
),
|
|
CONSTRAINT assessment_category_openings_opened_by_format CHECK (
|
|
char_length(opened_by) <= 128
|
|
AND opened_by ~ '^operator:[a-z0-9][a-z0-9._-]*$'
|
|
),
|
|
CONSTRAINT assessment_category_openings_required_positive CHECK (
|
|
required_families > 0
|
|
),
|
|
CONSTRAINT assessment_category_openings_coverage CHECK (
|
|
published_families >= required_families
|
|
)
|
|
);
|
|
|
|
CREATE INDEX assessment_category_openings_category_idx
|
|
ON assessment_category_openings (category, opened_at DESC, id DESC);
|
|
|
|
-- Checks the policy reference, derives the required coverage from the stored
|
|
-- policy document, counts the published families of the category, and stamps
|
|
-- the opening time. The coverage CHECK then refuses an opening whose category
|
|
-- has fewer published families than the policy requires. The requirement is
|
|
-- assessment.stop_rule.max_scored_items + assessment.stop_rule.seed_items, the
|
|
-- two paths that domain.RequiredPublishedAssessmentFamilies reads. A missing
|
|
-- policy version raises SQLSTATE 23503 under the constraint name that a
|
|
-- foreign key would have had; a document without the two values is refused
|
|
-- as well. The caller serializes openings, publications, and retirements of
|
|
-- a category with an advisory lock, so the count cannot miss a concurrent
|
|
-- retirement.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_category_openings_stamp() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
DECLARE
|
|
maximum_text text;
|
|
seed_text text;
|
|
BEGIN
|
|
SELECT
|
|
policy.document #>> '{assessment,stop_rule,max_scored_items}',
|
|
policy.document #>> '{assessment,stop_rule,seed_items}'
|
|
INTO maximum_text, seed_text
|
|
FROM policy_versions AS policy
|
|
WHERE policy.id = NEW.policy_version_id;
|
|
IF NOT FOUND THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'foreign_key_violation',
|
|
CONSTRAINT = 'assessment_category_openings_policy_version_id_fkey',
|
|
TABLE = 'assessment_category_openings',
|
|
MESSAGE = 'assessment category opening references an unknown policy version';
|
|
END IF;
|
|
-- The text is checked before the cast, so a malformed document of a row
|
|
-- written around the application raises the refusal below and not a cast
|
|
-- error (22P02 or 22003).
|
|
IF maximum_text IS NULL OR seed_text IS NULL
|
|
OR maximum_text !~ '^[0-9]{1,4}$' OR seed_text !~ '^[0-9]{1,4}$' THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'check_violation',
|
|
CONSTRAINT = 'assessment_category_openings_policy_thresholds',
|
|
TABLE = 'assessment_category_openings',
|
|
MESSAGE = 'the referenced policy version has no scored item bounds';
|
|
END IF;
|
|
NEW.required_families := maximum_text::integer + seed_text::integer;
|
|
NEW.opened_at := clock_timestamp();
|
|
SELECT count(DISTINCT family.family_key)::integer
|
|
INTO NEW.published_families
|
|
FROM assessment_families AS family
|
|
WHERE family.category = NEW.category AND family.lifecycle = 'published';
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_category_openings_stamp
|
|
BEFORE INSERT ON assessment_category_openings
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_category_openings_stamp();
|
|
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_category_openings_refuse_change() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'assessment_category_openings_immutable',
|
|
TABLE = 'assessment_category_openings',
|
|
MESSAGE = 'assessment category openings are immutable';
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_category_openings_refuse_update_delete
|
|
BEFORE UPDATE OR DELETE ON assessment_category_openings
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_category_openings_refuse_change();
|
|
|
|
CREATE TRIGGER assessment_category_openings_refuse_truncate
|
|
BEFORE TRUNCATE ON assessment_category_openings
|
|
FOR EACH STATEMENT EXECUTE FUNCTION assessment_category_openings_refuse_change();
|
|
|
|
-- +goose Down
|
|
DROP TABLE assessment_category_openings;
|
|
DROP FUNCTION assessment_category_openings_refuse_change();
|
|
DROP FUNCTION assessment_category_openings_stamp();
|
|
DROP TABLE assessment_families;
|
|
DROP FUNCTION assessment_families_refuse_removal();
|
|
DROP FUNCTION assessment_families_guard_update();
|
|
DROP FUNCTION assessment_families_stamp_creation();
|