From 5dc419b7a70c9b2d2ed849db4009d08fcdbbbbb3 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Thu, 23 Jan 2025 00:26:40 +0200 Subject: [PATCH] Fixes deletion of platforms with hosts/agents in them --- app/classes/platform.php | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/app/classes/platform.php b/app/classes/platform.php index d93e7ed..0b71199 100644 --- a/app/classes/platform.php +++ b/app/classes/platform.php @@ -122,17 +122,40 @@ class Platform { */ public function deletePlatform($platform_id) { try { - $sql = 'DELETE FROM platforms - WHERE - id = :platform_id'; + $this->db->beginTransaction(); + // First, get all hosts in this platform + $sql = 'SELECT id FROM hosts WHERE platform_id = :platform_id'; $query = $this->db->prepare($sql); $query->bindParam(':platform_id', $platform_id); - $query->execute(); + $hosts = $query->fetchAll(PDO::FETCH_ASSOC); + + // Delete all agents for each host + foreach ($hosts as $host) { + $sql = 'DELETE FROM jilo_agents WHERE host_id = :host_id'; + $query = $this->db->prepare($sql); + $query->bindParam(':host_id', $host['id']); + $query->execute(); + } + + // Delete all hosts in this platform + $sql = 'DELETE FROM hosts WHERE platform_id = :platform_id'; + $query = $this->db->prepare($sql); + $query->bindParam(':platform_id', $platform_id); + $query->execute(); + + // Finally, delete the platform + $sql = 'DELETE FROM platforms WHERE id = :platform_id'; + $query = $this->db->prepare($sql); + $query->bindParam(':platform_id', $platform_id); + $query->execute(); + + $this->db->commit(); return true; } catch (Exception $e) { + $this->db->rollBack(); return $e->getMessage(); } }