Skip to content

Commit e07fa46

Browse files
authored
Merge pull request #41 from arendjantetteroo/mongo2
Use mongodb/mongodb instead of doctrine/mongodb
2 parents 19ba9f0 + 622ea96 commit e07fa46

File tree

20 files changed

+225
-214
lines changed

20 files changed

+225
-214
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
},
1515
"require": {
1616
"php": "^7.1",
17-
"doctrine/mongodb": "^1.0",
18-
"symfony/console": "^2.7|^3.3|^4",
19-
"symfony/yaml": "^2.7|^3.3|^4"
17+
"ext-mongodb": "*",
18+
"mongodb/mongodb": "^1.4",
19+
"symfony/console": "^2.7|^3.4|^4",
20+
"symfony/yaml": "^2.7|^3.4|^4"
2021
},
2122
"require-dev": {
2223
"phpunit/phpunit": "^7.0",

demo/ConsoleApplication/Example/Migrations/TestAntiMattr/MongoDB/Version20140822185742.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Example\Migrations\TestAntiMattr\MongoDB;
44

55
use AntiMattr\MongoDB\Migrations\AbstractMigration;
6-
use Doctrine\MongoDB\Database;
6+
use \MongoDB\Database;
77

88
class Version20140822185742 extends AbstractMigration
99
{
@@ -20,7 +20,7 @@ public function up(Database $db)
2020
$testA = $db->selectCollection('test_a');
2121
$this->analyze($testA);
2222

23-
$testA->ensureIndex(['actor' => -1]);
23+
$testA->createIndex(['actor' => -1]);
2424
}
2525

2626
public function down(Database $db)
@@ -48,7 +48,7 @@ public function preUp(Database $db)
4848
$testDocuments[] = $testDocument;
4949
}
5050

51-
$testA->batchInsert($testDocuments);
51+
$testA->insertMany($testDocuments);
5252
}
5353

5454
/**

demo/ConsoleApplication/Example/Migrations/TestAntiMattr/MongoDB/Version20140822185743.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Example\Migrations\TestAntiMattr\MongoDB;
44

55
use AntiMattr\MongoDB\Migrations\AbstractMigration;
6-
use Doctrine\MongoDB\Database;
6+
use \MongoDB\Database;
77

88
class Version20140822185743 extends AbstractMigration
99
{
@@ -49,7 +49,7 @@ public function preUp(Database $db)
4949
$testDocuments[] = $testDocument;
5050
}
5151

52-
$testA->batchInsert($testDocuments);
52+
$testA->insertMany($testDocuments);
5353
}
5454

5555
/**

demo/ConsoleApplication/Example/Migrations/TestAntiMattr/MongoDB/Version20140822185744.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Example\Migrations\TestAntiMattr\MongoDB;
44

55
use AntiMattr\MongoDB\Migrations\AbstractMigration;
6-
use Doctrine\MongoDB\Database;
6+
use \MongoDB\Database;
77

88
/**
99
* Auto-generated Migration: Please modify to your needs!

phpunit.xml.dist

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,5 @@
1010
<whitelist processUncoveredFilesFromWhitelist="true">
1111
<directory suffix=".php">src</directory>
1212
</whitelist>
13-
<blacklist>
14-
<directory>vendor</directory>
15-
<directory>./tests</directory>
16-
</blacklist>
1713
</filter>
1814
</phpunit>

src/AntiMattr/MongoDB/Migrations/AbstractMigration.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
use AntiMattr\MongoDB\Migrations\Exception\AbortException;
1515
use AntiMattr\MongoDB\Migrations\Exception\IrreversibleException;
1616
use AntiMattr\MongoDB\Migrations\Exception\SkipException;
17-
use Doctrine\MongoDB\Collection;
18-
use Doctrine\MongoDB\Database;
17+
use \MongoDB\Collection;
18+
use \MongoDB\Database;
1919

2020
/**
2121
* @author Matthew Fitzgerald <matthewfitz@gmail.com>
@@ -56,15 +56,15 @@ abstract public function up(Database $db);
5656
abstract public function down(Database $db);
5757

5858
/**
59-
* @param \Doctrine\MongoDB\Collection
59+
* @param \MongoDB\Collection
6060
*/
6161
protected function analyze(Collection $collection)
6262
{
6363
$this->version->analyze($collection);
6464
}
6565

6666
/**
67-
* @param \Doctrine\MongoDB\Database
67+
* @param \MongoDB\Database
6868
* @param string $filename
6969
*/
7070
protected function executeScript(Database $db, $filename)

src/AntiMattr/MongoDB/Migrations/Collection/Statistics.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
namespace AntiMattr\MongoDB\Migrations\Collection;
1313

14-
use Doctrine\MongoDB\Collection;
14+
use \MongoDB\Collection;
1515
use Exception;
16+
use MongoDB\Database;
1617

