Skip to content

Commit

Permalink
Merge branch 'fix-addsql'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeSimonson committed Oct 18, 2015
2 parents c4c5e16 + d393217 commit 089dbbc
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 29 deletions.
73 changes: 44 additions & 29 deletions lib/Doctrine/DBAL/Migrations/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,43 +264,20 @@ public function execute($direction, $dryRun = false, $timeAllQueries = false)
$this->migration->$direction($toSchema);
$this->addSql($fromSchema->getMigrateToSql($toSchema, $this->platform));

if (! $dryRun) {
if (!empty($this->sql)) {
foreach ($this->sql as $key => $query) {
$queryStart = microtime(true);

if ( ! isset($this->params[$key])) {
$this->outputWriter->write(' <comment>-></comment> ' . $query);
$this->connection->exec($query);
} else {
$this->outputWriter->write(sprintf(' <comment>-</comment> %s (with parameters)', $query));
$this->connection->executeQuery($query, $this->params[$key], $this->types[$key]);
}

$this->outputQueryTime($queryStart, $timeAllQueries);
}
} else {
$this->outputWriter->write(sprintf(
'<error>Migration %s was executed but did not result in any SQL statements.</error>',
$this->version
));
}
$this->executeRegisteredSql($dryRun, $timeAllQueries);

$this->state = self::STATE_POST;
$this->migration->{'post' . ucfirst($direction)}($toSchema);
$this->executeRegisteredSql($dryRun, $timeAllQueries);

if (! $dryRun) {
if ($direction === self::DIRECTION_UP) {
$this->markMigrated();
} else {
$this->markNotMigrated();
}

} else {
foreach ($this->sql as $query) {
$this->outputWriter->write(' <comment>-></comment> ' . $query);
}
}

$this->state = self::STATE_POST;
$this->migration->{'post' . ucfirst($direction)}($toSchema);

$migrationEnd = microtime(true);
$this->time = round($migrationEnd - $migrationStart, 2);
if ($direction === self::DIRECTION_UP) {
Expand Down Expand Up @@ -392,4 +369,42 @@ public function __toString()
{
return $this->version;
}

private function executeRegisteredSql($dryRun = false, $timeAllQueries = false)
{
if (! $dryRun) {
if (!empty($this->sql)) {
foreach ($this->sql as $key => $query) {
$queryStart = microtime(true);

if ( ! isset($this->params[$key])) {
$this->outputWriter->write(' <comment>-></comment> ' . $query);
$this->connection->exec($query);
} else {
$this->outputWriter->write(sprintf(' <comment>-</comment> %s (with parameters)', $query));
$this->connection->executeQuery($query, $this->params[$key], $this->types[$key]);
}

$this->outputQueryTime($queryStart, $timeAllQueries);
}
} else {
$this->outputWriter->write(sprintf(
'<error>Migration %s was executed but did not result in any SQL statements.</error>',
$this->version
));
}
} else {
foreach ($this->sql as $query) {
$this->outputWriter->write(' <comment>-></comment> ' . $query);
}
}
$this->resetRegisteredSql();
}

private function resetRegisteredSql()
{
$this->sql = [];
$this->params = [];
$this->types = [];
}
}
28 changes: 28 additions & 0 deletions tests/Doctrine/DBAL/Migrations/Tests/Functional/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,34 @@ public function testAddSql()
$this->assertFalse($schema->hasTable('test_add_sql_table'));
}

public function testAddSqlInPostUp()
{
$this->config->registerMigration(1, 'Doctrine\DBAL\Migrations\Tests\Stub\Functional\MigrateAddSqlPostAndPreUpAndDownTest');
$tableName = \Doctrine\DBAL\Migrations\Tests\Stub\Functional\MigrateAddSqlPostAndPreUpAndDownTest::TABLE_NAME;

$this->config->getConnection()->executeQuery(sprintf("CREATE TABLE IF NOT EXISTS %s (test INT)", $tableName));

$migration = new \Doctrine\DBAL\Migrations\Migration($this->config);
$migration->migrate(1);

$migrations = $this->config->getMigrations();
$this->assertTrue($migrations[1]->isMigrated());

$check = $this->config->getConnection()->fetchColumn("select SUM(test) as sum from $tableName");

$this->assertNotEmpty($check);
$this->assertEquals(6, $check);

$migration->migrate(0);
$this->assertFalse($migrations[1]->isMigrated());
$check = $this->config->getConnection()->fetchColumn("select SUM(test) as sum from $tableName");
$this->assertNotEmpty($check);
$this->assertEquals(21, $check);


$this->config->getConnection()->executeQuery(sprintf("DROP TABLE %s ", $tableName));
}

public function testVersionInDatabaseWithoutRegisteredMigrationStillMigrates()
{
$this->config->registerMigration(1, 'Doctrine\DBAL\Migrations\Tests\Stub\Functional\MigrateAddSqlTest');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Doctrine\DBAL\Migrations\Tests\Stub\Functional;

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

class MigrateAddSqlPostAndPreUpAndDownTest extends AbstractMigration
{
const TABLE_NAME = 'test_add_sql_post_up_table';

public function preUp(Schema $schema)
{
$this->addSql(
sprintf("INSERT INTO %s (test) values (?)", self::TABLE_NAME),
[1]
);
}

public function up(Schema $schema)
{
$this->addSql(
sprintf("INSERT INTO %s (test) values (?)", self::TABLE_NAME),
[2]
);
}

public function postUp(Schema $schema)
{
$this->addSql(
sprintf("INSERT INTO %s (test) values (?)", self::TABLE_NAME),
[3]
);
}

public function preDown(Schema $schema)
{
$this->addSql(
sprintf("INSERT INTO %s (test) values (?)", self::TABLE_NAME),
[4]
);
}

public function down(Schema $schema)
{
$this->addSql(
sprintf("INSERT INTO %s (test) values (?)", self::TABLE_NAME),
[5]
);
}

public function postDown(Schema $schema)
{
$this->addSql(
sprintf("INSERT INTO %s (test) values (?)", self::TABLE_NAME),
[6]
);
}
}

0 comments on commit 089dbbc

Please sign in to comment.