@@ -143,7 +143,7 @@ public function dropAllTables()
143
143
}
144
144
}
145
145
146
- /** @param string|null $schema Database name */
146
+ /** @param string|null $schema Database name */
147
147
public function getTables ($ schema = null )
148
148
{
149
149
$ db = $ this ->connection ->getDatabase ($ schema );
@@ -161,12 +161,8 @@ public function getTables($schema = null)
161
161
$ isView = ($ collectionInfo ['type ' ] ?? '' ) === 'view ' ;
162
162
$ stats = null ;
163
163
164
- if (! $ isView ) {
165
- // Only run aggregation if it's a normal collection
166
- $ stats = $ db ->selectCollection ($ collectionName )->aggregate ([
167
- ['$collStats ' => ['storageStats ' => ['scale ' => 1 ]]],
168
- ['$project ' => ['storageStats.totalSize ' => 1 ]],
169
- ])->toArray ();
164
+ if ($ isView ) {
165
+ continue ;
170
166
}
171
167
172
168
$ collections [] = [
@@ -176,7 +172,45 @@ public function getTables($schema = null)
176
172
'size ' => $ stats [0 ]?->storageStats?->totalSize ?? null ,
177
173
'comment ' => null ,
178
174
'collation ' => null ,
179
- 'engine ' => $ isView ? 'view ' : 'collection ' ,
175
+ 'engine ' => null ,
176
+ ];
177
+ }
178
+
179
+ usort ($ collections , fn ($ a , $ b ) => $ a ['name ' ] <=> $ b ['name ' ]);
180
+
181
+ return $ collections ;
182
+ }
183
+
184
+ /** @param string|null $schema Database name */
185
+ public function getViews ($ schema = null )
186
+ {
187
+ $ db = $ this ->connection ->getDatabase ($ schema );
188
+ $ collections = [];
189
+
190
+ foreach ($ db ->listCollections () as $ collectionInfo ) {
191
+ $ collectionName = $ collectionInfo ->getName ();
192
+
193
+ // Skip system collections
194
+ if (str_starts_with ($ collectionName , 'system. ' )) {
195
+ continue ;
196
+ }
197
+
198
+ // Skip views it doesnt suport aggregate
199
+ $ isView = ($ collectionInfo ['type ' ] ?? '' ) === 'view ' ;
200
+ $ stats = null ;
201
+
202
+ if (! $ isView ) {
203
+ continue ;
204
+ }
205
+
206
+ $ collections [] = [
207
+ 'name ' => $ collectionName ,
208
+ 'schema ' => $ db ->getDatabaseName (),
209
+ 'schema_qualified_name ' => $ db ->getDatabaseName () . '. ' . $ collectionName ,
210
+ 'size ' => null ,
211
+ 'comment ' => null ,
212
+ 'collation ' => null ,
213
+ 'engine ' => null ,
180
214
];
181
215
}
182
216
0 commit comments