Skip to content

Commit

Permalink
Updated dev:refresh to allow for BYODB. (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
markdorison authored Oct 19, 2023
1 parent d745b40 commit 2d107b1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
44 changes: 34 additions & 10 deletions src/Robo/Plugin/Commands/DevelopmentModeBaseCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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,
Expand All @@ -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");
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Robo/Plugin/Commands/DevelopmentModeCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
);
}

Expand Down
9 changes: 9 additions & 0 deletions src/Robo/Plugin/Traits/DatabaseDownloadTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 2d107b1

Please sign in to comment.