Skip to content

Commit 2d107b1

Browse files
authored
Updated dev:refresh to allow for BYODB. (#173)
1 parent d745b40 commit 2d107b1

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

src/Robo/Plugin/Commands/DevelopmentModeBaseCommands.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,20 @@ public function __construct()
6767
*
6868
* @param string $siteName
6969
* The Drupal site name.
70+
* @option db
71+
* Provide a path to a database dump to be used instead of downloading the latest dump.
7072
*/
71-
public function databaseRefreshDdev(string $siteName = 'default'): Result
73+
public function databaseRefreshDdev(string $siteName = 'default', array $options = ['db' => '']): Result
7274
{
7375
$this->io()->title('DDEV database refresh.');
7476

75-
$dbPath = $this->databaseDownload($siteName);
77+
['db' => $dbPath] = $options;
78+
// Track whether a database path was provided by the user or not.
79+
$dbPathProvidedByUser = $dbPath !== '';
80+
81+
if (!$dbPathProvidedByUser) {
82+
$dbPath = $this->databaseDownload($siteName);
83+
}
7684

7785
$this->io()->section("importing $siteName database.");
7886
$this->say("Importing $dbPath");
@@ -82,8 +90,11 @@ public function databaseRefreshDdev(string $siteName = 'default'): Result
8290
->option('file', $dbPath)
8391
->run();
8492

85-
$this->say("Deleting $dbPath");
86-
$this->taskExec('rm')->args($dbPath)->run();
93+
// If a database was downloaded as part of this process, delete it.
94+
if (!$dbPathProvidedByUser) {
95+
$this->deleteDatabase($dbPath);
96+
}
97+
8798
return $this->drushDeployWith(
8899
localEnvironmentType: LocalDevEnvironmentTypes::DDEV,
89100
siteDir: $siteName,
@@ -95,12 +106,20 @@ public function databaseRefreshDdev(string $siteName = 'default'): Result
95106
*
96107
* @param string $siteName
97108
* The Drupal site name.
109+
* @option db
110+
* Provide a path to a database dump to be used instead of downloading the latest dump.
98111
*/
99-
public function databaseRefreshLando(string $siteName = 'default'): Result
112+
public function databaseRefreshLando(string $siteName = 'default', array $options = ['db' => '']): Result
100113
{
101114
$this->io()->title('lando database refresh.');
102115

103-
$dbPath = $this->databaseDownload($siteName);
116+
['db' => $dbPath] = $options;
117+
// Track whether a database path was provided by the user or not.
118+
$dbPathProvidedByUser = $dbPath !== '';
119+
120+
if (!$dbPathProvidedByUser) {
121+
$dbPath = $this->databaseDownload($siteName);
122+
}
104123

105124
$this->io()->section("importing $siteName database.");
106125
$this->say("Importing $dbPath");
@@ -112,8 +131,11 @@ public function databaseRefreshLando(string $siteName = 'default'): Result
112131
->arg($hostOption)
113132
->run();
114133

115-
$this->say("Deleting $dbPath");
116-
$this->taskExec('rm')->args($dbPath)->run();
134+
// If a database was downloaded as part of this process, delete it.
135+
if (!$dbPathProvidedByUser) {
136+
$this->deleteDatabase($dbPath);
137+
}
138+
117139
return $this->drushDeployWith(
118140
localEnvironmentType: LocalDevEnvironmentTypes::LANDO,
119141
siteDir: $siteName,
@@ -242,6 +264,7 @@ protected function devRefreshDrupal(
242264
LocalDevEnvironmentTypes $environmentType,
243265
string $siteName = 'default',
244266
bool $startLocalEnv = false,
267+
string $databasePath = '',
245268
): Result {
246269
$this->io()->title('development environment refresh. 🦄✨');
247270
$result = $this->taskComposerInstall()->run();
@@ -258,10 +281,11 @@ protected function devRefreshDrupal(
258281
$result = $this->taskExec("composer robo theme:build $siteName")
259282
->run();
260283
$result = $this->frontendDevEnableDrupal($siteName, ['yes' => true]);
284+
261285
if ($environmentType == LocalDevEnvironmentTypes::LANDO) {
262-
$result = $this->databaseRefreshLando($siteName);
286+
$result = $this->databaseRefreshLando(siteName: $siteName, options: ['db' => $databasePath]);
263287
} elseif ($environmentType == LocalDevEnvironmentTypes::DDEV) {
264-
$result = $this->databaseRefreshDdev($siteName);
288+
$result = $this->databaseRefreshDdev(siteName: $siteName, options: ['db' => $databasePath]);
265289
}
266290
return $this->drupalLoginLink($environmentType->value, $siteName);
267291
}

src/Robo/Plugin/Commands/DevelopmentModeCommands.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,21 @@ class DevelopmentModeCommands extends DevelopmentModeBaseCommands
2929
* The Drupal site name.
3030
* @option start-local-dev
3131
* Skip starting Lando.
32+
* @option db
33+
* Provide a database dump instead of relying on the latest available.
3234
*
3335
* @aliases magic
3436
*/
3537
public function devRefresh(
3638
string $environmentType,
3739
string $siteName = 'default',
38-
array $options = ['start-local-dev' => false],
40+
array $options = ['start-local-dev' => false, 'db' => ''],
3941
): Result {
4042
return $this->devRefreshDrupal(
4143
environmentType: LocalDevEnvironmentTypes::from($environmentType),
4244
siteName: $siteName,
4345
startLocalEnv: $options['start-local-dev'],
46+
databasePath: $options['db'],
4447
);
4548
}
4649

src/Robo/Plugin/Traits/DatabaseDownloadTrait.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,13 @@ public function sanitizeFileNameForWindows(string $fileName): string
190190
}
191191
return $fileName;
192192
}
193+
194+
/**
195+
* Delete the specified database.
196+
*/
197+
protected function deleteDatabase(string $dbPath): Result
198+
{
199+
$this->say("Deleting $dbPath");
200+
return $this->taskExec('rm')->args($dbPath)->run();
201+
}
193202
}

0 commit comments

Comments
 (0)