49 lines
1.8 KiB
PL/PgSQL
49 lines
1.8 KiB
PL/PgSQL
-- ============================================================
|
|
-- CRUCE DE CUENTAS GLM · LIMPIEZA ÚNICA DE HISTÓRICOS PANAMÁ
|
|
-- ============================================================
|
|
-- Objetivo:
|
|
-- Eliminar únicamente los reportes de prueba de Panamá (PA) que ya aparecen
|
|
-- en "Reportes Históricos" y las correcciones BambooHR asociadas a esos
|
|
-- reportes. NO elimina aliases BambooHR ni datos de Guatemala/Trinidad.
|
|
--
|
|
-- Ejecutar UNA SOLA VEZ en el SQL Editor de Supabase antes de entregar Panamá
|
|
-- a los usuarios finales.
|
|
|
|
begin;
|
|
|
|
-- Guardamos temporalmente los reportes PA que serán eliminados para poder
|
|
-- limpiar también correcciones ligadas por execution_id o report_url.
|
|
create temporary table tmp_cruce_cuentas_pa_reportes_a_borrar
|
|
on commit drop
|
|
as
|
|
select
|
|
r.id::text as report_id,
|
|
r.report_url
|
|
from public.cruces_cuentas_gt_reportes r
|
|
where upper(trim(coalesce(r.country, 'GT'))) = 'PA';
|
|
|
|
-- Elimina solamente correcciones vinculadas a los reportes PA de prueba.
|
|
delete from public.cruce_cuentas_bamboo_correcciones c
|
|
using tmp_cruce_cuentas_pa_reportes_a_borrar r
|
|
where upper(trim(coalesce(c.pais, ''))) = 'PA'
|
|
and (
|
|
trim(coalesce(c.execution_id, '')) = r.report_id
|
|
or (
|
|
nullif(trim(coalesce(c.report_url, '')), '') is not null
|
|
and nullif(trim(coalesce(r.report_url, '')), '') is not null
|
|
and c.report_url = r.report_url
|
|
)
|
|
);
|
|
|
|
-- Elimina los reportes históricos de Panamá. Los futuros reportes PA se
|
|
-- guardarán normalmente después de esta limpieza.
|
|
delete from public.cruces_cuentas_gt_reportes r
|
|
where upper(trim(coalesce(r.country, 'GT'))) = 'PA';
|
|
|
|
commit;
|
|
|
|
-- Validación final: debe devolver 0.
|
|
select count(*) as reportes_panama_restantes
|
|
from public.cruces_cuentas_gt_reportes r
|
|
where upper(trim(coalesce(r.country, 'GT'))) = 'PA';
|