Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DiffGenerator: fix removing schema from table name before applying regex filtering #1492

Open
wants to merge 2 commits into
base: 3.8.x
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions src/Generator/DiffGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ private function createToSchema(): Schema
*/
private function resolveTableName(string $name): string
{
if ($this->platform->supportsSchemas()) {
return $name;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After your change, I don't think the phpdoc comment is accurate anymore. Also, if the platform does not support schémas, surely the dot in a name should be interpreted literally? So... this method is no longer needed maybe?

$pos = strpos($name, '.');

return $pos === false ? $name : substr($name, $pos + 1);
Expand Down
65 changes: 65 additions & 0 deletions tests/Generator/DiffGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

use function array_map;
use function array_values;
use function preg_match;

class DiffGeneratorTest extends TestCase
{
private DBALConfiguration&MockObject $dbalConfiguration;
Expand Down Expand Up @@ -180,6 +184,67 @@ public function testGenerateFromEmptySchema(): void
self::assertSame('path2', $this->migrationDiffGenerator->generate('2345', null, false, 120, true, true));
}

/**
* @param array<int, string> $expectedTables
*
* @dataProvider getGenerateAppliesFilterOnMappedSchemaData
*/
public function testGenerateAppliesFilterOnMappedSchema(bool $platformSupportsSchemas, array $expectedTables): void
{
// a standard Regex SchemaAssetsFilter already registered on the DBAL
$dbalSchemaAssetsFilter = static function ($assetName): bool {
return (bool) preg_match('~^some_schema~', $assetName);
};

$fromSchema = new Schema();

$toTable1 = new Table('some_schema.table1');
$toTable2 = new Table('some_schema.table2');
$toSchema = new Schema([$toTable1, $toTable2]);

$this->platform->expects(self::atLeast(1))
->method('supportsSchemas')
->willReturn($platformSupportsSchemas);

$this->schemaManager->expects(self::once())
->method('introspectSchema')
->willReturn($fromSchema);

$this->schemaProvider->expects(self::once())
->method('createSchema')
->willReturn($toSchema);

$this->dbalConfiguration->expects(self::once())
->method('getSchemaAssetsFilter')
->willReturn($dbalSchemaAssetsFilter);

$schemaDiff = self::createStub(SchemaDiff::class);
$comparator = $this->mockComparator($schemaDiff);

$this->schemaManager->expects(self::once())
->method('createComparator')
->willReturn($comparator);

$this->migrationSqlGenerator->expects(self::exactly(2))
->method('generate')
->willReturnOnConsecutiveCalls('up', 'down');

$this->migrationDiffGenerator->generate('Version1234', null);

$filteredTableNames = array_map(static fn (Table $table) => $table->getName(), $toSchema->getTables());

self::assertSame($expectedTables, array_values($filteredTableNames));
}

/** @return array<string, array<int, bool|array<int, string>>> */
public static function getGenerateAppliesFilterOnMappedSchemaData(): array
{
return [
'platform without schemas supports' => [false, []],
'platform with schema schemas support' => [true, ['some_schema.table1', 'some_schema.table2']],
];
}

protected function setUp(): void
{
$this->dbalConfiguration = $this->createMock(DBALConfiguration::class);
Expand Down