Skip to content

PHPLIB-123: Do not throw when listing indexes on nonexistent collection #34

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 3 commits into from
Oct 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Operation/DropCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
class DropCollection implements Executable
{
private static $errorMessageNamespaceNotFound = 'ns not found';
private $databaseName;
private $collectionName;

Expand Down Expand Up @@ -44,7 +45,11 @@ public function execute(Server $server)
try {
$cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName)));
} catch (DriverRuntimeException $e) {
if ($e->getMessage() === 'ns not found') {
/* The server may return an error if the collection does not exist.
* Check for an error message (unfortunately, there isn't a code)
* and NOP instead of throwing.
*/
if ($e->getMessage() === self::$errorMessageNamespaceNotFound) {
return (object) ['ok' => 0, 'errmsg' => 'ns not found'];
}

Expand Down
19 changes: 18 additions & 1 deletion src/Operation/ListIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use MongoDB\Driver\Command;
use MongoDB\Driver\Query;
use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException;
use MongoDB\Model\IndexInfoIterator;
use MongoDB\Model\IndexInfoIteratorIterator;
use EmptyIterator;

/**
* Operation for the listIndexes command.
Expand All @@ -17,6 +19,8 @@
*/
class ListIndexes implements Executable
{
private static $errorCodeDatabaseNotFound = 60;
private static $errorCodeNamespaceNotFound = 26;
private static $wireVersionForCommand = 3;

private $databaseName;
Expand Down Expand Up @@ -75,7 +79,20 @@ private function executeCommand(Server $server)
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
}

$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
try {
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
} catch (RuntimeException $e) {
/* The server may return an error if the collection does not exist.
* Check for possible error codes (see: SERVER-20463) and return an
* empty iterator instead of throwing.
*/
if ($e->getCode() === self::$errorCodeNamespaceNotFound || $e->getCode() === self::$errorCodeDatabaseNotFound) {
return new IndexInfoIteratorIterator(new EmptyIterator);
}

throw $e;
}

$cursor->setTypeMap(array('root' => 'array', 'document' => 'array'));

return new IndexInfoIteratorIterator($cursor);
Expand Down
45 changes: 45 additions & 0 deletions tests/Operation/ListCollectionsFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace MongoDB\Tests\Operation;

use MongoDB\Driver\Server;
use MongoDB\Operation\DropDatabase;
use MongoDB\Operation\ListCollections;

class ListCollectionsFunctionalTest extends FunctionalTestCase
{
public function testListCollectionsForNewlyCreatedDatabase()
{
$server = $this->getPrimaryServer();

$operation = new DropDatabase($this->getDatabaseName());
$operation->execute($server);

$writeResult = $this->manager->executeInsert($this->getNamespace(), ['x' => 1]);
$this->assertEquals(1, $writeResult->getInsertedCount());

$operation = new ListCollections($this->getDatabaseName(), ['filter' => ['name' => $this->getCollectionName()]]);
// Convert the CollectionInfoIterator to an array since we cannot rewind its cursor
$collections = iterator_to_array($operation->execute($server));

$this->assertCount(1, $collections);

foreach ($collections as $collection) {
$this->assertInstanceOf('MongoDB\Model\CollectionInfo', $collection);
$this->assertEquals($this->getCollectionName(), $collection->getName());
}
}

public function testListCollectionsForNonexistentDatabase()
{
$server = $this->getPrimaryServer();

$operation = new DropDatabase($this->getDatabaseName());
$operation->execute($server);

$operation = new ListCollections($this->getDatabaseName());
$collections = $operation->execute($server);

$this->assertCount(0, $collections);
}
}
45 changes: 45 additions & 0 deletions tests/Operation/ListIndexesFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace MongoDB\Tests\Operation;

use MongoDB\Driver\Server;
use MongoDB\Operation\DropCollection;
use MongoDB\Operation\ListIndexes;

class ListIndexesFunctionalTest extends FunctionalTestCase
{
public function testListIndexesForNewlyCreatedCollection()
{
$server = $this->getPrimaryServer();

$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($server);

$writeResult = $this->manager->executeInsert($this->getNamespace(), ['x' => 1]);
$this->assertEquals(1, $writeResult->getInsertedCount());

$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
// Convert the CursorInfoIterator to an array since we cannot rewind its cursor
$indexes = iterator_to_array($operation->execute($server));

$this->assertCount(1, $indexes);

foreach ($indexes as $index) {
$this->assertInstanceOf('MongoDB\Model\IndexInfo', $index);
$this->assertEquals(['_id' => 1], $index->getKey());
}
}

public function testListIndexesForNonexistentCollection()
{
$server = $this->getPrimaryServer();

$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($server);

$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
$indexes = $operation->execute($server);

$this->assertCount(0, $indexes);
}
}