@@ -79,6 +79,11 @@ protected function loadAsync(?string $connectionName = null): void
79
79
$ dbDumpContents = gzdecode ($ dbDumpContents );
80
80
}
81
81
82
+ if (empty (trim ($ dbDumpContents ))) {
83
+ // Ignoriere leeren Snapshot
84
+ return ;
85
+ }
86
+
82
87
DB ::connection ($ connectionName )->unprepared ($ dbDumpContents );
83
88
}
84
89
@@ -96,69 +101,105 @@ protected function shouldIgnoreLine(string $line): bool
96
101
97
102
protected function loadStream (?string $ connectionName = null ): void
98
103
{
99
- $ directory = (new TemporaryDirectory (config ('db-snapshots.temporary_directory_path ' )))->create ();
104
+ $ temporaryDirectory = (new TemporaryDirectory (config ('db-snapshots.temporary_directory_path ' )))->create ();
105
+
106
+ $ this ->configureFilesystemDisk ($ temporaryDirectory ->path ());
100
107
108
+ $ localDisk = $ this ->filesystemFactory ->disk (self ::class);
109
+
110
+ try {
111
+ $ this ->processStream ($ localDisk , $ connectionName );
112
+ } finally {
113
+ $ temporaryDirectory ->delete ();
114
+ }
115
+ }
116
+
117
+ private function configureFilesystemDisk (string $ path ): void
118
+ {
101
119
config ([
102
120
'filesystems.disks. ' . self ::class => [
103
121
'driver ' => 'local ' ,
104
- 'root ' => $ directory -> path () ,
122
+ 'root ' => $ path ,
105
123
'throw ' => false ,
106
- ]
124
+ ],
107
125
]);
126
+ }
108
127
109
- $ localDisk = $ this ->filesystemFactory ->disk (self ::class);
128
+ private function processStream ($ localDisk , ?string $ connectionName ): void
129
+ {
130
+ $ this ->copyStreamToLocalDisk ($ localDisk );
131
+
132
+ $ stream = $ this ->openStream ($ localDisk );
110
133
111
134
try {
112
- LazyCollection:: make ( function () use ( $ localDisk ) {
113
- $ localDisk -> writeStream ( $ this -> fileName , $ this -> disk -> readStream ( $ this -> fileName ));
114
-
115
- $ stream = $ this -> compressionExtension === ' gz '
116
- ? gzopen ( $ localDisk -> path ( $ this -> fileName ), ' r ' )
117
- : $ localDisk -> readStream ( $ this -> fileName );
118
-
119
- $ statement = '' ;
120
- while (! feof ( $ stream )) {
121
- $ chunk = $ this -> compressionExtension === ' gz '
122
- ? gzread ( $ stream , self :: STREAM_BUFFER_SIZE )
123
- : fread ( $ stream , self :: STREAM_BUFFER_SIZE );
124
-
125
- $ lines = explode ( "\n" , $ chunk );
126
- foreach ( $ lines as $ idx => $ line ) {
127
- if ( $ this -> shouldIgnoreLine ( $ line )) {
128
- continue ;
129
- }
130
-
131
- $ statement .= $ line ;
132
-
133
- // Carry-over the last line to the next chunk since it
134
- // is possible that this chunk finished mid-line right on
135
- // a semi-colon.
136
- if ( count ( $ lines ) == $ idx + 1 ) {
137
- break ;
138
- }
139
-
140
- if ( str_ends_with ( trim ( $ statement ), ' ; ' )) {
141
- yield $ statement ;
142
- $ statement = '' ;
143
- }
144
- }
135
+ $ this -> processStatements ( $ stream , $ connectionName );
136
+ } finally {
137
+ $ this -> closeStream ( $ stream );
138
+ }
139
+ }
140
+
141
+ private function copyStreamToLocalDisk ( $ localDisk ): void
142
+ {
143
+ $ localDisk -> writeStream ( $ this -> fileName , $ this -> disk -> readStream ( $ this -> fileName ));
144
+ }
145
+
146
+ private function openStream ( $ localDisk )
147
+ {
148
+ return $ this -> compressionExtension === ' gz '
149
+ ? gzopen ( $ localDisk -> path ( $ this -> fileName ), ' r ' )
150
+ : $ localDisk -> readStream ( $ this -> fileName );
151
+ }
152
+
153
+ private function closeStream ( $ stream ): void
154
+ {
155
+ $ this -> compressionExtension === ' gz ' ? gzclose ( $ stream ) : fclose ( $ stream );
156
+ }
157
+
158
+ private function processStatements ( $ stream , ? string $ connectionName ): void
159
+ {
160
+ $ statement = '' ;
161
+ while (! feof ( $ stream )) {
162
+ $ chunk = $ this -> readChunk ( $ stream );
163
+ $ lines = explode ( "\n" , $ chunk );
164
+
165
+ foreach ( $ lines as $ idx => $ line ) {
166
+ if ( $ this -> shouldIgnoreLine ( $ line )) {
167
+ continue ;
145
168
}
146
169
147
- if (str_ends_with (trim ($ statement ), '; ' )) {
148
- yield $ statement ;
170
+ $ statement .= $ line ;
171
+
172
+ if ($ this ->isLastLineOfChunk ($ lines , $ idx )) {
173
+ break ;
149
174
}
150
175
151
- if ($ this ->compressionExtension === 'gz ' ) {
152
- gzclose ($ stream );
153
- } else {
154
- fclose ($ stream );
176
+ if ($ this ->isCompleteStatement ($ statement )) {
177
+ DB ::connection ($ connectionName )->unprepared ($ statement );
178
+ $ statement = '' ;
155
179
}
156
- })->each (function (string $ statement ) use ($ connectionName ) {
157
- DB ::connection ($ connectionName )->unprepared ($ statement );
158
- });
159
- } finally {
160
- $ directory ->delete ();
180
+ }
161
181
}
182
+
183
+ if ($ this ->isCompleteStatement ($ statement )) {
184
+ DB ::connection ($ connectionName )->unprepared ($ statement );
185
+ }
186
+ }
187
+
188
+ private function readChunk ($ stream ): string
189
+ {
190
+ return $ this ->compressionExtension === 'gz '
191
+ ? gzread ($ stream , self ::STREAM_BUFFER_SIZE )
192
+ : fread ($ stream , self ::STREAM_BUFFER_SIZE );
193
+ }
194
+
195
+ private function isLastLineOfChunk (array $ lines , int $ idx ): bool
196
+ {
197
+ return count ($ lines ) === $ idx + 1 ;
198
+ }
199
+
200
+ private function isCompleteStatement (string $ statement ): bool
201
+ {
202
+ return str_ends_with (trim ($ statement ), '; ' );
162
203
}
163
204
164
205
public function delete (): void
0 commit comments