Skip to content

Commit

Permalink
Merge pull request #599 from tobiasstadler/master
Browse files Browse the repository at this point in the history
Improve DB2 support
  • Loading branch information
mikeSimonson committed Apr 8, 2018
2 parents 50e9c94 + 6372e84 commit 634f590
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
31 changes: 23 additions & 8 deletions lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,16 @@ public function getMigrationsColumnName()
return $this->migrationsColumnName;
}

/**
* Returns the quoted migration column name
*
* @return string The quouted migration column name
*/
public function getQuotedMigrationsColumnName()
{
return $this->getMigrationsColumn()->getQuotedName($this->connection->getDatabasePlatform());
}

/**
* Set the new migrations directory where new migration classes are generated
*
Expand Down Expand Up @@ -524,7 +534,7 @@ public function hasVersionMigrated(Version $version)
$this->createMigrationTable();

$version = $this->connection->fetchColumn(
"SELECT " . $this->migrationsColumnName . " FROM " . $this->migrationsTableName . " WHERE " . $this->migrationsColumnName . " = ?",
"SELECT " . $this->getQuotedMigrationsColumnName() . " FROM " . $this->migrationsTableName . " WHERE " . $this->getQuotedMigrationsColumnName() . " = ?",
[$version->getVersion()]
);

Expand All @@ -546,7 +556,7 @@ public function getMigratedVersions()

$this->connect();

$ret = $this->connection->fetchAll("SELECT " . $this->migrationsColumnName . " FROM " . $this->migrationsTableName);
$ret = $this->connection->fetchAll("SELECT " . $this->getQuotedMigrationsColumnName() . " FROM " . $this->migrationsTableName);

return array_map('current', $ret);
}
Expand Down Expand Up @@ -596,15 +606,15 @@ public function getCurrentVersion()
foreach ($this->migrations as $migration) {
$migratedVersions[] = sprintf("'%s'", $migration->getVersion());
}
$where = " WHERE " . $this->migrationsColumnName . " IN (" . implode(', ', $migratedVersions) . ")";
$where = " WHERE " . $this->getQuotedMigrationsColumnName() . " IN (" . implode(', ', $migratedVersions) . ")";
}

$sql = sprintf(
"SELECT %s FROM %s%s ORDER BY %s DESC",
$this->migrationsColumnName,
$this->getQuotedMigrationsColumnName(),
$this->migrationsTableName,
$where,
$this->migrationsColumnName
$this->getQuotedMigrationsColumnName()
);

$sql = $this->connection->getDatabasePlatform()->modifyLimitQuery($sql, 1);
Expand Down Expand Up @@ -736,7 +746,7 @@ public function getNumberOfExecutedMigrations()
$this->connect();
$this->createMigrationTable();

$result = $this->connection->fetchColumn("SELECT COUNT(" . $this->migrationsColumnName . ") FROM " . $this->migrationsTableName);
$result = $this->connection->fetchColumn("SELECT COUNT(" . $this->getQuotedMigrationsColumnName() . ") FROM " . $this->migrationsTableName);

return $result !== false ? $result : 0;
}
Expand Down Expand Up @@ -797,7 +807,7 @@ public function createMigrationTable()
}

$columns = [
$this->migrationsColumnName => new Column($this->migrationsColumnName, Type::getType('string'), ['length' => 255]),
$this->migrationsColumnName => $this->getMigrationsColumn(),
];
$table = new Table($this->migrationsTableName, $columns);
$table->setPrimaryKey([$this->migrationsColumnName]);
Expand Down Expand Up @@ -979,7 +989,7 @@ public function getQueryWriter() : QueryWriter
{
if ($this->queryWriter === null) {
$this->queryWriter = new FileQueryWriter(
$this->migrationsColumnName,
$this->getQuotedMigrationsColumnName(),
$this->migrationsTableName,
$this->outputWriter
);
Expand All @@ -995,4 +1005,9 @@ public function setIsDryRun($isDryRun)
{
$this->isDryRun = $isDryRun;
}

private function getMigrationsColumn(): Column
{
return new Column($this->migrationsColumnName, Type::getType('string'), ['length' => 255]);
}
}
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Migrations/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private function markVersion($direction)
$this->configuration->createMigrationTable();
$this->connection->$action(
$this->configuration->getMigrationsTableName(),
[$this->configuration->getMigrationsColumnName() => $this->version]
[$this->configuration->getQuotedMigrationsColumnName() => $this->version]
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Connections\MasterSlaveConnection;
use Doctrine\DBAL\Driver\IBMDB2\DB2Driver;
use Doctrine\DBAL\Migrations\QueryWriter;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
Expand All @@ -13,6 +14,21 @@
use Doctrine\DBAL\Migrations\OutputWriter;
use Doctrine\DBAL\Migrations\Tests\MigrationTestCase;
use Doctrine\DBAL\Migrations\Tests\Stub\Configuration\AutoloadVersions\Version1Test;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\Keywords\KeywordList;

final class EmptyKeywordList extends KeywordList
{
protected function getKeywords()
{
return [];
}

public function getName()
{
return "EMPTY";
}
}

class ConfigurationTest extends MigrationTestCase
{
Expand Down Expand Up @@ -183,6 +199,9 @@ public function testMasterSlaveConnectionAlwaysConnectsToMaster()
$sm->expects($this->once())
->method('tablesExist')
->willReturn(true);
$dp = $this->getMockForAbstractClass(AbstractPlatform::class,[], '', false, true, true, ['getReservedKeywordsClass']);
$dp->method('getReservedKeywordsClass')
->willReturn(EmptyKeywordList::class);
$conn = $this->getMockBuilder(MasterSlaveConnection::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -192,6 +211,8 @@ public function testMasterSlaveConnectionAlwaysConnectsToMaster()
->willReturn(true);
$conn->method('getSchemaManager')
->willReturn($sm);
$conn->method('getDatabasePlatform')
->willReturn($dp);
$conn->expects($this->once())
->method('fetchAll')
->willReturn([
Expand Down Expand Up @@ -255,7 +276,14 @@ public function methodsThatNeedsVersionsLoaded()

public function testGetQueryWriterCreatesAnInstanceIfItWasNotConfigured() : void
{
$configuration = new Configuration($this->getConnectionMock());
$dp = $this->getMockForAbstractClass(AbstractPlatform::class,[], '', false, true, true, ['getReservedKeywordsClass']);
$dp->method('getReservedKeywordsClass')
->willReturn(EmptyKeywordList::class);
$conn = $this->getConnectionMock();
$conn->method('getDatabasePlatform')
->willReturn($dp);

$configuration = new Configuration($conn);
$queryWriter = $configuration->getQueryWriter();

self::assertAttributeSame($configuration->getMigrationsTableName(), 'tableName', $queryWriter);
Expand All @@ -270,4 +298,11 @@ public function testGetQueryWriterShouldReturnTheObjectGivenOnTheConstructor() :

self::assertSame($queryWriter, $configuration->getQueryWriter());
}

public function testDBWhereVersionIsKeywordReturnsColumnNameWithQuotes()
{
$config = new Configuration(new Connection([], new DB2Driver()));

$this->assertEquals('"version"', $config->getQuotedMigrationsColumnName());
}
}
5 changes: 4 additions & 1 deletion tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public function testWriteSqlFileShouldUseStandardCommentMarkerInSql()
/** @var Configuration|\PHPUnit_Framework_MockObject_MockObject $migration */
$config = $this->getMockBuilder(Configuration::class)
->disableOriginalConstructor()
->setMethods(['getCurrentVersion', 'getOutputWriter', 'getLatestVersion'])
->setMethods(['getCurrentVersion', 'getOutputWriter', 'getLatestVersion', 'getQuotedMigrationsColumnName'])
->getMock();

