Skip to content
Browse files

Merge pull request #463 from syzygymsu/master

Fixed relative version calculation for non-numeric values
  • Loading branch information...
2 parents 7308bcf + dbb7dcd commit e599b075890bc55aa22197f3f7a7be28ee720b0e @mikeSimonson mikeSimonson committed May 26, 2016
View
10 lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php
@@ -409,7 +409,7 @@ public function registerMigration($version, $class)
}
$version = new Version($this, $version, $class);
$this->migrations[$version->getVersion()] = $version;
- ksort($this->migrations);
+ ksort($this->migrations, SORT_STRING);
return $version;
}
@@ -601,15 +601,15 @@ public function getRelativeVersion($version, $delta)
$this->registerMigrationsFromDirectory($this->getMigrationsDirectory());
}
- $versions = array_keys($this->migrations);
- array_unshift($versions, 0);
- $offset = array_search($version, $versions);
+ $versions = array_map('strval', array_keys($this->migrations));
+ array_unshift($versions, '0');
+ $offset = array_search((string)$version, $versions);
if ($offset === false || !isset($versions[$offset + $delta])) {
// Unknown version or delta out of bounds.
return null;
}
- return (string) $versions[$offset + $delta];
+ return $versions[$offset + $delta];
}
/**
View
63 tests/Doctrine/DBAL/Migrations/Tests/ConfigurationTest.php
@@ -133,6 +133,69 @@ public function testRegisterDuplicateVersion()
$config->registerMigration(1234, Version1Test::class);
}
+ public function testRelativeVersion()
+ {
+ $config = $this->getSqliteConfiguration();
+ $config->registerMigrations([
+ '0initial' => Version1Test::class,
+ 1 => Version2Test::class,
+ 'final' => Version3Test::class,
+ ]);
+
+ // Relative to nonexistent version
+ $this->assertNull($config->getRelativeVersion('nonexistent', -1));
+ $this->assertNull($config->getRelativeVersion('nonexistent', 0));
+ $this->assertNull($config->getRelativeVersion('nonexistent', 1));
+
+ // Relative to version '0' as int
+ $this->assertNull($config->getRelativeVersion(0, -1));
+ $this->assertSame('0', $config->getRelativeVersion(0, 0));
+ $this->assertSame('0initial', $config->getRelativeVersion(0, 1));
+ $this->assertSame('1', $config->getRelativeVersion(0, 2));
+ $this->assertSame('final', $config->getRelativeVersion(0, 3));
+ $this->assertNull($config->getRelativeVersion(0, 4));
+
+ // Relative to version '0' as string
+ $this->assertNull($config->getRelativeVersion('0', -1));
+ $this->assertSame('0', $config->getRelativeVersion('0', 0));
+ $this->assertSame('0initial', $config->getRelativeVersion('0', 1));
+ $this->assertSame('1', $config->getRelativeVersion('0', 2));
+ $this->assertSame('final', $config->getRelativeVersion('0', 3));
+ $this->assertNull($config->getRelativeVersion('0', 4));
+
+ // Relative to version '0initial'
+ $this->assertNull($config->getRelativeVersion('0initial', -2));
+ $this->assertSame('0', $config->getRelativeVersion('0initial', -1));
+ $this->assertSame('0initial', $config->getRelativeVersion('0initial', 0));
+ $this->assertSame('1', $config->getRelativeVersion('0initial', 1));
+ $this->assertSame('final', $config->getRelativeVersion('0initial', 2));
+ $this->assertNull($config->getRelativeVersion('0initial', 3));
+
+ // Relative to version '1' as int
+ $this->assertNull($config->getRelativeVersion(1, -3));
+ $this->assertSame('0', $config->getRelativeVersion(1, -2));
+ $this->assertSame('0initial', $config->getRelativeVersion(1, -1));
+ $this->assertSame('1', $config->getRelativeVersion(1, 0));
+ $this->assertSame('final', $config->getRelativeVersion(1, 1));
+ $this->assertNull($config->getRelativeVersion(1, 2));
+
+ // Relative to version '1' as string
+ $this->assertNull($config->getRelativeVersion('1', -3));
+ $this->assertSame('0', $config->getRelativeVersion('1', -2));
+ $this->assertSame('0initial', $config->getRelativeVersion('1', -1));
+ $this->assertSame('1', $config->getRelativeVersion('1', 0));
+ $this->assertSame('final', $config->getRelativeVersion('1', 1));
+ $this->assertNull($config->getRelativeVersion('1', 2));
+
+ // Relative to version 'final'
+ $this->assertNull($config->getRelativeVersion('final', -4));
+ $this->assertSame('0', $config->getRelativeVersion('final', -3));
+ $this->assertSame('0initial', $config->getRelativeVersion('final', -2));
+ $this->assertSame('1', $config->getRelativeVersion('final', -1));
+ $this->assertSame('final', $config->getRelativeVersion('final', 0));
+ $this->assertNull($config->getRelativeVersion('final', 1));
+ }
+
public function testPreviousCurrentNextLatestVersion()
{
$config = $this->getSqliteConfiguration();

0 comments on commit e599b07

Please sign in to comment.
Something went wrong with that request. Please try again.