Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show Connection Details in Confirmation Prompt #20

Merged
merged 2 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Commands/RestoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Wnx\LaravelBackupRestore\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Wnx\LaravelBackupRestore\Actions\CleanupLocalBackupAction;
Expand Down Expand Up @@ -55,7 +56,7 @@ public function handle(
backupPassword: $this->getPassword(),
);

if (! $this->confirm("Proceed to restore \"{$pendingRestore->backup}\" using the \"{$pendingRestore->connection}\" database connection.", true)) {
if (! $this->confirmRestoreProcess($pendingRestore)) {
$this->warn('Abort.');

return self::INVALID;
Expand Down Expand Up @@ -165,4 +166,24 @@ private function runHealthChecks(PendingRestore $pendingRestore): int

return self::SUCCESS;
}

private function confirmRestoreProcess(PendingRestore $pendingRestore): bool
{
$connectionConfig = config("database.connections.{$pendingRestore->connection}");
$connectionInformationForConfirmation = collect([
'Host' => Arr::get($connectionConfig, 'host'),
'Database' => Arr::get($connectionConfig, 'database'),
'username' => Arr::get($connectionConfig, 'username'),
])->filter()->map(fn ($value, $key) => "{$key}: {$value}")->implode(', ');

return $this->confirm(
sprintf(
'Proceed to restore "%s" using the "%s" database connection. (%s)',
$pendingRestore->backup,
$pendingRestore->connection,
$connectionInformationForConfirmation
),
true
);
}
}
20 changes: 10 additions & 10 deletions tests/Commands/RestoreCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
use Wnx\LaravelBackupRestore\Exceptions\NoBackupsFound;

// MySQL
it('restores mysql database', function (string $backup, ?string $password = null) {
it('restores mysql database', function (string $backup, string $password = null) {
$this->artisan(RestoreCommand::class, [
'--disk' => 'remote',
'--backup' => $backup,
'--connection' => 'mysql',
'--password' => $password,
'--no-interaction' => true,
])
->expectsQuestion("Proceed to restore \"{$backup}\" using the \"mysql\" database connection.", true)
->expectsQuestion("Proceed to restore \"{$backup}\" using the \"mysql\" database connection. (Host: 127.0.0.1, Database: laravel_backup_restore, username: root)", true)
->expectsOutput('All health checks passed.')
->assertSuccessful();

Expand All @@ -42,15 +42,15 @@
])->group('mysql');

// sqlite
it('restores sqlite database', function (string $backup, ?string $password = null) {
it('restores sqlite database', function (string $backup, string $password = null) {
$this->artisan(RestoreCommand::class, [
'--disk' => 'remote',
'--backup' => $backup,
'--connection' => 'sqlite',
'--password' => $password,
'--no-interaction' => true,
])
->expectsQuestion("Proceed to restore \"{$backup}\" using the \"sqlite\" database connection.", true)
->expectsQuestion("Proceed to restore \"{$backup}\" using the \"sqlite\" database connection. (Database: database/database.sqlite)", true)
->assertSuccessful();

$result = DB::connection('sqlite')->table('users')->count();
Expand All @@ -74,15 +74,15 @@
])->group('sqlite');

// pgsql
it('restores pgsql database', function (string $backup, ?string $password = null) {
it('restores pgsql database', function (string $backup, string $password = null) {
$this->artisan(RestoreCommand::class, [
'--disk' => 'remote',
'--backup' => $backup,
'--connection' => 'pgsql',
'--password' => $password,
'--no-interaction' => true,
])
->expectsQuestion("Proceed to restore \"{$backup}\" using the \"pgsql\" database connection.", true)
->expectsQuestion("Proceed to restore \"{$backup}\" using the \"pgsql\" database connection. (Host: 127.0.0.1, Database: laravel_backup_restore, username: root)", true)
->assertSuccessful();

$result = DB::connection('pgsql')->table('users')->count();
Expand Down Expand Up @@ -121,7 +121,7 @@
])
->expectsConfirmation('Use encryption password from config?', false)
->expectsQuestion('What is the password to decrypt the backup? (leave empty if not encrypted)', 'password')
->expectsQuestion('Proceed to restore "Laravel/2023-01-28-mysql-no-compression-encrypted.zip" using the "mysql" database connection.', true)
->expectsQuestion('Proceed to restore "Laravel/2023-01-28-mysql-no-compression-encrypted.zip" using the "mysql" database connection. (Host: 127.0.0.1, Database: laravel_backup_restore, username: root)', true)
->assertSuccessful();

$result = DB::connection('mysql')->table('users')->count();
Expand All @@ -140,7 +140,7 @@
'--no-interaction' => true,
'--reset' => true,
])
->expectsQuestion('Proceed to restore "Laravel/2023-01-28-mysql-no-compression-no-encryption.zip" using the "mysql" database connection.', true)
->expectsQuestion('Proceed to restore "Laravel/2023-01-28-mysql-no-compression-no-encryption.zip" using the "mysql" database connection. (Host: 127.0.0.1, Database: laravel_backup_restore, username: root)', true)
->assertSuccessful();

Event::assertDispatched(DatabaseReset::class);
Expand All @@ -154,7 +154,7 @@
'--password' => null,
'--no-interaction' => true,
])
->expectsQuestion('Proceed to restore "Laravel/2023-01-28-mysql-no-compression-no-encryption-multiple-dumps.zip" using the "mysql" database connection.', true)
->expectsQuestion('Proceed to restore "Laravel/2023-01-28-mysql-no-compression-no-encryption-multiple-dumps.zip" using the "mysql" database connection. (Host: 127.0.0.1, Database: laravel_backup_restore, username: root)', true)
->assertSuccessful();

$result = DB::connection('mysql')->table('users')->count();
Expand All @@ -171,7 +171,7 @@
'--no-interaction' => true,
'--reset' => true,
])
->expectsQuestion('Proceed to restore "Laravel/2023-01-28-mysql-no-compression-no-encryption-empty-dump.zip" using the "mysql" database connection.', true)
->expectsQuestion('Proceed to restore "Laravel/2023-01-28-mysql-no-compression-no-encryption-empty-dump.zip" using the "mysql" database connection. (Host: 127.0.0.1, Database: laravel_backup_restore, username: root)', true)
->expectsOutput('Database has not tables after restore.')
->assertFailed();
});
Loading