From 2d107b1a137f2e1916ff884ee64c1daa0fd58c20 Mon Sep 17 00:00:00 2001 From: Mark Dorison Date: Thu, 19 Oct 2023 14:34:05 -0400 Subject: [PATCH] Updated dev:refresh to allow for BYODB. (#173) --- .../Commands/DevelopmentModeBaseCommands.php | 44 ++++++++++++++----- .../Commands/DevelopmentModeCommands.php | 5 ++- .../Plugin/Traits/DatabaseDownloadTrait.php | 9 ++++ 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/Robo/Plugin/Commands/DevelopmentModeBaseCommands.php b/src/Robo/Plugin/Commands/DevelopmentModeBaseCommands.php index 13e5229..3bc39e0 100644 --- a/src/Robo/Plugin/Commands/DevelopmentModeBaseCommands.php +++ b/src/Robo/Plugin/Commands/DevelopmentModeBaseCommands.php @@ -67,12 +67,20 @@ public function __construct() * * @param string $siteName * The Drupal site name. + * @option db + * Provide a path to a database dump to be used instead of downloading the latest dump. */ - public function databaseRefreshDdev(string $siteName = 'default'): Result + public function databaseRefreshDdev(string $siteName = 'default', array $options = ['db' => '']): Result { $this->io()->title('DDEV database refresh.'); - $dbPath = $this->databaseDownload($siteName); + ['db' => $dbPath] = $options; + // Track whether a database path was provided by the user or not. + $dbPathProvidedByUser = $dbPath !== ''; + + if (!$dbPathProvidedByUser) { + $dbPath = $this->databaseDownload($siteName); + } $this->io()->section("importing $siteName database."); $this->say("Importing $dbPath"); @@ -82,8 +90,11 @@ public function databaseRefreshDdev(string $siteName = 'default'): Result ->option('file', $dbPath) ->run(); - $this->say("Deleting $dbPath"); - $this->taskExec('rm')->args($dbPath)->run(); + // If a database was downloaded as part of this process, delete it. + if (!$dbPathProvidedByUser) { + $this->deleteDatabase($dbPath); + } + return $this->drushDeployWith( localEnvironmentType: LocalDevEnvironmentTypes::DDEV, siteDir: $siteName, @@ -95,12 +106,20 @@ public function databaseRefreshDdev(string $siteName = 'default'): Result * * @param string $siteName * The Drupal site name. + * @option db + * Provide a path to a database dump to be used instead of downloading the latest dump. */ - public function databaseRefreshLando(string $siteName = 'default'): Result + public function databaseRefreshLando(string $siteName = 'default', array $options = ['db' => '']): Result { $this->io()->title('lando database refresh.'); - $dbPath = $this->databaseDownload($siteName); + ['db' => $dbPath] = $options; + // Track whether a database path was provided by the user or not. + $dbPathProvidedByUser = $dbPath !== ''; + + if (!$dbPathProvidedByUser) { + $dbPath = $this->databaseDownload($siteName); + } $this->io()->section("importing $siteName database."); $this->say("Importing $dbPath"); @@ -112,8 +131,11 @@ public function databaseRefreshLando(string $siteName = 'default'): Result ->arg($hostOption) ->run(); - $this->say("Deleting $dbPath"); - $this->taskExec('rm')->args($dbPath)->run(); + // If a database was downloaded as part of this process, delete it. + if (!$dbPathProvidedByUser) { + $this->deleteDatabase($dbPath); + } + return $this->drushDeployWith( localEnvironmentType: LocalDevEnvironmentTypes::LANDO, siteDir: $siteName, @@ -242,6 +264,7 @@ protected function devRefreshDrupal( LocalDevEnvironmentTypes $environmentType, string $siteName = 'default', bool $startLocalEnv = false, + string $databasePath = '', ): Result { $this->io()->title('development environment refresh. 🦄✨'); $result = $this->taskComposerInstall()->run(); @@ -258,10 +281,11 @@ protected function devRefreshDrupal( $result = $this->taskExec("composer robo theme:build $siteName") ->run(); $result = $this->frontendDevEnableDrupal($siteName, ['yes' => true]); + if ($environmentType == LocalDevEnvironmentTypes::LANDO) { - $result = $this->databaseRefreshLando($siteName); + $result = $this->databaseRefreshLando(siteName: $siteName, options: ['db' => $databasePath]); } elseif ($environmentType == LocalDevEnvironmentTypes::DDEV) { - $result = $this->databaseRefreshDdev($siteName); + $result = $this->databaseRefreshDdev(siteName: $siteName, options: ['db' => $databasePath]); } return $this->drupalLoginLink($environmentType->value, $siteName); } diff --git a/src/Robo/Plugin/Commands/DevelopmentModeCommands.php b/src/Robo/Plugin/Commands/DevelopmentModeCommands.php index ea49dc6..e7acf6f 100644 --- a/src/Robo/Plugin/Commands/DevelopmentModeCommands.php +++ b/src/Robo/Plugin/Commands/DevelopmentModeCommands.php @@ -29,18 +29,21 @@ class DevelopmentModeCommands extends DevelopmentModeBaseCommands * The Drupal site name. * @option start-local-dev * Skip starting Lando. + * @option db + * Provide a database dump instead of relying on the latest available. * * @aliases magic */ public function devRefresh( string $environmentType, string $siteName = 'default', - array $options = ['start-local-dev' => false], + array $options = ['start-local-dev' => false, 'db' => ''], ): Result { return $this->devRefreshDrupal( environmentType: LocalDevEnvironmentTypes::from($environmentType), siteName: $siteName, startLocalEnv: $options['start-local-dev'], + databasePath: $options['db'], ); } diff --git a/src/Robo/Plugin/Traits/DatabaseDownloadTrait.php b/src/Robo/Plugin/Traits/DatabaseDownloadTrait.php index 471c642..945b610 100644 --- a/src/Robo/Plugin/Traits/DatabaseDownloadTrait.php +++ b/src/Robo/Plugin/Traits/DatabaseDownloadTrait.php @@ -190,4 +190,13 @@ public function sanitizeFileNameForWindows(string $fileName): string } return $fileName; } + + /** + * Delete the specified database. + */ + protected function deleteDatabase(string $dbPath): Result + { + $this->say("Deleting $dbPath"); + return $this->taskExec('rm')->args($dbPath)->run(); + } }