-- Add production_standard_id to project_flocks
ALTER TABLE project_flocks
    ADD COLUMN IF NOT EXISTS production_standard_id BIGINT;

DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'production_standards') THEN
        ALTER TABLE project_flocks
            ADD CONSTRAINT fk_project_flocks_production_standard_id
            FOREIGN KEY (production_standard_id) REFERENCES production_standards (id) ON DELETE RESTRICT ON UPDATE CASCADE;
    END IF;
END $$;

CREATE INDEX IF NOT EXISTS idx_project_flocks_production_standard_id
    ON project_flocks (production_standard_id);
