Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Generator/DiffGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function generate(
string $fqcn,
string|null $filterExpression,
bool $formatted = false,
bool|null $nowdocOutput = null,
int $lineLength = 120,
bool $checkDbPlatform = true,
bool $fromEmptySchema = false,
Expand Down Expand Up @@ -83,6 +84,7 @@ static function ($assetName) use ($filterExpression) {
$up = $this->migrationSqlGenerator->generate(
$upSql,
$formatted,
$nowdocOutput,
$lineLength,
$checkDbPlatform,
);
Expand All @@ -92,6 +94,7 @@ static function ($assetName) use ($filterExpression) {
$down = $this->migrationSqlGenerator->generate(
$downSql,
$formatted,
$nowdocOutput,
$lineLength,
$checkDbPlatform,
);
Expand Down
23 changes: 13 additions & 10 deletions src/Generator/SqlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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
Expand All @@ -40,12 +41,14 @@ public function __construct(
public function generate(
array $sql,
bool $formatted = false,
bool|null $nowdocOutput = null,
int $lineLength = 120,
bool $checkDbPlatform = true,
): string {
$code = [];

$storageConfiguration = $this->configuration->getMetadataStorageConfiguration();
$maxLength = $lineLength - 18 - 8; // max - php code length - indentation
foreach ($sql as $query) {
if (
$storageConfiguration instanceof TableMetadataStorageConfiguration
Expand All @@ -54,18 +57,18 @@ public function generate(
continue;
}

if ($formatted) {
$maxLength = $lineLength - 18 - 8; // max - php code length - indentation

if (strlen($query) > $maxLength) {
$query = $this->formatQuery($query);
}
if ($formatted && strlen($query) > $maxLength) {
$query = $this->formatQuery($query);
}

$code[] = sprintf(
"\$this->addSql(<<<'SQL'\n%s\nSQL);",
preg_replace('/^/m', str_repeat(' ', 4), $query),
);
if ($nowdocOutput === true || ($nowdocOutput !== false && $formatted && strlen($query) > $maxLength )) {
$code[] = sprintf(
"\$this->addSql(<<<'SQL'\n%s\nSQL);",
preg_replace('/^/m', str_repeat(' ', 4), $query),
);
} else {
$code[] = sprintf('$this->addSql(%s);', var_export($query, true));
}
}

if (count($code) !== 0 && $checkDbPlatform && $this->configuration->isDatabasePlatformChecked()) {
Expand Down
3 changes: 3 additions & 0 deletions src/SchemaDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function dump(
string $fqcn,
array $excludedTablesRegexes = [],
bool $formatted = false,
bool $nowdocOutput = false,
int $lineLength = 120,
): string {
$schema = $this->schemaManager->introspectSchema();
Expand All @@ -73,6 +74,7 @@ public function dump(
$upCode = $this->migrationSqlGenerator->generate(
$upSql,
$formatted,
$nowdocOutput,
$lineLength,
);

Expand All @@ -85,6 +87,7 @@ public function dump(
$downCode = $this->migrationSqlGenerator->generate(
$downSql,
$formatted,
$nowdocOutput,
$lineLength,
);

Expand Down
8 changes: 8 additions & 0 deletions src/Tools/Console/Command/DiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ protected function configure(): void
InputOption::VALUE_NONE,
'Format the generated SQL.',
)
->addOption(
'nowdoc',
null,
InputOption::VALUE_NEGATABLE,
'Output the generated SQL as a nowdoc string (always active for formatted queries).',
)
->addOption(
'line-length',
null,
Expand Down Expand Up @@ -102,6 +108,7 @@ protected function execute(
}

$formatted = filter_var($input->getOption('formatted'), FILTER_VALIDATE_BOOLEAN);
$nowdocOutput = filter_var($input->getOption('nowdoc'), FILTER_VALIDATE_BOOLEAN);
$lineLength = (int) $input->getOption('line-length');
$allowEmptyDiff = $input->getOption('allow-empty-diff');
$checkDbPlatform = filter_var($input->getOption('check-database-platform'), FILTER_VALIDATE_BOOLEAN);
Expand Down Expand Up @@ -135,6 +142,7 @@ protected function execute(
$fqcn,
$filterExpression,
$formatted,
$nowdocOutput,
$lineLength,
$checkDbPlatform,
$fromEmptySchema,
Expand Down
15 changes: 13 additions & 2 deletions src/Tools/Console/Command/DumpSchemaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@

use function addslashes;
use function class_exists;
use function filter_var;
use function sprintf;
use function str_contains;

use const FILTER_VALIDATE_BOOLEAN;

/**
* The DumpSchemaCommand class is responsible for dumping your current database schema to a migration class. This is
* intended to be used in conjunction with the RollupCommand.
Expand Down Expand Up @@ -49,6 +52,12 @@ protected function configure(): void
InputOption::VALUE_NONE,
'Format the generated SQL.',
)
->addOption(
'nowdoc',
null,
InputOption::VALUE_NONE,
'Output the generated SQL as a nowdoc string (always active for formatted queries).',
)
->addOption(
'namespace',
null,
Expand All @@ -75,8 +84,9 @@ public function execute(
InputInterface $input,
OutputInterface $output,
): int {
$formatted = $input->getOption('formatted');
$lineLength = (int) $input->getOption('line-length');
$formatted = filter_var($input->getOption('formatted'), FILTER_VALIDATE_BOOLEAN);
$nowdocOutput = filter_var($input->getOption('nowdoc'), FILTER_VALIDATE_BOOLEAN);
$lineLength = (int) $input->getOption('line-length');

$schemaDumper = $this->getDependencyFactory()->getSchemaDumper();

Expand All @@ -98,6 +108,7 @@ public function execute(
$fqcn,
$input->getOption('filter-tables'),
$formatted,
$nowdocOutput,
$lineLength,
);

Expand Down
7 changes: 4 additions & 3 deletions tests/Generator/DiffGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function testGenerate(): void
->with(self::logicalOr(
self::equalTo(['UPDATE table SET value = 2']),
self::equalTo(['UPDATE table SET value = 1']),
), true, 80)
), true, false, 80)
->willReturnOnConsecutiveCalls('test1', 'test2');

$this->migrationGenerator->expects(self::once())
Expand All @@ -114,6 +114,7 @@ public function testGenerate(): void
'1234',
'/table_name1/',
true,
false,
80,
));
}
Expand Down Expand Up @@ -169,15 +170,15 @@ public function testGenerateFromEmptySchema(): void
->with(self::logicalOr(
self::equalTo(['CREATE TABLE table_name']),
self::equalTo(['DROP TABLE table_name']),
), false, 120, true)
), false, false, 120, true)
->willReturnOnConsecutiveCalls('test up', 'test down');

$this->migrationGenerator->expects(self::once())
->method('generateMigration')
->with('2345', 'test up', 'test down')
->willReturn('path2');

self::assertSame('path2', $this->migrationDiffGenerator->generate('2345', null, false, 120, true, true));
self::assertSame('path2', $this->migrationDiffGenerator->generate('2345', null, false, false, 120, true, true));
}

protected function setUp(): void
Expand Down
101 changes: 77 additions & 24 deletions tests/Generator/SqlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,37 @@ public function testGenerate(): void
"Migration can only be executed safely on '\\$expectedPlatform'."
);

\$this->addSql(<<<'SQL'
SELECT 1
SQL);
\$this->addSql(<<<'SQL'
SELECT 2
SQL);
\$this->addSql('SELECT 1');
\$this->addSql('SELECT 2');
\$this->addSql(<<<'SQL'
%s
SQL);
CODE,
);

$code = $migrationSqlGenerator->generate($this->sql, true, 80);
$code = $migrationSqlGenerator->generate($this->sql, true, null, 80);

self::assertSame($expectedCode, $code);
}

public function testGenerationWithoutCheckingDatabasePlatform(): void
public function testGenerationWithoutFormatting(): void
{
$this->configuration->setCheckDatabasePlatform(true);

$expectedCode = $this->prepareGeneratedCode(
<<<'CODE'
$this->addSql('SELECT 1');
$this->addSql('SELECT 2');
$this->addSql('%s');
CODE,
formatted: false,
);

$code = $this->migrationSqlGenerator->generate($this->sql, false, null, 80, false);
self::assertSame($expectedCode, $code);
}

public function testGenerationWithNowDocOutput(): void
{
$this->configuration->setCheckDatabasePlatform(true);

Expand All @@ -84,9 +97,47 @@ public function testGenerationWithoutCheckingDatabasePlatform(): void
%s
SQL);
CODE,
formatted: false,
);

$code = $this->migrationSqlGenerator->generate($this->sql, false, true, 80, false);
self::assertSame($expectedCode, $code);
}

public function testGenerationWithNoNowDocFormatting(): void
{
$this->configuration->setCheckDatabasePlatform(true);

$expectedCode = $this->prepareGeneratedCode(
<<<'CODE'
$this->addSql('SELECT 1');
$this->addSql('SELECT 2');
$this->addSql('%s');
CODE,
formatted: true,
nowdoc: false,
);

$code = $this->migrationSqlGenerator->generate($this->sql, true, false, 80, false);

self::assertSame($expectedCode, $code);
}

public function testGenerationWithoutCheckingDatabasePlatform(): void
{
$this->configuration->setCheckDatabasePlatform(true);

$expectedCode = $this->prepareGeneratedCode(
<<<'CODE'
$this->addSql('SELECT 1');
$this->addSql('SELECT 2');
$this->addSql(<<<'SQL'
%s
SQL);
CODE,
);

$code = $this->migrationSqlGenerator->generate($this->sql, true, 80, false);
$code = $this->migrationSqlGenerator->generate($this->sql, true, null, 80, false);

self::assertSame($expectedCode, $code);
}
Expand All @@ -97,19 +148,15 @@ public function testGenerationWithoutCheckingDatabasePlatformWithConfiguration()

$expectedCode = $this->prepareGeneratedCode(
<<<'CODE'
$this->addSql(<<<'SQL'
SELECT 1
SQL);
$this->addSql(<<<'SQL'
SELECT 2
SQL);
$this->addSql('SELECT 1');
$this->addSql('SELECT 2');
$this->addSql(<<<'SQL'
%s
SQL);
CODE,
);

$code = $this->migrationSqlGenerator->generate($this->sql, true, 80);
$code = $this->migrationSqlGenerator->generate($this->sql, true, null, 80);

self::assertSame($expectedCode, $code);
}
Expand All @@ -127,7 +174,7 @@ protected function setUp(): void
);
}

private function prepareGeneratedCode(string $expectedCode): string
private function prepareGeneratedCode(string $expectedCode, bool $formatted = true, bool $nowdoc = true): string
{
$this->sql = [
'SELECT 1',
Expand All @@ -138,15 +185,21 @@ private function prepareGeneratedCode(string $expectedCode): string

$this->metadataConfig->setTableName('migrations_table_name');

if ($formatted) {
$formattedSql = (new SqlFormatter(new NullHighlighter()))->format($this->sql[2]);
if ($nowdoc) {
$formattedSql = implode(
"\n" . str_repeat(' ', 4),
explode("\n", $formattedSql),
);
}

return sprintf($expectedCode, $formattedSql);
}

return sprintf(
$expectedCode,
implode(
"\n" . str_repeat(' ', 4),
explode(
"\n",
(new SqlFormatter(new NullHighlighter()))->format($this->sql[2]),
),
),
$this->sql[2],
);
}
}
3 changes: 2 additions & 1 deletion tests/Tools/Console/Command/DiffCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ public function testExecute(): void

$this->migrationDiffGenerator->expects(self::once())
->method('generate')
->with('FooNs\\Version1234', 'filter expression', true, 80)
->with('FooNs\\Version1234', 'filter expression', true, false, 80)
->willReturn('/path/to/migration.php');

$this->diffCommandTester->execute([
'--filter-expression' => 'filter expression',
'--formatted' => true,
'--nowdoc' => false,
'--line-length' => 80,
'--allow-empty-diff' => true,
'--check-database-platform' => true,
Expand Down
3 changes: 2 additions & 1 deletion tests/Tools/Console/Command/DumpSchemaCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ public function testExecute(): void

$this->schemaDumper->expects(self::once())
->method('dump')
->with('FooNs\\Version1234', ['/foo/'], true, 80);
->with('FooNs\\Version1234', ['/foo/'], true, false, 80);

$this->dumpSchemaCommandTester->execute([
'--filter-tables' => ['/foo/'],
'--line-length' => 80,
'--formatted' => true,
'--nowdoc' => false,
]);

$output = $this->dumpSchemaCommandTester->getDisplay(true);
Expand Down