-- +goose Up -- The manual candidate profile (PRD FR02, TASK-066): full name and contacts, -- stack, roles, work experience, soft skills, and desired terms, each value -- with its confirmation state and provenance. Enum-like columns are text with -- CHECK constraints, as in the identity tables. -- -- The rows are personal data of the candidate (names, contacts, work -- history, the lowest salary), so the application never copies a value into -- logs, metrics, events, or URLs. Only the owning account reads them; no -- organization-visible query selects from these tables. -- -- The profile stands on its own. Nothing here references a resume file, the -- object storage, the ML service, or an FSP account: a profile that was typed -- by hand is complete, and a later import adds its own columns or tables -- (additively) instead of changing the meaning of these. -- -- A confirmed grade or category is deliberately not stored here. It comes -- from assessment results, and the self-assessed grade of a role is only a -- claim inside the value of a role claim, so no statement against these -- tables can write a confirmed category. CREATE TABLE candidate_profiles ( -- The account that owns the profile. One profile per candidate; the -- profile is created by the first save and the app never reads another -- account's row. user_id uuid NOT NULL, -- Optimistic concurrency version, advanced by every save that changes the -- profile. A first save creates version 1; the contract's version 0 is -- the absence of a row. version bigint NOT NULL, created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL, CONSTRAINT candidate_profiles_pkey PRIMARY KEY (user_id), -- The profile is personal data of the account and goes with it when the -- account is erased. The retention rules are an open legal question. CONSTRAINT candidate_profiles_user_id_fkey FOREIGN KEY ( user_id ) REFERENCES users (id) ON DELETE CASCADE, CONSTRAINT candidate_profiles_version_positive CHECK (version > 0), CONSTRAINT candidate_profiles_updated_after_creation CHECK ( updated_at >= created_at ) ); -- One row per stored claim: a single field (kind full_name, contact_*, -- terms_*, at ordinal 0) or one item of a list (stack, role, experience, -- soft_skill, at its position). A field without a value has no row, so the -- contract's `unknown` state is the absence of the row, and every row holds a -- value with a status of confirmed or extracted. The whole set of a profile is -- replaced in one transaction with the version bump. -- -- The value is jsonb because the kinds have different shapes (a string, a -- boolean, or an object). Each kind has its own CHECK for the shape and the -- bounds of the contract, and the domain validates the value in full before -- it is stored. CREATE TABLE candidate_profile_claims ( user_id uuid NOT NULL, kind text NOT NULL, -- Position in a list, from 0; always 0 for a single field. ordinal smallint NOT NULL, value jsonb NOT NULL, -- confirmed: the candidate typed or confirmed the value. extracted: a -- suggestion from an imported source that nobody confirmed. status text NOT NULL, -- manual, or an import origin (planned: extracted_from_resume, imported). origin text NOT NULL, -- Every claim of the profile has trust level 0 whatever its origin; -- higher levels belong to evidence, which is stored elsewhere. The column -- states the rule in the schema, so an import can never store a claim -- with a higher level. trust_level smallint NOT NULL, -- Excerpt of the imported document that the value came from. It can hold -- personal data. NULL for a manual value. quote text, -- Opaque label of the extractor version. NULL for a manual value. extraction_version text, -- When the candidate typed or confirmed the value; NULL while it is an -- unconfirmed suggestion. confirmed_at timestamptz, CONSTRAINT candidate_profile_claims_pkey PRIMARY KEY ( user_id, kind, ordinal ), -- The claims belong to the profile row and go with it. CONSTRAINT candidate_profile_claims_user_id_fkey FOREIGN KEY ( user_id ) REFERENCES candidate_profiles (user_id) ON DELETE CASCADE, CONSTRAINT candidate_profile_claims_kind_ordinal CHECK ( ( kind IN ( 'full_name', 'contact_email', 'contact_phone', 'contact_telegram', 'terms_location', 'terms_relocation', 'terms_salary_minimum' ) AND ordinal = 0 ) OR (kind = 'stack' AND ordinal BETWEEN 0 AND 29) OR (kind = 'role' AND ordinal BETWEEN 0 AND 8) OR (kind = 'experience' AND ordinal BETWEEN 0 AND 19) OR (kind = 'soft_skill' AND ordinal BETWEEN 0 AND 9) ), CONSTRAINT candidate_profile_claims_status_known CHECK ( status IN ('confirmed', 'extracted') ), CONSTRAINT candidate_profile_claims_origin_known CHECK ( origin IN ('manual', 'extracted_from_resume', 'imported') ), CONSTRAINT candidate_profile_claims_trust_level_zero CHECK ( trust_level = 0 ), -- A confirmed value says when; a suggestion does not. CONSTRAINT candidate_profile_claims_confirmation CHECK ( (status = 'confirmed' AND confirmed_at IS NOT NULL) OR (status = 'extracted' AND confirmed_at IS NULL) ), -- A manual value is confirmed by definition and has no import data. An -- imported value names its extractor, and an unconfirmed one is always -- an import. CONSTRAINT candidate_profile_claims_origin_provenance CHECK ( ( origin = 'manual' AND status = 'confirmed' AND quote IS NULL AND extraction_version IS NULL ) OR (origin <> 'manual' AND extraction_version IS NOT NULL) ), CONSTRAINT candidate_profile_claims_quote_format CHECK ( quote IS NULL OR ( char_length(quote) BETWEEN 1 AND 300 AND translate(quote, E'\n', '') !~ '[[:cntrl:]]' ) ), CONSTRAINT candidate_profile_claims_extraction_version_format CHECK ( extraction_version IS NULL OR extraction_version ~ '^[A-Za-z0-9._-]{1,64}$' ), -- Value shapes. The bounds repeat the schemas of the public contract. CONSTRAINT candidate_profile_claims_full_name_value CHECK ( kind <> 'full_name' OR ( jsonb_typeof(value) = 'string' AND char_length(value #>> '{}') BETWEEN 1 AND 200 AND value #>> '{}' !~ '^\s|\s$' AND value #>> '{}' !~ '[[:cntrl:]]' ) ), CONSTRAINT candidate_profile_claims_contact_email_value CHECK ( kind <> 'contact_email' OR ( jsonb_typeof(value) = 'string' AND char_length(value #>> '{}') BETWEEN 3 AND 254 AND value #>> '{}' ~ '^[^@\s]+@[^@\s]+$' ) ), -- E.164, the format of the contact disclosure. CONSTRAINT candidate_profile_claims_contact_phone_value CHECK ( kind <> 'contact_phone' OR ( jsonb_typeof(value) = 'string' AND value #>> '{}' ~ '^\+[1-9][0-9]{6,14}$' ) ), CONSTRAINT candidate_profile_claims_contact_telegram_value CHECK ( kind <> 'contact_telegram' OR ( jsonb_typeof(value) = 'string' AND value #>> '{}' ~ '^@[A-Za-z0-9_]{5,32}$' ) ), CONSTRAINT candidate_profile_claims_stack_value CHECK ( kind <> 'stack' OR ( jsonb_typeof(value) = 'string' AND char_length(value #>> '{}') BETWEEN 1 AND 50 AND value #>> '{}' !~ '^\s|\s$' AND value #>> '{}' !~ '[[:cntrl:]]' ) ), CONSTRAINT candidate_profile_claims_soft_skill_value CHECK ( kind <> 'soft_skill' OR ( jsonb_typeof(value) = 'string' AND char_length(value #>> '{}') BETWEEN 1 AND 50 AND value #>> '{}' !~ '^\s|\s$' AND value #>> '{}' !~ '[[:cntrl:]]' ) ), CONSTRAINT candidate_profile_claims_terms_location_value CHECK ( kind <> 'terms_location' OR ( jsonb_typeof(value) = 'string' AND char_length(value #>> '{}') BETWEEN 1 AND 100 AND value #>> '{}' !~ '^\s|\s$' AND value #>> '{}' !~ '[[:cntrl:]]' ) ), CONSTRAINT candidate_profile_claims_terms_relocation_value CHECK ( kind <> 'terms_relocation' OR jsonb_typeof(value) = 'boolean' ), -- A positive multiple of 1000 up to 100 000 000, in the only currency, -- period, and tax bases of the MVP. CONSTRAINT candidate_profile_claims_terms_salary_minimum_value CHECK ( kind <> 'terms_salary_minimum' OR ( jsonb_typeof(value) = 'object' AND value ?& ARRAY['amount', 'currency', 'period', 'tax_basis'] AND value - 'amount' - 'currency' - 'period' - 'tax_basis' = '{}'::jsonb AND jsonb_typeof(value -> 'amount') = 'number' AND value ->> 'amount' ~ '^([1-9][0-9]{0,4}000|100000000)$' AND value -> 'currency' = '"RUB"'::jsonb AND value -> 'period' = '"month"'::jsonb AND value ->> 'tax_basis' IN ('net', 'gross') ) ), -- The self-assessed grade is a claim of the role. A confirmed grade is not -- a key of the value. CONSTRAINT candidate_profile_claims_role_value CHECK ( kind <> 'role' OR ( jsonb_typeof(value) = 'object' AND value ?& ARRAY['specialization', 'self_assessed_grade'] AND value - 'specialization' - 'self_assessed_grade' = '{}'::jsonb AND value ->> 'specialization' IN ( 'backend', 'frontend', 'mobile', 'network_engineer', 'devops_sre', 'data_analyst', 'ds_ml', 'information_security', 'qa' ) AND ( jsonb_typeof(value -> 'self_assessed_grade') = 'null' OR value ->> 'self_assessed_grade' IN ( 'intern', 'junior', 'middle', 'senior' ) ) ) ), CONSTRAINT candidate_profile_claims_experience_value CHECK ( kind <> 'experience' OR ( jsonb_typeof(value) = 'object' AND value ?& ARRAY[ 'employer_name', 'specialization', 'started_on', 'ended_on', 'summary' ] AND value - 'employer_name' - 'specialization' - 'started_on' - 'ended_on' - 'summary' = '{}'::jsonb AND value ->> 'specialization' IN ( 'backend', 'frontend', 'mobile', 'network_engineer', 'devops_sre', 'data_analyst', 'ds_ml', 'information_security', 'qa' ) AND jsonb_typeof(value -> 'started_on') = 'string' AND value ->> 'started_on' ~ '^[0-9]{4}-(0[1-9]|1[0-2])$' AND ( jsonb_typeof(value -> 'ended_on') = 'null' OR ( jsonb_typeof(value -> 'ended_on') = 'string' AND value ->> 'ended_on' ~ '^[0-9]{4}-(0[1-9]|1[0-2])$' AND value ->> 'ended_on' >= value ->> 'started_on' ) ) AND ( jsonb_typeof(value -> 'employer_name') = 'null' OR ( jsonb_typeof(value -> 'employer_name') = 'string' AND char_length(value ->> 'employer_name') BETWEEN 1 AND 200 AND value ->> 'employer_name' !~ '^\s|\s$' AND value ->> 'employer_name' !~ '[[:cntrl:]]' ) ) AND ( jsonb_typeof(value -> 'summary') = 'null' OR ( jsonb_typeof(value -> 'summary') = 'string' AND char_length(value ->> 'summary') BETWEEN 1 AND 1000 AND value ->> 'summary' !~ '^\s|\s$' AND translate(value ->> 'summary', E'\n', '') !~ '[[:cntrl:]]' ) ) ) ) ); -- A tag or a skill appears once and a specialization appears in one role. The -- indexes compare the stored text exactly; the case-insensitive comparison -- of the contract belongs to the domain, because PostgreSQL lower() depends -- on the collation. CREATE UNIQUE INDEX candidate_profile_claims_stack_key ON candidate_profile_claims (user_id, (value #>> '{}')) WHERE kind = 'stack'; CREATE UNIQUE INDEX candidate_profile_claims_soft_skill_key ON candidate_profile_claims (user_id, (value #>> '{}')) WHERE kind = 'soft_skill'; CREATE UNIQUE INDEX candidate_profile_claims_role_specialization_key ON candidate_profile_claims (user_id, (value ->> 'specialization')) WHERE kind = 'role'; -- +goose Down DROP TABLE candidate_profile_claims; DROP TABLE candidate_profiles;