Skip to content

Commit a548ae1

Browse files
authored
Merge pull request #2 from wattnpapa/loadLocalTemp
Copy Files From External Storage To Local Storage for Import
2 parents b10aca9 + c574535 commit a548ae1

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/Snapshot.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
use Carbon\Carbon;
66
use Illuminate\Filesystem\FilesystemAdapter as Disk;
77
use Illuminate\Support\Facades\DB;
8+
use Illuminate\Support\Facades\Storage;
89
use Illuminate\Support\LazyCollection;
910
use Spatie\DbSnapshots\Events\DeletedSnapshot;
1011
use Spatie\DbSnapshots\Events\DeletingSnapshot;
1112
use Spatie\DbSnapshots\Events\LoadedSnapshot;
1213
use Spatie\DbSnapshots\Events\LoadingSnapshot;
14+
use Spatie\TemporaryDirectory\TemporaryDirectory;
1315

1416
class Snapshot
1517
{
@@ -90,10 +92,22 @@ protected function shouldIgnoreLine(string $line): bool
9092

9193
protected function loadStream(string $connectionName = null)
9294
{
95+
$directory = (new TemporaryDirectory(config('db-snapshots.temporary_directory_path')))->create();
96+
97+
config([
98+
'filesystems.disks.' . self::class => [
99+
'driver' => 'local',
100+
'root' => $directory->path(),
101+
'throw' => false,
102+
]
103+
]);
104+
93105
LazyCollection::make(function () {
106+
Storage::disk(self::class)->writeStream($this->fileName, $this->disk->readStream($this->fileName));
107+
94108
$stream = $this->compressionExtension === 'gz'
95-
? gzopen($this->disk->path($this->fileName), 'r')
96-
: $this->disk->readStream($this->fileName);
109+
? gzopen(Storage::disk(self::class)->path($this->fileName), 'r')
110+
: Storage::disk(self::class)->readStream($this->fileName);
97111

98112
$statement = '';
99113
while (! feof($stream)) {
@@ -128,6 +142,8 @@ protected function loadStream(string $connectionName = null)
128142
}
129143
})->each(function (string $statement) use ($connectionName) {
130144
DB::connection($connectionName)->unprepared($statement);
145+
})->after(function () use ($directory) {
146+
$directory->delete();
131147
});
132148
}
133149

0 commit comments

Comments
 (0)