Skip to content

Commit

Permalink
Add Parameters to the Output of Dry Run Migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisguitarguy committed Jul 5, 2016
1 parent 558e8aa commit 65abc8b
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions lib/Doctrine/DBAL/Migrations/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace Doctrine\DBAL\Migrations;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\DBAL\Migrations\Provider\LazySchemaDiffProvider;
use Doctrine\DBAL\Migrations\Provider\SchemaDiffProvider;
Expand Down Expand Up @@ -419,9 +420,54 @@ private function executeRegisteredSql($dryRun = false, $timeAllQueries = false)
));
}
} else {
foreach ($this->sql as $query) {
$this->outputWriter->write(' <comment>-></comment> ' . $query);
foreach ($this->sql as $idx => $query) {
$this->outputSqlQuery($idx, $query);
}
}
}

/**
* Outputs a SQL query via the `OutputWriter`.
*
* @param int $idx The SQL query index. Used to look up params.
* @param string $query the query to output
* @return void
*/
private function outputSqlQuery($idx, $query)
{
$params = $this->formatParamsForOutput(
isset($this->params[$idx]) ? $this->params[$idx] : [],
isset($this->types[$idx]) ? $this->types[$idx] : []
);

$this->outputWriter->write(rtrim(sprintf(
' <comment>-></comment> %s %s',
$query,
$params
)));
}

/**
* Formats a set of sql parameters for output with dry run.
*
* @param $params The query parameters
* @param $types The types of the query params. Default type is a string
* @return string|null a string of the parameters present.
*/
private function formatParamsForOutput(array $params, array $types)
{
if (empty($params)) {
return '';
}

$platform = $this->connection->getDatabasePlatform();
$out = [];
foreach ($params as $key => $value) {
$type = isset($types[$key]) ? $types[$key] : 'string';
$outval = Type::getType($type)->convertToDatabaseValue($value, $platform);
$out[] = is_string($key) ? sprintf(':%s => %s', $key, $outval) : $outval;
}

return sprintf('with parameters (%s)', implode(', ', $out));
}
}

0 comments on commit 65abc8b

Please sign in to comment.