From af30297d7464ee04eb3f375d70d239957289160e Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 28 Sep 2022 13:18:17 +0200 Subject: [PATCH] Add delete and purge Command --- composer.json | 2 +- src/Console/BackupDatabase.php | 3 +-- src/Console/DeleteDatabaseCommand.php | 39 +++++++++++++++++++++++++++ src/Console/PurgeDatabaseCommand.php | 39 +++++++++++++++++++++++++++ src/Service/DatabaseService.php | 21 ++++++++++++--- 5 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 src/Console/DeleteDatabaseCommand.php create mode 100644 src/Console/PurgeDatabaseCommand.php diff --git a/composer.json b/composer.json index 2d7aa54..d46ef4b 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "minimum-stability": "stable", "prefer-stable": true, "require": { - "php": ">=8.0", + "php": ">=8.1", "ext-ctype": "*", "ext-iconv": "*", "ext-mysqli": "*", diff --git a/src/Console/BackupDatabase.php b/src/Console/BackupDatabase.php index 8d586aa..9496625 100644 --- a/src/Console/BackupDatabase.php +++ b/src/Console/BackupDatabase.php @@ -17,8 +17,7 @@ class BackupDatabase extends Command protected static $defaultName = 'db:backup'; public function __construct( - private DatabaseService $db, - private BackupService $bs, + private readonly BackupService $bs, ) { parent::__construct(); diff --git a/src/Console/DeleteDatabaseCommand.php b/src/Console/DeleteDatabaseCommand.php new file mode 100644 index 0000000..5d485ef --- /dev/null +++ b/src/Console/DeleteDatabaseCommand.php @@ -0,0 +1,39 @@ +addArgument('name', InputArgument::OPTIONAL, 'Database name'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $db = $this->getDatabaseName($input, $output); + + $this->db->deleteDatabase($db); + $output->writeln(sprintf('Database deleted: %s', $db)); + + return Command::SUCCESS; + } +} \ No newline at end of file diff --git a/src/Console/PurgeDatabaseCommand.php b/src/Console/PurgeDatabaseCommand.php new file mode 100644 index 0000000..196bc20 --- /dev/null +++ b/src/Console/PurgeDatabaseCommand.php @@ -0,0 +1,39 @@ +addArgument('name', InputArgument::OPTIONAL, 'Database name'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $db = $this->getDatabaseName($input, $output); + + $this->db->purgeDatabase($db); + $output->writeln(sprintf('Database purged: %s', $db)); + + return Command::SUCCESS; + } +} \ No newline at end of file diff --git a/src/Service/DatabaseService.php b/src/Service/DatabaseService.php index 58319ec..9a3ddda 100644 --- a/src/Service/DatabaseService.php +++ b/src/Service/DatabaseService.php @@ -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); + } } \ No newline at end of file