From fd3157c2890062e7da03c4fee83db060173fc760 Mon Sep 17 00:00:00 2001 From: ReenExe Date: Wed, 3 Feb 2016 11:58:15 +0200 Subject: [PATCH] after review --- .../Configuration/Configuration.php | 26 ++++++-------- .../Tools/Console/Command/AbstractCommand.php | 34 +++++++++---------- .../Tools/Console/Command/StatusCommand.php | 2 +- .../Tools/Console/Command/VersionCommand.php | 8 +++-- lib/Doctrine/DBAL/Migrations/Version.php | 9 ++--- 5 files changed, 36 insertions(+), 43 deletions(-) diff --git a/lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php b/lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php index eee5a53fe2..9900dfbf9e 100644 --- a/lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php +++ b/lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php @@ -506,12 +506,8 @@ public function getMigratedVersions() $this->createMigrationTable(); $ret = $this->connection->fetchAll("SELECT " . $this->migrationsColumnName . " FROM " . $this->migrationsTableName); - $versions = []; - foreach ($ret as $version) { - $versions[] = current($version); - } - return $versions; + return array_map('current', $ret); } /** @@ -708,22 +704,22 @@ public function createMigrationTable() return false; } - if (!$this->connection->getSchemaManager()->tablesExist([$this->migrationsTableName])) { - $columns = [ - $this->migrationsColumnName => new Column($this->migrationsColumnName, Type::getType('string'), ['length' => 255]), - ]; - $table = new Table($this->migrationsTableName, $columns); - $table->setPrimaryKey([$this->migrationsColumnName]); - $this->connection->getSchemaManager()->createTable($table); - + if ($this->connection->getSchemaManager()->tablesExist([$this->migrationsTableName])) { $this->migrationTableCreated = true; - return true; + return false; } + $columns = [ + $this->migrationsColumnName => new Column($this->migrationsColumnName, Type::getType('string'), ['length' => 255]), + ]; + $table = new Table($this->migrationsTableName, $columns); + $table->setPrimaryKey([$this->migrationsColumnName]); + $this->connection->getSchemaManager()->createTable($table); + $this->migrationTableCreated = true; - return false; + return true; } /** diff --git a/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/AbstractCommand.php b/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/AbstractCommand.php index 3d44c83b4b..3c1cff17bd 100644 --- a/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/AbstractCommand.php +++ b/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/AbstractCommand.php @@ -159,27 +159,25 @@ private function getOutputWriter(OutputInterface $output) */ private function getConnection(InputInterface $input) { - if (!$this->connection) { - $chainLoader = new ConnectionConfigurationChainLoader( - [ - new ArrayConnectionConfigurationLoader($input->getOption('db-configuration')), - new ArrayConnectionConfigurationLoader('migrations-db.php'), - new ConnectionHelperLoader($this->getHelperSet(), 'connection'), - new ConnectionConfigurationLoader($this->configuration), - ] - ); - $connection = $chainLoader->chosen(); - - if ($connection) { - $this->connection = $connection; - - return $this->connection; - } + if ($this->connection) { + return $this->connection; + } - throw new \InvalidArgumentException('You have to specify a --db-configuration file or pass a Database Connection as a dependency to the Migrations.'); + $chainLoader = new ConnectionConfigurationChainLoader( + [ + new ArrayConnectionConfigurationLoader($input->getOption('db-configuration')), + new ArrayConnectionConfigurationLoader('migrations-db.php'), + new ConnectionHelperLoader($this->getHelperSet(), 'connection'), + new ConnectionConfigurationLoader($this->configuration), + ] + ); + $connection = $chainLoader->chosen(); + + if ($connection) { + return $this->connection = $connection; } - return $this->connection; + throw new \InvalidArgumentException('You have to specify a --db-configuration file or pass a Database Connection as a dependency to the Migrations.'); } } diff --git a/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/StatusCommand.php b/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/StatusCommand.php index 8ee7911a56..09badded21 100644 --- a/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/StatusCommand.php +++ b/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/StatusCommand.php @@ -90,7 +90,7 @@ public function execute(InputInterface $input, OutputInterface $output) } } - private function writeStatusInfosLineAligned($output, $title, $value) + private function writeStatusInfosLineAligned(OutputInterface $output, $title, $value) { $output->writeln(' >> ' . $title . ': ' . str_repeat(' ', 50 - strlen($title)) . $value); } diff --git a/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/VersionCommand.php b/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/VersionCommand.php index 4a6625c7d5..bb14792502 100644 --- a/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/VersionCommand.php +++ b/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/VersionCommand.php @@ -124,7 +124,9 @@ private function markVersions(InputInterface $input) if ($allOption && ($rangeFromOption !== null || $rangeToOption !== null)) { throw new \InvalidArgumentException('Options --all and --range-to/--range-from both used. You should use only one of them.'); - } elseif ($rangeFromOption !== null ^ $rangeToOption !== null) { + } + + if ($rangeFromOption !== null ^ $rangeToOption !== null) { throw new \InvalidArgumentException('Options --range-to and --range-from should be used together.'); } @@ -153,17 +155,17 @@ private function mark($version, $all = false) $version = $this->configuration->getVersion($version); if ($this->markMigrated && $this->configuration->hasVersionMigrated($version)) { - $marked = true; if (! $all) { throw new \InvalidArgumentException(sprintf('The version "%s" already exists in the version table.', $version)); } + $marked = true; } if ( ! $this->markMigrated && ! $this->configuration->hasVersionMigrated($version)) { - $marked = false; if (! $all) { throw new \InvalidArgumentException(sprintf('The version "%s" does not exists in the version table.', $version)); } + $marked = false; } if ( ! isset($marked)) { diff --git a/lib/Doctrine/DBAL/Migrations/Version.php b/lib/Doctrine/DBAL/Migrations/Version.php index 69023fa7d3..0dfdb3121c 100644 --- a/lib/Doctrine/DBAL/Migrations/Version.php +++ b/lib/Doctrine/DBAL/Migrations/Version.php @@ -158,11 +158,8 @@ public function markMigrated() private function markVersion($direction) { - if ($direction == 'up') { - $action = 'insert'; - } else { - $action = 'delete'; - } + $action = $direction === 'up' ? 'insert' : 'delete'; + $this->configuration->createMigrationTable(); $this->connection->$action( $this->configuration->getMigrationsTableName(), @@ -187,7 +184,7 @@ public function addSql($sql, array $params = [], array $types = []) if (is_array($sql)) { foreach ($sql as $key => $query) { $this->sql[] = $query; - if (isset($params[$key]) && !empty($params[$key])) { + if (!empty($params[$key])) { $queryTypes = isset($types[$key]) ? $types[$key] : []; $this->addQueryParams($params[$key], $queryTypes); }