-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathBuilder.php
406 lines (348 loc) · 12.5 KB
/
Builder.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
declare(strict_types=1);
namespace MongoDB\Laravel\Schema;
use Closure;
use MongoDB\Collection;
use MongoDB\Driver\Exception\ServerException;
use MongoDB\Laravel\Connection;
use MongoDB\Model\CollectionInfo;
use MongoDB\Model\IndexInfo;
use function array_column;
use function array_fill_keys;
use function array_filter;
use function array_keys;
use function array_map;
use function array_merge;
use function array_values;
use function assert;
use function count;
use function current;
use function implode;
use function in_array;
use function is_array;
use function is_string;
use function iterator_to_array;
use function sort;
use function sprintf;
use function str_ends_with;
use function substr;
use function usort;
/** @property Connection $connection */
class Builder extends \Illuminate\Database\Schema\Builder
{
/**
* Check if column exists in the collection schema.
*
* @param string $table
* @param string $column
*/
public function hasColumn($table, $column): bool
{
return $this->hasColumns($table, [$column]);
}
/**
* Check if columns exists in the collection schema.
*
* @param string $table
* @param string[] $columns
*/
public function hasColumns($table, array $columns): bool
{
// The field "id" (alias of "_id") always exists in MongoDB documents
$columns = array_filter($columns, fn (string $column): bool => ! in_array($column, ['_id', 'id'], true));
// Any subfield named "*.id" is an alias of "*._id"
$columns = array_map(fn (string $column): string => str_ends_with($column, '.id') ? substr($column, 0, -3) . '._id' : $column, $columns);
if ($columns === []) {
return true;
}
$collection = $this->connection->table($table);
return $collection
->where(array_fill_keys($columns, ['$exists' => true]))
->project(['_id' => 1])
->exists();
}
/**
* Determine if the given collection exists.
*
* @param string $name
*
* @return bool
*/
public function hasCollection($name)
{
$db = $this->connection->getDatabase();
$collections = iterator_to_array($db->listCollections([
'filter' => ['name' => $name],
]), false);
return count($collections) !== 0;
}
/** @inheritdoc */
public function hasTable($table)
{
return $this->hasCollection($table);
}
/** @inheritdoc */
public function table($table, Closure $callback)
{
$blueprint = $this->createBlueprint($table);
if ($callback) {
$callback($blueprint);
}
}
/** @inheritdoc */
public function create($table, ?Closure $callback = null, array $options = [])
{
$blueprint = $this->createBlueprint($table);
$blueprint->create($options);
if ($callback) {
$callback($blueprint);
}
}
/** @inheritdoc */
public function dropIfExists($table)
{
if ($this->hasCollection($table)) {
$this->drop($table);
}
}
/** @inheritdoc */
public function drop($table)
{
$blueprint = $this->createBlueprint($table);
$blueprint->drop();
}
/**
* @inheritdoc
*
* Drops the entire database instead of deleting each collection individually.
*
* In MongoDB, dropping the whole database is much faster than dropping collections
* one by one. The database will be automatically recreated when a new connection
* writes to it.
*/
public function dropAllTables()
{
$this->connection->getDatabase()->drop();
}
/** @param string|null $schema Database name */
public function getTables($schema = null)
{
$db = $this->connection->getDatabase($schema);
$collections = [];
foreach ($db->listCollections() as $collectionInfo) {
$collectionName = $collectionInfo->getName();
// Skip views, which don't support aggregate
if ($collectionInfo->getType() === 'view') {
continue;
}
$stats = $db->selectCollection($collectionName)->aggregate([
['$collStats' => ['storageStats' => ['scale' => 1]]],
['$project' => ['storageStats.totalSize' => 1]],
])->toArray();
$collections[] = [
'name' => $collectionName,
'schema' => $db->getDatabaseName(),
'schema_qualified_name' => $db->getDatabaseName() . '.' . $collectionName,
'size' => $stats[0]?->storageStats?->totalSize ?? null,
'comment' => null,
'collation' => null,
'engine' => null,
];
}
usort($collections, fn ($a, $b) => $a['name'] <=> $b['name']);
return $collections;
}
/** @param string|null $schema Database name */
public function getViews($schema = null)
{
$db = $this->connection->getDatabase($schema);
$collections = [];
foreach ($db->listCollections() as $collectionInfo) {
$collectionName = $collectionInfo->getName();
// Skip normal type collection
if ($collectionInfo->getType() !== 'view') {
continue;
}
$collections[] = [
'name' => $collectionName,
'schema' => $db->getDatabaseName(),
'schema_qualified_name' => $db->getDatabaseName() . '.' . $collectionName,
'size' => null,
'comment' => null,
'collation' => null,
'engine' => null,
];
}
usort($collections, fn ($a, $b) => $a['name'] <=> $b['name']);
return $collections;
}
/**
* @param string|null $schema
* @param bool $schemaQualified If a schema is provided, prefix the collection names with the schema name
*
* @return array
*/
public function getTableListing($schema = null, $schemaQualified = false)
{
$collections = [];
if ($schema === null || is_string($schema)) {
$collections[$schema ?? 0] = iterator_to_array($this->connection->getDatabase($schema)->listCollectionNames());
} elseif (is_array($schema)) {
foreach ($schema as $db) {
$collections[$db] = iterator_to_array($this->connection->getDatabase($db)->listCollectionNames());
}
}
if ($schema && $schemaQualified) {
$collections = array_map(fn ($db, $collections) => array_map(static fn ($collection) => $db . '.' . $collection, $collections), array_keys($collections), $collections);
}
$collections = array_merge(...array_values($collections));
sort($collections);
return $collections;
}
public function getColumns($table)
{
$stats = $this->connection->getDatabase()->selectCollection($table)->aggregate([
// Sample 1,000 documents to get a representative sample of the collection
['$sample' => ['size' => 1_000]],
// Convert each document to an array of fields
['$project' => ['fields' => ['$objectToArray' => '$$ROOT']]],
// Unwind to get one document per field
['$unwind' => '$fields'],
// Group by field name, count the number of occurrences and get the types
[
'$group' => [
'_id' => '$fields.k',
'total' => ['$sum' => 1],
'types' => ['$addToSet' => ['$type' => '$fields.v']],
],
],
// Get the most seen field names
['$sort' => ['total' => -1]],
// Limit to 1,000 fields
['$limit' => 1_000],
// Sort by field name
['$sort' => ['_id' => 1]],
], [
'typeMap' => ['array' => 'array'],
'allowDiskUse' => true,
])->toArray();
$columns = [];
foreach ($stats as $stat) {
sort($stat->types);
$type = implode(', ', $stat->types);
$name = $stat->_id;
if ($name === '_id') {
$name = 'id';
}
$columns[] = [
'name' => $name,
'type_name' => $type,
'type' => $type,
'collation' => null,
'nullable' => $name !== 'id',
'default' => null,
'auto_increment' => false,
'comment' => sprintf('%d occurrences', $stat->total),
'generation' => $name === 'id' ? ['type' => 'objectId', 'expression' => null] : null,
];
}
return $columns;
}
public function getIndexes($table)
{
$collection = $this->connection->getDatabase()->selectCollection($table);
assert($collection instanceof Collection);
$indexList = [];
$indexes = $collection->listIndexes();
foreach ($indexes as $index) {
assert($index instanceof IndexInfo);
$indexList[] = [
'name' => $index->getName(),
'columns' => array_keys($index->getKey()),
'primary' => $index->getKey() === ['_id' => 1],
'type' => match (true) {
$index->isText() => 'text',
$index->is2dSphere() => '2dsphere',
$index->isTtl() => 'ttl',
default => null,
},
'unique' => $index->isUnique(),
];
}
try {
$indexes = $collection->listSearchIndexes(['typeMap' => ['root' => 'array', 'array' => 'array', 'document' => 'array']]);
foreach ($indexes as $index) {
// Status 'DOES_NOT_EXIST' means the index has been dropped but is still in the process of being removed
if ($index['status'] === 'DOES_NOT_EXIST') {
continue;
}
$indexList[] = [
'name' => $index['name'],
'columns' => match ($index['type']) {
'search' => array_merge(
$index['latestDefinition']['mappings']['dynamic'] ? ['dynamic'] : [],
array_keys($index['latestDefinition']['mappings']['fields'] ?? []),
),
'vectorSearch' => array_column($index['latestDefinition']['fields'], 'path'),
},
'type' => $index['type'],
'primary' => false,
'unique' => false,
];
}
} catch (ServerException $exception) {
if (! self::isAtlasSearchNotSupportedException($exception)) {
throw $exception;
}
}
return $indexList;
}
public function getForeignKeys($table)
{
return [];
}
/** @inheritdoc */
protected function createBlueprint($table, ?Closure $callback = null)
{
return new Blueprint($this->connection, $table);
}
/**
* Get collection.
*
* @param string $name
*
* @return bool|CollectionInfo
*/
public function getCollection($name)
{
$db = $this->connection->getDatabase();
$collections = iterator_to_array($db->listCollections([
'filter' => ['name' => $name],
]), false);
return count($collections) ? current($collections) : false;
}
/**
* Get all of the collections names for the database.
*
* @return array
*/
protected function getAllCollections()
{
$collections = [];
foreach ($this->connection->getDatabase()->listCollections() as $collection) {
$collections[] = $collection->getName();
}
return $collections;
}
/** @internal */
public static function isAtlasSearchNotSupportedException(ServerException $e): bool
{
return in_array($e->getCode(), [
59, // MongoDB 4 to 6, 7-community: no such command: 'createSearchIndexes'
40324, // MongoDB 4 to 6: Unrecognized pipeline stage name: '$listSearchIndexes'
115, // MongoDB 7-ent: Search index commands are only supported with Atlas.
6047401, // MongoDB 7: $listSearchIndexes stage is only allowed on MongoDB Atlas
31082, // MongoDB 8: Using Atlas Search Database Commands and the $listSearchIndexes aggregation stage requires additional configuration.
], true);
}
}