|
6 | 6 |
|
7 | 7 | use Closure; |
8 | 8 | use MongoDB\Collection; |
| 9 | +use MongoDB\Database; |
9 | 10 | use MongoDB\Driver\Exception\ServerException; |
10 | 11 | use MongoDB\Laravel\Connection; |
11 | 12 | use MongoDB\Model\CollectionInfo; |
@@ -156,66 +157,13 @@ public function dropAllTables() |
156 | 157 | /** @param string|null $schema Database name */ |
157 | 158 | public function getTables($schema = null) |
158 | 159 | { |
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); |
189 | 161 | } |
190 | 162 |
|
191 | 163 | /** @param string|null $schema Database name */ |
192 | 164 | public function getViews($schema = null) |
193 | 165 | { |
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); |
219 | 167 | } |
220 | 168 |
|
221 | 169 | /** |
@@ -254,7 +202,7 @@ public function getColumns($table) |
254 | 202 | [$db, $table] = explode('.', $table, 2); |
255 | 203 | } |
256 | 204 |
|
257 | | - $stats = $this->connection->getDatabase($db)->selectCollection($table)->aggregate([ |
| 205 | + $stats = $this->connection->getDatabase($db)->getCollection($table)->aggregate([ |
258 | 206 | // Sample 1,000 documents to get a representative sample of the collection |
259 | 207 | ['$sample' => ['size' => 1_000]], |
260 | 208 | // Convert each document to an array of fields |
@@ -389,7 +337,7 @@ public function getCollection($name) |
389 | 337 | } |
390 | 338 |
|
391 | 339 | /** |
392 | | - * Get all of the collections names for the database. |
| 340 | + * Get all the collections names for the database. |
393 | 341 | * |
394 | 342 | * @deprecated |
395 | 343 | * |
@@ -418,4 +366,39 @@ public static function isAtlasSearchNotSupportedException(ServerException $e): b |
418 | 366 | 31082, // MongoDB 8: Using Atlas Search Database Commands and the $listSearchIndexes aggregation stage requires additional configuration. |
419 | 367 | ], true); |
420 | 368 | } |
| 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 | + } |
421 | 404 | } |
0 commit comments