Skip to content

Commit

Permalink
allow the remote option to serve as fallback in conjunction with othe…
Browse files Browse the repository at this point in the history
…r options.
  • Loading branch information
gael-connan-cybex committed Sep 28, 2021
1 parent e452615 commit eab81a6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ To download and import the server database in one go, run
```bash
php artisan protector:import --remote
```
When used with other options, remote will serve as fallback behavior.

To import a database you downloaded earlier, run
```bash
php artisan protector:import --file=<your backup file>
```

To import the latest existing database file, run
```bash
php artisan protector:import --latest
```

To learn more about import options run
```bash
php artisan protector:import --help
Expand Down
45 changes: 31 additions & 14 deletions src/Commands/ImportDump.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ImportDump extends Command
{--allow-production : Enable importing SQL dumps on a production system. }
{--force : Forces the import of the given file or remote download. Requires the dump, file or remote option. }
{--i|ignore-connection-filter : Ignores filter of dumps to defined connections. }
{--r|remote : Pull a fresh dump from the remote server as configured in the .env file. }
{--r|remote : Pull a fresh dump from the remote server as configured in the .env file. Will be used as fallback when combined with other options. }
{--flush : Delete all existing dumps in the dump folder when using a remote dump. }
{--l|latest : Import the most recent dump available in the configured dumps directory. }';

Expand Down Expand Up @@ -74,6 +74,7 @@ public function handle()
$basePath = config('protector.baseDirectory');
$destinationFilename = $optionDump ?: $protector->createFilename();
$relativePath = $optionFile ?: $basePath . DIRECTORY_SEPARATOR . $destinationFilename;
$importFilePath = null;

$connectionName = null;

Expand All @@ -95,17 +96,40 @@ public function handle()
}

if (!($optionRemote || $optionFile || $optionDump || $optionLatest)) {
$downloadRemoteDump = 'Download remote dump';
if ($this->choice('Do you want to download and import a fresh dump from the server or an existing local dump?',
[
'1' => 'Download remote dump',
'1' => $downloadRemoteDump,
'2' => 'Import existing local dump',
],
'Download remote dump') == 'Download remote dump') {
$downloadRemoteDump) == $downloadRemoteDump) {
$optionRemote = true;
};
}

if ($optionRemote) {
if ($optionLatest) {
try {
$importFilePath = $protector->getLatestDumpName();
} catch (FileNotFoundException $fileNotFoundException) {
if (!$optionRemote) {
$this->error($fileNotFoundException->getMessage());
return;
} else {
$this->warn(sprintf('There are no files in %s', $disk->path($basePath)));
}
}
} elseif ($optionFile || $optionDump) {
if ($disk->exists($relativePath)) {
$importFilePath = $relativePath;
} elseif (!$optionRemote) {
$this->error((new FileNotFoundException($relativePath))->getMessage());
return;
} else {
$this->warn(sprintf('File not found: %s', $relativePath));
}
}

if (!$importFilePath && $optionRemote) {
if ($this->option('flush')) {
$disk->delete($disk->files($basePath));
$this->warn(sprintf('Deleted all files in %s', $disk->path($basePath)));
Expand All @@ -123,16 +147,9 @@ public function handle()
$this->line(sprintf('>>> Successfully retrieved remote dump from %s', app('protector')->getServerUrl()));

$importFilePath = $fullRemoteDumpFileName;
} elseif ($optionFile || $optionDump) {
$importFilePath = $relativePath;
} elseif ($optionLatest) {
try {
$importFilePath = $protector->getLatestDumpName();
} catch (FileNotFoundException $fileNotFoundException) {
$this->error($fileNotFoundException->getMessage());
return;
}
} else {
}

if (!$importFilePath) {
$directoryFiles = $disk->files($basePath);
$matchingFiles = collect();

Expand Down

0 comments on commit eab81a6

Please sign in to comment.