File tree 3 files changed +44
-7
lines changed
3 files changed +44
-7
lines changed Original file line number Diff line number Diff line change 1
1
# Changelog
2
2
All notable changes to this project will be documented in this file.
3
3
4
- ## [ 4.9 .0] - coming soon
4
+ ## [ 4.7 .0] - coming soon
5
5
6
6
* Add ` Connection::getServerVersion() ` by @GromNaN in [ #3043 ] ( https://github.com/mongodb/laravel-mongodb/pull/3043 )
7
7
* Add ` Schema\Builder::getTables() ` and ` getTableListing() ` by @GromNaN in [ #3044 ] ( https://github.com/mongodb/laravel-mongodb/pull/3044 )
8
8
* Add ` Schema\Builder::getColumns() ` and ` getIndexes() ` by @GromNaN in [ #3045 ] ( https://github.com/mongodb/laravel-mongodb/pull/3045 )
9
+ * Add ` Schema\Builder::hasColumn ` and ` hasColumns ` method by @Alex-Belyi in [ #3002 ] ( https://github.com/mongodb/laravel-mongodb/pull/3001 )
9
10
10
11
## [ 4.6.0] - 2024-07-09
11
12
Original file line number Diff line number Diff line change 8
8
use MongoDB \Model \CollectionInfo ;
9
9
use MongoDB \Model \IndexInfo ;
10
10
11
+ use function array_fill_keys ;
11
12
use function array_keys ;
12
13
use function assert ;
13
14
use function count ;
20
21
21
22
class Builder extends \Illuminate \Database \Schema \Builder
22
23
{
23
- /** @inheritdoc */
24
- public function hasColumn ($ table , $ column )
24
+ /**
25
+ * Check if column exists in the collection schema.
26
+ *
27
+ * @param string $table
28
+ * @param string $column
29
+ */
30
+ public function hasColumn ($ table , $ column ): bool
25
31
{
26
- return true ;
32
+ return $ this -> hasColumns ( $ table , [ $ column ]) ;
27
33
}
28
34
29
- /** @inheritdoc */
30
- public function hasColumns ($ table , array $ columns )
35
+ /**
36
+ * Check if columns exists in the collection schema.
37
+ *
38
+ * @param string $table
39
+ * @param string[] $columns
40
+ */
41
+ public function hasColumns ($ table , array $ columns ): bool
31
42
{
32
- return true ;
43
+ $ collection = $ this ->connection ->table ($ table );
44
+
45
+ return $ collection
46
+ ->where (array_fill_keys ($ columns , ['$exists ' => true ]))
47
+ ->project (['_id ' => 1 ])
48
+ ->exists ();
33
49
}
34
50
35
51
/**
Original file line number Diff line number Diff line change @@ -382,6 +382,26 @@ public function testRenameColumn(): void
382
382
$ this ->assertSame ($ check [2 ]['column ' ], $ check2 [2 ]['column ' ]);
383
383
}
384
384
385
+ public function testHasColumn (): void
386
+ {
387
+ DB ::connection ()->collection ('newcollection ' )->insert (['column1 ' => 'value ' ]);
388
+
389
+ $ this ->assertTrue (Schema::hasColumn ('newcollection ' , 'column1 ' ));
390
+ $ this ->assertFalse (Schema::hasColumn ('newcollection ' , 'column2 ' ));
391
+ }
392
+
393
+ public function testHasColumns (): void
394
+ {
395
+ // Insert documents with both column1 and column2
396
+ DB ::connection ()->collection ('newcollection ' )->insert ([
397
+ ['column1 ' => 'value1 ' , 'column2 ' => 'value2 ' ],
398
+ ['column1 ' => 'value3 ' ],
399
+ ]);
400
+
401
+ $ this ->assertTrue (Schema::hasColumns ('newcollection ' , ['column1 ' , 'column2 ' ]));
402
+ $ this ->assertFalse (Schema::hasColumns ('newcollection ' , ['column1 ' , 'column3 ' ]));
403
+ }
404
+
385
405
public function testGetTables ()
386
406
{
387
407
DB ::connection ('mongodb ' )->collection ('newcollection ' )->insert (['test ' => 'value ' ]);
You can’t perform that action at this time.
0 commit comments