Files
lincodex/backend/db/migrations/00020_create_candidate_profile_history.sql

75 lines
3.2 KiB
PL/PgSQL

-- +goose Up
-- Each row in candidate_profile_overlap_confirmations represents the
-- candidate's acknowledgement of one exact pair of periods in the current
-- profile. Replacing a profile replaces these rows in the same transaction.
CREATE TABLE candidate_profile_overlap_confirmations (
user_id uuid NOT NULL,
first_index smallint NOT NULL,
second_index smallint NOT NULL,
first_started_on text NOT NULL,
first_ended_on text,
second_started_on text NOT NULL,
second_ended_on text,
CONSTRAINT candidate_profile_overlap_confirmations_pkey PRIMARY KEY (
user_id, first_index, second_index
),
CONSTRAINT candidate_profile_overlap_confirmations_profile_fkey FOREIGN KEY (
user_id
) REFERENCES candidate_profiles (user_id) ON DELETE CASCADE,
CONSTRAINT candidate_profile_overlap_confirmations_indices CHECK (
first_index BETWEEN 0 AND 18
AND second_index > first_index
AND second_index BETWEEN 1 AND 19
),
CONSTRAINT candidate_profile_overlap_confirmations_months CHECK (
first_started_on ~ '^[0-9]{4}-(0[1-9]|1[0-2])$'
AND (first_ended_on IS NULL OR first_ended_on ~ '^[0-9]{4}-(0[1-9]|1[0-2])$')
AND second_started_on ~ '^[0-9]{4}-(0[1-9]|1[0-2])$'
AND (second_ended_on IS NULL OR second_ended_on ~ '^[0-9]{4}-(0[1-9]|1[0-2])$')
AND (first_ended_on IS NULL OR first_ended_on >= first_started_on)
AND (second_ended_on IS NULL OR second_ended_on >= second_started_on)
AND (first_ended_on IS NULL OR second_started_on <= first_ended_on)
AND (second_ended_on IS NULL OR first_started_on <= second_ended_on)
)
);
-- The snapshots are private, append-only application records of every
-- changed profile version. They go with the owning account and have no
-- automatic retention limit. Each individual snapshot is bounded by the
-- complete public profile schema.
CREATE TABLE candidate_profile_history (
user_id uuid NOT NULL,
version bigint NOT NULL,
changed_at timestamptz NOT NULL,
snapshot jsonb NOT NULL,
CONSTRAINT candidate_profile_history_pkey PRIMARY KEY (user_id, version),
CONSTRAINT candidate_profile_history_profile_fkey FOREIGN KEY (
user_id
) REFERENCES candidate_profiles (user_id) ON DELETE CASCADE,
CONSTRAINT candidate_profile_history_version_positive CHECK (version > 0),
CONSTRAINT candidate_profile_history_snapshot_bounded CHECK (
jsonb_typeof(snapshot) = 'object'
AND octet_length(snapshot::text) <= 262144
)
);
-- +goose StatementBegin
CREATE FUNCTION reject_candidate_profile_history_update() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION 'candidate profile history does not allow updates'
USING ERRCODE = 'integrity_constraint_violation';
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER candidate_profile_history_no_update
BEFORE UPDATE ON candidate_profile_history
FOR EACH ROW EXECUTE FUNCTION reject_candidate_profile_history_update();
-- +goose Down
DROP TRIGGER candidate_profile_history_no_update ON candidate_profile_history;
DROP FUNCTION reject_candidate_profile_history_update();
DROP TABLE candidate_profile_history;
DROP TABLE candidate_profile_overlap_confirmations;