Files
petitspas/database/migrations/2026_absences_garde.sql
T
jmartinandCursor d7783badca feat(#193): table absences_garde + drop evenements.
Vérité métier absences/congés/arrêt par placement (périodes, expire_at).
Entité TypeORM, BDD.sql, docs enums/FK, seed et verify alignés.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-24 11:09:39 +02:00

86 lines
3.3 KiB
SQL

-- Ticket #193 — Back métier absences / congés / arrêt (périodes par placement)
-- Idempotent : safe à rejouer.
-- Remplace la table legacy `evenements` (mal conçue, non utilisée en API).
-- ---------------------------------------------------------------------------
-- Enums
-- ---------------------------------------------------------------------------
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'type_absence_garde_type') THEN
CREATE TYPE type_absence_garde_type AS ENUM (
'absence_enfant',
'conge_am',
'arret_maladie_am'
);
END IF;
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'statut_absence_garde_type') THEN
CREATE TYPE statut_absence_garde_type AS ENUM (
'en_attente',
'accepte',
'refuse'
);
END IF;
END $$;
-- ---------------------------------------------------------------------------
-- Table absences_garde (1 ligne = 1 période)
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS absences_garde (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
id_placement UUID NOT NULL
REFERENCES enfants_assistantes_maternelles(id) ON DELETE CASCADE,
type type_absence_garde_type NOT NULL,
date_debut DATE NOT NULL,
date_fin DATE NOT NULL,
statut statut_absence_garde_type NOT NULL DEFAULT 'en_attente',
-- TTL purge : obligatoire ; pour statut=accepte utiliser 'infinity' (pas de purge métier)
expire_at TIMESTAMPTZ NOT NULL,
cree_par UUID REFERENCES utilisateurs(id) ON DELETE SET NULL,
id_card_instance UUID NULL,
motif TEXT,
cree_le TIMESTAMPTZ NOT NULL DEFAULT now(),
modifie_le TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT chk_absences_garde_dates CHECK (date_fin >= date_debut)
);
CREATE INDEX IF NOT EXISTS idx_absences_garde_placement_dates
ON absences_garde (id_placement, date_debut, date_fin);
CREATE INDEX IF NOT EXISTS idx_absences_garde_placement_type_statut
ON absences_garde (id_placement, type, statut);
CREATE INDEX IF NOT EXISTS idx_absences_garde_expire_at
ON absences_garde (expire_at)
WHERE statut IN ('en_attente', 'refuse');
COMMENT ON TABLE absences_garde IS
'Vérité métier des absences/congés/arrêts par couple AM↔enfant (placement). Les cartes collectent ; cette table stocke.';
COMMENT ON COLUMN absences_garde.expire_at IS
'Purge auto (job ultérieur) pour en_attente/refuse. accepte → infinity.';
COMMENT ON COLUMN absences_garde.id_card_instance IS
'Réf. carte de collecte (FK cards quand le module Cartes existera).';
-- ---------------------------------------------------------------------------
-- Drop legacy evenements (+ enums si plus référencés)
-- ---------------------------------------------------------------------------
DROP TABLE IF EXISTS evenements CASCADE;
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM pg_type WHERE typname = 'type_evenement_type')
AND NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE udt_name = 'type_evenement_type'
) THEN
DROP TYPE type_evenement_type;
END IF;
IF EXISTS (SELECT 1 FROM pg_type WHERE typname = 'statut_evenement_type')
AND NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE udt_name = 'statut_evenement_type'
) THEN
DROP TYPE statut_evenement_type;
END IF;
END $$;