|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of Ymir command-line tool. |
| 7 | + * |
| 8 | + * (c) Carl Alexander <support@ymirapp.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Ymir\Cli\Command\Database; |
| 15 | + |
| 16 | +use Carbon\Carbon; |
| 17 | +use Symfony\Component\Console\Exception\RuntimeException; |
| 18 | +use Symfony\Component\Console\Input\InputArgument; |
| 19 | +use Symfony\Component\Console\Input\InputInterface; |
| 20 | +use Symfony\Component\Filesystem\Filesystem; |
| 21 | +use Ymir\Cli\ApiClient; |
| 22 | +use Ymir\Cli\CliConfiguration; |
| 23 | +use Ymir\Cli\Console\OutputInterface; |
| 24 | +use Ymir\Cli\Process\Process; |
| 25 | +use Ymir\Cli\ProjectConfiguration\ProjectConfiguration; |
| 26 | + |
| 27 | +class ExportDatabaseCommand extends AbstractDatabaseCommand |
| 28 | +{ |
| 29 | + /** |
| 30 | + * The name of the command. |
| 31 | + * |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + public const NAME = 'database:export'; |
| 35 | + |
| 36 | + /** |
| 37 | + * The file system. |
| 38 | + * |
| 39 | + * @var Filesystem |
| 40 | + */ |
| 41 | + private $filesystem; |
| 42 | + |
| 43 | + /** |
| 44 | + * The project directory where the project files are copied from. |
| 45 | + * |
| 46 | + * @var string |
| 47 | + */ |
| 48 | + private $projectDirectory; |
| 49 | + |
| 50 | + /** |
| 51 | + * Constructor. |
| 52 | + */ |
| 53 | + public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, Filesystem $filesystem, ProjectConfiguration $projectConfiguration) |
| 54 | + { |
| 55 | + parent::__construct($apiClient, $cliConfiguration, $projectConfiguration); |
| 56 | + |
| 57 | + $this->filesystem = $filesystem; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * {@inheritdoc} |
| 62 | + */ |
| 63 | + protected function configure() |
| 64 | + { |
| 65 | + $this |
| 66 | + ->setName(self::NAME) |
| 67 | + ->setDescription('Export a database') |
| 68 | + ->addArgument('database', InputArgument::OPTIONAL, 'The ID or name of the database server to export a database from') |
| 69 | + ->addArgument('name', InputArgument::OPTIONAL, 'The name of the database to export') |
| 70 | + ->addArgument('user', InputArgument::OPTIONAL, 'The user used to connect to the database server') |
| 71 | + ->addArgument('password', InputArgument::OPTIONAL, 'The password of the user connecting to the database server'); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * {@inheritdoc} |
| 76 | + */ |
| 77 | + protected function perform(InputInterface $input, OutputInterface $output) |
| 78 | + { |
| 79 | + $databaseServer = $this->determineDatabaseServer('Which database server would you like to export a database from?', $input, $output); |
| 80 | + $name = $this->getStringArgument($input, 'name'); |
| 81 | + $user = $this->getStringArgument($input, 'user'); |
| 82 | + $password = $this->getStringArgument($input, 'password'); |
| 83 | + |
| 84 | + $databases = $this->apiClient->getDatabases($databaseServer['id']); |
| 85 | + |
| 86 | + if (empty($name)) { |
| 87 | + $name = $output->choice('Which database would you like to export?', $databases); |
| 88 | + } elseif (!$databases->has($name)) { |
| 89 | + throw new RuntimeException(sprintf('The "%s" database doesn\'t exist on the "%s" database server', $name, $databaseServer['name'])); |
| 90 | + } |
| 91 | + |
| 92 | + if (empty($user)) { |
| 93 | + $user = $output->ask('Which user do you want to use to connect to the database server?', 'ymir'); |
| 94 | + } |
| 95 | + |
| 96 | + if (empty($password)) { |
| 97 | + $password = $output->askHidden(sprintf('What\'s the "<comment>%s</comment>" password?', $user)); |
| 98 | + } |
| 99 | + |
| 100 | + $filename = sprintf('%s_%s.sql.gz', $name, Carbon::now()->toDateString()); |
| 101 | + |
| 102 | + if ($this->filesystem->exists($filename) && !$output->confirm(sprintf('The "<comment>%s</comment>" backup file already exists. Do you want to overwrite it?', $filename), false)) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + $output->infoWithDelayWarning('Exporting database'); |
| 107 | + |
| 108 | + Process::runShellCommandline(sprintf('mysqldump --quick --single-transaction --default-character-set=utf8mb4 --host=%s --user=%s --password=%s %s | gzip > %s', $databaseServer['endpoint'], $user, $password, $name, $filename)); |
| 109 | + |
| 110 | + $output->infoWithValue('Database exported successfully to', $filename); |
| 111 | + } |
| 112 | +} |
0 commit comments