35 lines
1.8 KiB
PL/PgSQL
35 lines
1.8 KiB
PL/PgSQL
-- ═══════════════════════════════════════════════════════════════════════════
|
|
-- Unifica paciente_id → patient_id em agenda_eventos
|
|
-- ═══════════════════════════════════════════════════════════════════════════
|
|
-- Contexto:
|
|
-- Campo legado `paciente_id` (texto, sem FK) coexiste com `patient_id`
|
|
-- (uuid, com FK → patients.id). Eventos antigos têm `paciente_id` preenchido
|
|
-- mas `patient_id = null`. Esta migration corrige isso e remove a coluna legada.
|
|
--
|
|
-- SEGURANÇA:
|
|
-- Execute em transação. Verifique os counts antes do COMMIT.
|
|
-- ═══════════════════════════════════════════════════════════════════════════
|
|
|
|
BEGIN;
|
|
|
|
-- 1. Copia paciente_id → patient_id onde patient_id ainda é null
|
|
-- paciente_id já é uuid no banco — sem necessidade de cast ou validação de regex
|
|
UPDATE public.agenda_eventos
|
|
SET patient_id = paciente_id
|
|
WHERE patient_id IS NULL
|
|
AND paciente_id IS NOT NULL;
|
|
|
|
-- 2. Verificação: deve retornar 0
|
|
SELECT COUNT(*) AS "orfaos_restantes"
|
|
FROM public.agenda_eventos
|
|
WHERE patient_id IS NULL AND paciente_id IS NOT NULL;
|
|
|
|
-- 3. Remove a coluna legada
|
|
ALTER TABLE public.agenda_eventos DROP COLUMN IF EXISTS paciente_id;
|
|
|
|
-- 4. Remove FK e coluna legada de terapeuta_id se existir equivalente
|
|
-- (opcional — remova o comentário se quiser limpar terapeuta_id também)
|
|
-- ALTER TABLE public.agenda_eventos DROP COLUMN IF EXISTS terapeuta_id;
|
|
|
|
COMMIT;
|