Skip to content
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

code refactoring for clarity #428

Merged
merged 1 commit into from
Feb 6, 2016
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
26 changes: 11 additions & 15 deletions lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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(' <comment>>></comment> ' . $title . ': ' . str_repeat(' ', 50 - strlen($title)) . $value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

Expand Down Expand Up @@ -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)) {
Expand Down
9 changes: 3 additions & 6 deletions lib/Doctrine/DBAL/Migrations/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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);
}
Expand Down