Build the facility_programs upsert rows referenced by stays. Identity is the program NAME
((facility_id, name)), so any stay carrying a program_name contributes a row — an external id
is no longer required (EMR program ids churn; program_external_id is kept only as informational
metadata). Rows are deduped by normalized (trimmed) name (last wins) so two stays referencing the
same program produce a single upsert row (avoiding "ON CONFLICT cannot affect row a second time").
The repo trims name to the stored identity.
Build the
facility_programsupsert rows referenced bystays. Identity is the program NAME ((facility_id, name)), so any stay carrying aprogram_namecontributes a row — an external id is no longer required (EMR program ids churn;program_external_idis kept only as informational metadata). Rows are deduped by normalized (trimmed) name (last wins) so two stays referencing the same program produce a single upsert row (avoiding "ON CONFLICT cannot affect row a second time"). The repo trimsnameto the stored identity.