DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`roadmapStop_beforeDelete`
	BEFORE DELETE ON `roadmapStop`
	FOR EACH ROW
BEGIN
	DECLARE vMaxEta DATETIME;
	DECLARE vRoadmapEta DATETIME;

	IF OLD.roadmapFk IS NOT NULL THEN
		SELECT MAX(eta) INTO vMaxEta
			FROM roadmapStop
			WHERE roadmapFk = OLD.roadmapFk
				AND id <> OLD.id;

		SELECT eta INTO vRoadmapEta
			FROM roadmap
			WHERE id = OLD.roadmapFk;

		IF vMaxEta <> vRoadmapEta OR vMaxEta IS NULL THEN
			UPDATE roadmap
				SET eta = vMaxEta
				WHERE id = OLD.roadmapFk;
		END IF;
	END IF;
END$$
DELIMITER ;