ALTER TABLE recording_depletions
    ADD COLUMN IF NOT EXISTS pending_qty numeric(15,3) NOT NULL DEFAULT 0,
    ADD COLUMN IF NOT EXISTS source_product_warehouse_id bigint;

UPDATE recording_depletions rd
SET source_product_warehouse_id = src.product_warehouse_id
FROM recordings r
JOIN LATERAL (
    SELECT pfp.product_warehouse_id
    FROM project_chickins pc
    JOIN project_flock_populations pfp ON pfp.project_chickin_id = pc.id
    WHERE pc.project_flock_kandang_id = r.project_flock_kandangs_id
    ORDER BY pfp.created_at ASC, pfp.id ASC
    LIMIT 1
) AS src ON true
WHERE r.id = rd.recording_id
  AND rd.source_product_warehouse_id IS NULL;
