From d604fcf7cecbb8ef94be7d5d8444047ac065d826 Mon Sep 17 00:00:00 2001 From: Adam Malone Date: Mon, 17 Feb 2020 17:43:55 +1100 Subject: [PATCH] Updates DB backups command to pull latest db and tests constructor. --- src/Commands/DbBackupCommand.php | 170 +++++++++++++++++++++++++++++++ src/Commands/DbCommand.php | 143 ++------------------------ src/Commands/SshCommand.php | 12 ++- 3 files changed, 191 insertions(+), 134 deletions(-) create mode 100644 src/Commands/DbBackupCommand.php diff --git a/src/Commands/DbBackupCommand.php b/src/Commands/DbBackupCommand.php new file mode 100644 index 0000000..8fa1021 --- /dev/null +++ b/src/Commands/DbBackupCommand.php @@ -0,0 +1,170 @@ +databaseBackupsAdapter = new DatabaseBackups($this->cloudapi); + } + + /** + * Backs up all DBs in an environment. + * + * @param string $uuid + * @param EnvironmentResponse $environment + * + * @command database:backup + * @aliases db:backup + */ + public function dbBackup($uuid, $environment) + { + $this->backupAllEnvironmentDbs($uuid, $environment); + } + + /** + * Shows a list of database backups for all databases in an environment. + * + * @param string $uuid + * @param EnvironmentResponse $environment + * @param string $dbName + * + * @command database:backup:list + * @aliases db:backup:list + */ + public function dbBackupList($uuid, $environment, $dbName = null) + { + if (null !== $dbName) { + $this->cloudapi->addQuery('filter', "name=${dbName}"); + } + $dbAdapter = new Databases($this->cloudapi); + $databases = $dbAdapter->getAll($uuid); + $this->cloudapi->clearQuery(); + + $table = new Table($this->output()); + $table->setHeaders(['ID', 'Type', 'Timestamp']); + + foreach ($databases as $database) { + $backups = $this->databaseBackupsAdapter->getAll($environment->uuid, $database->name); + $table + ->addRows( + [ + [new TableCell($database->name, ['colspan' => 3])], + new TableSeparator() + ] + ); + + foreach ($backups as $backup) { + $table + ->addRows([ + [ + $backup->id, + ucfirst($backup->type), + $backup->completedAt, + ], + ]); + } + } + $table->render(); + } + + /** + * Restores a database from a saved backup. + * + * @param string $uuid + * @param EnvironmentResponse $environment + * @param string $dbName + * @param int $backupId + * + * @command database:backup:restore + * @aliases db:backup:restore + */ + public function dbBackupRestore($uuid, $environment, $dbName, $backupId) + { + if ($this->confirm( + sprintf('Are you sure you want to restore backup id %s to %s?', $backupId, $environment->label) + )) { + $response = $this->databaseBackupsAdapter->restore($environment->uuid, $dbName, $backupId); + $this->waitForNotification($response); + } + } + + /** + * Provides a database backup link. + * + * @param string $uuid + * @param EnvironmentResponse $environment + * @param string $dbName + * @param int $backupId + * + * @command database:backup:link + * @aliases db:backup:link + */ + public function dbBackupLink($uuid, $environment, $dbName, $backupId) + { + $environmentUuid = $environment->uuid; + $this->say(Connector::BASE_URI . + "/environments/${environmentUuid}/databases/${dbName}/backups/${backupId}/actions/download"); + } + + /** + * Downloads a database backup. + * + * @param string $uuid + * @param EnvironmentResponse $environment + * @param string $dbName + * + * @command database:backup:download + * @aliases db:backup:download + * @option $backup Select which backup to download by backup ID. If omitted, the latest will be downloaded. + * @option $path Select a path to download the backup to. + */ + public function dbBackupDownload($uuid, $environment, $dbName, $opts = ['backup' => null, 'path' => null]) + { + if (!$opts['backup']) { + $this->cloudapi->addQuery('sort', '-created'); + $this->cloudapi->addQuery('limit', 1); + $backup = $this->databaseBackupsAdapter->getAll($environment->uuid, $dbName); + $this->cloudapi->clearQuery(); + if (empty($backup)) { + throw new Exception('Unable to find a database backup to download.'); + } + $backupId = $backup[0]->id; + } else { + $backupId = $opts['backup']; + } + + $backup = $this->databaseBackupsAdapter->download($environment->uuid, $dbName, $backupId); + $backupName = sprintf('%s-%s-%s', $environment->name, $dbName, $backupId); + + if (null === $opts['path']) { + $location = tempnam(sys_get_temp_dir(), $backupName) . '.sql.gz'; + } else { + $location = sprintf("%s/%s.sql.gz", $opts['path'], $backupName); + } + $this->say(sprintf('Downloading database backup to %s', $location)); + if (file_put_contents($location, $backup, LOCK_EX)) { + $this->say(sprintf('Database backup downloaded to %s', $location)); + } else { + $this->say('Unable to download database backup.'); + } + } +} diff --git a/src/Commands/DbCommand.php b/src/Commands/DbCommand.php index d055d89..87ec63d 100644 --- a/src/Commands/DbCommand.php +++ b/src/Commands/DbCommand.php @@ -9,6 +9,8 @@ use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\TableSeparator; use Symfony\Component\Console\Helper\TableCell; +use Symfony\Component\Console\Output\BufferedOutput; + /** * Class DomainCommand @@ -17,18 +19,13 @@ class DbCommand extends AcquiaCommand { - /** - * Backs up all DBs in an environment. - * - * @param string $uuid - * @param EnvironmentResponse $environment - * - * @command database:backup - * @aliases db:backup - */ - public function dbBackup($uuid, $environment) + public $databaseAdapter; + + public function __construct() { - $this->backupAllEnvironmentDbs($uuid, $environment); + parent::__construct(); + + $this->databaseAdapter = new Databases($this->cloudapi); } /** @@ -41,11 +38,11 @@ public function dbBackup($uuid, $environment) */ public function dbList($uuid) { - $dbAdapter = new Databases($this->cloudapi); - $databases = $dbAdapter->getAll($uuid); + $databases = $this->databaseAdapter->getAll($uuid); $table = new Table($this->output()); $table->setHeaders(['Databases']); foreach ($databases as $database) { + echo $database->name; $table ->addRows([ [ @@ -56,123 +53,6 @@ public function dbList($uuid) $table->render(); } - /** - * Shows a list of database backups for all databases in an environment. - * - * @param string $uuid - * @param EnvironmentResponse $environment - * @param string $dbName - * - * @command database:backup:list - * @aliases db:backup:list - */ - public function dbBackupList($uuid, $environment, $dbName = null) - { - if (null !== $dbName) { - $this->cloudapi->addQuery('filter', "name=${dbName}"); - } - $dbAdapter = new Databases($this->cloudapi); - $databases = $dbAdapter->getAll($uuid); - $this->cloudapi->clearQuery(); - - $table = new Table($this->output()); - $table->setHeaders(['ID', 'Type', 'Timestamp']); - - foreach ($databases as $database) { - $dbBackupsAdapter = new DatabaseBackups($this->cloudapi); - $backups = $dbBackupsAdapter->getAll($environment->uuid, $database->name); - $table - ->addRows( - [ - [new TableCell($database->name, ['colspan' => 3])], - new TableSeparator() - ] - ); - - foreach ($backups as $backup) { - $table - ->addRows([ - [ - $backup->id, - ucfirst($backup->type), - $backup->completedAt, - ], - ]); - } - } - $table->render(); - } - - /** - * Restores a database from a saved backup. - * - * @param string $uuid - * @param EnvironmentResponse $environment - * @param string $dbName - * @param int $backupId - * - * @command database:backup:restore - * @aliases db:backup:restore - */ - public function dbBackupRestore($uuid, $environment, $dbName, $backupId) - { - if ($this->confirm( - sprintf('Are you sure you want to restore backup id %s to %s?', $backupId, $environment->label) - )) { - $dbAdapter = new DatabaseBackups($this->cloudapi); - $response = $dbAdapter->restore($environment->uuid, $dbName, $backupId); - $this->waitForNotification($response); - } - } - - /** - * Provides a database backup link. - * - * @param string $uuid - * @param EnvironmentResponse $environment - * @param string $dbName - * @param int $backupId - * - * @command database:backup:link - * @aliases db:backup:link - */ - public function dbBackupLink($uuid, $environment, $dbName, $backupId) - { - $environmentUuid = $environment->uuid; - $this->say(Connector::BASE_URI . - "/environments/${environmentUuid}/databases/${dbName}/backups/${backupId}/actions/download"); - } - - /** - * Downloads a database backup. - * - * @param string $uuid - * @param EnvironmentResponse $environment - * @param string $dbName - * @param int $backupId - * - * @command database:backup:download - * @aliases db:backup:download - */ - public function dbBackupDownload($uuid, $environment, $dbName, $backupId, $path = null) - { - - $dbAdapter = new DatabaseBackups($this->cloudapi); - $backupName = sprintf('%s-%s-%s', $environment->name, $dbName, $backupId); - $backup = $dbAdapter->download($environment->uuid, $dbName, $backupId); - - if (null === $path) { - $location = tempnam(sys_get_temp_dir(), $backupName) . '.sql.gz'; - } else { - $location = $path . $backupName . ".sql.gz"; - } - if (file_put_contents($location, $backup, LOCK_EX)) { - $this->say(sprintf('Database backup downloaded to %s', $location)); - } else { - $this->say('Unable to download database backup.'); - } - } - /** * Creates a database. * @@ -184,8 +64,7 @@ public function dbBackupDownload($uuid, $environment, $dbName, $backupId, $path */ public function dbCreate($uuid, $dbName) { - $dbAdapter = new Databases($this->cloudapi); - $response = $dbAdapter->create($uuid, $dbName); + $response = $this->databaseAdapter->create($uuid, $dbName); $this->waitForNotification($response); } diff --git a/src/Commands/SshCommand.php b/src/Commands/SshCommand.php index 5dd32c6..fb5ce03 100644 --- a/src/Commands/SshCommand.php +++ b/src/Commands/SshCommand.php @@ -12,6 +12,15 @@ class SshCommand extends AcquiaCommand { + public $environmentsAdapter; + + public function __construct() + { + parent::__construct(); + + $this->environmentsAdapter = new Environments($this->cloudapi); + } + /** * Shows SSH connection strings for specified environments. * @@ -27,8 +36,7 @@ public function sshInfo($uuid, $env = null) $this->cloudapi->addQuery('filter', "name=${env}"); } - $environmentAdapter = new Environments($this->cloudapi); - $environments = $environmentAdapter->getAll($uuid); + $environments = $this->environmentsAdapter->getAll($uuid); $this->cloudapi->clearQuery();