Add delete and purge Command

This commit is contained in:
Tim
2022-09-28 13:18:17 +02:00
parent 7a4cc21f8e
commit af30297d74
5 changed files with 98 additions and 6 deletions

View File

@ -2,6 +2,7 @@
namespace App\Service;
use Exception;
use mysqli;
class DatabaseService
@ -26,16 +27,16 @@ class DatabaseService
return $dbs;
}
public function createDatabase(string $name)
public function createDatabase(string $name): void
{
$dbs = $this->listDatabases();
if (in_array($name, $dbs)) {
throw new \Exception(sprintf('Database "%s" already exists', $name));
throw new Exception(sprintf('Database "%s" already exists', $name));
}
$result = $this->conn->query(sprintf('CREATE DATABASE `%s`', $name));
if (!$result) {
throw new \Exception(sprintf('Database create error: %s', $this->conn->error));
throw new Exception(sprintf('Database create error: %s', $this->conn->error));
}
}
@ -54,4 +55,18 @@ class DatabaseService
return $users;
}
public function deleteDatabase(string $name): void
{
$result = $this->conn->query(sprintf('DROP DATABASE `%s`', $name));
if (!$result) {
throw new Exception(sprintf('Database delete error: %s', $this->conn->error));
}
}
public function purgeDatabase(string $name): void
{
$this->deleteDatabase($name);
$this->createDatabase($name);
}
}