Skip to content

[Updated PR#1745] Bugfix create collection with options #1953

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
Feb 17, 2020
8 changes: 5 additions & 3 deletions src/Jenssegers/Mongodb/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,18 @@ public function expire($columns, $seconds)
}

/**
* @inheritdoc
* Indicate that the collection needs to be created.
* @param array $options
* @return void
*/
public function create()
public function create($options = [])
{
$collection = $this->collection->getCollectionName();

$db = $this->connection->getMongoDB();

// Ensure the collection is created.
$db->createCollection($collection);
$db->createCollection($collection, $options);
}

/**
Expand Down
34 changes: 26 additions & 8 deletions src/Jenssegers/Mongodb/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ public function hasColumns($table, array $columns)

/**
* Determine if the given collection exists.
* @param string $collection
* @param string $name
* @return bool
*/
public function hasCollection($collection)
public function hasCollection($name)
{
$db = $this->connection->getMongoDB();

foreach ($db->listCollections() as $collectionFromMongo) {
if ($collectionFromMongo->getName() == $collection) {
return true;
}
}
$collections = $db->listCollections([
'filter' => [
'name' => $name,
],
]);

return false;
return $collections->count() ? true : false;
}

/**
Expand Down Expand Up @@ -134,6 +134,24 @@ protected function createBlueprint($collection, Closure $callback = null)
return new Blueprint($this->connection, $collection);
}

/**
* Get collection.
* @param string $name
* @return bool|\MongoDB\Model\CollectionInfo
*/
public function getCollection($name)
{
$db = $this->connection->getMongoDB();

$collections = $db->listCollections([
'filter' => [
'name' => $name,
],
]);

return $collections->count() ? (($collections = iterator_to_array($collections) ) ? current($collections) : false) : false;
}

/**
* Get all of the collections names for the database.
* @return array
Expand Down
4 changes: 4 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function testCreateWithOptions(): void
Schema::create('newcollection_two', null, ['capped' => true, 'size' => 1024]);
$this->assertTrue(Schema::hasCollection('newcollection_two'));
$this->assertTrue(Schema::hasTable('newcollection_two'));

$collection = Schema::getCollection('newcollection_two');
$this->assertTrue($collection['options']['capped']);
$this->assertEquals(1024, $collection['options']['size']);
}

public function testDrop(): void
Expand Down