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

@ -4,7 +4,7 @@
"minimum-stability": "stable", "minimum-stability": "stable",
"prefer-stable": true, "prefer-stable": true,
"require": { "require": {
"php": ">=8.0", "php": ">=8.1",
"ext-ctype": "*", "ext-ctype": "*",
"ext-iconv": "*", "ext-iconv": "*",
"ext-mysqli": "*", "ext-mysqli": "*",

View File

@ -17,8 +17,7 @@ class BackupDatabase extends Command
protected static $defaultName = 'db:backup'; protected static $defaultName = 'db:backup';
public function __construct( public function __construct(
private DatabaseService $db, private readonly BackupService $bs,
private BackupService $bs,
) )
{ {
parent::__construct(); parent::__construct();

View File

@ -0,0 +1,39 @@
<?php
namespace App\Console;
use App\Service\DatabaseService;
use App\Service\Traits\SelectDatabaseQuestion;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class DeleteDatabaseCommand extends Command
{
use SelectDatabaseQuestion;
protected static $defaultName = 'db:delete';
public function __construct(
private DatabaseService $db,
)
{
parent::__construct();
}
protected function configure(): void
{
$this->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;
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace App\Console;
use App\Service\DatabaseService;
use App\Service\Traits\SelectDatabaseQuestion;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class PurgeDatabaseCommand extends Command
{
use SelectDatabaseQuestion;
protected static $defaultName = 'db:purge';
public function __construct(
private DatabaseService $db,
)
{
parent::__construct();
}
protected function configure(): void
{
$this->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;
}
}

View File

@ -2,6 +2,7 @@
namespace App\Service; namespace App\Service;
use Exception;
use mysqli; use mysqli;
class DatabaseService class DatabaseService
@ -26,16 +27,16 @@ class DatabaseService
return $dbs; return $dbs;
} }
public function createDatabase(string $name) public function createDatabase(string $name): void
{ {
$dbs = $this->listDatabases(); $dbs = $this->listDatabases();
if (in_array($name, $dbs)) { 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)); $result = $this->conn->query(sprintf('CREATE DATABASE `%s`', $name));
if (!$result) { 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; 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);
}
} }