From 73a09ec8845745f26384b8c0939708dbd19dfdae Mon Sep 17 00:00:00 2001 From: Illya Usenko Date: Mon, 29 Jul 2019 17:18:09 +0300 Subject: [PATCH 1/5] Updates to use migration with doctrine 2. Based of [changes](https://github.com/doesntmattr/mongodb-migrations/pull/41) --- .../MongoDB/Migrations/AbstractMigration.php | 8 +-- .../Migrations/Collection/Statistics.php | 23 ++++---- .../Configuration/Configuration.php | 52 ++++++++----------- .../Configuration/ConfigurationBuilder.php | 8 +-- .../Tools/Console/Command/AbstractCommand.php | 12 ++--- .../Tools/Console/Command/GenerateCommand.php | 2 +- src/AntiMattr/MongoDB/Migrations/Version.php | 37 +++++-------- 7 files changed, 62 insertions(+), 80 deletions(-) diff --git a/src/AntiMattr/MongoDB/Migrations/AbstractMigration.php b/src/AntiMattr/MongoDB/Migrations/AbstractMigration.php index 330bca3..3ec16f7 100644 --- a/src/AntiMattr/MongoDB/Migrations/AbstractMigration.php +++ b/src/AntiMattr/MongoDB/Migrations/AbstractMigration.php @@ -14,8 +14,8 @@ use AntiMattr\MongoDB\Migrations\Exception\AbortException; use AntiMattr\MongoDB\Migrations\Exception\IrreversibleException; use AntiMattr\MongoDB\Migrations\Exception\SkipException; -use Doctrine\MongoDB\Collection; -use Doctrine\MongoDB\Database; +use \MongoDB\Collection; +use \MongoDB\Database; /** * @author Matthew Fitzgerald @@ -56,7 +56,7 @@ abstract public function up(Database $db); abstract public function down(Database $db); /** - * @param \Doctrine\MongoDB\Collection + * @param \MongoDB\Collection */ protected function analyze(Collection $collection) { @@ -64,7 +64,7 @@ protected function analyze(Collection $collection) } /** - * @param \Doctrine\MongoDB\Database + * @param \MongoDB\Database * @param string $filename */ protected function executeScript(Database $db, $filename) diff --git a/src/AntiMattr/MongoDB/Migrations/Collection/Statistics.php b/src/AntiMattr/MongoDB/Migrations/Collection/Statistics.php index 59accf4..4587f0d 100644 --- a/src/AntiMattr/MongoDB/Migrations/Collection/Statistics.php +++ b/src/AntiMattr/MongoDB/Migrations/Collection/Statistics.php @@ -11,8 +11,9 @@ namespace AntiMattr\MongoDB\Migrations\Collection; -use Doctrine\MongoDB\Collection; +use \MongoDB\Collection; use Exception; +use MongoDB\Database; /** * @author Matthew Fitzgerald @@ -42,7 +43,7 @@ class Statistics ]; /** - * @var \Doctrine\MongoDB\Collection + * @var \MongoDB\Collection */ private $collection; @@ -56,8 +57,13 @@ class Statistics */ private $after = []; + public function setDatabase(Database $database) + { + $this->database = $database; + } + /** - * @param \Doctrine\MongoDB\Collection + * @param \MongoDB\Collection */ public function setCollection(Collection $collection) { @@ -65,7 +71,7 @@ public function setCollection(Collection $collection) } /** - * @return \Doctrine\MongoDB\Collection + * @return \MongoDB\Collection */ public function getCollection() { @@ -115,10 +121,9 @@ public function getAfter() */ protected function getCollectionStats() { - $database = $this->collection->getDatabase(); - $name = $this->collection->getName(); + $name = $this->collection->getCollectionName(); - if (!$data = $database->command(['collStats' => $name])) { + if (!$data = $this->database->command(['collStats' => $name])) { $message = sprintf( 'Statistics not found for collection %s', $name @@ -126,10 +131,6 @@ protected function getCollectionStats() throw new Exception($message); } - if (isset($data['errmsg'])) { - throw new Exception($data['errmsg']); - } - return $data; } } diff --git a/src/AntiMattr/MongoDB/Migrations/Configuration/Configuration.php b/src/AntiMattr/MongoDB/Migrations/Configuration/Configuration.php index 0f4533c..92dac87 100644 --- a/src/AntiMattr/MongoDB/Migrations/Configuration/Configuration.php +++ b/src/AntiMattr/MongoDB/Migrations/Configuration/Configuration.php @@ -16,8 +16,7 @@ use AntiMattr\MongoDB\Migrations\Exception\UnknownVersionException; use AntiMattr\MongoDB\Migrations\OutputWriter; use AntiMattr\MongoDB\Migrations\Version; -use Doctrine\MongoDB\Connection; -use Doctrine\MongoDB\Database; +use MongoDB\Client; /** * @author Matthew Fitzgerald @@ -25,25 +24,20 @@ class Configuration { /** - * @var \Doctrine\MongoDB\Collection + * @var \MongoDB\Collection */ private $collection; /** - * @var \Doctrine\MongoDB\Connection + * @var \MongoDB\Client */ private $connection; /** - * @var \Doctrine\MongoDB\Database + * @var \MongoDB\Database */ private $database; - /** - * @var \Doctrine\MongoDB\Connection - */ - private $migrationsDatabase; - /** * The migration database name to track versions in. * @@ -109,10 +103,10 @@ class Configuration private $file; /** - * @param \Doctrine\MongoDB\Connection $connection + * @param \MongoDB\Client $connection * @param \AntiMattr\MongoDB\Migrations\OutputWriter $outputWriter */ - public function __construct(Connection $connection, OutputWriter $outputWriter = null) + public function __construct(Client $connection, OutputWriter $outputWriter = null) { $this->connection = $connection; if (null === $outputWriter) { @@ -156,7 +150,7 @@ public function getAvailableVersions() } /** - * @return \Doctrine\MongoDB\Collection + * @return \MongoDB\Collection */ public function getCollection() { @@ -170,7 +164,7 @@ public function getCollection() } /** - * @return \Doctrine\MongoDB\Connection + * @return \MongoDB\Client */ public function getConnection() { @@ -178,9 +172,9 @@ public function getConnection() } /** - * @return \Doctrine\MongoDB\Database + * @return \MongoDB\Database */ - public function getDatabase(): ?Database + public function getDatabase(): ?\MongoDB\Database { if (isset($this->database)) { return $this->database; @@ -347,17 +341,18 @@ public function getMigratedTimestamp($version): int ['v' => $version] ); - if (!$cursor->count()) { + $result = $cursor->toArray(); + if (!count($result)) { throw new UnknownVersionException($version); } - if ($cursor->count() > 1) { + if (count($result) > 1) { throw new \DomainException( 'Unexpected duplicate version records in the database' ); } - $returnVersion = $cursor->getNext(); + $returnVersion = $result[0]; // Convert to normalised timestamp $ts = new Timestamp($returnVersion['t']); @@ -408,9 +403,7 @@ public function getNumberOfExecutedMigrations() { $this->createMigrationCollection(); - $cursor = $this->getCollection()->find(); - - return $cursor->count(); + return $this->getCollection()->countDocuments(); } /** @@ -560,17 +553,16 @@ public function getCurrentVersion() $cursor = $this->getCollection() ->find( - ['v' => ['$in' => $migratedVersions]] - ) - ->sort(['v' => -1]) - ->limit(1); + ['v' => ['$in' => $migratedVersions]], + ['sort' => ['v' => -1], 'limit' => 1] + ); - if (0 === $cursor->count()) { + $versions = $cursor->toArray(); + if (0 === \count($versions)) { return '0'; } - $version = $cursor->getNext(); - + $version = $versions[0]; return $version['v']; } @@ -598,7 +590,7 @@ public function createMigrationCollection() if (true !== $this->migrationCollectionCreated) { $collection = $this->getCollection(); - $collection->ensureIndex(['v' => -1], ['name' => 'version', 'unique' => true]); + $collection->createIndex(['v' => -1], ['name' => 'version', 'unique' => true]); $this->migrationCollectionCreated = true; } diff --git a/src/AntiMattr/MongoDB/Migrations/Configuration/ConfigurationBuilder.php b/src/AntiMattr/MongoDB/Migrations/Configuration/ConfigurationBuilder.php index bdf5463..dec519f 100644 --- a/src/AntiMattr/MongoDB/Migrations/Configuration/ConfigurationBuilder.php +++ b/src/AntiMattr/MongoDB/Migrations/Configuration/ConfigurationBuilder.php @@ -14,7 +14,7 @@ namespace AntiMattr\MongoDB\Migrations\Configuration; use AntiMattr\MongoDB\Migrations\OutputWriter; -use Doctrine\MongoDB\Connection; +use MongoDB\Client; use Symfony\Component\Yaml\Yaml; /** @@ -23,7 +23,7 @@ class ConfigurationBuilder { /** - * @var \Doctrine\MongoDB\Connection + * @var \MongoDB\Client */ private $connection; @@ -64,11 +64,11 @@ public static function create(): ConfigurationBuilder } /** - * @param Connection $connection + * @param Client $connection * * @return ConfigurationBuilder */ - public function setConnection(Connection $connection): ConfigurationBuilder + public function setConnection(Client $connection): ConfigurationBuilder { $this->connection = $connection; diff --git a/src/AntiMattr/MongoDB/Migrations/Tools/Console/Command/AbstractCommand.php b/src/AntiMattr/MongoDB/Migrations/Tools/Console/Command/AbstractCommand.php index 03823f1..098b6e9 100644 --- a/src/AntiMattr/MongoDB/Migrations/Tools/Console/Command/AbstractCommand.php +++ b/src/AntiMattr/MongoDB/Migrations/Tools/Console/Command/AbstractCommand.php @@ -14,7 +14,7 @@ use AntiMattr\MongoDB\Migrations\Configuration\Configuration; use AntiMattr\MongoDB\Migrations\Configuration\ConfigurationBuilder; use AntiMattr\MongoDB\Migrations\OutputWriter; -use Doctrine\MongoDB\Connection; +use MongoDB\Client; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -98,15 +98,15 @@ protected function getMigrationConfiguration( /** * @param InputInterface $input * - * @return Connection + * @return Client */ - protected function getDatabaseConnection(InputInterface $input): Connection + protected function getDatabaseConnection(InputInterface $input): Client { // Default to document manager helper set if ($this->getApplication()->getHelperSet()->has('dm')) { return $this->getHelper('dm') ->getDocumentManager() - ->getConnection(); + ->getClient(); } // PHP array file @@ -136,7 +136,7 @@ protected function getDatabaseConnection(InputInterface $input): Connection /** * @param array $params * - * @return \Doctrine\MongoDB\Connection + * @return \MongoDB\Client */ protected function createConnection($params) { @@ -166,6 +166,6 @@ protected function createConnection($params) $options = $params['options']; } - return new Connection($server, $options); + return new Client($server, $options); } } diff --git a/src/AntiMattr/MongoDB/Migrations/Tools/Console/Command/GenerateCommand.php b/src/AntiMattr/MongoDB/Migrations/Tools/Console/Command/GenerateCommand.php index 67d423b..c4a332f 100644 --- a/src/AntiMattr/MongoDB/Migrations/Tools/Console/Command/GenerateCommand.php +++ b/src/AntiMattr/MongoDB/Migrations/Tools/Console/Command/GenerateCommand.php @@ -29,7 +29,7 @@ class GenerateCommand extends AbstractCommand namespace ; use AntiMattr\MongoDB\Migrations\AbstractMigration; -use Doctrine\MongoDB\Database; +use MongoDB\Database; /** * Auto-generated Migration: Please modify to your needs! diff --git a/src/AntiMattr/MongoDB/Migrations/Version.php b/src/AntiMattr/MongoDB/Migrations/Version.php index febd3d9..f522c4b 100644 --- a/src/AntiMattr/MongoDB/Migrations/Version.php +++ b/src/AntiMattr/MongoDB/Migrations/Version.php @@ -15,8 +15,8 @@ use AntiMattr\MongoDB\Migrations\Configuration\Configuration; use AntiMattr\MongoDB\Migrations\Exception\SkipException; use AntiMattr\MongoDB\Migrations\Exception\AbortException; -use Doctrine\MongoDB\Collection; -use Doctrine\MongoDB\Database; +use \MongoDB\Collection; +use \MongoDB\Database; use Exception; use MongoDB\BSON\UTCDateTime; @@ -41,12 +41,7 @@ class Version private $configuration; /** - * @var \Doctrine\MongoDB\Connection - */ - private $connection; - - /** - * @var \Doctrine\MongoDB\Database + * @var \MongoDB\Database */ private $db; @@ -87,7 +82,6 @@ public function __construct(Configuration $configuration, $version, $class) $this->configuration = $configuration; $this->outputWriter = $configuration->getOutputWriter(); $this->class = $class; - $this->connection = $configuration->getConnection(); $this->db = $configuration->getDatabase(); $this->migration = $this->createMigration(); $this->version = $version; @@ -150,13 +144,14 @@ public function getVersion() } /** - * @param \Doctrine\MongoDB\Collection + * @param \MongoDB\Collection */ public function analyze(Collection $collection) { $statistics = $this->createStatistics(); + $statistics->setDatabase($this->db); $statistics->setCollection($collection); - $name = $collection->getName(); + $name = $collection->getCollectionName(); $this->statistics[$name] = $statistics; try { @@ -249,7 +244,7 @@ public function execute($direction, $replay = false) } /** - * @param \Doctrine\MongoDB\Database + * @param \MongoDB\Database * @param string $file * * @return array @@ -279,13 +274,7 @@ public function executeScript(Database $db, $file) throw $e; } - $result = $db->command(['$eval' => $js, 'nolock' => true]); - - if (isset($result['errmsg'])) { - throw new \Exception($result['errmsg'], isset($result['errno']) ? $result['errno'] : null); - } - - return $result; + return $db->command(['$eval' => $js, 'nolock' => true]); } /** @@ -305,9 +294,9 @@ public function markMigrated($replay = false) // If the user asked for a 'replay' of a migration that // has not been run, it will be inserted anew $options = ['upsert' => true]; - $collection->update($query, $document, $options); + $collection->updateOne($query, $document, $options); } else { - $collection->insert($document); + $collection->insertOne($document); } } @@ -315,7 +304,7 @@ public function markNotMigrated() { $this->configuration->createMigrationCollection(); $collection = $this->configuration->getCollection(); - $collection->remove(['v' => $this->version]); + $collection->deleteOne(['v' => $this->version]); } protected function updateStatisticsAfter() @@ -323,7 +312,7 @@ protected function updateStatisticsAfter() foreach ($this->statistics as $name => $statistic) { try { $statistic->updateAfter(); - $name = $statistic->getCollection()->getName(); + $name = $statistic->getCollection()->getCollectionName(); $this->statistics[$name] = $statistic; } catch (\Exception $e) { $message = sprintf(' Warning during %s: %s', @@ -375,7 +364,7 @@ private function summarizeStatistics() public function __toString() { - return $this->version; + return (string) $this->version; } /** From 10184c3e5689c2819e99f0c51d79843515bd222b Mon Sep 17 00:00:00 2001 From: Illya Usenko Date: Mon, 29 Jul 2019 17:33:31 +0300 Subject: [PATCH 2/5] Updates repo name deseretdigital/mongodb-migrations --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 59734d0..3006253 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "doesntmattr/mongodb-migrations", + "name": "deseretdigital/mongodb-migrations", "type": "library", "description": "Managed Database Migrations for MongoDB", "keywords": ["doesntmattr", "antimattr", "database", "doctrine", "migration", "mongodb"], From 4defb7877061541a809e31b3dd6580158c1d3be5 Mon Sep 17 00:00:00 2001 From: Illya Usenko Date: Wed, 31 Jul 2019 22:40:08 +0300 Subject: [PATCH 3/5] Adds doctrine to migrations. --- .../MongoDB/Migrations/AbstractMigration.php | 11 ++++++++ .../Configuration/Configuration.php | 26 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/AntiMattr/MongoDB/Migrations/AbstractMigration.php b/src/AntiMattr/MongoDB/Migrations/AbstractMigration.php index 3ec16f7..9ac4c5a 100644 --- a/src/AntiMattr/MongoDB/Migrations/AbstractMigration.php +++ b/src/AntiMattr/MongoDB/Migrations/AbstractMigration.php @@ -16,6 +16,7 @@ use AntiMattr\MongoDB\Migrations\Exception\SkipException; use \MongoDB\Collection; use \MongoDB\Database; +use Doctrine\ODM\MongoDB\DocumentManager; /** * @author Matthew Fitzgerald @@ -142,6 +143,16 @@ public function skipIf($condition, $message = '') } } + /** + * Return document manager or null + * + * @return DocumentManager|null + */ + public function getDocumentManager() : ?DocumentManager + { + return $this->configuration->getDocumentManager(); + } + public function preUp(Database $db) { } diff --git a/src/AntiMattr/MongoDB/Migrations/Configuration/Configuration.php b/src/AntiMattr/MongoDB/Migrations/Configuration/Configuration.php index 92dac87..f0f04b4 100644 --- a/src/AntiMattr/MongoDB/Migrations/Configuration/Configuration.php +++ b/src/AntiMattr/MongoDB/Migrations/Configuration/Configuration.php @@ -17,6 +17,7 @@ use AntiMattr\MongoDB\Migrations\OutputWriter; use AntiMattr\MongoDB\Migrations\Version; use MongoDB\Client; +use Doctrine\ODM\MongoDB\DocumentManager; /** * @author Matthew Fitzgerald @@ -38,6 +39,11 @@ class Configuration */ private $database; + /** + * @var DocumentManager + */ + private $documentManager; + /** * The migration database name to track versions in. * @@ -185,6 +191,26 @@ public function getDatabase(): ?\MongoDB\Database return $this->database; } + /** + * Return document manager + * + * @return DocumentManager + */ + public function getDocumentManager(): ?DocumentManager + { + return $this->documentManager; + } + + /** + * Set document manager + * + * @param DocumentManager $documentManager + */ + public function setDocumentManager(DocumentManager $documentManager) + { + $this->documentManager = $documentManager; + } + /** * Get the array of registered migration versions. * From 4cac2a445565e74a918282b7085ab0735bac0225 Mon Sep 17 00:00:00 2001 From: Nate Christensen Date: Thu, 1 Aug 2019 14:04:17 -0600 Subject: [PATCH 4/5] Remove dev-master alias --- composer.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/composer.json b/composer.json index 3006253..a72b451 100644 --- a/composer.json +++ b/composer.json @@ -36,11 +36,6 @@ ] } }, - "extra": { - "branch-alias": { - "dev-master": "2.0.x-dev" - } - }, "archive": { "exclude": ["bin", "demo", "tests", "*phpunit.xml"] } From 37030738f183577b98914ee3a82ca4a64e7943a2 Mon Sep 17 00:00:00 2001 From: Justin Carmony Date: Wed, 14 Feb 2024 11:52:16 -0700 Subject: [PATCH 5/5] automated: Add CODEOWNERS file for team @deseretdigital/architecture --- CODEOWNERS | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 CODEOWNERS diff --git a/CODEOWNERS b/CODEOWNERS new file mode 100644 index 0000000..52ea92b --- /dev/null +++ b/CODEOWNERS @@ -0,0 +1,9 @@ +# FILE GENERATRED BY gendoc-update-codeowners-and-properties + +# This file is used to define code owners for the repository. +# The format is: +# +# where scope is the directory or file to which the team is assigned. +# For example: +# * @team +* @deseretdigital/architecture \ No newline at end of file