$config->method('getCurrentVersion')
Expand All @@ -203,6 +203,9 @@ public function testWriteSqlFileShouldUseStandardCommentMarkerInSql()
$config->method('getOutputWriter')
->willReturn($this->getOutputWriter());

$config->method('getQuotedMigrationsColumnName')
->willReturn('version');

/** @var Migration|\PHPUnit_Framework_MockObject_MockObject $migration */
$migration = $this->getMockBuilder(Migration::class)
->setConstructorArgs([$config])
Expand Down
5 changes: 4 additions & 1 deletion tests/Doctrine/DBAL/Migrations/Tests/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public function testWriteSqlFileShouldUseStandardCommentMarkerInSql()
/** @var Configuration|\PHPUnit_Framework_MockObject_MockObject $migration */
$config = $this->getMockBuilder(Configuration::class)
->disableOriginalConstructor()
->setMethods(['getOutputWriter', 'getConnection'])
->setMethods(['getOutputWriter', 'getConnection', 'getQuotedMigrationsColumnName'])
->getMock();

$config->method('getOutputWriter')
Expand All @@ -417,6 +417,9 @@ public function testWriteSqlFileShouldUseStandardCommentMarkerInSql()
$config->method('getConnection')
->willReturn($connection);

$config->method('getQuotedMigrationsColumnName')
->willReturn('version');


/** @var Version|\PHPUnit_Framework_MockObject_MockObject $migration */
$migration = $this->getMockBuilder(Version::class)
Expand Down

0 comments on commit 634f590

Please sign in to comment.