Permalink
Browse files

Fix for when future version exists in database but is not a registere…

…d migration.
  • Loading branch information...
1 parent 9e81984 commit 29b1b3e67ce608d2d526271314fce889a6f7d0c0 @jwage jwage committed May 1, 2012
@@ -384,7 +384,12 @@ public function getCurrentVersion()
{
$this->createMigrationTable();
- $sql = "SELECT version FROM " . $this->migrationsTableName . " ORDER BY version DESC";
+ $migratedVersions = array();
+ foreach ($this->migrations as $migration) {
+ $migratedVersions[] = $migration->getVersion();
+ }
+
+ $sql = "SELECT version FROM " . $this->migrationsTableName . " WHERE version IN (" . implode(', ', $migratedVersions) . ") ORDER BY version DESC";
@VeN
VeN May 2, 2012

On PostgreSQL, it causes:

  [PDOException]                                                                                              
  SQLSTATE[42883]: Undefined function: 7 ERROR:  operator does not exist: character varying = bigint          
  LINE 1: ...ECT version FROM migration_versions WHERE version IN (201204...                                  
                                                               ^                                              
  HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.  

You should quote version numbers:

$migratedVersions = array();
foreach ($this->migrations as $migration) {
   $migratedVersions[] = sprintf("'%s'", $migration->getVersion());
}

$sql = "SELECT version FROM " . $this->migrationsTableName . " WHERE version IN (" . implode(', ', $migratedVersions) . ") ORDER BY version DESC";
$sql = $this->connection->getDatabasePlatform()->modifyLimitQuery($sql, 1);
$result = $this->connection->fetchColumn($sql);
return $result !== false ? (string) $result : '0';
@@ -177,6 +177,30 @@ public function testAddSql()
$schema = $this->config->getConnection()->getSchemaManager()->createSchema();
$this->assertFalse($schema->hasTable('test_add_sql_table'));
}
+
+ public function testVersionInDatabaseWithoutRegisteredMigrationStillMigrates()
+ {
+ $this->config->registerMigration(1, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrateAddSqlTest');
+ $this->config->registerMigration(10, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationMigrateFurther');
+
+ $migration = new \Doctrine\DBAL\Migrations\Migration($this->config);
+ $migration->migrate();
+
+ $config = new Configuration($this->connection);
+ $config->setMigrationsNamespace('Doctrine\DBAL\Migrations\Tests\Functional');
+ $config->setMigrationsDirectory('.');
+ $config->registerMigration(1, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrateAddSqlTest');
+ $config->registerMigration(2, 'Doctrine\DBAL\Migrations\Tests\Functional\MigrationMigrateUp');
+
+ $migration = new \Doctrine\DBAL\Migrations\Migration($config);
+ $migration->migrate();
+
+ $migrations = $config->getMigrations();
+ $this->assertTrue($migrations[1]->isMigrated());
+ $this->assertTrue($migrations[2]->isMigrated());
+
+ $this->assertEquals(2, $config->getCurrentVersion());
+ }
}
class MigrateAddSqlTest extends \Doctrine\DBAL\Migrations\AbstractMigration

0 comments on commit 29b1b3e

Please sign in to comment.