|
5 | 5 | use Carbon\Carbon;
|
6 | 6 | use Illuminate\Filesystem\FilesystemAdapter as Disk;
|
7 | 7 | use Illuminate\Support\Facades\DB;
|
| 8 | +use Illuminate\Support\Facades\Storage; |
8 | 9 | use Illuminate\Support\LazyCollection;
|
9 | 10 | use Spatie\DbSnapshots\Events\DeletedSnapshot;
|
10 | 11 | use Spatie\DbSnapshots\Events\DeletingSnapshot;
|
11 | 12 | use Spatie\DbSnapshots\Events\LoadedSnapshot;
|
12 | 13 | use Spatie\DbSnapshots\Events\LoadingSnapshot;
|
| 14 | +use Spatie\TemporaryDirectory\TemporaryDirectory; |
13 | 15 |
|
14 | 16 | class Snapshot
|
15 | 17 | {
|
@@ -90,10 +92,22 @@ protected function shouldIgnoreLine(string $line): bool
|
90 | 92 |
|
91 | 93 | protected function loadStream(string $connectionName = null)
|
92 | 94 | {
|
| 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 | + |
93 | 105 | LazyCollection::make(function () {
|
| 106 | + Storage::disk(self::class)->writeStream($this->fileName, $this->disk->readStream($this->fileName)); |
| 107 | + |
94 | 108 | $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); |
97 | 111 |
|
98 | 112 | $statement = '';
|
99 | 113 | while (! feof($stream)) {
|
@@ -128,6 +142,8 @@ protected function loadStream(string $connectionName = null)
|
128 | 142 | }
|
129 | 143 | })->each(function (string $statement) use ($connectionName) {
|
130 | 144 | DB::connection($connectionName)->unprepared($statement);
|
| 145 | + })->after(function () use ($directory) { |
| 146 | + $directory->delete(); |
131 | 147 | });
|
132 | 148 | }
|
133 | 149 |
|
|
0 commit comments