diff --git a/CHANGELOG.md b/CHANGELOG.md index 4525b0848..e7e394001 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,12 @@ # Changelog All notable changes to this project will be documented in this file. -## [4.9.0] - coming soon +## [4.7.0] - coming soon * Add `Connection::getServerVersion()` by @GromNaN in [#3043](https://github.com/mongodb/laravel-mongodb/pull/3043) * Add `Schema\Builder::getTables()` and `getTableListing()` by @GromNaN in [#3044](https://github.com/mongodb/laravel-mongodb/pull/3044) * Add `Schema\Builder::getColumns()` and `getIndexes()` by @GromNaN in [#3045](https://github.com/mongodb/laravel-mongodb/pull/3045) +* Add `Schema\Builder::hasColumn` and `hasColumns` method by @Alex-Belyi in [#3002](https://github.com/mongodb/laravel-mongodb/pull/3001) ## [4.6.0] - 2024-07-09 diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 32fc9f482..29f089d7d 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -8,6 +8,7 @@ use MongoDB\Model\CollectionInfo; use MongoDB\Model\IndexInfo; +use function array_fill_keys; use function array_keys; use function assert; use function count; @@ -20,16 +21,31 @@ class Builder extends \Illuminate\Database\Schema\Builder { - /** @inheritdoc */ - public function hasColumn($table, $column) + /** + * Check if column exists in the collection schema. + * + * @param string $table + * @param string $column + */ + public function hasColumn($table, $column): bool { - return true; + return $this->hasColumns($table, [$column]); } - /** @inheritdoc */ - public function hasColumns($table, array $columns) + /** + * Check if columns exists in the collection schema. + * + * @param string $table + * @param string[] $columns + */ + public function hasColumns($table, array $columns): bool { - return true; + $collection = $this->connection->table($table); + + return $collection + ->where(array_fill_keys($columns, ['$exists' => true])) + ->project(['_id' => 1]) + ->exists(); } /** diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 1d99627ce..e9d039fa7 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -382,6 +382,26 @@ public function testRenameColumn(): void $this->assertSame($check[2]['column'], $check2[2]['column']); } + public function testHasColumn(): void + { + DB::connection()->collection('newcollection')->insert(['column1' => 'value']); + + $this->assertTrue(Schema::hasColumn('newcollection', 'column1')); + $this->assertFalse(Schema::hasColumn('newcollection', 'column2')); + } + + public function testHasColumns(): void + { + // Insert documents with both column1 and column2 + DB::connection()->collection('newcollection')->insert([ + ['column1' => 'value1', 'column2' => 'value2'], + ['column1' => 'value3'], + ]); + + $this->assertTrue(Schema::hasColumns('newcollection', ['column1', 'column2'])); + $this->assertFalse(Schema::hasColumns('newcollection', ['column1', 'column3'])); + } + public function testGetTables() { DB::connection('mongodb')->collection('newcollection')->insert(['test' => 'value']);