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
}
/**
* @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);
}
/**
......
......@@ -33,20 +33,20 @@ class Builder extends \Illuminate\Database\Schema\Builder
/**
* 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;
}
/**
......@@ -134,6 +134,24 @@ class Builder extends \Illuminate\Database\Schema\Builder
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
......
......@@ -34,6 +34,10 @@ class SchemaTest extends TestCase
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
......
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