Skip to content

Commit a811ef2

Browse files
committed
Fix removing schema from table name on asset filtering
This change prevents the DiffGenerator from stripping schemas from table names before applying the asset filter (usually a regex). This aligns its behavior with the ORM's schema generator and fixes a bug on platforms that do not support schemas, where a table name containing a dot would have the part before the dot removed. Fixes #1487
1 parent fa94c6f commit a811ef2

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

phpstan.neon.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ parameters:
5656
-
5757
message: '#^Call to function method_exists\(\) with Doctrine\\DBAL\\Schema\\Table and ''getObjectName'' will always evaluate to true\.$#'
5858
path: src/SchemaDumper.php
59+
-
60+
message: '~^Parameter #1 \$array \(list<string>\) of array_values is already a list, call has no effect\.$~'
61+
path: tests/Generator/DiffGeneratorTest.php
5962

6063
symfony:
6164
consoleApplicationLoader: tests/doctrine-migrations-phpstan-app.php

src/Generator/DiffGenerator.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
use function class_exists;
1717
use function method_exists;
1818
use function preg_match;
19-
use function strpos;
20-
use function substr;
2119

2220
/**
2321
* The DiffGenerator class is responsible for comparing two Doctrine\DBAL\Schema\Schema instances and generating a
@@ -136,7 +134,7 @@ private function createToSchema(): Schema
136134
foreach ($toSchema->getTables() as $table) {
137135
$tableName = $table->getName();
138136

139-
if ($schemaAssetsFilter($this->resolveTableName($tableName))) {
137+
if ($schemaAssetsFilter($tableName)) {
140138
continue;
141139
}
142140

@@ -146,17 +144,4 @@ private function createToSchema(): Schema
146144

147145
return $toSchema;
148146
}
149-
150-
/**
151-
* Resolve a table name from its fully qualified name. The `$name` argument
152-
* comes from Doctrine\DBAL\Schema\Table#getName which can sometimes return
153-
* a namespaced name with the form `{namespace}.{tableName}`. This extracts
154-
* the table name from that.
155-
*/
156-
private function resolveTableName(string $name): string
157-
{
158-
$pos = strpos($name, '.');
159-
160-
return $pos === false ? $name : substr($name, $pos + 1);
161-
}
162147
}

tests/Generator/DiffGeneratorTest.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
use PHPUnit\Framework\MockObject\MockObject;
1919
use PHPUnit\Framework\TestCase;
2020

21+
use function array_map;
22+
use function array_values;
23+
use function preg_match;
24+
2125
class DiffGeneratorTest extends TestCase
2226
{
2327
private DBALConfiguration&MockObject $dbalConfiguration;
@@ -43,7 +47,7 @@ public function testGenerate(): void
4347
$this->dbalConfiguration->expects(self::once())
4448
->method('getSchemaAssetsFilter')
4549
->willReturn(
46-
static fn ($name): bool => $name === 'table_name1',
50+
static fn ($name): bool => $name === 'schema.table_name1',
4751
);
4852

4953
$table1 = $this->createMock(Table::class);
@@ -181,6 +185,49 @@ public function testGenerateFromEmptySchema(): void
181185
self::assertSame('path2', $this->migrationDiffGenerator->generate('2345', null, false, false, 120, true, true));
182186
}
183187

188+
public function testGenerateAppliesFilterOnMappedSchema(): void
189+
{
190+
// a standard Regex SchemaAssetsFilter already registered on the DBAL
191+
$dbalSchemaAssetsFilter = static function ($assetName): bool {
192+
return (bool) preg_match('~^some_schema~', $assetName);
193+
};
194+
195+
$fromSchema = new Schema();
196+
197+
$toTable1 = new Table('some_schema.table1');
198+
$toTable2 = new Table('some_schema.table2');
199+
$toSchema = new Schema([$toTable1, $toTable2]);
200+
201+
$this->schemaManager->expects(self::once())
202+
->method('introspectSchema')
203+
->willReturn($fromSchema);
204+
205+
$this->schemaProvider->expects(self::once())
206+
->method('createSchema')
207+
->willReturn($toSchema);
208+
209+
$this->dbalConfiguration->expects(self::once())
210+
->method('getSchemaAssetsFilter')
211+
->willReturn($dbalSchemaAssetsFilter);
212+
213+
$schemaDiff = self::createStub(SchemaDiff::class);
214+
$comparator = $this->mockComparator($schemaDiff);
215+
216+
$this->schemaManager->expects(self::once())
217+
->method('createComparator')
218+
->willReturn($comparator);
219+
220+
$this->migrationSqlGenerator->expects(self::exactly(2))
221+
->method('generate')
222+
->willReturnOnConsecutiveCalls('up', 'down');
223+
224+
$this->migrationDiffGenerator->generate('Version1234', null);
225+
226+
$filteredTableNames = array_map(static fn (Table $table) => $table->getName(), $toSchema->getTables());
227+
228+
self::assertSame(['some_schema.table1', 'some_schema.table2'], array_values($filteredTableNames));
229+
}
230+
184231
protected function setUp(): void
185232
{
186233
$this->dbalConfiguration = $this->createMock(DBALConfiguration::class);

0 commit comments

Comments
 (0)