Skip to content
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: 0 additions & 4 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ parameters:
message: '~^Call to an undefined method Doctrine\\DBAL\\Connection\:\:getEventManager\(\)\.$~'
path: src/DependencyFactory.php

-
message: '~^Strict comparison using !== between callable\(\)\: mixed and null will always evaluate to true\.$~'
path: src/Generator/DiffGenerator.php

-
message: '~Doctrine\\ORM\\Tools\\Console\\Helper\\EntityManagerHelper~'
path: src/Tools/Console/ConsoleRunner.php
Expand Down
47 changes: 11 additions & 36 deletions src/Generator/DiffGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

use function method_exists;
use function preg_match;
use function strpos;
use function substr;

/**
* The DiffGenerator class is responsible for comparing two Doctrine\DBAL\Schema\Schema instances and generating a
Expand Down Expand Up @@ -46,14 +44,22 @@
bool $checkDbPlatform = true,
bool $fromEmptySchema = false,
): string {
$toSchema = $this->createToSchema();

if ($filterExpression !== null) {
// whitelist assets we already know about in $toSchema, use the existing $filterExpression otherwise
// @see https://github.com/doctrine/orm/pull/7875
$this->dbalConfiguration->setSchemaAssetsFilter(
static function ($assetName) use ($filterExpression) {
static function ($assetName) use ($filterExpression, $toSchema): bool {
if ($assetName instanceof AbstractAsset) {
$assetName = $assetName->getName();
}

return preg_match($filterExpression, $assetName);
if ($toSchema->hasTable($assetName) || $toSchema->hasSequence($assetName)) {
return true;

Check warning on line 59 in src/Generator/DiffGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/Generator/DiffGenerator.php#L59

Added line #L59 was not covered by tests
}

return (bool) preg_match($filterExpression, $assetName);
Copy link
Contributor Author

@cristi-contiu cristi-contiu Feb 14, 2025

Choose a reason for hiding this comment

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

Casting to bool for docs and DoctrineBundle alignment

},
);
}
Expand All @@ -62,8 +68,6 @@
? $this->createEmptySchema()
: $this->createFromSchema();

$toSchema = $this->createToSchema();

// prior to DBAL 4.0, the schema name was set to the first element in the search path,
// which is not necessarily the default schema name
if (
Expand Down Expand Up @@ -119,35 +123,6 @@

private function createToSchema(): Schema
{
$toSchema = $this->schemaProvider->createSchema();

$schemaAssetsFilter = $this->dbalConfiguration->getSchemaAssetsFilter();

if ($schemaAssetsFilter !== null) {
foreach ($toSchema->getTables() as $table) {
$tableName = $table->getName();

if ($schemaAssetsFilter($this->resolveTableName($tableName))) {
continue;
}

$toSchema->dropTable($tableName);
}
}

return $toSchema;
}

/**
* Resolve a table name from its fully qualified name. The `$name` argument
* comes from Doctrine\DBAL\Schema\Table#getName which can sometimes return
* a namespaced name with the form `{namespace}.{tableName}`. This extracts
* the table name from that.
*/
private function resolveTableName(string $name): string
{
$pos = strpos($name, '.');

return $pos === false ? $name : substr($name, $pos + 1);
return $this->schemaProvider->createSchema();
}
}
39 changes: 5 additions & 34 deletions tests/Generator/DiffGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,11 @@ public function testGenerate(): void
$toSchema = $this->createMock(Schema::class);

$this->dbalConfiguration->expects(self::once())
->method('setSchemaAssetsFilter');

$this->dbalConfiguration->expects(self::once())
->method('getSchemaAssetsFilter')
->willReturn(
static fn ($name): bool => $name === 'table_name1',
);

$table1 = $this->createMock(Table::class);
$table1->expects(self::once())
->method('getName')
->willReturn('schema.table_name1');

$table2 = $this->createMock(Table::class);
$table2->expects(self::once())
->method('getName')
->willReturn('schema.table_name2');

$table3 = $this->createMock(Table::class);
$table3->expects(self::once())
->method('getName')
->willReturn('schema.table_name3');

$toSchema->expects(self::once())
->method('getTables')
->willReturn([$table1, $table2, $table3]);
->method('setSchemaAssetsFilter')
->willReturnCallback(static function (callable $schemaAssetsFilter): void {
// also test the closure provided to setSchemaAssetsFilter
self::assertTrue($schemaAssetsFilter('table_name1'));
});

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

$toSchema->expects(self::exactly(2))
->method('dropTable')
->willReturnSelf();

$schemaDiff = self::createStub(SchemaDiff::class);

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

$this->dbalConfiguration->expects(self::once())
->method('getSchemaAssetsFilter')
->willReturn(static fn () => true);

$toSchema->method('getTables')
->willReturn([new Table('table_name')]);

Expand Down