From 11089d82bc07493683420b0b49d6b42cf9e55127 Mon Sep 17 00:00:00 2001 From: tim Date: Thu, 16 Dec 2021 00:58:17 +0100 Subject: [PATCH] Implement mysqldump/backup service/command --- composer.json | 8 +- composer.lock | 73 +++++++++++++++++-- src/Console/BackupDatabase.php | 23 +++--- src/Console/CreateDatabaseCommand.php | 4 +- src/Console/ListDatabasesCommand.php | 4 +- src/Console/ListUsersCommand.php | 4 +- src/Service/BackupService.php | 30 ++++++++ ...atabaseManager.php => DatabaseService.php} | 2 +- symfony.lock | 3 + 9 files changed, 122 insertions(+), 29 deletions(-) create mode 100644 src/Service/BackupService.php rename src/Service/{DatabaseManager.php => DatabaseService.php} (98%) diff --git a/composer.json b/composer.json index e9d700c..d2cdbef 100644 --- a/composer.json +++ b/composer.json @@ -7,15 +7,15 @@ "php": ">=8.0", "ext-ctype": "*", "ext-iconv": "*", + "ext-mysqli": "*", "symfony/console": "5.3.*", "symfony/dotenv": "5.3.*", + "symfony/filesystem": "5.3.*", "symfony/flex": "^1.3.1", "symfony/framework-bundle": "5.3.*", + "symfony/process": "5.3.*", "symfony/runtime": "5.3.*", - "symfony/yaml": "5.3.*", - "ext-mysqli": "*" - }, - "require-dev": { + "symfony/yaml": "5.3.*" }, "config": { "optimize-autoloader": true, diff --git a/composer.lock b/composer.lock index d697b0b..7fb7d0c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e77b7d594056580678fe7de815ac7270", + "content-hash": "8c7050f7bee5a93ee3509d8014488736", "packages": [ { "name": "psr/cache", @@ -2107,6 +2107,68 @@ ], "time": "2021-05-21T13:25:03+00:00" }, + { + "name": "symfony/process", + "version": "v5.3.12", + "source": { + "type": "git", + "url": "https://github.com/symfony/process.git", + "reference": "e498803a6e95ede78e9d5646ad32a2255c033a6a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/process/zipball/e498803a6e95ede78e9d5646ad32a2255c033a6a", + "reference": "e498803a6e95ede78e9d5646ad32a2255c033a6a", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "symfony/polyfill-php80": "^1.16" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Process\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Executes commands in sub-processes", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/process/tree/v5.3.12" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2021-11-22T22:39:13+00:00" + }, { "name": "symfony/routing", "version": "v5.3.7", @@ -2680,12 +2742,11 @@ "prefer-stable": true, "prefer-lowest": false, "platform": { - "php": ">=7.2.5", + "php": ">=8.0", "ext-ctype": "*", - "ext-iconv": "*" - }, - "platform-dev": { - "php": "*" + "ext-iconv": "*", + "ext-mysqli": "*" }, + "platform-dev": [], "plugin-api-version": "2.1.0" } diff --git a/src/Console/BackupDatabase.php b/src/Console/BackupDatabase.php index ef52b24..1ac6e4e 100644 --- a/src/Console/BackupDatabase.php +++ b/src/Console/BackupDatabase.php @@ -2,19 +2,23 @@ namespace App\Console; -use App\Service\DatabaseManager; +use App\Service\BackupService; +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; -use Symfony\Component\Console\Question\Question; class BackupDatabase extends Command { - protected static $defaultName = 'app:db:create'; + use SelectDatabaseQuestion; + + protected static $defaultName = 'app:db:backup'; public function __construct( - private DatabaseManager $db, + private DatabaseService $db, + private BackupService $bs, ) { parent::__construct(); @@ -27,15 +31,10 @@ class BackupDatabase extends Command protected function execute(InputInterface $input, OutputInterface $output): int { - $question = $this->getHelper('question'); + $db = $this->getDatabaseName($input, $output); - if (!$name = $input->getArgument('name')) { - $name = $question->ask($input, $output, new Question('Database name: ')); - } - - $this->db->createDatabase($name); - - $output->writeln(sprintf('Database "%s" successfully created', $name)); + $output = $this->bs->backup($db); + dump($output); return Command::SUCCESS; } diff --git a/src/Console/CreateDatabaseCommand.php b/src/Console/CreateDatabaseCommand.php index 090993a..cd1b060 100644 --- a/src/Console/CreateDatabaseCommand.php +++ b/src/Console/CreateDatabaseCommand.php @@ -2,7 +2,7 @@ namespace App\Console; -use App\Service\DatabaseManager; +use App\Service\DatabaseService; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -14,7 +14,7 @@ class CreateDatabaseCommand extends Command protected static $defaultName = 'app:db:create'; public function __construct( - private DatabaseManager $db, + private DatabaseService $db, ) { parent::__construct(); diff --git a/src/Console/ListDatabasesCommand.php b/src/Console/ListDatabasesCommand.php index 558f2ab..723cf91 100644 --- a/src/Console/ListDatabasesCommand.php +++ b/src/Console/ListDatabasesCommand.php @@ -2,7 +2,7 @@ namespace App\Console; -use App\Service\DatabaseManager; +use App\Service\DatabaseService; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -12,7 +12,7 @@ class ListDatabasesCommand extends Command protected static $defaultName = 'app:db:list'; public function __construct( - private DatabaseManager $db, + private DatabaseService $db, ) { parent::__construct(); diff --git a/src/Console/ListUsersCommand.php b/src/Console/ListUsersCommand.php index d34aab2..39996da 100644 --- a/src/Console/ListUsersCommand.php +++ b/src/Console/ListUsersCommand.php @@ -2,7 +2,7 @@ namespace App\Console; -use App\Service\DatabaseManager; +use App\Service\DatabaseService; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -12,7 +12,7 @@ class ListUsersCommand extends Command protected static $defaultName = 'app:users:list'; public function __construct( - private DatabaseManager $db, + private DatabaseService $db, ) { parent::__construct(); diff --git a/src/Service/BackupService.php b/src/Service/BackupService.php new file mode 100644 index 0000000..4c01922 --- /dev/null +++ b/src/Service/BackupService.php @@ -0,0 +1,30 @@ +credentials->getUser(), + ]); + $process->setEnv(['MYSQL_PWD' => $this->credentials->getPassword()]); + $process->run(); + + $fileName = sprintf('%s_backup.sql', $db); + $filesystem->dumpFile($fileName, $process->getOutput()); + + return $fileName; + } +} \ No newline at end of file diff --git a/src/Service/DatabaseManager.php b/src/Service/DatabaseService.php similarity index 98% rename from src/Service/DatabaseManager.php rename to src/Service/DatabaseService.php index e15c5a1..58319ec 100644 --- a/src/Service/DatabaseManager.php +++ b/src/Service/DatabaseService.php @@ -4,7 +4,7 @@ namespace App\Service; use mysqli; -class DatabaseManager +class DatabaseService { private mysqli $conn; diff --git a/symfony.lock b/symfony.lock index 288c8a9..cbb2fa8 100644 --- a/symfony.lock +++ b/symfony.lock @@ -114,6 +114,9 @@ "symfony/polyfill-php81": { "version": "v1.23.0" }, + "symfony/process": { + "version": "v5.3.12" + }, "symfony/routing": { "version": "5.3", "recipe": {