From ca095ffcc6f30ae7e8ab3a628b6840611751f799 Mon Sep 17 00:00:00 2001 From: Arthur Vandenberghe Date: Fri, 5 Apr 2019 00:04:09 +0200 Subject: [PATCH 1/5] Bugfix add collection with options Adding a fix for #1734 . I might come up with a pull request for json schema's: https://docs.mongodb.com/manual/reference/operator/query/jsonSchema/#op._S_jsonSchema @jenssegers would you like to have this feature? --- src/Jenssegers/Mongodb/Schema/Blueprint.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index 0c01c96aa..a6ae1bc53 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -188,14 +188,14 @@ public function expire($columns, $seconds) /** * @inheritdoc */ - 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); } /** From e5d0dd71e572bb945c4fabc44a8da4d11bb2c58e Mon Sep 17 00:00:00 2001 From: Ditty Date: Sun, 9 Feb 2020 06:02:45 +0300 Subject: [PATCH 2/5] Add collection info and tests --- src/Jenssegers/Mongodb/Schema/Builder.php | 7 ++++--- tests/SchemaTest.php | 4 ++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Jenssegers/Mongodb/Schema/Builder.php b/src/Jenssegers/Mongodb/Schema/Builder.php index 1bca6c02e..8c389a898 100644 --- a/src/Jenssegers/Mongodb/Schema/Builder.php +++ b/src/Jenssegers/Mongodb/Schema/Builder.php @@ -34,15 +34,16 @@ public function hasColumns($table, array $columns) /** * Determine if the given collection exists. * @param string $collection - * @return bool + * @param bool $collection_info + * @return bool|\MongoDB\Model\CollectionInfo */ - public function hasCollection($collection) + public function hasCollection($collection, $collection_info = false) { $db = $this->connection->getMongoDB(); foreach ($db->listCollections() as $collectionFromMongo) { if ($collectionFromMongo->getName() == $collection) { - return true; + return $collection_info ? $collectionFromMongo : true; } } diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index aa5685df1..8eb8c43ff 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -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::hasCollection('newcollection_two', true); + $this->assertTrue($collection['options']['capped']); + $this->assertEquals(1024, $collection['options']['size']); } public function testDrop(): void From c4a916204df3ce3f1e3a3f7cb855c799422963b3 Mon Sep 17 00:00:00 2001 From: Ditty Date: Sun, 9 Feb 2020 23:05:15 +0300 Subject: [PATCH 3/5] Add correct docblock --- src/Jenssegers/Mongodb/Schema/Blueprint.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index f3b9c2ad5..cc62ec6c1 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -234,7 +234,9 @@ public function expire($columns, $seconds) } /** - * @inheritdoc + * Indicate that the collection needs to be created. + * @param array $options + * @return void */ public function create($options = []) { From 2dcfa5b5aa6c4c2be2a6da87294228ea683a5b72 Mon Sep 17 00:00:00 2001 From: Ditty Date: Mon, 17 Feb 2020 12:57:27 +0300 Subject: [PATCH 4/5] Add getCollection function and little improvements Add getCollection function and little improvements --- src/Jenssegers/Mongodb/Schema/Builder.php | 37 +++++++++++++++++------ tests/SchemaTest.php | 2 +- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/Jenssegers/Mongodb/Schema/Builder.php b/src/Jenssegers/Mongodb/Schema/Builder.php index 8c389a898..d948d5528 100644 --- a/src/Jenssegers/Mongodb/Schema/Builder.php +++ b/src/Jenssegers/Mongodb/Schema/Builder.php @@ -33,21 +33,20 @@ public function hasColumns($table, array $columns) /** * Determine if the given collection exists. - * @param string $collection - * @param bool $collection_info - * @return bool|\MongoDB\Model\CollectionInfo + * @param string $name + * @return bool */ - public function hasCollection($collection, $collection_info = false) + public function hasCollection($name) { $db = $this->connection->getMongoDB(); - foreach ($db->listCollections() as $collectionFromMongo) { - if ($collectionFromMongo->getName() == $collection) { - return $collection_info ? $collectionFromMongo : true; - } - } + $collections = $db->listCollections([ + 'filter' => [ + 'name' => $name, + ], + ]); - return false; + return $collections->count() ? true : false; } /** @@ -135,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 diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 8eb8c43ff..575b01113 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -35,7 +35,7 @@ public function testCreateWithOptions(): void $this->assertTrue(Schema::hasCollection('newcollection_two')); $this->assertTrue(Schema::hasTable('newcollection_two')); - $collection = Schema::hasCollection('newcollection_two', true); + $collection = Schema::getCollection('newcollection_two'); $this->assertTrue($collection['options']['capped']); $this->assertEquals(1024, $collection['options']['size']); } From ae88c82d591eb16b99695dcc13c4f82eaa6dbe8f Mon Sep 17 00:00:00 2001 From: Ditty Date: Mon, 17 Feb 2020 13:46:05 +0300 Subject: [PATCH 5/5] Refactor getCollection and hasCollection --- src/Jenssegers/Mongodb/Schema/Builder.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Jenssegers/Mongodb/Schema/Builder.php b/src/Jenssegers/Mongodb/Schema/Builder.php index d948d5528..c01d12426 100644 --- a/src/Jenssegers/Mongodb/Schema/Builder.php +++ b/src/Jenssegers/Mongodb/Schema/Builder.php @@ -40,13 +40,13 @@ public function hasCollection($name) { $db = $this->connection->getMongoDB(); - $collections = $db->listCollections([ + $collections = iterator_to_array($db->listCollections([ 'filter' => [ 'name' => $name, ], - ]); + ]), false); - return $collections->count() ? true : false; + return count($collections) ? true : false; } /** @@ -143,13 +143,13 @@ public function getCollection($name) { $db = $this->connection->getMongoDB(); - $collections = $db->listCollections([ + $collections = iterator_to_array($db->listCollections([ 'filter' => [ 'name' => $name, ], - ]); + ]), false); - return $collections->count() ? (($collections = iterator_to_array($collections) ) ? current($collections) : false) : false; + return count($collections) ? current($collections) : false; } /**