1718
/**
1819
* @author Matthew Fitzgerald <matthewfitz@gmail.com>
@@ -42,7 +43,7 @@ class Statistics
4243
];
4344

4445
/**
45-
* @var \Doctrine\MongoDB\Collection
46+
* @var \MongoDB\Collection
4647
*/
4748
private $collection;
4849

@@ -56,16 +57,21 @@ class Statistics
5657
*/
5758
private $after = [];
5859

60+
public function setDatabase(Database $database)
61+
{
62+
$this->database = $database;
63+
}
64+
5965
/**
60-
* @param \Doctrine\MongoDB\Collection
66+
* @param \MongoDB\Collection
6167
*/
6268
public function setCollection(Collection $collection)
6369
{
6470
$this->collection = $collection;
6571
}
6672

6773
/**
68-
* @return \Doctrine\MongoDB\Collection
74+
* @return \MongoDB\Collection
6975
*/
7076
public function getCollection()
7177
{
@@ -115,21 +121,16 @@ public function getAfter()
115121
*/
116122
protected function getCollectionStats()
117123
{
118-
$database = $this->collection->getDatabase();
119-
$name = $this->collection->getName();
124+
$name = $this->collection->getCollectionName();
120125

121-
if (!$data = $database->command(['collStats' => $name])) {
126+
if (!$data = $this->database->command(['collStats' => $name])) {
122127
$message = sprintf(
123128
'Statistics not found for collection %s',
124129
$name
125130
);
126131
throw new Exception($message);
127132
}
128133

129-
if (isset($data['errmsg'])) {
130-
throw new Exception($data['errmsg']);
131-
}
132-
133134
return $data;
134135
}
135136
}

src/AntiMattr/MongoDB/Migrations/Configuration/Configuration.php

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,28 @@
1616
use AntiMattr\MongoDB\Migrations\Exception\UnknownVersionException;
1717
use AntiMattr\MongoDB\Migrations\OutputWriter;
1818
use AntiMattr\MongoDB\Migrations\Version;
19-
use Doctrine\MongoDB\Connection;
20-
use Doctrine\MongoDB\Database;
19+
use MongoDB\Client;
2120

2221
/**
2322
* @author Matthew Fitzgerald <matthewfitz@gmail.com>
2423
*/
2524
class Configuration
2625
{
2726
/**
28-
* @var \Doctrine\MongoDB\Collection
27+
* @var \MongoDB\Collection
2928
*/
3029
private $collection;
3130

3231
/**
33-
* @var \Doctrine\MongoDB\Connection
32+
* @var \MongoDB\Client
3433
*/
3534
private $connection;
3635

3736
/**
38-
* @var \Doctrine\MongoDB\Database
37+
* @var \MongoDB\Database
3938
*/
4039
private $database;
4140

42-
/**
43-
* @var \Doctrine\MongoDB\Connection
44-
*/
45-
private $migrationsDatabase;
46-
4741
/**
4842
* The migration database name to track versions in.
4943
*
@@ -109,10 +103,10 @@ class Configuration
109103
private $file;
110104

111105
/**
112-
* @param \Doctrine\MongoDB\Connection $connection
106+
* @param \MongoDB\Client $connection
113107
* @param \AntiMattr\MongoDB\Migrations\OutputWriter $outputWriter
114108
*/
115-
public function __construct(Connection $connection, OutputWriter $outputWriter = null)
109+
public function __construct(Client $connection, OutputWriter $outputWriter = null)
116110
{
117111
$this->connection = $connection;
118112
if (null === $outputWriter) {
@@ -156,7 +150,7 @@ public function getAvailableVersions()
156150
}
157151

158152
/**
159-
* @return \Doctrine\MongoDB\Collection
153+
* @return \MongoDB\Collection
160154
*/
161155
public function getCollection()
162156
{
@@ -170,17 +164,17 @@ public function getCollection()
170164
}
171165

172166
/**
173-
* @return \Doctrine\MongoDB\Connection
167+
* @return \MongoDB\Client
174168
*/
175169
public function getConnection()
176170
{
177171
return $this->connection;
178172
}
179173

180174
/**
181-
* @return \Doctrine\MongoDB\Database
175+
* @return \MongoDB\Database
182176
*/
183-
public function getDatabase(): ?Database
177+
public function getDatabase(): ?\MongoDB\Database
184178
{
185179
if (isset($this->database)) {
186180
return $this->database;
@@ -347,17 +341,18 @@ public function getMigratedTimestamp($version): int
347341
['v' => $version]
348342
);
349343

350-
if (!$cursor->count()) {
344+
$result = $cursor->toArray();
345+
if (!count($result)) {
351346
throw new UnknownVersionException($version);
352347
}
353348

354-
if ($cursor->count() > 1) {
349+
if (count($result) > 1) {
355350
throw new \DomainException(
356351
'Unexpected duplicate version records in the database'
357352
);
358353
}
359354

360-
$returnVersion = $cursor->getNext();
355+
$returnVersion = $result[0];
361356

362357
// Convert to normalised timestamp
363358
$ts = new Timestamp($returnVersion['t']);
@@ -408,9 +403,7 @@ public function getNumberOfExecutedMigrations()
408403
{
409404
$this->createMigrationCollection();
410405

411-
$cursor = $this->getCollection()->find();
412-
413-
return $cursor->count();
406+
return $this->getCollection()->countDocuments();
414407
}
415408

416409
/**
@@ -560,17 +553,16 @@ public function getCurrentVersion()
560553

561554
$cursor = $this->getCollection()
562555
->find(
563-
['v' => ['$in' => $migratedVersions]]
564-
)
565-
->sort(['v' => -1])
566-
->limit(1);
556+
['v' => ['$in' => $migratedVersions]],
557+
['sort' => ['v' => -1], 'limit' => 1]
558+
);
567559

568-
if (0 === $cursor->count()) {
560+
$versions = $cursor->toArray();
561+
if (0 === \count($versions)) {
569562
return '0';
570563
}
571564

572-
$version = $cursor->getNext();
573-
565+
$version = $versions[0];
574566
return $version['v'];
575567
}
576568

@@ -598,7 +590,7 @@ public function createMigrationCollection()
598590

599591
if (true !== $this->migrationCollectionCreated) {
600592
$collection = $this->getCollection();
601-
$collection->ensureIndex(['v' => -1], ['name' => 'version', 'unique' => true]);
593+
$collection->createIndex(['v' => -1], ['name' => 'version', 'unique' => true]);
602594
$this->migrationCollectionCreated = true;
603595
}
604596

src/AntiMattr/MongoDB/Migrations/Configuration/ConfigurationBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace AntiMattr\MongoDB\Migrations\Configuration;
1515

1616
use AntiMattr\MongoDB\Migrations\OutputWriter;
17-
use Doctrine\MongoDB\Connection;
17+
use MongoDB\Client;
1818
use Symfony\Component\Yaml\Yaml;
1919

2020
/**
@@ -23,7 +23,7 @@
2323
class ConfigurationBuilder
2424
{
2525
/**
26-
* @var \Doctrine\MongoDB\Connection
26+
* @var \MongoDB\Client
2727
*/
2828
private $connection;
2929

@@ -64,11 +64,11 @@ public static function create(): ConfigurationBuilder
6464
}
6565

6666
/**
67-
* @param Connection $connection
67+
* @param Client $connection
6868
*
6969
* @return ConfigurationBuilder
7070
*/
71-
public function setConnection(Connection $connection): ConfigurationBuilder
71+
public function setConnection(Client $connection): ConfigurationBuilder
7272
{
7373
$this->connection = $connection;
7474

src/AntiMattr/MongoDB/Migrations/Tools/Console/Command/AbstractCommand.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use AntiMattr\MongoDB\Migrations\Configuration\Configuration;
1515
use AntiMattr\MongoDB\Migrations\Configuration\ConfigurationBuilder;
1616
use AntiMattr\MongoDB\Migrations\OutputWriter;
17-
use Doctrine\MongoDB\Connection;
17+
use MongoDB\Client;
1818
use Symfony\Component\Console\Command\Command;
1919
use Symfony\Component\Console\Input\InputInterface;
2020
use Symfony\Component\Console\Input\InputOption;
@@ -98,15 +98,15 @@ protected function getMigrationConfiguration(
9898
/**
9999
* @param InputInterface $input
100100
*
101-
* @return Connection
101+
* @return Client
102102
*/
103-
protected function getDatabaseConnection(InputInterface $input): Connection
103+
protected function getDatabaseConnection(InputInterface $input): Client
104104
{
105105
// Default to document manager helper set
106106
if ($this->getApplication()->getHelperSet()->has('dm')) {
107107
return $this->getHelper('dm')
108108
->getDocumentManager()
109-
->getConnection();
109+
->getClient();
110110
}
111111

112112
// PHP array file
@@ -136,7 +136,7 @@ protected function getDatabaseConnection(InputInterface $input): Connection
136136
/**
137137
* @param array $params
138138
*
139-
* @return \Doctrine\MongoDB\Connection
139+
* @return \MongoDB\Client
140140
*/
141141
protected function createConnection($params)
142142
{
@@ -166,6 +166,6 @@ protected function createConnection($params)
166166
$options = $params['options'];
167167
}
168168

169-
return new Connection($server, $options);
169+
return new Client($server, $options);
170170
}
171171
}

0 commit comments

Comments
 (0)