From 03cb3f8d3bbd1c8b60a5c2475ae7d97c4560377d Mon Sep 17 00:00:00 2001 From: oleksandrb Date: Mon, 10 Jun 2024 11:57:45 +0300 Subject: [PATCH 1/8] Add hasColumn method --- src/Schema/Builder.php | 33 ++++++++++++++++++++++++++++----- tests/SchemaTest.php | 17 +++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index bfa0e4715..96b0e4168 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -5,6 +5,7 @@ namespace MongoDB\Laravel\Schema; use Closure; +use Illuminate\Support\Facades\DB; use MongoDB\Model\CollectionInfo; use function count; @@ -13,15 +14,37 @@ class Builder extends \Illuminate\Database\Schema\Builder { - /** @inheritdoc */ - public function hasColumn($table, $column) + /** + * Check if column exists in the collection schema. + * + * @param $table + * @param $column + * @return bool + */ + public function hasColumn($table, $column): bool { - return true; + $collection = $this->connection->table($table); + + return $collection->where($column, 'exists', true) + ->project(['_id' => 1]) + ->exists(); } - /** @inheritdoc */ - public function hasColumns($table, array $columns) + /** + * Check if columns exists in the collection schema. + * + * @param $table + * @param array $columns + * + * @return bool + */ + public function hasColumns($table, array $columns): bool { + foreach ($columns as $column) { + if (!$this->hasColumn($table, $column)) { + return false; + } + } return true; } diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 6e6248beb..5f987b4ee 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -377,6 +377,23 @@ 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 + { + DB::connection()->collection('newcollection')->insert(['column1' => 'value']); + DB::connection()->collection('newcollection')->insert(['column2' => 'value']); + + $this->assertTrue(Schema::hasColumns('newcollection', ['column1', 'column2'])); + $this->assertFalse(Schema::hasColumns('newcollection', ['column1', 'column3'])); + } + protected function getIndex(string $collection, string $name) { $collection = DB::getCollection($collection); From 28288e488797328ca244f633c90ead92da00042a Mon Sep 17 00:00:00 2001 From: oleksandrb Date: Mon, 10 Jun 2024 11:59:30 +0300 Subject: [PATCH 2/8] Remove unused usage --- src/Schema/Builder.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 96b0e4168..3311a7ff6 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -5,7 +5,6 @@ namespace MongoDB\Laravel\Schema; use Closure; -use Illuminate\Support\Facades\DB; use MongoDB\Model\CollectionInfo; use function count; From 3fc6316f4fe33c759041d1697863c390f0b9ff27 Mon Sep 17 00:00:00 2001 From: oleksandrb Date: Mon, 10 Jun 2024 12:10:55 +0300 Subject: [PATCH 3/8] Changelod updated --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0345701b8..c91878e46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file. ## [4.5.0] - upcoming * Add GridFS integration for Laravel File Storage by @GromNaN in [#2984](https://github.com/mongodb/laravel-mongodb/pull/2985) - +* Add hasColumn/hasColumns by @Alex-Belyi in [#3001](https://github.com/mongodb/laravel-mongodb/pull/3001) ## [4.4.0] - 2024-05-31 * Support collection name prefix by @GromNaN in [#2930](https://github.com/mongodb/laravel-mongodb/pull/2930) From 3a895be584ee8a779c50f9248b7296c42d086804 Mon Sep 17 00:00:00 2001 From: oleksandrb Date: Mon, 10 Jun 2024 12:11:30 +0300 Subject: [PATCH 4/8] Changelod updated --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c91878e46..7278147b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file. ## [4.5.0] - upcoming * Add GridFS integration for Laravel File Storage by @GromNaN in [#2984](https://github.com/mongodb/laravel-mongodb/pull/2985) -* Add hasColumn/hasColumns by @Alex-Belyi in [#3001](https://github.com/mongodb/laravel-mongodb/pull/3001) +* Add `hasColumn` and `hasColumns` method by @Alex-Belyi in [#3001](https://github.com/mongodb/laravel-mongodb/pull/3001) ## [4.4.0] - 2024-05-31 * Support collection name prefix by @GromNaN in [#2930](https://github.com/mongodb/laravel-mongodb/pull/2930) From 680433c1ee5b030b19ffdcf975af50966bdcde95 Mon Sep 17 00:00:00 2001 From: oleksandrb Date: Mon, 10 Jun 2024 13:20:43 +0300 Subject: [PATCH 5/8] Add link to issue --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7278147b7..cff70992f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file. ## [4.5.0] - upcoming * Add GridFS integration for Laravel File Storage by @GromNaN in [#2984](https://github.com/mongodb/laravel-mongodb/pull/2985) -* Add `hasColumn` and `hasColumns` method by @Alex-Belyi in [#3001](https://github.com/mongodb/laravel-mongodb/pull/3001) +* Add `hasColumn` and `hasColumns` method by @Alex-Belyi in [#3002](https://github.com/mongodb/laravel-mongodb/pull/3001) ## [4.4.0] - 2024-05-31 * Support collection name prefix by @GromNaN in [#2930](https://github.com/mongodb/laravel-mongodb/pull/2930) From ca0285fe3daac147b09b0648ac7a265685561c99 Mon Sep 17 00:00:00 2001 From: oleksandrb Date: Mon, 10 Jun 2024 15:52:24 +0300 Subject: [PATCH 6/8] Refactor hasColumns method and test --- src/Schema/Builder.php | 11 +++++------ tests/SchemaTest.php | 6 ++++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 3311a7ff6..fdee24df4 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -39,12 +39,11 @@ public function hasColumn($table, $column): bool */ public function hasColumns($table, array $columns): bool { - foreach ($columns as $column) { - if (!$this->hasColumn($table, $column)) { - return false; - } - } - return true; + $collection = $this->connection->table($table); + + return $collection->whereAll($columns, 'exists', true) + ->project(['_id' => 1]) + ->exists(); } /** diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 5f987b4ee..0e3d44484 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -387,8 +387,10 @@ public function testHasColumn(): void public function testHasColumns(): void { - DB::connection()->collection('newcollection')->insert(['column1' => 'value']); - DB::connection()->collection('newcollection')->insert(['column2' => 'value']); + // Insert documents with both column1 and column2 + DB::connection()->collection('newcollection')->insert([ + ['column1' => 'value1', 'column2' => 'value2'] + ]); $this->assertTrue(Schema::hasColumns('newcollection', ['column1', 'column2'])); $this->assertFalse(Schema::hasColumns('newcollection', ['column1', 'column3'])); From 2150aba566d2e4b5203afda2c3f213f14bd4279b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 16 Jul 2024 17:37:08 +0200 Subject: [PATCH 7/8] Fix CS --- src/Schema/Builder.php | 9 +++++---- tests/SchemaTest.php | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index b213b9f70..e6ebbf5c4 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -23,8 +23,9 @@ class Builder extends \Illuminate\Database\Schema\Builder /** * Check if column exists in the collection schema. * - * @param $table - * @param $column + * @param string $table + * @param string $column + * * @return bool */ public function hasColumn($table, $column): bool @@ -39,8 +40,8 @@ public function hasColumn($table, $column): bool /** * Check if columns exists in the collection schema. * - * @param $table - * @param array $columns + * @param string $table + * @param string[] $columns * * @return bool */ diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 3c7f5afe1..a8be842d9 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -394,7 +394,7 @@ public function testHasColumns(): void { // Insert documents with both column1 and column2 DB::connection()->collection('newcollection')->insert([ - ['column1' => 'value1', 'column2' => 'value2'] + ['column1' => 'value1', 'column2' => 'value2'], ]); $this->assertTrue(Schema::hasColumns('newcollection', ['column1', 'column2'])); From 4fef190087f3055bf901ff47540e0d291fbb4a21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 16 Jul 2024 18:57:01 +0200 Subject: [PATCH 8/8] Fix compatibility with Laravel 10 --- src/Schema/Builder.php | 14 ++++---------- tests/SchemaTest.php | 1 + 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index e6ebbf5c4..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; @@ -25,16 +26,10 @@ class Builder extends \Illuminate\Database\Schema\Builder * * @param string $table * @param string $column - * - * @return bool */ public function hasColumn($table, $column): bool { - $collection = $this->connection->table($table); - - return $collection->where($column, 'exists', true) - ->project(['_id' => 1]) - ->exists(); + return $this->hasColumns($table, [$column]); } /** @@ -42,14 +37,13 @@ public function hasColumn($table, $column): bool * * @param string $table * @param string[] $columns - * - * @return bool */ public function hasColumns($table, array $columns): bool { $collection = $this->connection->table($table); - return $collection->whereAll($columns, 'exists', true) + return $collection + ->where(array_fill_keys($columns, ['$exists' => true])) ->project(['_id' => 1]) ->exists(); } diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index a8be842d9..e9d039fa7 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -395,6 +395,7 @@ 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']));