21
21
use function assert ;
22
22
use function count ;
23
23
use function current ;
24
+ use function explode ;
24
25
use function implode ;
25
26
use function in_array ;
26
27
use function is_array ;
27
28
use function is_string ;
28
29
use function iterator_to_array ;
29
30
use function sort ;
30
31
use function sprintf ;
32
+ use function str_contains ;
31
33
use function str_ends_with ;
32
34
use function substr ;
35
+ use function trigger_error ;
33
36
use function usort ;
34
37
38
+ use const E_USER_DEPRECATED ;
39
+
35
40
/** @property Connection $connection */
36
41
class Builder extends \Illuminate \Database \Schema \Builder
37
42
{
@@ -47,7 +52,7 @@ public function hasColumn($table, $column): bool
47
52
}
48
53
49
54
/**
50
- * Check if columns exists in the collection schema.
55
+ * Check if columns exist in the collection schema.
51
56
*
52
57
* @param string $table
53
58
* @param string[] $columns
@@ -134,12 +139,18 @@ public function drop($table)
134
139
$ blueprint ->drop ();
135
140
}
136
141
137
- /** @inheritdoc */
142
+ /**
143
+ * @inheritdoc
144
+ *
145
+ * Drops the entire database instead of deleting each collection individually.
146
+ *
147
+ * In MongoDB, dropping the whole database is much faster than dropping collections
148
+ * one by one. The database will be automatically recreated when a new connection
149
+ * writes to it.
150
+ */
138
151
public function dropAllTables ()
139
152
{
140
- foreach ($ this ->getAllCollections () as $ collection ) {
141
- $ this ->drop ($ collection );
142
- }
153
+ $ this ->connection ->getDatabase ()->drop ();
143
154
}
144
155
145
156
/** @param string|null $schema Database name */
@@ -148,7 +159,14 @@ public function getTables($schema = null)
148
159
$ db = $ this ->connection ->getDatabase ($ schema );
149
160
$ collections = [];
150
161
151
- foreach ($ db ->listCollectionNames () as $ collectionName ) {
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
+
152
170
$ stats = $ db ->selectCollection ($ collectionName )->aggregate ([
153
171
['$collStats ' => ['storageStats ' => ['scale ' => 1 ]]],
154
172
['$project ' => ['storageStats.totalSize ' => 1 ]],
@@ -165,9 +183,37 @@ public function getTables($schema = null)
165
183
];
166
184
}
167
185
168
- usort ($ collections , function ($ a , $ b ) {
169
- return $ a ['name ' ] <=> $ b ['name ' ];
170
- });
186
+ usort ($ collections , fn ($ a , $ b ) => $ a ['name ' ] <=> $ b ['name ' ]);
187
+
188
+ return $ collections ;
189
+ }
190
+
191
+ /** @param string|null $schema Database name */
192
+ public function getViews ($ schema = null )
193
+ {
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 ' ]);
171
217
172
218
return $ collections ;
173
219
}
@@ -203,7 +249,12 @@ public function getTableListing($schema = null, $schemaQualified = false)
203
249
204
250
public function getColumns ($ table )
205
251
{
206
- $ stats = $ this ->connection ->getDatabase ()->selectCollection ($ table )->aggregate ([
252
+ $ db = null ;
253
+ if (str_contains ($ table , '. ' )) {
254
+ [$ db , $ table ] = explode ('. ' , $ table , 2 );
255
+ }
256
+
257
+ $ stats = $ this ->connection ->getDatabase ($ db )->selectCollection ($ table )->aggregate ([
207
258
// Sample 1,000 documents to get a representative sample of the collection
208
259
['$sample ' => ['size ' => 1_000 ]],
209
260
// Convert each document to an array of fields
@@ -340,10 +391,14 @@ public function getCollection($name)
340
391
/**
341
392
* Get all of the collections names for the database.
342
393
*
394
+ * @deprecated
395
+ *
343
396
* @return array
344
397
*/
345
398
protected function getAllCollections ()
346
399
{
400
+ trigger_error (sprintf ('Since mongodb/laravel-mongodb:5.4, Method "%s()" is deprecated without replacement. ' , __METHOD__ ), E_USER_DEPRECATED );
401
+
347
402
$ collections = [];
348
403
foreach ($ this ->connection ->getDatabase ()->listCollections () as $ collection ) {
349
404
$ collections [] = $ collection ->getName ();
0 commit comments