Skip to content

Commit ca0285f

Browse files
author
oleksandrb
committed
Refactor hasColumns method and test
1 parent 680433c commit ca0285f

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/Schema/Builder.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ public function hasColumn($table, $column): bool
3939
*/
4040
public function hasColumns($table, array $columns): bool
4141
{
42-
foreach ($columns as $column) {
43-
if (!$this->hasColumn($table, $column)) {
44-
return false;
45-
}
46-
}
47-
return true;
42+
$collection = $this->connection->table($table);
43+
44+
return $collection->whereAll($columns, 'exists', true)
45+
->project(['_id' => 1])
46+
->exists();
4847
}
4948

5049
/**

tests/SchemaTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,10 @@ public function testHasColumn(): void
387387

388388
public function testHasColumns(): void
389389
{
390-
DB::connection()->collection('newcollection')->insert(['column1' => 'value']);
391-
DB::connection()->collection('newcollection')->insert(['column2' => 'value']);
390+
// Insert documents with both column1 and column2
391+
DB::connection()->collection('newcollection')->insert([
392+
['column1' => 'value1', 'column2' => 'value2']
393+
]);
392394

393395
$this->assertTrue(Schema::hasColumns('newcollection', ['column1', 'column2']));
394396
$this->assertFalse(Schema::hasColumns('newcollection', ['column1', 'column3']));

0 commit comments

Comments
 (0)