Skip to content

Commit 92b5f6c

Browse files
committed
DiffGenerator: align generated diff with ORM SchemaTool
1 parent fe0fcea commit 92b5f6c

File tree

3 files changed

+16
-74
lines changed

3 files changed

+16
-74
lines changed

Diff for: phpstan.neon.dist

-4
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ parameters:
3434
message: '~^Call to an undefined method Doctrine\\DBAL\\Connection\:\:getEventManager\(\)\.$~'
3535
path: src/DependencyFactory.php
3636

37-
-
38-
message: '~^Strict comparison using !== between callable\(\)\: mixed and null will always evaluate to true\.$~'
39-
path: src/Generator/DiffGenerator.php
40-
4137
-
4238
message: '~Doctrine\\ORM\\Tools\\Console\\Helper\\EntityManagerHelper~'
4339
path: src/Tools/Console/ConsoleRunner.php

Diff for: src/Generator/DiffGenerator.php

+11-36
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
use function method_exists;
1616
use function preg_match;
17-
use function strpos;
18-
use function substr;
1917

2018
/**
2119
* The DiffGenerator class is responsible for comparing two Doctrine\DBAL\Schema\Schema instances and generating a
@@ -46,14 +44,22 @@ public function generate(
4644
bool $checkDbPlatform = true,
4745
bool $fromEmptySchema = false,
4846
): string {
47+
$toSchema = $this->createToSchema();
48+
4949
if ($filterExpression !== null) {
50+
// whitelist assets we already know about in $toSchema, use the existing $filterExpression otherwise
51+
// @see https://github.com/doctrine/orm/pull/7875
5052
$this->dbalConfiguration->setSchemaAssetsFilter(
51-
static function ($assetName) use ($filterExpression) {
53+
static function ($assetName) use ($filterExpression, $toSchema): bool {
5254
if ($assetName instanceof AbstractAsset) {
5355
$assetName = $assetName->getName();
5456
}
5557

56-
return preg_match($filterExpression, $assetName);
58+
if ($toSchema->hasTable($assetName) || $toSchema->hasSequence($assetName)) {
59+
return true;
60+
}
61+
62+
return (bool) preg_match($filterExpression, $assetName);
5763
},
5864
);
5965
}
@@ -62,8 +68,6 @@ static function ($assetName) use ($filterExpression) {
6268
? $this->createEmptySchema()
6369
: $this->createFromSchema();
6470

65-
$toSchema = $this->createToSchema();
66-
6771
// prior to DBAL 4.0, the schema name was set to the first element in the search path,
6872
// which is not necessarily the default schema name
6973
if (
@@ -119,35 +123,6 @@ private function createFromSchema(): Schema
119123

120124
private function createToSchema(): Schema
121125
{
122-
$toSchema = $this->schemaProvider->createSchema();
123-
124-
$schemaAssetsFilter = $this->dbalConfiguration->getSchemaAssetsFilter();
125-
126-
if ($schemaAssetsFilter !== null) {
127-
foreach ($toSchema->getTables() as $table) {
128-
$tableName = $table->getName();
129-
130-
if ($schemaAssetsFilter($this->resolveTableName($tableName))) {
131-
continue;
132-
}
133-
134-
$toSchema->dropTable($tableName);
135-
}
136-
}
137-
138-
return $toSchema;
139-
}
140-
141-
/**
142-
* Resolve a table name from its fully qualified name. The `$name` argument
143-
* comes from Doctrine\DBAL\Schema\Table#getName which can sometimes return
144-
* a namespaced name with the form `{namespace}.{tableName}`. This extracts
145-
* the table name from that.
146-
*/
147-
private function resolveTableName(string $name): string
148-
{
149-
$pos = strpos($name, '.');
150-
151-
return $pos === false ? $name : substr($name, $pos + 1);
126+
return $this->schemaProvider->createSchema();
152127
}
153128
}

Diff for: tests/Generator/DiffGeneratorTest.php

+5-34
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,11 @@ public function testGenerate(): void
3838
$toSchema = $this->createMock(Schema::class);
3939

4040
$this->dbalConfiguration->expects(self::once())
41-
->method('setSchemaAssetsFilter');
42-
43-
$this->dbalConfiguration->expects(self::once())
44-
->method('getSchemaAssetsFilter')
45-
->willReturn(
46-
static fn ($name): bool => $name === 'table_name1',
47-
);
48-
49-
$table1 = $this->createMock(Table::class);
50-
$table1->expects(self::once())
51-
->method('getName')
52-
->willReturn('schema.table_name1');
53-
54-
$table2 = $this->createMock(Table::class);
55-
$table2->expects(self::once())
56-
->method('getName')
57-
->willReturn('schema.table_name2');
58-
59-
$table3 = $this->createMock(Table::class);
60-
$table3->expects(self::once())
61-
->method('getName')
62-
->willReturn('schema.table_name3');
63-
64-
$toSchema->expects(self::once())
65-
->method('getTables')
66-
->willReturn([$table1, $table2, $table3]);
41+
->method('setSchemaAssetsFilter')
42+
->willReturnCallback(static function (callable $schemaAssetsFilter): void {
43+
// also test the code inside the closures provided to setSchemaAssetsFilter
44+
self::assertTrue($schemaAssetsFilter('table_name1'));
45+
});
6746

6847
$this->emptySchemaProvider->expects(self::never())
6948
->method('createSchema');
@@ -76,10 +55,6 @@ public function testGenerate(): void
7655
->method('createSchema')
7756
->willReturn($toSchema);
7857

79-
$toSchema->expects(self::exactly(2))
80-
->method('dropTable')
81-
->willReturnSelf();
82-
8358
$schemaDiff = self::createStub(SchemaDiff::class);
8459

8560
$this->platform->method('getAlterSchemaSQL')->willReturnCallback(static function (): array {
@@ -126,10 +101,6 @@ public function testGenerateFromEmptySchema(): void
126101
$this->dbalConfiguration->expects(self::never())
127102
->method('setSchemaAssetsFilter');
128103

129-
$this->dbalConfiguration->expects(self::once())
130-
->method('getSchemaAssetsFilter')
131-
->willReturn(static fn () => true);
132-
133104
$toSchema->method('getTables')
134105
->willReturn([new Table('table_name')]);
135106

0 commit comments

Comments
 (0)