266 lines
10 KiB
PL/PgSQL
266 lines
10 KiB
PL/PgSQL
-- +goose Up
|
|
-- Candidate projects: a new list kind of profile claims, a publishable card
|
|
-- field `projects`, and the stored server check of each linked GitHub
|
|
-- repository. A project is a claim of trust level 0 like every other; the
|
|
-- repository check is separate evidence and never changes the claim.
|
|
|
|
ALTER TABLE candidate_profile_claims
|
|
DROP CONSTRAINT candidate_profile_claims_kind_ordinal,
|
|
ADD 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)
|
|
OR (kind = 'project' AND ordinal BETWEEN 0 AND 9)
|
|
),
|
|
-- The bounds repeat ProfileProjectItem of the public contract. The address
|
|
-- is the canonical https://github.com/<owner>/<name> that the domain stores.
|
|
ADD CONSTRAINT candidate_profile_claims_project_value CHECK (
|
|
kind <> 'project' OR (
|
|
jsonb_typeof(value) = 'object'
|
|
AND value ?& ARRAY['title', 'description', 'stack', 'repository_url']
|
|
AND value - 'title' - 'description' - 'stack' - 'repository_url' = '{}'::jsonb
|
|
AND jsonb_typeof(value -> 'title') = 'string'
|
|
AND char_length(value ->> 'title') BETWEEN 1 AND 100
|
|
AND value ->> 'title' !~ '^\s|\s$'
|
|
AND value ->> 'title' !~ '[[:cntrl:]]'
|
|
AND (
|
|
jsonb_typeof(value -> 'description') = 'null'
|
|
OR (
|
|
jsonb_typeof(value -> 'description') = 'string'
|
|
AND char_length(value ->> 'description') BETWEEN 1 AND 500
|
|
AND value ->> 'description' !~ '^\s|\s$'
|
|
AND translate(value ->> 'description', E'\n', '') !~ '[[:cntrl:]]'
|
|
)
|
|
)
|
|
AND jsonb_typeof(value -> 'stack') = 'array'
|
|
AND jsonb_array_length(value -> 'stack') <= 10
|
|
AND (
|
|
jsonb_typeof(value -> 'repository_url') = 'null'
|
|
OR (
|
|
jsonb_typeof(value -> 'repository_url') = 'string'
|
|
AND value ->> 'repository_url'
|
|
~ '^https://github\.com/[A-Za-z0-9][A-Za-z0-9-]{0,38}/[A-Za-z0-9._-]{1,100}$'
|
|
)
|
|
)
|
|
)
|
|
);
|
|
|
|
-- The publishable card fields gain `projects`; saved sets stay valid.
|
|
-- +goose StatementBegin
|
|
CREATE OR REPLACE FUNCTION candidate_card_fields_are_canonical(fields text []) RETURNS boolean
|
|
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
|
SELECT
|
|
(cardinality(fields) = 0 OR array_ndims(fields) = 1)
|
|
AND cardinality(fields) BETWEEN 0 AND 10
|
|
AND array_position(fields, NULL) IS NULL
|
|
AND fields <@ ARRAY[
|
|
'categories', 'experience', 'location', 'projects', 'relocation',
|
|
'salary_compatibility', 'self_assessment', 'soft_skills',
|
|
'stack', 'work_formats'
|
|
]::text []
|
|
AND fields = ARRAY(
|
|
SELECT DISTINCT field COLLATE "C"
|
|
FROM unnest(fields) AS field
|
|
ORDER BY field COLLATE "C"
|
|
);
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
-- A card view can now return the published projects.
|
|
-- +goose StatementBegin
|
|
CREATE OR REPLACE FUNCTION candidate_card_access_fields_are_valid(kind text, field_codes text []) RETURNS boolean
|
|
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
|
SELECT
|
|
cardinality(field_codes) BETWEEN 0 AND 7
|
|
AND array_position(field_codes, NULL) IS NULL
|
|
AND field_codes = ARRAY(
|
|
SELECT DISTINCT code COLLATE "C"
|
|
FROM unnest(field_codes) AS code
|
|
ORDER BY code COLLATE "C"
|
|
)
|
|
AND (
|
|
(
|
|
kind = 'card_viewed'
|
|
AND field_codes <@ ARRAY[
|
|
'categories', 'experience', 'projects', 'salary_compatibility',
|
|
'self_assessment', 'stack', 'work_formats'
|
|
]::text []
|
|
)
|
|
OR (
|
|
kind = 'contact_read'
|
|
AND cardinality(field_codes) BETWEEN 1 AND 4
|
|
AND field_codes <@ ARRAY['email', 'full_name', 'phone', 'telegram']::text []
|
|
)
|
|
);
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
-- The latest server check of each repository that a candidate's projects
|
|
-- link. The key is "<owner>/<name>" in lower case. Fetched metadata is kept
|
|
-- only while the repository is verified public and is used for ranking only;
|
|
-- the API never returns it. No token, header, or response body other than the
|
|
-- bounded fields below is stored.
|
|
CREATE TABLE candidate_project_repositories (
|
|
candidate_id uuid NOT NULL,
|
|
repository_key text NOT NULL,
|
|
state text NOT NULL,
|
|
github_id bigint,
|
|
description text,
|
|
topics text [] NOT NULL DEFAULT '{}',
|
|
languages text [] NOT NULL DEFAULT '{}',
|
|
readme_excerpt text,
|
|
repository_etag text,
|
|
readme_etag text,
|
|
failure_class text,
|
|
checked_at timestamptz,
|
|
requested_at timestamptz NOT NULL,
|
|
created_at timestamptz NOT NULL,
|
|
updated_at timestamptz NOT NULL,
|
|
CONSTRAINT candidate_project_repositories_pkey PRIMARY KEY (candidate_id, repository_key),
|
|
CONSTRAINT candidate_project_repositories_candidate_fkey FOREIGN KEY (candidate_id)
|
|
REFERENCES users (id) ON DELETE CASCADE,
|
|
CONSTRAINT candidate_project_repositories_key_format CHECK (
|
|
repository_key ~ '^[a-z0-9][a-z0-9-]{0,38}/[a-z0-9._-]{1,100}$'
|
|
),
|
|
CONSTRAINT candidate_project_repositories_state_known CHECK (
|
|
state IN ('pending', 'verified', 'not_found', 'unavailable')
|
|
),
|
|
CONSTRAINT candidate_project_repositories_failure_known CHECK (
|
|
failure_class IS NULL OR failure_class IN (
|
|
'rate_limited', 'server_error', 'network', 'invalid_response'
|
|
)
|
|
),
|
|
CONSTRAINT candidate_project_repositories_failure_state CHECK (
|
|
(state = 'unavailable') = (failure_class IS NOT NULL)
|
|
),
|
|
CONSTRAINT candidate_project_repositories_checked CHECK (
|
|
state = 'pending' OR checked_at IS NOT NULL
|
|
),
|
|
-- Metadata exists only for a verified public repository.
|
|
CONSTRAINT candidate_project_repositories_evidence_verified CHECK (
|
|
state = 'verified' OR (
|
|
github_id IS NULL
|
|
AND description IS NULL
|
|
AND readme_excerpt IS NULL
|
|
AND cardinality(topics) = 0
|
|
AND cardinality(languages) = 0
|
|
)
|
|
),
|
|
CONSTRAINT candidate_project_repositories_github_id CHECK (
|
|
github_id IS NULL OR github_id > 0
|
|
),
|
|
CONSTRAINT candidate_project_repositories_bounds CHECK (
|
|
(description IS NULL OR char_length(description) <= 1000)
|
|
AND (readme_excerpt IS NULL OR char_length(readme_excerpt) <= 4000)
|
|
AND cardinality(topics) <= 20
|
|
AND cardinality(languages) <= 10
|
|
AND (repository_etag IS NULL OR char_length(repository_etag) <= 200)
|
|
AND (readme_etag IS NULL OR char_length(readme_etag) <= 200)
|
|
),
|
|
CONSTRAINT candidate_project_repositories_times CHECK (
|
|
updated_at >= created_at
|
|
)
|
|
);
|
|
|
|
-- +goose Down
|
|
-- Refuse to strand stored projects or published project fields outside the
|
|
-- older contract; remove them through the product first.
|
|
-- +goose StatementBegin
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (SELECT 1 FROM candidate_profile_claims WHERE kind = 'project')
|
|
OR EXISTS (SELECT 1 FROM candidate_publications WHERE 'projects' = ANY (fields))
|
|
OR EXISTS (SELECT 1 FROM candidate_privacy_events WHERE 'projects' = ANY (fields))
|
|
OR EXISTS (SELECT 1 FROM candidate_card_access_events WHERE 'projects' = ANY (field_codes))
|
|
THEN
|
|
RAISE EXCEPTION 'candidate projects are still stored or published';
|
|
END IF;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
DROP TABLE candidate_project_repositories;
|
|
|
|
-- +goose StatementBegin
|
|
CREATE OR REPLACE FUNCTION candidate_card_access_fields_are_valid(kind text, field_codes text []) RETURNS boolean
|
|
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
|
SELECT
|
|
cardinality(field_codes) BETWEEN 0 AND 6
|
|
AND array_position(field_codes, NULL) IS NULL
|
|
AND field_codes = ARRAY(
|
|
SELECT DISTINCT code COLLATE "C"
|
|
FROM unnest(field_codes) AS code
|
|
ORDER BY code COLLATE "C"
|
|
)
|
|
AND (
|
|
(
|
|
kind = 'card_viewed'
|
|
AND field_codes <@ ARRAY[
|
|
'categories', 'experience', 'salary_compatibility',
|
|
'self_assessment', 'stack', 'work_formats'
|
|
]::text []
|
|
)
|
|
OR (
|
|
kind = 'contact_read'
|
|
AND cardinality(field_codes) BETWEEN 1 AND 4
|
|
AND field_codes <@ ARRAY['email', 'full_name', 'phone', 'telegram']::text []
|
|
)
|
|
);
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
-- +goose StatementBegin
|
|
CREATE OR REPLACE FUNCTION candidate_card_fields_are_canonical(fields text []) RETURNS boolean
|
|
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
|
SELECT
|
|
(cardinality(fields) = 0 OR array_ndims(fields) = 1)
|
|
AND cardinality(fields) BETWEEN 0 AND 9
|
|
AND array_position(fields, NULL) IS NULL
|
|
AND fields <@ ARRAY[
|
|
'categories', 'experience', 'location', 'relocation',
|
|
'salary_compatibility', 'self_assessment', 'soft_skills',
|
|
'stack', 'work_formats'
|
|
]::text []
|
|
AND fields = ARRAY(
|
|
SELECT DISTINCT field COLLATE "C"
|
|
FROM unnest(fields) AS field
|
|
ORDER BY field COLLATE "C"
|
|
);
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
ALTER TABLE candidate_profile_claims
|
|
DROP CONSTRAINT candidate_profile_claims_project_value,
|
|
DROP CONSTRAINT candidate_profile_claims_kind_ordinal,
|
|
ADD 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)
|
|
);
|