Skip to content

Commit

Permalink
Merge pull request mongodb#1953 from divine/pr_1745
Browse files Browse the repository at this point in the history
[Updated PR#1745] Bugfix create collection with options
  • Loading branch information
mnphpexpert committed Feb 17, 2020
2 parents 1272916 + 18e966e commit 51b832f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
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 = iterator_to_array($db->listCollections([
'filter' => [
'name' => $name,
],
]), false);

return false;
return count($collections) ? 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 = iterator_to_array($db->listCollections([
'filter' => [
'name' => $name,
],
]), false);

return count($collections) ? current($collections) : 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

0 comments on commit 51b832f

Please sign in to comment.