Skip to content

Commit

Permalink
Use the standard comment marker in SQL migration files
Browse files Browse the repository at this point in the history
  • Loading branch information
aaa2000 committed Jun 20, 2016
1 parent e599b07 commit 2c5bd9a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -24,7 +24,8 @@
"doctrine/coding-standard": "dev-master",
"mockery/mockery": "^0.9.4",
"johnkary/phpunit-speedtrap": "~1.0@dev",
"jdorn/sql-formatter": "~1.1"
"jdorn/sql-formatter": "~1.1",
"mikey179/vfsStream": "^1.6"
},
"suggest": {
"jdorn/sql-formatter": "Allows to generate formatted SQL with the diff command."
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Migrations/Migration.php
Expand Up @@ -92,7 +92,7 @@ public function writeSqlFile($path, $to = null)

$direction = $from > $to ? Version::DIRECTION_DOWN : Version::DIRECTION_UP;

$this->outputWriter->write(sprintf("# Migrating from %s to %s\n", $from, $to));
$this->outputWriter->write(sprintf("-- Migrating from %s to %s\n", $from, $to));

$sqlWriter = new SqlFileWriter(
$this->configuration->getMigrationsColumnName(),
Expand Down
5 changes: 2 additions & 3 deletions lib/Doctrine/DBAL/Migrations/SqlFileWriter.php
Expand Up @@ -77,10 +77,10 @@ public function write(array $queriesByVersion, $direction)

private function buildMigrationFile(array $queriesByVersion, $direction)
{
$string = sprintf("# Doctrine Migration File Generated on %s\n", date('Y-m-d H:i:s'));
$string = sprintf("-- Doctrine Migration File Generated on %s\n", date('Y-m-d H:i:s'));

foreach ($queriesByVersion as $version => $queries) {
$string .= "\n# Version " . $version . "\n";
$string .= "\n-- Version " . $version . "\n";
foreach ($queries as $query) {
$string .= $query . ";\n";
}
Expand All @@ -107,7 +107,6 @@ private function buildMigrationFilePath()
{
$path = $this->destPath;
if (is_dir($path)) {
$path = realpath($path);
$path = $path . '/doctrine_migration_' . date('YmdHis') . '.sql';
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Migrations/Version.php
Expand Up @@ -223,7 +223,7 @@ public function writeSqlFile($path, $direction = self::DIRECTION_UP)
throw MigrationException::migrationNotConvertibleToSql($this->class);
}

$this->outputWriter->write("\n# Version " . $this->version . "\n");
$this->outputWriter->write("\n-- Version " . $this->version . "\n");

$sqlQueries = [$this->version => $queries];
$sqlWriter = new SqlFileWriter(
Expand Down
25 changes: 25 additions & 0 deletions tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php
Expand Up @@ -24,6 +24,8 @@
use Doctrine\DBAL\Migrations\MigrationException;
use Doctrine\DBAL\Migrations\OutputWriter;
use \Mockery as m;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamFile;

/**
* @runTestsInSeparateProcesses
Expand All @@ -33,6 +35,8 @@ class MigrationTest extends MigrationTestCase
{
/** @var Configuration */
private $config;

protected $output;

protected function setUp()
{
Expand Down Expand Up @@ -140,4 +144,25 @@ public function writeSqlFileProvider()
];
}

public function testWriteSqlFileShouldUseStandardCommentMarkerInSql()
{
$config = m::mock(Configuration::class)->makePartial();
$config->shouldReceive('getCurrentVersion')->andReturn(0);
$config->shouldReceive('getOutputWriter')->andReturn($this->getOutputWriter());
$migration = m::mock('Doctrine\DBAL\Migrations\Migration[getSql]', [$config])->makePartial();
$migration->shouldReceive('getSql')->andReturn(['1' => ['SHOW DATABASES']]);


$sqlFilesDir = vfsStream::setup('sql_files_dir');
$migration->writeSqlFile(vfsStream::url('sql_files_dir'), 1);

$this->assertRegExp('/^\s*-- Migrating from 0 to 1/m', $this->getOutputStreamContent($this->output));

/** @var vfsStreamFile $sqlMigrationFile */
$sqlMigrationFile = current($sqlFilesDir->getChildren());
$this->assertInstanceOf(vfsStreamFile::class, $sqlMigrationFile);
$this->assertRegExp('/^\s*-- Doctrine Migration File Generated on/m', $sqlMigrationFile->getContent());
$this->assertRegExp('/^\s*-- Version 1/m', $sqlMigrationFile->getContent());
$this->assertNotRegExp('/^\s*#/m', $sqlMigrationFile->getContent());
}
}
27 changes: 27 additions & 0 deletions tests/Doctrine/DBAL/Migrations/Tests/VersionTest.php
Expand Up @@ -29,6 +29,8 @@
use Doctrine\DBAL\Migrations\Version;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
use \Mockery as m;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamFile;

/**
* @runTestsInSeparateProcesses
Expand Down Expand Up @@ -329,4 +331,29 @@ public function sqlWriteProvider()
[Version::DIRECTION_DOWN, 'fkqsdmfjl', 'balalala'],
];
}

public function testWriteSqlFileShouldUseStandardCommentMarkerInSql()
{
$version = 1;

$connection = $this->getSqliteConnection();

$config = m::mock(Configuration::class)
->makePartial();
$config->shouldReceive('getOutputWriter')->andReturn($this->getOutputWriter());
$config->shouldReceive('getConnection')->andReturn($connection);

$migration = m::mock('Doctrine\DBAL\Migrations\Version[execute]', [$config, $version, 'stdClass'])->makePartial();
$migration->shouldReceive('execute')->andReturn(['SHOW DATABASES;']);

$sqlFilesDir = vfsStream::setup('sql_files_dir');
$migration->writeSqlFile(vfsStream::url('sql_files_dir'), Version::DIRECTION_UP);

$this->assertRegExp('/^\s*-- Version 1/m', $this->getOutputStreamContent($this->output));

/** @var vfsStreamFile $sqlMigrationFile */
$sqlMigrationFile = current($sqlFilesDir->getChildren());
$this->assertInstanceOf(vfsStreamFile::class, $sqlMigrationFile);
$this->assertNotRegExp('/^\s*#/m', $sqlMigrationFile->getContent());
}
}

0 comments on commit 2c5bd9a

Please sign in to comment.