chore: checkpoint assessment and inbox integration
This commit is contained in:
1 parent
88a31ba0d5
commit
5c52fd8b91
31 files changed
+4608
-181
No files matched your search
@@ -0,0 +1,383 @@
|
||||
-- +goose Up
|
||||
-- Synthetic category decisions extend immutable result rows without changing
|
||||
-- the existing result_snapshot document. Version 1 rows remain readable as-is.
|
||||
|
||||
ALTER TABLE assessment_attempt_results
|
||||
DROP CONSTRAINT assessment_attempt_results_schema,
|
||||
ADD COLUMN category_decision jsonb,
|
||||
ADD COLUMN current_confirmed_grade text,
|
||||
ADD CONSTRAINT assessment_attempt_results_schema CHECK (schema_version IN (1, 2)),
|
||||
ADD CONSTRAINT assessment_attempt_results_category_extension CHECK (
|
||||
(schema_version = 1 AND category_decision IS NULL AND current_confirmed_grade IS NULL)
|
||||
OR (
|
||||
schema_version = 2
|
||||
AND kind IN ('root', 'corrective') AND mode = 'demo_basic'
|
||||
AND jsonb_typeof(category_decision) = 'object'
|
||||
AND category_decision->>'attempt_id' = attempt_id::text
|
||||
AND category_decision->>'specialization' IS NOT NULL
|
||||
AND category_decision->>'attempted_grade' IS NOT NULL
|
||||
AND category_decision->>'product_policy_version_id' = product_policy_version_id::text
|
||||
AND (category_decision->>'product_policy_version')::integer = product_policy_version
|
||||
AND category_decision->>'grading_policy_version_id' = grading_policy_version_id::text
|
||||
AND category_decision->>'opening_id' = opening_id::text
|
||||
AND category_decision->>'opening_manifest_sha256' = encode(opening_manifest_sha256, 'hex')
|
||||
AND (category_decision->>'decided_at')::timestamptz = decided_at
|
||||
AND category_decision->>'status' IN ('not_eligible', 'not_confirmed', 'assigned', 'unchanged')
|
||||
AND NULLIF(category_decision->>'current_grade', '') IS NOT DISTINCT FROM current_confirmed_grade
|
||||
AND (current_confirmed_grade IS NULL OR current_confirmed_grade IN ('intern', 'junior', 'middle', 'senior'))
|
||||
)
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX assessment_attempt_results_attempt_owner_key
|
||||
ON assessment_attempt_results (attempt_id, user_id);
|
||||
|
||||
CREATE INDEX assessment_attempt_results_history_idx
|
||||
ON assessment_attempt_results (user_id, decided_at DESC, attempt_id DESC)
|
||||
WHERE kind IN ('root', 'corrective');
|
||||
|
||||
CREATE TABLE assessment_synthetic_category_reviews (
|
||||
id uuid NOT NULL,
|
||||
opening_id uuid NOT NULL REFERENCES assessment_category_openings (id),
|
||||
category text NOT NULL,
|
||||
source text NOT NULL,
|
||||
opening_manifest_sha256 bytea NOT NULL,
|
||||
product_policy_version_id uuid NOT NULL REFERENCES policy_versions (id),
|
||||
product_policy_version integer NOT NULL,
|
||||
reviewed_by text NOT NULL,
|
||||
reviewed_at timestamptz NOT NULL,
|
||||
CONSTRAINT assessment_synthetic_category_reviews_pkey PRIMARY KEY (id),
|
||||
CONSTRAINT assessment_synthetic_category_reviews_exact_source_key UNIQUE (
|
||||
opening_id, product_policy_version_id, opening_manifest_sha256, source
|
||||
),
|
||||
CONSTRAINT assessment_synthetic_category_reviews_source CHECK (source = 'synthetic_demo'),
|
||||
CONSTRAINT assessment_synthetic_category_reviews_hash CHECK (octet_length(opening_manifest_sha256) = 32),
|
||||
CONSTRAINT assessment_synthetic_category_reviews_policy_version CHECK (product_policy_version > 0),
|
||||
CONSTRAINT assessment_synthetic_category_reviews_reviewer CHECK (
|
||||
char_length(reviewed_by) <= 128
|
||||
AND reviewed_by ~ '^operator:[a-z0-9][a-z0-9._-]*$'
|
||||
)
|
||||
);
|
||||
|
||||
-- A review attestation is accepted only for the exact immutable opening and
|
||||
-- reviewed catalog manifest. The policy is pinned by ID and version; domain
|
||||
-- validation additionally requires the complete explicit demo V2 policy.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION assessment_synthetic_category_review_guard() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
DECLARE
|
||||
opening_category text;
|
||||
opening_policy_id uuid;
|
||||
manifest_hash bytea;
|
||||
policy_version integer;
|
||||
BEGIN
|
||||
SELECT opening.category, opening.policy_version_id, binding.published_manifest_sha256
|
||||
INTO opening_category, opening_policy_id, manifest_hash
|
||||
FROM assessment_category_openings AS opening
|
||||
JOIN assessment_category_catalog_bindings AS binding ON binding.opening_id = opening.id
|
||||
WHERE opening.id = NEW.opening_id;
|
||||
|
||||
IF NOT FOUND OR opening_category <> NEW.category OR
|
||||
manifest_hash IS DISTINCT FROM NEW.opening_manifest_sha256 THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23514', CONSTRAINT = 'assessment_synthetic_category_review_source',
|
||||
MESSAGE = 'synthetic category review does not match its immutable opening and manifest';
|
||||
END IF;
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM policy_versions AS policy
|
||||
WHERE policy.id = opening_policy_id AND policy.policy_key = 'product'
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23514', CONSTRAINT = 'assessment_synthetic_category_review_opening_policy',
|
||||
MESSAGE = 'synthetic category review opening has no product grading policy';
|
||||
END IF;
|
||||
|
||||
SELECT policy.version INTO policy_version
|
||||
FROM policy_versions AS policy
|
||||
WHERE policy.id = NEW.product_policy_version_id
|
||||
AND policy.policy_key = 'product'
|
||||
AND policy.kind = 'demo'
|
||||
AND policy.version = NEW.product_policy_version
|
||||
AND policy.document #>> '{assessment,attempt,real_mode}' = 'demo_basic'
|
||||
AND policy.document #>> '{assessment,attempt,basic_pass_mark_bp}' = '8000';
|
||||
IF NOT FOUND OR policy_version <> NEW.product_policy_version THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23514', CONSTRAINT = 'assessment_synthetic_category_review_policy',
|
||||
MESSAGE = 'synthetic category review policy is not an explicit demo basic policy';
|
||||
END IF;
|
||||
|
||||
NEW.reviewed_at := clock_timestamp();
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER assessment_synthetic_category_review_guard
|
||||
BEFORE INSERT ON assessment_synthetic_category_reviews
|
||||
FOR EACH ROW EXECUTE FUNCTION assessment_synthetic_category_review_guard();
|
||||
|
||||
-- Review attestations are provenance records and cannot be rewritten or
|
||||
-- removed independently of account cleanup.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION assessment_synthetic_category_review_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 = 'assessment_synthetic_category_review_immutable',
|
||||
MESSAGE = 'synthetic category reviews are immutable';
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER assessment_synthetic_category_review_refuse_mutation
|
||||
BEFORE UPDATE OR DELETE ON assessment_synthetic_category_reviews
|
||||
FOR EACH ROW EXECUTE FUNCTION assessment_synthetic_category_review_refuse_change();
|
||||
|
||||
CREATE TABLE candidate_assessment_categories (
|
||||
candidate_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
specialization text NOT NULL,
|
||||
grade text NOT NULL,
|
||||
source_attempt_id uuid NOT NULL,
|
||||
source_review_id uuid NOT NULL REFERENCES assessment_synthetic_category_reviews (id),
|
||||
product_policy_version_id uuid NOT NULL REFERENCES policy_versions (id),
|
||||
product_policy_version integer NOT NULL,
|
||||
grading_policy_version_id uuid NOT NULL REFERENCES policy_versions (id),
|
||||
opening_id uuid NOT NULL REFERENCES assessment_category_openings (id),
|
||||
opening_manifest_sha256 bytea NOT NULL,
|
||||
source text NOT NULL,
|
||||
calibration_status text NOT NULL,
|
||||
assigned_at timestamptz NOT NULL,
|
||||
CONSTRAINT candidate_assessment_categories_pkey PRIMARY KEY (candidate_id, specialization),
|
||||
CONSTRAINT candidate_assessment_categories_source_attempt_key UNIQUE (source_attempt_id),
|
||||
CONSTRAINT candidate_assessment_categories_attempt_owner_fk FOREIGN KEY (source_attempt_id, candidate_id)
|
||||
REFERENCES assessment_attempt_results (attempt_id, user_id) ON DELETE CASCADE,
|
||||
CONSTRAINT candidate_assessment_categories_specialization CHECK (
|
||||
specialization IN ('backend', 'frontend', 'mobile', 'network_engineer', 'devops_sre',
|
||||
'data_analyst', 'ds_ml', 'information_security', 'qa')
|
||||
),
|
||||
CONSTRAINT candidate_assessment_categories_grade CHECK (grade IN ('intern', 'junior', 'middle', 'senior')),
|
||||
CONSTRAINT candidate_assessment_categories_source CHECK (source = 'synthetic_demo'),
|
||||
CONSTRAINT candidate_assessment_categories_calibration CHECK (calibration_status = 'uncalibrated'),
|
||||
CONSTRAINT candidate_assessment_categories_hash CHECK (octet_length(opening_manifest_sha256) = 32),
|
||||
CONSTRAINT candidate_assessment_categories_policy_version CHECK (product_policy_version > 0)
|
||||
);
|
||||
|
||||
-- Current grade rows are derived only from an assigned immutable v2 result.
|
||||
-- Upgrades are monotonic; a lower passing result remains visible in its own
|
||||
-- result record and cannot lower the current category.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION candidate_assessment_category_guard() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
DECLARE
|
||||
decision jsonb;
|
||||
result_grade text;
|
||||
result_user_id uuid;
|
||||
result_at timestamptz;
|
||||
review assessment_synthetic_category_reviews%ROWTYPE;
|
||||
prior_rank integer;
|
||||
next_rank integer;
|
||||
BEGIN
|
||||
SELECT result.category_decision, result.user_id, result.decided_at
|
||||
INTO decision, result_user_id, result_at
|
||||
FROM assessment_attempt_results AS result
|
||||
WHERE result.attempt_id = NEW.source_attempt_id AND result.user_id = NEW.candidate_id
|
||||
AND result.schema_version = 2;
|
||||
|
||||
result_grade := decision->>'current_grade';
|
||||
IF NOT FOUND OR decision->>'status' <> 'assigned' OR result_user_id <> NEW.candidate_id OR
|
||||
result_grade <> NEW.grade OR decision->>'specialization' <> NEW.specialization OR
|
||||
decision->>'source' <> NEW.source OR decision->>'calibration_status' <> NEW.calibration_status OR
|
||||
decision->>'review_id' <> NEW.source_review_id::text OR
|
||||
decision->>'product_policy_version_id' <> NEW.product_policy_version_id::text OR
|
||||
(decision->>'product_policy_version')::integer <> NEW.product_policy_version OR
|
||||
decision->>'grading_policy_version_id' <> NEW.grading_policy_version_id::text OR
|
||||
decision->>'opening_id' <> NEW.opening_id::text OR
|
||||
decision->>'opening_manifest_sha256' <> encode(NEW.opening_manifest_sha256, 'hex') OR
|
||||
NEW.assigned_at <> result_at THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23514', CONSTRAINT = 'candidate_assessment_categories_result_match',
|
||||
MESSAGE = 'current category does not match an assigned immutable result';
|
||||
END IF;
|
||||
|
||||
SELECT * INTO review
|
||||
FROM assessment_synthetic_category_reviews AS source
|
||||
WHERE source.id = NEW.source_review_id
|
||||
AND source.opening_id = NEW.opening_id
|
||||
AND source.category = (SELECT attempt.bank_category FROM assessment_attempts AS attempt WHERE attempt.id = NEW.source_attempt_id)
|
||||
AND source.source = NEW.source
|
||||
AND source.opening_manifest_sha256 = NEW.opening_manifest_sha256
|
||||
AND source.product_policy_version_id = NEW.product_policy_version_id
|
||||
AND source.product_policy_version = NEW.product_policy_version
|
||||
AND source.reviewed_at <= result_at;
|
||||
IF NOT FOUND THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23514', CONSTRAINT = 'candidate_assessment_categories_review_match',
|
||||
MESSAGE = 'current category is missing its exact reviewed synthetic source';
|
||||
END IF;
|
||||
|
||||
next_rank := CASE NEW.grade WHEN 'intern' THEN 0 WHEN 'junior' THEN 1 WHEN 'middle' THEN 2 WHEN 'senior' THEN 3 ELSE -1 END;
|
||||
IF TG_OP = 'UPDATE' THEN
|
||||
prior_rank := CASE OLD.grade WHEN 'intern' THEN 0 WHEN 'junior' THEN 1 WHEN 'middle' THEN 2 WHEN 'senior' THEN 3 ELSE -1 END;
|
||||
IF OLD.candidate_id <> NEW.candidate_id OR OLD.specialization <> NEW.specialization OR next_rank <= prior_rank THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23514', CONSTRAINT = 'candidate_assessment_categories_monotonic',
|
||||
MESSAGE = 'current category updates must be a strict upgrade in the same scope';
|
||||
END IF;
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER candidate_assessment_category_guard
|
||||
BEFORE INSERT OR UPDATE ON candidate_assessment_categories
|
||||
FOR EACH ROW EXECUTE FUNCTION candidate_assessment_category_guard();
|
||||
|
||||
-- Do not permit manual removal of current category state. Account deletion may
|
||||
-- cascade through the candidate owner row.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION candidate_assessment_category_refuse_delete() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF pg_trigger_depth() > 1 THEN
|
||||
RETURN OLD;
|
||||
END IF;
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23000', CONSTRAINT = 'candidate_assessment_categories_delete',
|
||||
MESSAGE = 'current category can only change through a qualified result';
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER candidate_assessment_category_refuse_delete
|
||||
BEFORE DELETE ON candidate_assessment_categories
|
||||
FOR EACH ROW EXECUTE FUNCTION candidate_assessment_category_refuse_delete();
|
||||
|
||||
-- The extension guard ties a typed v2 decision to the row's immutable attempt
|
||||
-- and exact reviewed source. Legacy v1 rows must keep both new columns NULL.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION assessment_attempt_result_category_guard() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
DECLARE
|
||||
review assessment_synthetic_category_reviews%ROWTYPE;
|
||||
attempt assessment_attempts%ROWTYPE;
|
||||
BEGIN
|
||||
IF NEW.schema_version = 1 THEN
|
||||
RETURN NEW;
|
||||
END IF;
|
||||
|
||||
SELECT * INTO attempt
|
||||
FROM assessment_attempts
|
||||
WHERE id = NEW.attempt_id AND user_id = NEW.user_id;
|
||||
IF NOT FOUND OR NEW.category_decision->>'bank_category' IS DISTINCT FROM attempt.bank_category OR
|
||||
NEW.category_decision->>'specialization' IS DISTINCT FROM attempt.specialization OR
|
||||
NEW.category_decision->>'attempted_grade' IS DISTINCT FROM attempt.target_grade OR
|
||||
NEW.category_decision->>'grading_policy_version_id' IS DISTINCT FROM attempt.grading_policy_version_id::text OR
|
||||
NEW.category_decision->>'opening_id' IS DISTINCT FROM attempt.opening_id::text OR
|
||||
NEW.category_decision->>'opening_manifest_sha256' IS DISTINCT FROM encode(attempt.opening_manifest_sha256, 'hex') THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_result_category_decision',
|
||||
MESSAGE = 'category decision does not match its attempt';
|
||||
END IF;
|
||||
|
||||
IF NEW.category_decision->>'status' = 'not_eligible' THEN
|
||||
IF NEW.category_decision ? 'review_id' OR NEW.category_decision->>'source' IS NOT NULL OR
|
||||
NEW.category_decision->>'method' IS NOT NULL OR NEW.category_decision->>'calibration_status' IS NOT NULL OR
|
||||
NEW.category_decision->>'current_grade' IS DISTINCT FROM NEW.category_decision->>'previous_grade' THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_result_category_decision',
|
||||
MESSAGE = 'ineligible category decision contains review or assignment data';
|
||||
END IF;
|
||||
ELSIF NEW.category_decision->>'status' IN ('not_confirmed', 'assigned', 'unchanged') THEN
|
||||
IF NOT (NEW.category_decision ? 'review_id') OR
|
||||
NEW.category_decision->>'source' <> 'synthetic_demo' OR
|
||||
NEW.category_decision->>'calibration_status' <> 'uncalibrated' OR
|
||||
NEW.category_decision->>'method' <> 'demo_basic_pass_mark' THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_result_category_decision',
|
||||
MESSAGE = 'reviewed category decision lacks its synthetic provenance';
|
||||
END IF;
|
||||
ELSE
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_result_category_decision',
|
||||
MESSAGE = 'category decision status is unsupported';
|
||||
END IF;
|
||||
|
||||
IF NEW.category_decision ? 'review_id' THEN
|
||||
SELECT * INTO review
|
||||
FROM assessment_synthetic_category_reviews AS source
|
||||
WHERE source.id = (NEW.category_decision->>'review_id')::uuid
|
||||
AND source.opening_id = NEW.opening_id
|
||||
AND source.category = NEW.category_decision->>'bank_category'
|
||||
AND source.source = NEW.category_decision->>'source'
|
||||
AND source.opening_manifest_sha256 = NEW.opening_manifest_sha256
|
||||
AND source.product_policy_version_id = NEW.product_policy_version_id
|
||||
AND source.product_policy_version = NEW.product_policy_version
|
||||
AND (SELECT policy_version_id FROM assessment_category_openings WHERE id = source.opening_id) = NEW.grading_policy_version_id
|
||||
AND source.reviewed_at <= NEW.decided_at;
|
||||
IF NOT FOUND THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_result_category_review',
|
||||
MESSAGE = 'category decision does not match its exact reviewed source';
|
||||
END IF;
|
||||
ELSIF NEW.category_decision->>'status' <> 'not_eligible' THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_result_category_review_required',
|
||||
MESSAGE = 'reviewed category decisions require an immutable source attestation';
|
||||
END IF;
|
||||
|
||||
IF NEW.category_decision->>'status' = 'assigned' AND
|
||||
(NEW.outcome <> 'confirmed' OR NEW.category_decision->>'source' <> 'synthetic_demo' OR
|
||||
NEW.category_decision->>'calibration_status' <> 'uncalibrated' OR
|
||||
NEW.category_decision->>'method' <> 'demo_basic_pass_mark' OR
|
||||
NEW.current_confirmed_grade IS DISTINCT FROM NEW.category_decision->>'attempted_grade') THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = '23514', CONSTRAINT = 'assessment_attempt_result_category_assignment',
|
||||
MESSAGE = 'category assignment is not a passing reviewed demo decision';
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER assessment_attempt_result_category_guard
|
||||
BEFORE INSERT ON assessment_attempt_results
|
||||
FOR EACH ROW EXECUTE FUNCTION assessment_attempt_result_category_guard();
|
||||
|
||||
-- +goose Down
|
||||
-- Preserve category evidence and v2 result snapshots rather than silently
|
||||
-- discarding their meanings when this migration is rolled back.
|
||||
-- +goose StatementBegin
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM assessment_attempt_results WHERE schema_version = 2)
|
||||
OR EXISTS (SELECT 1 FROM assessment_synthetic_category_reviews)
|
||||
OR EXISTS (SELECT 1 FROM candidate_assessment_categories) THEN
|
||||
RAISE EXCEPTION 'assessment category data uses migration 00031 and must be retained';
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
DROP TRIGGER candidate_assessment_category_refuse_delete ON candidate_assessment_categories;
|
||||
DROP FUNCTION candidate_assessment_category_refuse_delete();
|
||||
DROP TRIGGER candidate_assessment_category_guard ON candidate_assessment_categories;
|
||||
DROP FUNCTION candidate_assessment_category_guard();
|
||||
DROP TABLE candidate_assessment_categories;
|
||||
DROP TRIGGER assessment_synthetic_category_review_refuse_mutation ON assessment_synthetic_category_reviews;
|
||||
DROP FUNCTION assessment_synthetic_category_review_refuse_change();
|
||||
DROP TRIGGER assessment_synthetic_category_review_guard ON assessment_synthetic_category_reviews;
|
||||
DROP FUNCTION assessment_synthetic_category_review_guard();
|
||||
DROP TABLE assessment_synthetic_category_reviews;
|
||||
DROP TRIGGER assessment_attempt_result_category_guard ON assessment_attempt_results;
|
||||
DROP FUNCTION assessment_attempt_result_category_guard();
|
||||
DROP INDEX assessment_attempt_results_history_idx;
|
||||
DROP INDEX assessment_attempt_results_attempt_owner_key;
|
||||
ALTER TABLE assessment_attempt_results
|
||||
DROP CONSTRAINT assessment_attempt_results_category_extension,
|
||||
DROP COLUMN category_decision,
|
||||
DROP COLUMN current_confirmed_grade,
|
||||
DROP CONSTRAINT assessment_attempt_results_schema,
|
||||
ADD CONSTRAINT assessment_attempt_results_schema CHECK (schema_version = 1);
|
||||
@@ -0,0 +1,115 @@
|
||||
-- Inbox rows reference current product records instead of copying their
|
||||
-- content. Recipient deletion removes the private inbox; organization context
|
||||
-- remains retained while a source event refers to it.
|
||||
-- +goose Up
|
||||
CREATE TABLE notification_inbox_events (
|
||||
recipient_user_id uuid NOT NULL,
|
||||
event_id uuid NOT NULL,
|
||||
organization_id uuid NOT NULL,
|
||||
event_type text NOT NULL,
|
||||
schema_version integer NOT NULL,
|
||||
source_type text NOT NULL,
|
||||
source_id uuid NOT NULL,
|
||||
occurred_at timestamptz NOT NULL,
|
||||
read_at timestamptz,
|
||||
CONSTRAINT notification_inbox_events_pkey PRIMARY KEY (
|
||||
recipient_user_id, event_id
|
||||
),
|
||||
CONSTRAINT notification_inbox_events_recipient_fkey FOREIGN KEY (
|
||||
recipient_user_id
|
||||
) REFERENCES users (id) ON DELETE CASCADE,
|
||||
CONSTRAINT notification_inbox_events_organization_fkey FOREIGN KEY (
|
||||
organization_id
|
||||
) REFERENCES organizations (id) ON DELETE RESTRICT,
|
||||
CONSTRAINT notification_inbox_events_type_known CHECK (
|
||||
event_type = 'invitation.delivered'
|
||||
AND schema_version = 1
|
||||
AND source_type = 'invitation'
|
||||
)
|
||||
);
|
||||
|
||||
-- Supports a recipient's chronological inbox and exact organization filters.
|
||||
CREATE INDEX notification_inbox_events_recipient_occurred_idx
|
||||
ON notification_inbox_events (recipient_user_id, occurred_at DESC, event_id DESC);
|
||||
|
||||
CREATE INDEX notification_inbox_events_recipient_organization_occurred_idx
|
||||
ON notification_inbox_events (
|
||||
recipient_user_id, organization_id, occurred_at DESC, event_id DESC
|
||||
);
|
||||
|
||||
-- The event reference is immutable. Only the first read timestamp may be
|
||||
-- added; repeated mark-read calls may keep that timestamp unchanged.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION notification_inbox_events_refuse_update() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF NEW.recipient_user_id IS NOT DISTINCT FROM OLD.recipient_user_id
|
||||
AND NEW.event_id IS NOT DISTINCT FROM OLD.event_id
|
||||
AND NEW.organization_id IS NOT DISTINCT FROM OLD.organization_id
|
||||
AND NEW.event_type IS NOT DISTINCT FROM OLD.event_type
|
||||
AND NEW.schema_version IS NOT DISTINCT FROM OLD.schema_version
|
||||
AND NEW.source_type IS NOT DISTINCT FROM OLD.source_type
|
||||
AND NEW.source_id IS NOT DISTINCT FROM OLD.source_id
|
||||
AND NEW.occurred_at IS NOT DISTINCT FROM OLD.occurred_at
|
||||
AND (
|
||||
NEW.read_at IS NOT DISTINCT FROM OLD.read_at
|
||||
OR (OLD.read_at IS NULL AND NEW.read_at IS NOT NULL)
|
||||
) THEN
|
||||
RETURN NEW;
|
||||
END IF;
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'notification_inbox_events_immutable',
|
||||
TABLE = 'notification_inbox_events',
|
||||
MESSAGE = 'notification inbox event metadata is immutable';
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER notification_inbox_events_refuse_update
|
||||
BEFORE UPDATE ON notification_inbox_events
|
||||
FOR EACH ROW EXECUTE FUNCTION notification_inbox_events_refuse_update();
|
||||
|
||||
-- Prevent clearing a recipient's event marker while allowing account
|
||||
-- deletion to cascade through private inbox rows.
|
||||
-- +goose StatementBegin
|
||||
CREATE FUNCTION notification_inbox_events_refuse_delete() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF pg_trigger_depth() > 1 THEN
|
||||
RETURN OLD;
|
||||
END IF;
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'notification_inbox_events_retained',
|
||||
TABLE = 'notification_inbox_events',
|
||||
MESSAGE = 'notification inbox events cannot be deleted directly';
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
CREATE TRIGGER notification_inbox_events_refuse_delete
|
||||
BEFORE DELETE ON notification_inbox_events
|
||||
FOR EACH ROW EXECUTE FUNCTION notification_inbox_events_refuse_delete();
|
||||
|
||||
-- A rollback must not silently remove recipient-visible history.
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM notification_inbox_events) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'notification_inbox_events_retained',
|
||||
TABLE = 'notification_inbox_events',
|
||||
MESSAGE = 'notification inbox records must be resolved before rollback';
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
DROP TRIGGER notification_inbox_events_refuse_update ON notification_inbox_events;
|
||||
DROP FUNCTION notification_inbox_events_refuse_update();
|
||||
DROP TRIGGER notification_inbox_events_refuse_delete ON notification_inbox_events;
|
||||
DROP FUNCTION notification_inbox_events_refuse_delete();
|
||||
DROP TABLE notification_inbox_events;
|
||||
Reference in new issue
Block a user