BEGIN;

ALTER TABLE recordings
    DROP CONSTRAINT IF EXISTS chk_recordings_nonnegatives_v3;

ALTER TABLE recordings
    DROP COLUMN IF EXISTS daily_gain,
    DROP COLUMN IF EXISTS avg_daily_gain;

DO $$
BEGIN
    IF EXISTS (
        SELECT 1
        FROM information_schema.columns
        WHERE table_name = 'recordings' AND column_name = 'hand_day'
    ) THEN
        ALTER TABLE recordings RENAME COLUMN hand_day TO hen_day;
    END IF;
END $$;

DO $$
BEGIN
    IF EXISTS (
        SELECT 1
        FROM information_schema.columns
        WHERE table_name = 'recordings' AND column_name = 'hand_house'
    ) THEN
        ALTER TABLE recordings RENAME COLUMN hand_house TO hen_house;
    END IF;
END $$;

DO $$
BEGIN
    IF EXISTS (
        SELECT 1
        FROM information_schema.columns
        WHERE table_name = 'recordings' AND column_name = 'egg_mesh'
    ) THEN
        ALTER TABLE recordings RENAME COLUMN egg_mesh TO egg_mass;
    END IF;
END $$;

ALTER TABLE recordings
    ADD CONSTRAINT chk_recordings_nonnegatives_v4 CHECK (
        (total_depletion_qty IS NULL OR total_depletion_qty >= 0) AND
        (cum_depletion_rate  IS NULL OR cum_depletion_rate  >= 0) AND
        (cum_intake          IS NULL OR cum_intake          >= 0) AND
        (fcr_value           IS NULL OR fcr_value           >= 0) AND
        (total_chick_qty     IS NULL OR total_chick_qty     >= 0) AND
        (hen_day             IS NULL OR hen_day             >= 0) AND
        (hen_house           IS NULL OR hen_house           >= 0) AND
        (feed_intake         IS NULL OR feed_intake         >= 0) AND
        (egg_mass            IS NULL OR egg_mass            >= 0) AND
        (egg_weight          IS NULL OR egg_weight          >= 0)
    );

COMMIT;
