diff --git a/src/Generator/SqlGenerator.php b/src/Generator/SqlGenerator.php index 2a87cbee0..8738256b5 100644 --- a/src/Generator/SqlGenerator.php +++ b/src/Generator/SqlGenerator.php @@ -14,10 +14,11 @@ use function count; use function get_class; use function implode; +use function preg_replace; use function sprintf; +use function str_repeat; use function stripos; use function strlen; -use function var_export; /** * The SqlGenerator class is responsible for generating the body of the up() and down() methods for a migration @@ -61,7 +62,10 @@ public function generate( } } - $code[] = sprintf('$this->addSql(%s);', var_export($query, true)); + $code[] = sprintf( + "\$this->addSql(<<<'SQL'\n%s\nSQL);", + preg_replace('/^/m', str_repeat(' ', 4), $query), + ); } if (count($code) !== 0 && $checkDbPlatform && $this->configuration->isDatabasePlatformChecked()) { diff --git a/tests/Generator/SqlGeneratorTest.php b/tests/Generator/SqlGeneratorTest.php index e84154208..422a1eb4c 100644 --- a/tests/Generator/SqlGeneratorTest.php +++ b/tests/Generator/SqlGeneratorTest.php @@ -14,7 +14,10 @@ use PHPUnit\Framework\TestCase; use function class_exists; +use function explode; +use function implode; use function sprintf; +use function str_repeat; // DBAL 3 compatibility class_exists('Doctrine\DBAL\Platforms\SqlitePlatform'); @@ -47,10 +50,16 @@ public function testGenerate(): void !\$this->connection->getDatabasePlatform() instanceof \\$expectedPlatform, "Migration can only be executed safely on '\\$expectedPlatform'." ); - - \$this->addSql('SELECT 1'); - \$this->addSql('SELECT 2'); - \$this->addSql('%s'); + + \$this->addSql(<<<'SQL' + SELECT 1 + SQL); + \$this->addSql(<<<'SQL' + SELECT 2 + SQL); + \$this->addSql(<<<'SQL' + %s + SQL); CODE, ); @@ -65,9 +74,15 @@ public function testGenerationWithoutCheckingDatabasePlatform(): void $expectedCode = $this->prepareGeneratedCode( <<<'CODE' - $this->addSql('SELECT 1'); - $this->addSql('SELECT 2'); - $this->addSql('%s'); + $this->addSql(<<<'SQL' + SELECT 1 + SQL); + $this->addSql(<<<'SQL' + SELECT 2 + SQL); + $this->addSql(<<<'SQL' + %s + SQL); CODE, ); @@ -82,9 +97,15 @@ public function testGenerationWithoutCheckingDatabasePlatformWithConfiguration() $expectedCode = $this->prepareGeneratedCode( <<<'CODE' - $this->addSql('SELECT 1'); - $this->addSql('SELECT 2'); - $this->addSql('%s'); + $this->addSql(<<<'SQL' + SELECT 1 + SQL); + $this->addSql(<<<'SQL' + SELECT 2 + SQL); + $this->addSql(<<<'SQL' + %s + SQL); CODE, ); @@ -119,7 +140,13 @@ private function prepareGeneratedCode(string $expectedCode): string return sprintf( $expectedCode, - (new SqlFormatter(new NullHighlighter()))->format($this->sql[2]), + implode( + "\n" . str_repeat(' ', 4), + explode( + "\n", + (new SqlFormatter(new NullHighlighter()))->format($this->sql[2]), + ), + ), ); } }