-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathSnapshot.php
159 lines (119 loc) · 4.22 KB
/
Snapshot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace Spatie\DbSnapshots;
use Carbon\Carbon;
use Illuminate\Filesystem\FilesystemAdapter as Disk;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\LazyCollection;
use Spatie\DbSnapshots\Events\DeletedSnapshot;
use Spatie\DbSnapshots\Events\DeletingSnapshot;
use Spatie\DbSnapshots\Events\LoadedSnapshot;
use Spatie\DbSnapshots\Events\LoadingSnapshot;
class Snapshot
{
public Disk $disk;
public string $fileName;
public string $name;
public ?string $compressionExtension = null;
private bool $useStream = false;
public const STREAM_BUFFER_SIZE = 16384;
public function __construct(Disk $disk, string $fileName)
{
$this->disk = $disk;
$this->fileName = $fileName;
$pathinfo = pathinfo($fileName);
if ($pathinfo['extension'] === 'gz') {
$this->compressionExtension = $pathinfo['extension'];
$fileName = $pathinfo['filename'];
}
$this->name = pathinfo($fileName, PATHINFO_FILENAME);
}
public function useStream()
{
$this->useStream = true;
return $this;
}
public function load(string $connectionName = null, bool $dropTables = true): void
{
event(new LoadingSnapshot($this));
if ($connectionName !== null) {
DB::setDefaultConnection($connectionName);
}
if ($dropTables) {
$this->dropAllCurrentTables();
}
$this->useStream ? $this->loadStream($connectionName) : $this->loadAsync($connectionName);
event(new LoadedSnapshot($this));
}
protected function loadAsync(string $connectionName = null)
{
$dbDumpContents = $this->disk->get($this->fileName);
if ($this->compressionExtension === 'gz') {
$dbDumpContents = gzdecode($dbDumpContents);
}
DB::connection($connectionName)->unprepared($dbDumpContents);
}
protected function isASqlComment(string $line): bool
{
return substr($line, 0, 2) === '--';
}
protected function shouldIgnoreLine(string $line): bool
{
$line = trim($line);
return empty($line) || $this->isASqlComment($line);
}
protected function loadStream(string $connectionName = null)
{
LazyCollection::make(function () {
$stream = $this->disk->readStream($this->fileName);
$statement = '';
while (! feof($stream)) {
$chunk = $this->compressionExtension === 'gz'
? gzdecode(gzread($stream, self::STREAM_BUFFER_SIZE))
: fread($stream, self::STREAM_BUFFER_SIZE);
$lines = explode("\n", $chunk);
foreach ($lines as $idx => $line) {
if ($this->shouldIgnoreLine($line)) {
continue;
}
$statement .= $line;
// Carry-over the last line to the next chunk since it
// is possible that this chunk finished mid-line right on
// a semi-colon.
if (count($lines) == $idx + 1) {
break;
}
if (substr(trim($statement), -1, 1) === ';') {
yield $statement;
$statement = '';
}
}
}
if (substr(trim($statement), -1, 1) === ';') {
yield $statement;
}
})->each(function (string $statement) use ($connectionName) {
DB::connection($connectionName)->unprepared($statement);
});
}
public function delete(): void
{
event(new DeletingSnapshot($this));
$this->disk->delete($this->fileName);
event(new DeletedSnapshot($this->fileName, $this->disk));
}
public function size(): int
{
return $this->disk->size($this->fileName);
}
public function createdAt(): Carbon
{
return Carbon::createFromTimestamp($this->disk->lastModified($this->fileName));
}
protected function dropAllCurrentTables()
{
DB::connection(DB::getDefaultConnection())
->getSchemaBuilder()
->dropAllTables();
DB::reconnect();
}
}