-- Migration: Ajouter table d'historique pour tracer les déplacements des techniciens -- Date: 2025-12-15 -- Table pour position actuelle (déjà existante, mais on s'assure de sa structure) CREATE TABLE IF NOT EXISTS `technician_locations` ( `user_id` INT NOT NULL, `latitude` DECIMAL(10,8) NOT NULL, `longitude` DECIMAL(11,8) NOT NULL, `accuracy` FLOAT NULL COMMENT 'Précision en mètres', `heading` FLOAT NULL COMMENT 'Direction (0-360°)', `speed` FLOAT NULL COMMENT 'Vitesse en m/s', `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`user_id`), CONSTRAINT `fk_tech_location_user` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Position actuelle des techniciens en temps réel'; -- Nouvelle table d'historique pour tracer les trajectoires CREATE TABLE IF NOT EXISTS `technician_location_history` ( `id` INT AUTO_INCREMENT PRIMARY KEY, `user_id` INT NOT NULL, `latitude` DECIMAL(10,8) NOT NULL, `longitude` DECIMAL(11,8) NOT NULL, `accuracy` FLOAT NULL COMMENT 'Précision en mètres', `heading` FLOAT NULL COMMENT 'Direction (0-360°)', `speed` FLOAT NULL COMMENT 'Vitesse en m/s', `recorded_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, INDEX `idx_user_time` (`user_id`, `recorded_at`), INDEX `idx_recorded_at` (`recorded_at`), CONSTRAINT `fk_tech_history_user` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Historique des positions pour tracer les trajectoires'; -- Optionnel: Nettoyage automatique de l'historique (garder 7 jours) -- Créer un événement planifié (nécessite EVENT_SCHEDULER=ON dans MySQL) SET GLOBAL event_scheduler = ON; DROP EVENT IF EXISTS `cleanup_old_location_history`; CREATE EVENT `cleanup_old_location_history` ON SCHEDULE EVERY 1 DAY STARTS CURRENT_TIMESTAMP DO DELETE FROM `technician_location_history` WHERE `recorded_at` < DATE_SUB(NOW(), INTERVAL 7 DAY); -- Afficher les tables créées SHOW TABLES LIKE 'technician_location%'; -- Afficher la structure DESCRIBE technician_locations; DESCRIBE technician_location_history; SELECT 'Migration terminée avec succès!' AS message;