89 lines
3.3 KiB
SQL
89 lines
3.3 KiB
SQL
-- Migration: create table horaires_substitutions
|
|
-- Cette table est utilisée par l'API front-end (src/api/horairesSubstitutions.js)
|
|
CREATE TABLE IF NOT EXISTS horaires_substitutions (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
|
|
-- Référence éventuelle vers un horaire source
|
|
horaire_id UUID REFERENCES horaires(id) ON DELETE SET NULL,
|
|
|
|
-- Région (filtrage RBAC)
|
|
region_id UUID,
|
|
region VARCHAR(255),
|
|
|
|
-- Informations générales (copie similaire à `horaires`)
|
|
numero_train VARCHAR(50),
|
|
type_train VARCHAR(100),
|
|
type_train_id BIGINT,
|
|
|
|
gare_depart_id UUID,
|
|
gare_depart_nom VARCHAR(255),
|
|
heure_depart TIME,
|
|
|
|
gare_arrivee_id UUID,
|
|
gare_arrivee_nom VARCHAR(255),
|
|
heure_arrivee TIME,
|
|
|
|
-- Période de validité de la substitution
|
|
date_debut TIMESTAMP WITH TIME ZONE,
|
|
date_fin TIMESTAMP WITH TIME ZONE,
|
|
|
|
-- Jours / exceptions
|
|
circule_lundi BOOLEAN DEFAULT true,
|
|
circule_mardi BOOLEAN DEFAULT true,
|
|
circule_mercredi BOOLEAN DEFAULT true,
|
|
circule_jeudi BOOLEAN DEFAULT true,
|
|
circule_vendredi BOOLEAN DEFAULT true,
|
|
circule_samedi BOOLEAN DEFAULT true,
|
|
circule_dimanche BOOLEAN DEFAULT true,
|
|
circule_jours_feries BOOLEAN DEFAULT false,
|
|
|
|
jours_personnalises JSONB DEFAULT '[]'::jsonb,
|
|
jours_non_circulation JSONB DEFAULT '[]'::jsonb,
|
|
|
|
materiel_roulant_id BIGINT,
|
|
|
|
ligne_id UUID,
|
|
ligne_nom VARCHAR(255),
|
|
|
|
service_annuel_id UUID,
|
|
service_annuel_nom VARCHAR(255),
|
|
|
|
-- Indique qu'il s'agit d'une substitution
|
|
est_substitution BOOLEAN DEFAULT true,
|
|
motif_substitution VARCHAR(255),
|
|
|
|
-- Snapshot des gares desservies et composition
|
|
gares_desservies JSONB DEFAULT '[]'::jsonb,
|
|
composition_train JSONB DEFAULT '[]'::jsonb,
|
|
|
|
-- Métadonnées
|
|
actif BOOLEAN DEFAULT true,
|
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
|
deleted_at TIMESTAMP WITH TIME ZONE
|
|
);
|
|
|
|
-- Indexes utiles
|
|
CREATE INDEX IF NOT EXISTS idx_horaires_substitutions_region ON horaires_substitutions(region_id);
|
|
CREATE INDEX IF NOT EXISTS idx_horaires_substitutions_horaire ON horaires_substitutions(horaire_id);
|
|
CREATE INDEX IF NOT EXISTS idx_horaires_substitutions_actif ON horaires_substitutions(actif) WHERE deleted_at IS NULL;
|
|
CREATE INDEX IF NOT EXISTS idx_horaires_substitutions_datefin ON horaires_substitutions(date_fin);
|
|
|
|
-- Trigger pour updated_at si la fonction existe
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (SELECT 1 FROM pg_proc WHERE proname = 'update_updated_at_column') THEN
|
|
DROP TRIGGER IF EXISTS update_horaires_substitutions_updated_at ON horaires_substitutions;
|
|
CREATE TRIGGER update_horaires_substitutions_updated_at
|
|
BEFORE UPDATE ON horaires_substitutions
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION update_updated_at_column();
|
|
END IF;
|
|
END$$;
|
|
|
|
COMMENT ON TABLE horaires_substitutions IS 'Table des horaires de substitution (autocar / remplacement)';
|
|
COMMENT ON COLUMN horaires_substitutions.gares_desservies IS 'Liste JSON des gares desservies par la substitution';
|
|
COMMENT ON COLUMN horaires_substitutions.composition_train IS 'Composition JSON du matériel roulant (position, id, nom)';
|
|
COMMENT ON COLUMN horaires_substitutions.date_fin IS 'Date/heure de fin de validité de la substitution';
|
|
|