Add delete and purge Command
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user