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 ;
@@ -154,23 +155,25 @@ public function dropAllTables()
154
155
}
155
156
156
157
/** @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
+ ) {
159
163
$ db = $ this ->connection ->getDatabase ($ schema );
160
164
$ collections = [];
161
165
162
166
foreach ($ db ->listCollections () as $ collectionInfo ) {
163
167
$ collectionName = $ collectionInfo ->getName ();
164
168
165
- // Skip views, which don't support aggregate
166
- if ($ collectionInfo ->getType () === 'view ' ) {
169
+ if ($ collectionType && $ collectionInfo ->getType () !== $ collectionType ) {
167
170
continue ;
168
171
}
169
172
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 ;
174
177
175
178
$ collections [] = [
176
179
'name ' => $ collectionName ,
@@ -188,34 +191,23 @@ public function getTables($schema = null)
188
191
return $ collections ;
189
192
}
190
193
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 )
193
196
{
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
+ };
215
203
216
- usort ($ collections , fn ($ a , $ b ) => $ a ['name ' ] <=> $ b ['name ' ]);
204
+ return $ this ->getCollectionRows ($ schema , 'collection ' , $ aggregateStats );
205
+ }
217
206
218
- return $ collections ;
207
+ /** @param string|null $schema Database name */
208
+ public function getViews ($ schema = null )
209
+ {
210
+ return $ this ->getCollectionRows ($ schema , 'view ' );
219
211
}
220
212
221
213
/**
0 commit comments