Skip to content

Commit e022b82

Browse files
committed
Add Support for Binary Import
1 parent 4ea3e91 commit e022b82

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

src/Commands/RestoreCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ private function confirmRestoreProcess(PendingRestore $pendingRestore): bool
225225
protected function getBackupOptions(Collection $listOfBackups, int $labelLength): Collection
226226
{
227227
return $listOfBackups->mapWithKeys(fn ($backup): array => [
228-
$backup['path'] => str_pad($backup['path'] . ' ', ($labelLength - strlen($backup['size'])), '.', STR_PAD_RIGHT) . ' ' . $backup['size'],
228+
$backup['path'] => str_pad($backup['path'].' ', ($labelLength - strlen($backup['size'])), '.', STR_PAD_RIGHT).' '.$backup['size'],
229229
]);
230230
}
231231
}

src/Databases/PostgreSql.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,35 @@ public function getImportCommand(string $dumpFile, string $connection): string
2424
$dumper = DbDumperFactory::createFromConnection($connection);
2525
$dumper->getContentsOfCredentialsFile();
2626

27+
$username = config("database.connections.{$connection}.username");
28+
$password = config("database.connections.{$connection}.password");
29+
$host = config("database.connections.{$connection}.host");
30+
$port = config("database.connections.{$connection}.port");
31+
$database = config("database.connections.{$connection}.database");
2732
if (str($dumpFile)->endsWith('sql')) {
2833
return collect([
2934
$this->dumpBinaryPath.'psql',
3035
'postgresql://'.
31-
urldecode(config("database.connections.{$connection}.username")).':'.
32-
urlencode(config("database.connections.{$connection}.password")).'@'.
33-
config("database.connections.{$connection}.host").':'.
34-
config("database.connections.{$connection}.port").'/'.
35-
config("database.connections.{$connection}.database"),
36+
urldecode($username).':'.
37+
urlencode($password).'@'.
38+
$host.':'.
39+
$port.'/'.
40+
$database,
3641
'< '.$dumpFile,
3742
])->implode(' ');
3843
}
3944

45+
if ($this->isBinaryDump($dumpFile)) {
46+
return sprintf(
47+
'pg_restore --verbose --no-owner --host=%s --port=%s --username=%s --dbname=%s %s',
48+
escapeshellarg($host),
49+
escapeshellarg($port),
50+
escapeshellarg($username),
51+
escapeshellarg($database),
52+
escapeshellarg($dumpFile)
53+
);
54+
}
55+
4056
// @todo: Improve detection of compressed files
4157
$decompressCommand = match (File::extension($dumpFile)) {
4258
'gz' => "gunzip -c {$dumpFile}",
@@ -49,16 +65,23 @@ public function getImportCommand(string $dumpFile, string $connection): string
4965
'|',
5066
$this->dumpBinaryPath.'psql',
5167
'postgresql://'.
52-
urldecode(config("database.connections.{$connection}.username")).':'.
53-
urldecode(config("database.connections.{$connection}.password")).'@'.
54-
config("database.connections.{$connection}.host").':'.
55-
config("database.connections.{$connection}.port").'/'.
56-
config("database.connections.{$connection}.database"),
68+
urldecode($username).':'.
69+
urldecode($password).'@'.
70+
$host.':'.
71+
$port.'/'.
72+
$database,
5773
])->implode(' ');
5874
}
5975

6076
public function getCliName(): string
6177
{
6278
return 'psql';
6379
}
80+
81+
public function isBinaryDump(string $dumpFile): bool
82+
{
83+
return str($dumpFile)->endsWith([
84+
'.backup',
85+
]);
86+
}
6487
}

tests/Commands/RestoreCommandTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
use Illuminate\Support\Facades\DB;
66
use Illuminate\Support\Facades\Event;
7+
use Illuminate\Support\Facades\Storage;
78
use Wnx\LaravelBackupRestore\Commands\RestoreCommand;
89
use Wnx\LaravelBackupRestore\Events\DatabaseReset;
910
use Wnx\LaravelBackupRestore\Events\LocalBackupRemoved;
1011
use Wnx\LaravelBackupRestore\Exceptions\NoBackupsFound;
1112

13+
use function Pest\Laravel\artisan;
14+
1215
// MySQL
1316
it('restores mysql database', function (string $backup, ?string $password = null) {
1417
$this->artisan(RestoreCommand::class, [
@@ -192,13 +195,17 @@
192195
->assertSuccessful();
193196

194197
Event::assertNotDispatched(LocalBackupRemoved::class);
195-
$files = \Illuminate\Support\Facades\Storage::disk('local')->allFiles('backup-restore-temp');
198+
$files = Storage::disk('local')->allFiles('backup-restore-temp');
196199

197200
expect($files)->not->toBeEmpty();
198201

199202
})->group('sqlite');
200203

201204
it('restores pgsql database with binary dump', function (string $backup, ?string $password = null) {
205+
artisan('db:wipe', [
206+
'--database' => 'pgsql-restore',
207+
]);
208+
202209
config([
203210
'backup.backup.database_dump_file_extension' => 'backup',
204211
]);
@@ -216,6 +223,10 @@
216223
$result = DB::connection('pgsql')->table('users')->count();
217224

218225
expect($result)->toBe(1);
226+
227+
artisan('db:wipe', [
228+
'--database' => 'pgsql-restore',
229+
]);
219230
})->with([
220231
[
221232
'backup' => 'Laravel/2025-12-26-pgsql-no-compression-custom-extension-binary-dump.zip',

0 commit comments

Comments
 (0)