Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

…d migration.
  • Loading branch information...
commit 29b1b3e67ce608d2d526271314fce889a6f7d0c0 1 parent 9e81984
Jonathan H. Wage jwage authored
7 lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php
View
@@ -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";
Václav Novotný
VeN added a note

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";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
$sql = $this->connection->getDatabasePlatform()->modifyLimitQuery($sql, 1);
$result = $this->connection->fetchColumn($sql);
return $result !== false ? (string) $result : '0';
24 tests/Doctrine/DBAL/Migrations/Tests/Functional/FunctionalTest.php
View
@@ -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
Václav Novotný

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";
Please sign in to comment.
Something went wrong with that request. Please try again.