Skip to content

Add hasColumn/hasColumns method #3001

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 9 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [#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)
Expand Down
33 changes: 27 additions & 6 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,37 @@

class Builder extends \Illuminate\Database\Schema\Builder
{
/** @inheritdoc */
public function hasColumn($table, $column)
/**
* Check if column exists in the collection schema.
*
* @param $table

Check failure on line 19 in src/Schema/Builder.php

View workflow job for this annotation

GitHub Actions / phpcs

Missing parameter type

Check failure on line 19 in src/Schema/Builder.php

View workflow job for this annotation

GitHub Actions / phpcs

Missing parameter type
* @param $column

Check failure on line 20 in src/Schema/Builder.php

View workflow job for this annotation

GitHub Actions / phpcs

Missing parameter type

Check failure on line 20 in src/Schema/Builder.php

View workflow job for this annotation

GitHub Actions / phpcs

Missing parameter type
* @return bool
*/
public function hasColumn($table, $column): bool
{
return true;
Copy link
Member

@GromNaN GromNaN Jun 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, that's a breaking change as you change the return value of this method. MongoDB doesn't have columns and there is no restriction on field names (unless you use a schema validation). I understand that this method is not useful as it is, as it returns always true, but I don't think the proposed change is correct as it depends on the data in the collection. Which can lead to a different results depending on the data stored.

$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

Check failure on line 36 in src/Schema/Builder.php

View workflow job for this annotation

GitHub Actions / phpcs

Missing parameter type

Check failure on line 36 in src/Schema/Builder.php

View workflow job for this annotation

GitHub Actions / phpcs

Missing parameter type
*
* @return bool
*/
public function hasColumns($table, array $columns): bool
{
return true;
$collection = $this->connection->table($table);

return $collection->whereAll($columns, 'exists', true)
->project(['_id' => 1])
->exists();
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,25 @@ 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']
]);

$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);
Expand Down
Loading