-- +goose Up -- TASK-089 adds separately chosen opt-in groups to the anonymous card. The -- publication and event constraints call this function, so replacing it -- changes the accepted contract without rewriting any saved field set. -- +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 -- +goose Down -- Do not strand stored publication or immutable history rows outside the old -- contract. An operator must close/remove the newer publications through the -- normal product workflow before this migration can be rolled back. -- +goose StatementBegin DO $$ BEGIN IF EXISTS ( SELECT 1 FROM candidate_publications WHERE fields && ARRAY['salary_compatibility', 'self_assessment', 'work_formats']::text [] ) OR EXISTS ( SELECT 1 FROM candidate_privacy_events WHERE fields && ARRAY['salary_compatibility', 'self_assessment', 'work_formats']::text [] ) THEN RAISE EXCEPTION 'candidate card opt-in fields are still referenced by publication history'; END IF; END; $$; -- +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 6 AND array_position(fields, NULL) IS NULL AND fields <@ ARRAY[ 'categories', 'experience', 'location', 'relocation', 'soft_skills', 'stack' ]::text [] AND fields = ARRAY( SELECT DISTINCT field COLLATE "C" FROM unnest(fields) AS field ORDER BY field COLLATE "C" ); $$; -- +goose StatementEnd