diff --git a/lib/Doctrine/DBAL/Migrations/Version.php b/lib/Doctrine/DBAL/Migrations/Version.php
index 45e086dc4a..24335650fc 100644
--- a/lib/Doctrine/DBAL/Migrations/Version.php
+++ b/lib/Doctrine/DBAL/Migrations/Version.php
@@ -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(' -> ' . $query);
- $this->connection->exec($query);
- } else {
- $this->outputWriter->write(sprintf(' - %s (with parameters)', $query));
- $this->connection->executeQuery($query, $this->params[$key], $this->types[$key]);
- }
-
- $this->outputQueryTime($queryStart, $timeAllQueries);
- }
- } else {
- $this->outputWriter->write(sprintf(
- 'Migration %s was executed but did not result in any SQL statements.',
- $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(' -> ' . $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) {
@@ -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(' -> ' . $query);
+ $this->connection->exec($query);
+ } else {
+ $this->outputWriter->write(sprintf(' - %s (with parameters)', $query));
+ $this->connection->executeQuery($query, $this->params[$key], $this->types[$key]);
+ }
+
+ $this->outputQueryTime($queryStart, $timeAllQueries);
+ }
+ } else {
+ $this->outputWriter->write(sprintf(
+ 'Migration %s was executed but did not result in any SQL statements.',
+ $this->version
+ ));
+ }
+ } else {
+ foreach ($this->sql as $query) {
+ $this->outputWriter->write(' -> ' . $query);
+ }
+ }
+ $this->resetRegisteredSql();
+ }
+
+ private function resetRegisteredSql()
+ {
+ $this->sql = [];
+ $this->params = [];
+ $this->types = [];
+ }
}
diff --git a/tests/Doctrine/DBAL/Migrations/Tests/Functional/FunctionalTest.php b/tests/Doctrine/DBAL/Migrations/Tests/Functional/FunctionalTest.php
index 658019eb11..a2cf866ac6 100644
--- a/tests/Doctrine/DBAL/Migrations/Tests/Functional/FunctionalTest.php
+++ b/tests/Doctrine/DBAL/Migrations/Tests/Functional/FunctionalTest.php
@@ -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');
diff --git a/tests/Doctrine/DBAL/Migrations/Tests/Stub/Functional/MigrateAddSqlPostAndPreUpAndDownTest.php b/tests/Doctrine/DBAL/Migrations/Tests/Stub/Functional/MigrateAddSqlPostAndPreUpAndDownTest.php
new file mode 100644
index 0000000000..1aa756f81e
--- /dev/null
+++ b/tests/Doctrine/DBAL/Migrations/Tests/Stub/Functional/MigrateAddSqlPostAndPreUpAndDownTest.php
@@ -0,0 +1,60 @@
+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]
+ );
+ }
+}