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

Add Parameters to the Output of Dry Run Migrations #477

Merged
merged 3 commits into from
Jul 5, 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
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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Doctrine\DBAL\Migrations\Tests\Stub;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

class VersionDryRunNamedParams extends AbstractMigration
{
public function down(Schema $schema)
{
}

public function up(Schema $schema)
{
$this->addSql('INSERT INTO test VALUES (:one, :two)', [
'one' => 'one',
'two' => 'two',
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Doctrine\DBAL\Migrations\Tests\Stub;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

class VersionDryRunQuestionMarkParams extends AbstractMigration
{
public function down(Schema $schema)
{
}

public function up(Schema $schema)
{
$this->addSql('INSERT INTO test VALUES (?, ?)', ['one', 'two']);
}
}
27 changes: 27 additions & 0 deletions tests/Doctrine/DBAL/Migrations/Tests/Stub/VersionDryRunTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Doctrine\DBAL\Migrations\Tests\Stub;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

class VersionDryRunTypes extends AbstractMigration
{
private $value;
private $type;

public function setParam($value, $type)
{
$this->value = $value;
$this->type = $type;
}

public function down(Schema $schema)
{
}

public function up(Schema $schema)
{
$this->addSql('INSERT INTO test VALUES (?)', [$this->value], [$this->type]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Doctrine\DBAL\Migrations\Tests\Stub;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

class VersionDryRunWithoutParams extends AbstractMigration
{
public function down(Schema $schema)
{
}

public function up(Schema $schema)
{
$this->addSql('SELECT 1 WHERE 1');
}
}
97 changes: 96 additions & 1 deletion tests/Doctrine/DBAL/Migrations/Tests/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ function realpath($path)
use Doctrine\DBAL\Migrations\Tests\Stub\VersionDummy;
use Doctrine\DBAL\Migrations\Tests\Stub\VersionDummyDescription;
use Doctrine\DBAL\Migrations\Tests\Stub\VersionDummyException;
use Doctrine\DBAL\Migrations\Tests\Stub\VersionDryRunNamedParams;
use Doctrine\DBAL\Migrations\Tests\Stub\VersionDryRunTypes;
use Doctrine\DBAL\Migrations\Tests\Stub\VersionDryRunQuestionMarkParams;
use Doctrine\DBAL\Migrations\Tests\Stub\VersionDryRunWithoutParams;
use Doctrine\DBAL\Migrations\Tests\Stub\VersionOutputSql;
use Doctrine\DBAL\Migrations\Tests\Stub\VersionOutputSqlWithParam;
use Doctrine\DBAL\Migrations\Version;
Expand Down Expand Up @@ -376,5 +380,96 @@ public function testWriteSqlFileShouldUseStandardCommentMarkerInSql()
$sqlMigrationFile = current($sqlFilesDir->getChildren());
$this->assertInstanceOf(vfsStreamFile::class, $sqlMigrationFile);
$this->assertNotRegExp('/^\s*#/m', $sqlMigrationFile->getContent());
}
}

public function testDryRunCausesSqlToBeOutputViaTheOutputWriter()
{
$messages = [];
$ow = new OutputWriter(function ($msg) use (&$messages) {
$messages[] = trim($msg);
});
$config = new Configuration($this->getSqliteConnection(), $ow);
$version = new Version(
$config,
'006',
VersionDryRunWithoutParams::class
);

$version->execute(Version::DIRECTION_UP, true);

$this->assertCount(3, $messages, 'should have written three messages (header, footer, 1 SQL statement)');
$this->assertContains('SELECT 1 WHERE 1', $messages[1]);
}

public function testDryRunWithQuestionMarkedParamsOutputsParamsWithSqlStatement()
{
$messages = [];
$ow = new OutputWriter(function ($msg) use (&$messages) {
$messages[] = trim($msg);
});
$config = new Configuration($this->getSqliteConnection(), $ow);
$version = new Version(
$config,
'006',
VersionDryRunQuestionMarkParams::class
);

$version->execute(Version::DIRECTION_UP, true);

$this->assertCount(3, $messages, 'should have written three messages (header, footer, 1 SQL statement)');
$this->assertContains('INSERT INTO test VALUES (?, ?)', $messages[1]);
$this->assertContains('with parameters (one, two)', $messages[1]);
}

public function testDryRunWithNamedParametersOutputsParamsAndNamesWithSqlStatement()
{
$messages = [];
$ow = new OutputWriter(function ($msg) use (&$messages) {
$messages[] = trim($msg);
});
$config = new Configuration($this->getSqliteConnection(), $ow);
$version = new Version(
$config,
'006',
VersionDryRunNamedParams::class
);

$version->execute(Version::DIRECTION_UP, true);

$this->assertCount(3, $messages, 'should have written three messages (header, footer, 1 SQL statement)');
$this->assertContains('INSERT INTO test VALUES (:one, :two)', $messages[1]);
$this->assertContains('with parameters (:one => one, :two => two)', $messages[1]);
}

public static function dryRunTypes()
{
return [
'datetime' => [new \DateTime('2016-07-05 01:00:00'), 'datetime', '2016-07-05 01:00:00'],
'array' => [['one' => 'two'], 'array', serialize(['one' => 'two'])],
];
}

/**
* @dataProvider dryRunTypes
*/
public function testDryRunWithParametersOfComplexTypesCorrectFormatsParameters($value, $type, $output)
{
$messages = [];
$ow = new OutputWriter(function ($msg) use (&$messages) {
$messages[] = trim($msg);
});
$config = new Configuration($this->getSqliteConnection(), $ow);
$version = new Version(
$config,
'006',
VersionDryRunTypes::class
);
$version->getMigration()->setParam($value, $type);

$version->execute(Version::DIRECTION_UP, true);

$this->assertCount(3, $messages, 'should have written three messages (header, footer, 1 SQL statement)');
$this->assertContains('INSERT INTO test VALUES (?)', $messages[1]);
$this->assertContains(sprintf('with parameters (%s)', $output), $messages[1]);
}
}