Migration live + BDD.sql from-scratch, module Nest / API /evenements-agenda, FK cartes id_evenement. Front stub path aligné. Co-authored-by: Cursor <cursoragent@cursor.com>
107 lines
3.8 KiB
SQL
107 lines
3.8 KiB
SQL
-- Ticket #205 — Renommer absences_garde → evenements_agenda
|
|
-- Migration base live. Idempotent autant que possible.
|
|
|
|
-- ---------------------------------------------------------------------------
|
|
-- 1. Table + enums
|
|
-- ---------------------------------------------------------------------------
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'absences_garde')
|
|
AND NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'evenements_agenda') THEN
|
|
ALTER TABLE absences_garde RENAME TO evenements_agenda;
|
|
END IF;
|
|
END $$;
|
|
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (SELECT 1 FROM pg_type WHERE typname = 'type_absence_garde_type')
|
|
AND NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'type_evenement_agenda_type') THEN
|
|
ALTER TYPE type_absence_garde_type RENAME TO type_evenement_agenda_type;
|
|
END IF;
|
|
IF EXISTS (SELECT 1 FROM pg_type WHERE typname = 'statut_absence_garde_type')
|
|
AND NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'statut_evenement_agenda_type') THEN
|
|
ALTER TYPE statut_absence_garde_type RENAME TO statut_evenement_agenda_type;
|
|
END IF;
|
|
END $$;
|
|
|
|
-- Contraintes / index (noms)
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'chk_absences_garde_dates') THEN
|
|
ALTER TABLE evenements_agenda RENAME CONSTRAINT chk_absences_garde_dates TO chk_evenements_agenda_dates;
|
|
END IF;
|
|
END $$;
|
|
|
|
ALTER INDEX IF EXISTS idx_absences_garde_placement_dates
|
|
RENAME TO idx_evenements_agenda_placement_dates;
|
|
ALTER INDEX IF EXISTS idx_absences_garde_placement_type_statut
|
|
RENAME TO idx_evenements_agenda_placement_type_statut;
|
|
ALTER INDEX IF EXISTS idx_absences_garde_expire_at
|
|
RENAME TO idx_evenements_agenda_expire_at;
|
|
ALTER INDEX IF EXISTS absences_garde_pkey
|
|
RENAME TO evenements_agenda_pkey;
|
|
|
|
COMMENT ON TABLE evenements_agenda IS
|
|
'Vérité métier agenda (absences, congés, arrêts, futurs soins/école/mairie/sorties…) par placement. Les cartes collectent ; cette table historise.';
|
|
COMMENT ON COLUMN evenements_agenda.expire_at IS
|
|
'Purge auto (job) pour en_attente/refuse. accepte → infinity.';
|
|
COMMENT ON COLUMN evenements_agenda.id_card_instance IS
|
|
'Réf. carte de collecte (module Cartes).';
|
|
|
|
-- ---------------------------------------------------------------------------
|
|
-- 2. card_instances.id_absence → id_evenement
|
|
-- ---------------------------------------------------------------------------
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (
|
|
SELECT 1 FROM information_schema.columns
|
|
WHERE table_name = 'card_instances' AND column_name = 'id_absence'
|
|
) AND NOT EXISTS (
|
|
SELECT 1 FROM information_schema.columns
|
|
WHERE table_name = 'card_instances' AND column_name = 'id_evenement'
|
|
) THEN
|
|
ALTER TABLE card_instances RENAME COLUMN id_absence TO id_evenement;
|
|
END IF;
|
|
END $$;
|
|
|
|
-- Recréer index si ancien nom
|
|
ALTER INDEX IF EXISTS idx_card_instances_absence
|
|
RENAME TO idx_card_instances_evenement;
|
|
|
|
-- FK : drop ancienne (nom variable) puis recreate
|
|
DO $$
|
|
DECLARE
|
|
fk_name text;
|
|
BEGIN
|
|
SELECT conname INTO fk_name
|
|
FROM pg_constraint
|
|
WHERE conrelid = 'card_instances'::regclass
|
|
AND contype = 'f'
|
|
AND pg_get_constraintdef(oid) ILIKE '%id_evenement%'
|
|
LIMIT 1;
|
|
|
|
IF fk_name IS NULL THEN
|
|
SELECT conname INTO fk_name
|
|
FROM pg_constraint
|
|
WHERE conrelid = 'card_instances'::regclass
|
|
AND contype = 'f'
|
|
AND pg_get_constraintdef(oid) ILIKE '%id_absence%'
|
|
LIMIT 1;
|
|
END IF;
|
|
|
|
IF fk_name IS NOT NULL THEN
|
|
EXECUTE format('ALTER TABLE card_instances DROP CONSTRAINT %I', fk_name);
|
|
END IF;
|
|
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM pg_constraint
|
|
WHERE conname = 'card_instances_id_evenement_fkey'
|
|
) THEN
|
|
ALTER TABLE card_instances
|
|
ADD CONSTRAINT card_instances_id_evenement_fkey
|
|
FOREIGN KEY (id_evenement)
|
|
REFERENCES evenements_agenda(id)
|
|
ON DELETE SET NULL;
|
|
END IF;
|
|
END $$;
|