diff --git a/composer.json b/composer.json index 4abc978b2c..ae1a39193d 100644 --- a/composer.json +++ b/composer.json @@ -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." diff --git a/lib/Doctrine/DBAL/Migrations/Migration.php b/lib/Doctrine/DBAL/Migrations/Migration.php index 11ee1464ca..39ad772a76 100644 --- a/lib/Doctrine/DBAL/Migrations/Migration.php +++ b/lib/Doctrine/DBAL/Migrations/Migration.php @@ -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(), diff --git a/lib/Doctrine/DBAL/Migrations/SqlFileWriter.php b/lib/Doctrine/DBAL/Migrations/SqlFileWriter.php index c2aec6424c..f7ae4cc7cd 100644 --- a/lib/Doctrine/DBAL/Migrations/SqlFileWriter.php +++ b/lib/Doctrine/DBAL/Migrations/SqlFileWriter.php @@ -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"; } @@ -107,7 +107,6 @@ private function buildMigrationFilePath() { $path = $this->destPath; if (is_dir($path)) { - $path = realpath($path); $path = $path . '/doctrine_migration_' . date('YmdHis') . '.sql'; } diff --git a/lib/Doctrine/DBAL/Migrations/Version.php b/lib/Doctrine/DBAL/Migrations/Version.php index 635968878f..65be8ef5a6 100644 --- a/lib/Doctrine/DBAL/Migrations/Version.php +++ b/lib/Doctrine/DBAL/Migrations/Version.php @@ -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( diff --git a/tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php b/tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php index b4dd05e8cb..03633b4ac8 100644 --- a/tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php +++ b/tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php @@ -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 @@ -33,6 +35,8 @@ class MigrationTest extends MigrationTestCase { /** @var Configuration */ private $config; + + protected $output; protected function setUp() { @@ -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()); + } } diff --git a/tests/Doctrine/DBAL/Migrations/Tests/VersionTest.php b/tests/Doctrine/DBAL/Migrations/Tests/VersionTest.php index 74501ecdb1..1b01b27e25 100644 --- a/tests/Doctrine/DBAL/Migrations/Tests/VersionTest.php +++ b/tests/Doctrine/DBAL/Migrations/Tests/VersionTest.php @@ -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 @@ -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()); + } }