BEGIN;
ALTER TABLE gigachatAudience RENAME COLUMN Sex TO Sex_old;
ALTER TABLE gigachatAudience ADD COLUMN Sex INTEGER;
UPDATE gigachatAudience
SET Sex = CASE
    WHEN Sex_old = FALSE THEN 0
    WHEN Sex_old = TRUE  THEN 1
    ELSE 2
END;
ALTER TABLE gigachatAudience DROP COLUMN Sex_old;
ALTER TABLE gigachatAudience ALTER COLUMN Sex SET NOT NULL;
ALTER TABLE gigachatAudience
    ADD CONSTRAINT check_sex_valid_values CHECK (Sex IN (0, 1, 2));
COMMIT;
