Skip to content

Commit 0613127

Browse files
Alex-BelyiGromNaN
authored andcommitted
Add hasColumn/hasColumns method (mongodb#3001)
Co-authored-by: Jérôme Tamarelle <jerome.tamarelle@mongodb.com>
1 parent d828534 commit 0613127

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4-
## [4.9.0] - coming soon
4+
## [4.7.0] - coming soon
55

66
* Add `Connection::getServerVersion()` by @GromNaN in [#3043](https://github.com/mongodb/laravel-mongodb/pull/3043)
77
* Add `Schema\Builder::getTables()` and `getTableListing()` by @GromNaN in [#3044](https://github.com/mongodb/laravel-mongodb/pull/3044)
88
* 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)
910

1011
## [4.6.0] - 2024-07-09
1112

src/Schema/Builder.php

+22-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MongoDB\Model\CollectionInfo;
99
use MongoDB\Model\IndexInfo;
1010

11+
use function array_fill_keys;
1112
use function array_keys;
1213
use function assert;
1314
use function count;
@@ -20,16 +21,31 @@
2021

2122
class Builder extends \Illuminate\Database\Schema\Builder
2223
{
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
2531
{
26-
return true;
32+
return $this->hasColumns($table, [$column]);
2733
}
2834

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
3142
{
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();
3349
}
3450

3551
/**

tests/SchemaTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,26 @@ public function testRenameColumn(): void
382382
$this->assertSame($check[2]['column'], $check2[2]['column']);
383383
}
384384

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+
385405
public function testGetTables()
386406
{
387407
DB::connection('mongodb')->collection('newcollection')->insert(['test' => 'value']);

0 commit comments

Comments
 (0)