From 0cf254b9a28ccf8aa9e0e18f802b63d58f3e5e92 Mon Sep 17 00:00:00 2001 From: Emir Beganovic Date: Tue, 11 Apr 2017 20:53:00 +0200 Subject: [PATCH 1/2] Check different behaviour for index version for MongoDB 3.4 --- .travis.yml | 134 ++++++++++++++++++--- .../MongoDbAdapter/Mongo/MongoCollectionTest.php | 22 ++-- tests/Alcaeus/MongoDbAdapter/Mongo/TestCase.php | 0 tests/Alcaeus/MongoDbAdapter/TestCase.php | 11 ++ 4 files changed, 139 insertions(+), 28 deletions(-) create mode 100644 tests/Alcaeus/MongoDbAdapter/Mongo/TestCase.php diff --git a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php index 52a163f..b1f1ede 100644 --- a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php +++ b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php @@ -1301,12 +1301,14 @@ public function testDeleteIndexesForNonExistingCollection() $this->assertSame($expected, $this->getcollection('nonExisting')->deleteIndexes()); } - public static function dataGetIndexInfo() + public function dataGetIndexInfo() { + $indexVersion = $this->getDefaultIndexVersion(); + return [ 'plainIndex' => [ 'expectedIndex' => [ - 'v' => 1, + 'v' => $indexVersion, 'key' => ['foo' => 1], 'name' => 'foo_1', 'ns' => 'mongo-php-adapter.test', @@ -1316,7 +1318,7 @@ public static function dataGetIndexInfo() ], 'uniqueIndex' => [ 'expectedIndex' => [ - 'v' => 1, + 'v' => $indexVersion, 'key' => ['foo' => 1], 'name' => 'foo_1', 'ns' => 'mongo-php-adapter.test', @@ -1327,7 +1329,7 @@ public static function dataGetIndexInfo() ], 'sparseIndex' => [ 'expectedIndex' => [ - 'v' => 1, + 'v' => $indexVersion, 'key' => ['foo' => 1], 'name' => 'foo_1', 'ns' => 'mongo-php-adapter.test', @@ -1338,7 +1340,7 @@ public static function dataGetIndexInfo() ], 'ttlIndex' => [ 'expectedIndex' => [ - 'v' => 1, + 'v' => $indexVersion, 'key' => ['foo' => 1], 'name' => 'foo_1', 'ns' => 'mongo-php-adapter.test', @@ -1349,7 +1351,7 @@ public static function dataGetIndexInfo() ], 'textIndex' => [ 'expectedIndex' => [ - 'v' => 1, + 'v' => $indexVersion, 'key' => [ '_fts' => 'text', '_ftsx' => 1, @@ -1368,7 +1370,7 @@ public static function dataGetIndexInfo() ], 'partialFilterExpression' => [ 'expectedIndex' => [ - 'v' => 1, + 'v' => $indexVersion, 'key' => ['foo' => 1], 'name' => 'foo_1', 'ns' => 'mongo-php-adapter.test', @@ -1383,7 +1385,7 @@ public static function dataGetIndexInfo() ], 'geoSpatial' => [ 'expectedIndex' => [ - 'v' => 1, + 'v' => $indexVersion, 'key' => ['foo' => '2dsphere'], 'name' => 'foo_2dsphere', 'ns' => 'mongo-php-adapter.test', @@ -1394,7 +1396,7 @@ public static function dataGetIndexInfo() ], 'geoHaystack' => [ 'expectedIndex' => [ - 'v' => 1, + 'v' => $indexVersion, 'key' => ['foo' => 'geoHaystack', 'bar' => 1], 'name' => 'foo_geoHaystack_bar_1', 'ns' => 'mongo-php-adapter.test', @@ -1412,7 +1414,7 @@ public static function dataGetIndexInfo() public function testGetIndexInfo($expectedIndex, $fields, $options) { $idIndex = [ - 'v' => 1, + 'v' => $this->getDefaultIndexVersion(), 'key' => ['_id' => 1], 'name' => '_id_', 'ns' => 'mongo-php-adapter.test', diff --git a/tests/Alcaeus/MongoDbAdapter/Mongo/TestCase.php b/tests/Alcaeus/MongoDbAdapter/Mongo/TestCase.php new file mode 100644 index 0000000..e69de29 diff --git a/tests/Alcaeus/MongoDbAdapter/TestCase.php b/tests/Alcaeus/MongoDbAdapter/TestCase.php index 6964357..3dd96d6 100644 --- a/tests/Alcaeus/MongoDbAdapter/TestCase.php +++ b/tests/Alcaeus/MongoDbAdapter/TestCase.php @@ -163,4 +163,15 @@ protected function skipTestIf($condition) $this->markTestSkipped('Test only applies when running against mongo-php-adapter'); } } + + /** + * Indexes created in MongoDB 3.4 default to v: 2. + * @return int + * @see https://docs.mongodb.com/manual/release-notes/3.4-compatibility/#backwards-incompatible-features + */ + protected function getDefaultIndexVersion() + { + $serverInfo = $this->getDatabase()->command(array('buildinfo' => true)); + return isset($serverInfo['version']) && version_compare($serverInfo['version'], '3.4.0', '>=') ? 2 : 1; + } } From 425d996ec86c5e5c93ae61bb1d0caf26cf06bda4 Mon Sep 17 00:00:00 2001 From: Emir Beganovic Date: Wed, 12 Apr 2017 01:51:41 +0200 Subject: [PATCH 2/2] One-liner --- .travis.yml | 1 - 1 file changed, 1 deletion(-)