Add delete and purge Command
This commit is contained in:
parent
7a4cc21f8e
commit
af30297d74
@ -4,7 +4,7 @@
|
||||
"minimum-stability": "stable",
|
||||
"prefer-stable": true,
|
||||
"require": {
|
||||
"php": ">=8.0",
|
||||
"php": ">=8.1",
|
||||
"ext-ctype": "*",
|
||||
"ext-iconv": "*",
|
||||
"ext-mysqli": "*",
|
||||
|
@ -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();
|
||||
|
39
src/Console/DeleteDatabaseCommand.php
Normal file
39
src/Console/DeleteDatabaseCommand.php
Normal 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;
|
||||
}
|
||||
}
|
39
src/Console/PurgeDatabaseCommand.php
Normal file
39
src/Console/PurgeDatabaseCommand.php
Normal 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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user