Fixes deletion of platforms with hosts/agents in them

main
Yasen Pramatarov 2025-01-23 00:26:40 +02:00
parent aa2dcc027d
commit 5dc419b7a7
1 changed files with 27 additions and 4 deletions

View File

@ -122,17 +122,40 @@ class Platform {
*/ */
public function deletePlatform($platform_id) { public function deletePlatform($platform_id) {
try { try {
$sql = 'DELETE FROM platforms $this->db->beginTransaction();
WHERE
id = :platform_id';
// First, get all hosts in this platform
$sql = 'SELECT id FROM hosts WHERE platform_id = :platform_id';
$query = $this->db->prepare($sql); $query = $this->db->prepare($sql);
$query->bindParam(':platform_id', $platform_id); $query->bindParam(':platform_id', $platform_id);
$query->execute(); $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; return true;
} catch (Exception $e) { } catch (Exception $e) {
$this->db->rollBack();
return $e->getMessage(); return $e->getMessage();
} }
} }