alter table public.rutero_proyectos add column if not exists pais text; -- Preserve the country of projects created before pais was stored explicitly. update public.rutero_proyectos proyecto set pais = source.pais from ( select distinct on (pdv.proyecto_id) pdv.proyecto_id, nullif(trim(pdv.pdv_data ->> 'País'), '') as pais from public.rutero_proyecto_pdvs pdv where nullif(trim(pdv.pdv_data ->> 'País'), '') is not null order by pdv.proyecto_id, pdv.created_at ) source where proyecto.id = source.proyecto_id and proyecto.pais is null; alter table public.rutero_proyectos alter column estado drop not null, alter column estado drop default; update public.rutero_proyectos set estado = null where estado = 'borrador';