-
-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathSushi.php
More file actions
291 lines (238 loc) · 9.05 KB
/
Sushi.php
File metadata and controls
291 lines (238 loc) · 9.05 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
namespace Sushi;
use Closure;
use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Str;
trait Sushi
{
protected static $sushiConnection;
public function getRows()
{
return $this->rows;
}
public function getSchema()
{
return $this->schema ?? [];
}
protected function sushiCacheReferencePath()
{
return (new \ReflectionClass(static::class))->getFileName();
}
protected function sushiShouldCache()
{
return property_exists(static::class, 'rows');
}
public static function resolveConnection($connection = null)
{
return static::$sushiConnection;
}
protected function sushiCachePath()
{
return implode(DIRECTORY_SEPARATOR, [
$this->sushiCacheDirectory(),
$this->sushiCacheFileName(),
]);
}
protected function sushiCacheFileName()
{
return config('sushi.cache-prefix', 'sushi').'-'.Str::kebab(str_replace('\\', '', static::class)).'.sqlite';
}
protected function sushiCacheDirectory()
{
return realpath(config('sushi.cache-path', storage_path('framework/cache')));
}
public static function bootSushi()
{
// Laravel 13 (laravel/framework#55685) throws a LogicException if a new
// model instance is created while the model is still booting (i.e.
// during a boot* trait method). Since Sushi needs to create an instance
// to configure its SQLite connection, we defer that work until after
// booting has finished using the "whenBooted" method, which was added
// in Laravel 12.8 (laravel/framework#55286). For older Laravel versions
// we call it directly since there is no re-entrancy guard and the
// original behaviour works fine.
if (method_exists(static::class, 'whenBooted')) {
static::whenBooted(function () {
static::configureSushiConnection();
});
} else {
static::configureSushiConnection();
}
}
protected static function configureSushiConnection()
{
$instance = new static;
$cachePath = $instance->sushiCachePath();
$dataPath = $instance->sushiCacheReferencePath();
$states = [
'cache-file-found-and-up-to-date' => function () use ($cachePath) {
static::setSqliteConnection($cachePath);
},
'cache-file-not-found-or-stale' => function () use ($cachePath, $dataPath, $instance) {
static::cacheFileNotFoundOrStale($cachePath, $dataPath, $instance);
},
'no-caching-capabilities' => function () use ($instance) {
static::setSqliteConnection(':memory:');
$instance->migrate();
},
];
switch (true) {
case ! $instance->sushiShouldCache():
$states['no-caching-capabilities']();
break;
case file_exists($cachePath) && filesize($cachePath) > 0 && filemtime($dataPath) <= filemtime($cachePath):
$states['cache-file-found-and-up-to-date']();
break;
case file_exists($instance->sushiCacheDirectory()) && is_writable($instance->sushiCacheDirectory()):
$states['cache-file-not-found-or-stale']();
break;
default:
$states['no-caching-capabilities']();
break;
}
}
protected static function cacheFileNotFoundOrStale($cachePath, $dataPath, $instance)
{
$tempPath = $cachePath.'.tmp.'.str_replace('.', '', uniqid('', true));
try {
file_put_contents($tempPath, '');
static::setSqliteConnection($tempPath);
$instance->migrate();
touch($tempPath, filemtime($dataPath));
rename($tempPath, $cachePath);
static::setSqliteConnection($cachePath);
} finally {
if (file_exists($tempPath)) {
@unlink($tempPath);
}
}
}
protected function newRelatedInstance($class)
{
return tap(new $class, function ($instance) {
if (!$instance->getConnectionName()) {
$instance->setConnection($this->getConnectionResolver()->getDefaultConnection());
}
});
}
protected static function setSqliteConnection($database)
{
$config = [
'driver' => 'sqlite',
'database' => $database,
];
static::$sushiConnection = app(ConnectionFactory::class)->make($config);
app('config')->set('database.connections.'.static::class, $config);
}
public function migrate()
{
$rows = $this->getRows();
$tableName = $this->getTable();
if (count($rows)) {
$this->createTable($tableName, $rows[0]);
} else {
$this->createTableWithNoData($tableName);
}
foreach (array_chunk($rows, $this->getSushiInsertChunkSize()) ?? [] as $inserts) {
if (! empty($inserts)) {
static::insert($inserts);
}
}
}
public function createTable(string $tableName, $firstRow)
{
$this->createTableSafely($tableName, function ($table) use ($firstRow) {
// Add the "id" column if it doesn't already exist in the rows.
if ($this->incrementing && ! array_key_exists($this->primaryKey, $firstRow)) {
$table->increments($this->primaryKey);
}
foreach ($firstRow as $column => $value) {
switch (true) {
case is_int($value):
$type = 'integer';
break;
case is_numeric($value):
$type = 'float';
break;
case is_string($value):
$type = 'string';
break;
case is_object($value) && $value instanceof \DateTime:
$type = 'dateTime';
break;
default:
$type = 'string';
}
if ($column === $this->primaryKey && $type == 'integer') {
$table->increments($this->primaryKey);
continue;
}
$schema = $this->getSchema();
$type = $schema[$column] ?? $type;
$table->{$type}($column)->nullable();
}
if ($this->usesTimestamps() && (! in_array('updated_at', array_keys($firstRow)) || ! in_array('created_at', array_keys($firstRow)))) {
$table->timestamps();
}
$this->afterMigrate($table);
});
}
protected function afterMigrate(BluePrint $table)
{
//
}
public function createTableWithNoData(string $tableName)
{
$this->createTableSafely($tableName, function ($table) {
$schema = $this->getSchema();
if ($this->incrementing && ! in_array($this->primaryKey, array_keys($schema))) {
$table->increments($this->primaryKey);
}
foreach ($schema as $name => $type) {
if ($name === $this->primaryKey && $type == 'integer') {
$table->increments($this->primaryKey);
continue;
}
$table->{$type}($name)->nullable();
}
if ($this->usesTimestamps() && (! in_array('updated_at', array_keys($schema)) || ! in_array('created_at', array_keys($schema)))) {
$table->timestamps();
}
});
}
protected function createTableSafely(string $tableName, Closure $callback)
{
/** @var \Illuminate\Database\Schema\SQLiteBuilder $schemaBuilder */
$schemaBuilder = static::resolveConnection()->getSchemaBuilder();
try {
$schemaBuilder->create($tableName, $callback);
} catch (QueryException $e) {
if (Str::contains($e->getMessage(), [
'already exists (SQL: create table',
sprintf('table "%s" already exists', $tableName),
])) {
// This error can happen in rare circumstances due to a race condition.
// Concurrent requests may both see the necessary preconditions for
// the table creation, but only one can actually succeed.
return;
}
throw $e;
}
}
public function usesTimestamps()
{
// Override the Laravel default value of $timestamps = true; Unless otherwise set.
return (new \ReflectionClass($this))->getProperty('timestamps')->class === static::class
? parent::usesTimestamps()
: false;
}
public function getSushiInsertChunkSize() {
return $this->sushiInsertChunkSize ?? 100;
}
public function getConnectionName()
{
return static::class;
}
}