110 lines
3.4 KiB
SQL
110 lines
3.4 KiB
SQL
-- ============================================================================
|
|
-- MIGRATION: Ajout du système de gestion des perturbations
|
|
-- Date: 2026-02-06
|
|
-- Description: Ajoute les tables nécessaires pour gérer les perturbations
|
|
-- affectant les horaires de trains
|
|
-- ============================================================================
|
|
|
|
-- Vérifier que les tables n'existent pas déjà
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'perturbations') THEN
|
|
RAISE NOTICE 'La table perturbations existe déjà, migration annulée';
|
|
RETURN;
|
|
END IF;
|
|
END $$;
|
|
|
|
-- Exécuter le script principal
|
|
-- supabase_perturbations_tables.sql a déjà été importé précédemment
|
|
|
|
-- Vérification post-migration
|
|
DO $$
|
|
DECLARE
|
|
perturbations_count INTEGER;
|
|
modifications_count INTEGER;
|
|
BEGIN
|
|
-- Vérifier que les tables ont été créées
|
|
SELECT COUNT(*) INTO perturbations_count
|
|
FROM information_schema.tables
|
|
WHERE table_name = 'perturbations';
|
|
|
|
SELECT COUNT(*) INTO modifications_count
|
|
FROM information_schema.tables
|
|
WHERE table_name = 'perturbations_modifications_parcours';
|
|
|
|
IF perturbations_count = 1 AND modifications_count = 1 THEN
|
|
RAISE NOTICE '✓ Migration réussie : Tables créées avec succès';
|
|
ELSE
|
|
RAISE EXCEPTION '✗ Échec de la migration : Les tables n''ont pas été créées correctement';
|
|
END IF;
|
|
|
|
-- Afficher les colonnes créées
|
|
RAISE NOTICE 'Colonnes de la table perturbations:';
|
|
FOR rec IN (
|
|
SELECT column_name, data_type
|
|
FROM information_schema.columns
|
|
WHERE table_name = 'perturbations'
|
|
ORDER BY ordinal_position
|
|
) LOOP
|
|
RAISE NOTICE ' - % (%)', rec.column_name, rec.data_type;
|
|
END LOOP;
|
|
|
|
END $$;
|
|
|
|
-- Test d'insertion (rollback automatique)
|
|
DO $$
|
|
DECLARE
|
|
test_horaire_id UUID;
|
|
test_perturbation_id UUID;
|
|
BEGIN
|
|
-- Récupérer un horaire existant pour le test
|
|
SELECT id INTO test_horaire_id FROM horaires LIMIT 1;
|
|
|
|
IF test_horaire_id IS NULL THEN
|
|
RAISE NOTICE 'Aucun horaire disponible pour le test';
|
|
RETURN;
|
|
END IF;
|
|
|
|
-- Test d'insertion d'une perturbation
|
|
INSERT INTO perturbations (
|
|
horaire_id,
|
|
type_perturbation,
|
|
duree_retard_minutes,
|
|
cause,
|
|
cause_details,
|
|
actif
|
|
) VALUES (
|
|
test_horaire_id,
|
|
'retard',
|
|
15,
|
|
'incident_technique',
|
|
'Test de migration',
|
|
false
|
|
) RETURNING id INTO test_perturbation_id;
|
|
|
|
IF test_perturbation_id IS NOT NULL THEN
|
|
RAISE NOTICE '✓ Test d''insertion réussi';
|
|
-- Supprimer le test
|
|
DELETE FROM perturbations WHERE id = test_perturbation_id;
|
|
ELSE
|
|
RAISE EXCEPTION '✗ Échec du test d''insertion';
|
|
END IF;
|
|
|
|
EXCEPTION
|
|
WHEN OTHERS THEN
|
|
RAISE NOTICE '✗ Erreur lors du test : %', SQLERRM;
|
|
END $$;
|
|
|
|
-- Afficher un résumé
|
|
SELECT
|
|
'Migration terminée' as statut,
|
|
NOW() as date_migration,
|
|
(SELECT COUNT(*) FROM perturbations) as nombre_perturbations,
|
|
(SELECT COUNT(*) FROM perturbations_modifications_parcours) as nombre_modifications;
|
|
|
|
-- ============================================================================
|
|
-- ROLLBACK (à utiliser en cas de problème)
|
|
-- ============================================================================
|
|
-- DROP TABLE IF EXISTS perturbations_modifications_parcours CASCADE;
|
|
-- DROP TABLE IF EXISTS perturbations CASCADE;
|