-- Ticket #214 — Agenda B0 : lots versionnés, soft-cancel, prefs, remonte_le -- Migration base live. Idempotent. -- --------------------------------------------------------------------------- -- 1. Enums -- --------------------------------------------------------------------------- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'agenda_lot_statut_type') THEN CREATE TYPE agenda_lot_statut_type AS ENUM ( 'en_attente', 'accepte', 'refuse', 'annule' ); END IF; END $$; -- PG : ADD VALUE IF NOT EXISTS (hors sous-transaction fragile) ALTER TYPE statut_evenement_agenda_type ADD VALUE IF NOT EXISTS 'annule'; ALTER TYPE card_instance_statut_type ADD VALUE IF NOT EXISTS 'annulee'; ALTER TYPE card_operation_type ADD VALUE IF NOT EXISTS 'cancel'; -- --------------------------------------------------------------------------- -- 2. Table agenda_lots -- --------------------------------------------------------------------------- CREATE TABLE IF NOT EXISTS agenda_lots ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), type type_evenement_agenda_type NOT NULL, statut agenda_lot_statut_type NOT NULL DEFAULT 'en_attente', revision INT NOT NULL DEFAULT 1, soumis_le TIMESTAMPTZ NOT NULL DEFAULT now(), verrouille_le TIMESTAMPTZ, refuse_le TIMESTAMPTZ, annule_le TIMESTAMPTZ, annule_par UUID REFERENCES utilisateurs(id) ON DELETE SET NULL, cree_par UUID REFERENCES utilisateurs(id) ON DELETE SET NULL, cree_le TIMESTAMPTZ NOT NULL DEFAULT now(), modifie_le TIMESTAMPTZ NOT NULL DEFAULT now(), CONSTRAINT chk_agenda_lots_revision CHECK (revision >= 1) ); CREATE INDEX IF NOT EXISTS idx_agenda_lots_type_statut ON agenda_lots (type, statut); CREATE INDEX IF NOT EXISTS idx_agenda_lots_soumis_le ON agenda_lots (soumis_le); COMMENT ON TABLE agenda_lots IS 'Lots collectifs Agenda (congé/arrêt/absence groupés) — cycle versionné (#214).'; COMMENT ON COLUMN agenda_lots.revision IS 'Incrémenté à chaque resoumission ; les card_responses.revision_lot doivent correspondre.'; -- --------------------------------------------------------------------------- -- 3. evenements_agenda — soft-cancel + lot + heure -- --------------------------------------------------------------------------- ALTER TABLE evenements_agenda ADD COLUMN IF NOT EXISTS annule_le TIMESTAMPTZ, ADD COLUMN IF NOT EXISTS annule_par UUID, ADD COLUMN IF NOT EXISTS verrouille_le TIMESTAMPTZ, ADD COLUMN IF NOT EXISTS refuse_le TIMESTAMPTZ, ADD COLUMN IF NOT EXISTS heure TIME, ADD COLUMN IF NOT EXISTS id_lot UUID; DO $$ BEGIN IF NOT EXISTS ( SELECT 1 FROM pg_constraint WHERE conname = 'evenements_agenda_annule_par_fkey' ) THEN ALTER TABLE evenements_agenda ADD CONSTRAINT evenements_agenda_annule_par_fkey FOREIGN KEY (annule_par) REFERENCES utilisateurs(id) ON DELETE SET NULL; END IF; IF NOT EXISTS ( SELECT 1 FROM pg_constraint WHERE conname = 'evenements_agenda_id_lot_fkey' ) THEN ALTER TABLE evenements_agenda ADD CONSTRAINT evenements_agenda_id_lot_fkey FOREIGN KEY (id_lot) REFERENCES agenda_lots(id) ON DELETE SET NULL; END IF; END $$; CREATE INDEX IF NOT EXISTS idx_evenements_agenda_id_lot ON evenements_agenda (id_lot) WHERE id_lot IS NOT NULL; CREATE INDEX IF NOT EXISTS idx_evenements_agenda_verrouille_le ON evenements_agenda (verrouille_le) WHERE verrouille_le IS NOT NULL; CREATE INDEX IF NOT EXISTS idx_evenements_agenda_refuse_le ON evenements_agenda (refuse_le) WHERE refuse_le IS NOT NULL; COMMENT ON COLUMN evenements_agenda.heure IS 'Heure métier (soin) — durable hors payload carte purgeable.'; COMMENT ON COLUMN evenements_agenda.verrouille_le IS 'Écrit à la soumission/resoumission — jamais pendant un GET.'; COMMENT ON COLUMN evenements_agenda.id_lot IS 'Lot collectif optionnel (unanimité congé / envoi groupé arrêt-absence).'; -- --------------------------------------------------------------------------- -- 4. card_instances — soft-cancel + id_lot + remonte_le -- --------------------------------------------------------------------------- ALTER TABLE card_instances ADD COLUMN IF NOT EXISTS id_lot UUID, ADD COLUMN IF NOT EXISTS annule_le TIMESTAMPTZ, ADD COLUMN IF NOT EXISTS annule_par UUID, ADD COLUMN IF NOT EXISTS remonte_le TIMESTAMPTZ; DO $$ BEGIN IF NOT EXISTS ( SELECT 1 FROM pg_constraint WHERE conname = 'card_instances_id_lot_fkey' ) THEN ALTER TABLE card_instances ADD CONSTRAINT card_instances_id_lot_fkey FOREIGN KEY (id_lot) REFERENCES agenda_lots(id) ON DELETE SET NULL; END IF; IF NOT EXISTS ( SELECT 1 FROM pg_constraint WHERE conname = 'card_instances_annule_par_fkey' ) THEN ALTER TABLE card_instances ADD CONSTRAINT card_instances_annule_par_fkey FOREIGN KEY (annule_par) REFERENCES utilisateurs(id) ON DELETE SET NULL; END IF; END $$; CREATE INDEX IF NOT EXISTS idx_card_instances_id_lot ON card_instances (id_lot) WHERE id_lot IS NOT NULL; CREATE INDEX IF NOT EXISTS idx_card_instances_remonte_le ON card_instances (remonte_le DESC NULLS LAST); COMMENT ON COLUMN card_instances.remonte_le IS 'Renseigné uniquement à la réanimation d''une carte sortie du fil ; tri fil = COALESCE(remonte_le, cree_le).'; -- --------------------------------------------------------------------------- -- 5. card_responses — revision_lot -- --------------------------------------------------------------------------- ALTER TABLE card_responses ADD COLUMN IF NOT EXISTS revision_lot INT NOT NULL DEFAULT 1; DO $$ BEGIN IF NOT EXISTS ( SELECT 1 FROM pg_constraint WHERE conname = 'chk_card_responses_revision_lot' ) THEN ALTER TABLE card_responses ADD CONSTRAINT chk_card_responses_revision_lot CHECK (revision_lot >= 1); END IF; END $$; CREATE INDEX IF NOT EXISTS idx_card_responses_card_revision ON card_responses (id_card, revision_lot); COMMENT ON COLUMN card_responses.revision_lot IS 'Doit matcher agenda_lots.revision courante pour compter dans l''unanimité.'; -- Backfill : réponses existantes = revision 1 (déjà DEFAULT) UPDATE card_responses SET revision_lot = 1 WHERE revision_lot IS NULL; -- --------------------------------------------------------------------------- -- 6. utilisateurs.preferences -- --------------------------------------------------------------------------- ALTER TABLE utilisateurs ADD COLUMN IF NOT EXISTS preferences JSONB NOT NULL DEFAULT '{}'::jsonb; COMMENT ON COLUMN utilisateurs.preferences IS 'Préférences UI (ex. jours_affiches_semaine). PATCH fusionne les clés.'; -- --------------------------------------------------------------------------- -- 7. Index lecture Agenda (statut hors annule côté app ; index utile quand même) -- --------------------------------------------------------------------------- CREATE INDEX IF NOT EXISTS idx_evenements_agenda_placement_dates_actif ON evenements_agenda (id_placement, date_debut, date_fin) WHERE statut IS DISTINCT FROM 'annule';