Skip to content

Commit 48c9739

Browse files
committed
Extract duplicated collection methods in Builder
The logic for fetching views and collections are very nearly the same bar aggregation support.
1 parent c2eb145 commit 48c9739

File tree

1 file changed

+40
-57
lines changed

1 file changed

+40
-57
lines changed

src/Schema/Builder.php

Lines changed: 40 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Closure;
88
use MongoDB\Collection;
9+
use MongoDB\Database;
910
use MongoDB\Driver\Exception\ServerException;
1011
use MongoDB\Laravel\Connection;
1112
use MongoDB\Model\CollectionInfo;
@@ -156,66 +157,13 @@ public function dropAllTables()
156157
/** @param string|null $schema Database name */
157158
public function getTables($schema = null)
158159
{
159-
$db = $this->connection->getDatabase($schema);
160-
$collections = [];
161-
162-
foreach ($db->listCollections() as $collectionInfo) {
163-
$collectionName = $collectionInfo->getName();
164-
165-
// Skip views, which don't support aggregate
166-
if ($collectionInfo->getType() === 'view') {
167-
continue;
168-
}
169-
170-
$stats = $db->selectCollection($collectionName)->aggregate([
171-
['$collStats' => ['storageStats' => ['scale' => 1]]],
172-
['$project' => ['storageStats.totalSize' => 1]],
173-
])->toArray();
174-
175-
$collections[] = [
176-
'name' => $collectionName,
177-
'schema' => $db->getDatabaseName(),
178-
'schema_qualified_name' => $db->getDatabaseName() . '.' . $collectionName,
179-
'size' => $stats[0]?->storageStats?->totalSize ?? null,
180-
'comment' => null,
181-
'collation' => null,
182-
'engine' => null,
183-
];
184-
}
185-
186-
usort($collections, fn ($a, $b) => $a['name'] <=> $b['name']);
187-
188-
return $collections;
160+
return $this->getCollectionRows('collection', $schema);
189161
}
190162

191163
/** @param string|null $schema Database name */
192164
public function getViews($schema = null)
193165
{
194-
$db = $this->connection->getDatabase($schema);
195-
$collections = [];
196-
197-
foreach ($db->listCollections() as $collectionInfo) {
198-
$collectionName = $collectionInfo->getName();
199-
200-
// Skip normal type collection
201-
if ($collectionInfo->getType() !== 'view') {
202-
continue;
203-
}
204-
205-
$collections[] = [
206-
'name' => $collectionName,
207-
'schema' => $db->getDatabaseName(),
208-
'schema_qualified_name' => $db->getDatabaseName() . '.' . $collectionName,
209-
'size' => null,
210-
'comment' => null,
211-
'collation' => null,
212-
'engine' => null,
213-
];
214-
}
215-
216-
usort($collections, fn ($a, $b) => $a['name'] <=> $b['name']);
217-
218-
return $collections;
166+
return $this->getCollectionRows('view', $schema);
219167
}
220168

221169
/**
@@ -254,7 +202,7 @@ public function getColumns($table)
254202
[$db, $table] = explode('.', $table, 2);
255203
}
256204

257-
$stats = $this->connection->getDatabase($db)->selectCollection($table)->aggregate([
205+
$stats = $this->connection->getDatabase($db)->getCollection($table)->aggregate([
258206
// Sample 1,000 documents to get a representative sample of the collection
259207
['$sample' => ['size' => 1_000]],
260208
// Convert each document to an array of fields
@@ -389,7 +337,7 @@ public function getCollection($name)
389337
}
390338

391339
/**
392-
* Get all of the collections names for the database.
340+
* Get all the collections names for the database.
393341
*
394342
* @deprecated
395343
*
@@ -418,4 +366,39 @@ public static function isAtlasSearchNotSupportedException(ServerException $e): b
418366
31082, // MongoDB 8: Using Atlas Search Database Commands and the $listSearchIndexes aggregation stage requires additional configuration.
419367
], true);
420368
}
369+
370+
/** @param string|null $schema Database name */
371+
private function getCollectionRows(string $collectionType, $schema = null)
372+
{
373+
$db = $this->connection->getDatabase($schema);
374+
$collections = [];
375+
376+
foreach ($db->listCollections() as $collectionInfo) {
377+
$collectionName = $collectionInfo->getName();
378+
379+
if ($collectionInfo->getType() !== $collectionType) {
380+
continue;
381+
}
382+
383+
// Aggregation is not supported on views
384+
$stats = $collectionType !== 'view' ? $db->selectCollection($collectionName)->aggregate([
385+
['$collStats' => ['storageStats' => ['scale' => 1]]],
386+
['$project' => ['storageStats.totalSize' => 1]],
387+
])->toArray() : null;
388+
389+
$collections[] = [
390+
'name' => $collectionName,
391+
'schema' => $db->getDatabaseName(),
392+
'schema_qualified_name' => $db->getDatabaseName() . '.' . $collectionName,
393+
'size' => $stats[0]?->storageStats?->totalSize ?? null,
394+
'comment' => null,
395+
'collation' => null,
396+
'engine' => null,
397+
];
398+
}
399+
400+
usort($collections, fn ($a, $b) => $a['name'] <=> $b['name']);
401+
402+
return $collections;
403+
}
421404
}

0 commit comments

Comments
 (0)