Unverified Commit 3f5b1dc5 authored by Stas's avatar Stas Committed by GitHub

Merge pull request #1953 from divine/pr_1745

[Updated PR#1745] Bugfix create collection with options
parents 6cea8aae ae88c82d
...@@ -234,16 +234,18 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint ...@@ -234,16 +234,18 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
} }
/** /**
* @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(); $collection = $this->collection->getCollectionName();
$db = $this->connection->getMongoDB(); $db = $this->connection->getMongoDB();
// Ensure the collection is created. // Ensure the collection is created.
$db->createCollection($collection); $db->createCollection($collection, $options);
} }
/** /**
......
...@@ -33,20 +33,20 @@ class Builder extends \Illuminate\Database\Schema\Builder ...@@ -33,20 +33,20 @@ class Builder extends \Illuminate\Database\Schema\Builder
/** /**
* Determine if the given collection exists. * Determine if the given collection exists.
* @param string $collection * @param string $name
* @return bool * @return bool
*/ */
public function hasCollection($collection) public function hasCollection($name)
{ {
$db = $this->connection->getMongoDB(); $db = $this->connection->getMongoDB();
foreach ($db->listCollections() as $collectionFromMongo) { $collections = iterator_to_array($db->listCollections([
if ($collectionFromMongo->getName() == $collection) { 'filter' => [
return true; 'name' => $name,
} ],
} ]), false);
return false; return count($collections) ? true : false;
} }
/** /**
...@@ -134,6 +134,24 @@ class Builder extends \Illuminate\Database\Schema\Builder ...@@ -134,6 +134,24 @@ class Builder extends \Illuminate\Database\Schema\Builder
return new Blueprint($this->connection, $collection); 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. * Get all of the collections names for the database.
* @return array * @return array
......
...@@ -34,6 +34,10 @@ class SchemaTest extends TestCase ...@@ -34,6 +34,10 @@ class SchemaTest extends TestCase
Schema::create('newcollection_two', null, ['capped' => true, 'size' => 1024]); Schema::create('newcollection_two', null, ['capped' => true, 'size' => 1024]);
$this->assertTrue(Schema::hasCollection('newcollection_two')); $this->assertTrue(Schema::hasCollection('newcollection_two'));
$this->assertTrue(Schema::hasTable('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 public function testDrop(): void
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment