Skip to content

Add hasColumn/hasColumns method #3001

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
28 changes: 22 additions & 6 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
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.

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();
}

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