Skip to content

Commit fbfbcf4

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 6260b47 commit fbfbcf4

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed

src/Schema/Builder.php

Lines changed: 25 additions & 33 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;
@@ -154,23 +155,25 @@ public function dropAllTables()
154155
}
155156

156157
/** @param string|null $schema Database name */
157-
public function getTables($schema = null)
158-
{
158+
private function getCollectionRows(
159+
$schema = null,
160+
?string $collectionType = null,
161+
?callable $aggregateStats = null,
162+
) {
159163
$db = $this->connection->getDatabase($schema);
160164
$collections = [];
161165

162166
foreach ($db->listCollections() as $collectionInfo) {
163167
$collectionName = $collectionInfo->getName();
164168

165-
// Skip views, which don't support aggregate
166-
if ($collectionInfo->getType() === 'view') {
169+
if ($collectionType && $collectionInfo->getType() !== $collectionType) {
167170
continue;
168171
}
169172

170-
$stats = $db->selectCollection($collectionName)->aggregate([
171-
['$collStats' => ['storageStats' => ['scale' => 1]]],
172-
['$project' => ['storageStats.totalSize' => 1]],
173-
])->toArray();
173+
// Aggregation is not supported on views
174+
$shouldAggregateStats = $aggregateStats !== null && $collectionInfo->getType() !== 'view';
175+
176+
$stats = $shouldAggregateStats ? $aggregateStats($db, $collectionName) : null;
174177

175178
$collections[] = [
176179
'name' => $collectionName,
@@ -188,34 +191,23 @@ public function getTables($schema = null)
188191
return $collections;
189192
}
190193

191-
/** @param string|null $schema Database name */
192-
public function getViews($schema = null)
194+
/** @param string|null $schema Database name */
195+
public function getTables($schema = null)
193196
{
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-
}
197+
$aggregateStats = function (Database $db, string $collectionName): array {
198+
return $db->selectCollection($collectionName)->aggregate([
199+
['$collStats' => ['storageStats' => ['scale' => 1]]],
200+
['$project' => ['storageStats.totalSize' => 1]],
201+
])->toArray();
202+
};
215203

216-
usort($collections, fn ($a, $b) => $a['name'] <=> $b['name']);
204+
return $this->getCollectionRows($schema, 'collection', $aggregateStats);
205+
}
217206

218-
return $collections;
207+
/** @param string|null $schema Database name */
208+
public function getViews($schema = null)
209+
{
210+
return $this->getCollectionRows($schema, 'view');
219211
}
220212

221213
/**

0 commit comments

Comments
 (0)