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

Re-add comparison for foreign key names with opt-out configuration #6520

Closed
wants to merge 3 commits into from
Closed
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
18 changes: 18 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@
*/
private bool $disableTypeComments = false;

/**
* Whether changes in the foreign key names should lead to a schema change.
* If you opt-out of this, you need to handle name changes of foreign keys yourself.
* Databases created based on the current schema might have different foreign key names
* than those migrated from older schemas if you turn this off.
*/
private bool $compareForeignKeyNames = true;

private ?SchemaManagerFactory $schemaManagerFactory = null;

public function __construct()
Expand Down Expand Up @@ -262,4 +270,14 @@

return $this;
}

public function getCompareForeignKeyNames(): bool
{
return $this->compareForeignKeyNames;
}

public function setCompareForeignKeyNames(bool $compareForeignKeyNames): void

Check warning on line 279 in src/Configuration.php

View check run for this annotation

Codecov / codecov/patch

src/Configuration.php#L279

Added line #L279 was not covered by tests
{
$this->compareForeignKeyNames = $compareForeignKeyNames;

Check warning on line 281 in src/Configuration.php

View check run for this annotation

Codecov / codecov/patch

src/Configuration.php#L281

Added line #L281 was not covered by tests
}
}
2 changes: 2 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public function __construct(
$this->platform = $params['platform'];
$this->platform->setEventManager($this->_eventManager);
$this->platform->setDisableTypeComments($config->getDisableTypeComments());
$this->platform->setCompareForeignKeyNames($config->getCompareForeignKeyNames());
}

$this->_expr = $this->createExpressionBuilder();
Expand Down Expand Up @@ -318,6 +319,7 @@ public function getDatabasePlatform()
$this->platform = $this->detectDatabasePlatform();
$this->platform->setEventManager($this->_eventManager);
$this->platform->setDisableTypeComments($this->_config->getDisableTypeComments());
$this->platform->setCompareForeignKeyNames($this->_config->getCompareForeignKeyNames());
}

return $this->platform;
Expand Down
14 changes: 14 additions & 0 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,26 @@ abstract class AbstractPlatform

private bool $disableTypeComments = false;

private bool $compareForeignKeyNames = true;

/** @internal */
final public function setDisableTypeComments(bool $value): void
{
$this->disableTypeComments = $value;
}

/** @internal */
final public function setCompareForeignKeyNames(bool $compare): void
{
$this->compareForeignKeyNames = $compare;
}

/** @internal */
public function getCompareForeignKeyNames(): bool
{
return $this->compareForeignKeyNames;
}

/**
* Sets the EventManager used by the Platform.
*
Expand Down
7 changes: 7 additions & 0 deletions src/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,13 @@ private function detectRenamedIndexes(array &$addedIndexes, array &$removedIndex
*/
public function diffForeignKey(ForeignKeyConstraint $key1, ForeignKeyConstraint $key2)
{
if (
(!$this->platform || $this->platform->getCompareForeignKeyNames())
&& strtolower($key1->getName()) !== strtolower($key2->getName())
) {
return true;
}

if (
array_map('strtolower', $key1->getUnquotedLocalColumns())
!== array_map('strtolower', $key2->getUnquotedLocalColumns())
Expand Down
27 changes: 23 additions & 4 deletions tests/Schema/AbstractComparatorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\DBAL\Tests\Schema;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Comparator;
Expand Down Expand Up @@ -652,7 +653,7 @@ public function testCompareColumnCompareCaseInsensitive(): void
self::assertFalse($tableDiff);
}

public function testCompareIndexBasedOnPropertiesNotName(): void
public function testDetectIndexNameChange(): void
{
$tableA = new Table('foo');
$tableA->addColumn('id', Types::INTEGER);
Expand All @@ -672,7 +673,7 @@ public function testCompareIndexBasedOnPropertiesNotName(): void
);
}

public function testCompareForeignKeyBasedOnPropertiesNotName(): void
public function testDetectForeignKeyNameChange(): void
{
$tableA = new Table('foo');
$tableA->addColumn('id', Types::INTEGER);
Expand All @@ -682,9 +683,27 @@ public function testCompareForeignKeyBasedOnPropertiesNotName(): void
$tableB->addColumn('ID', Types::INTEGER);
$tableB->addForeignKeyConstraint('bar', ['id'], ['id'], [], 'bar_constraint');

$tableDiff = $this->comparator->diffTable($tableA, $tableB);
$fkA = new ForeignKeyConstraint(['id'], 'bar', ['id'], 'foo_constraint');
$fkA->setLocalTable($tableA);
$fkB = new ForeignKeyConstraint(['id'], 'bar', ['id'], 'bar_constraint');
$fkB->setLocalTable($tableB);
$tableDiff = new TableDiff('foo', [], [], [], [], [], [], $tableA, [$fkB], [], [$fkA]);
self::assertEquals($tableDiff, $this->comparator->compareTables($tableA, $tableB));
}

self::assertFalse($tableDiff);
public function testCompareForeignKeyNameChangeDisabled(): void
{
$fk1 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk1');
$fk2 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk2');

//Not disabled:
self::assertTrue($this->comparator->diffForeignKey($fk1, $fk2));

//Disabled foreign key name comparison:
$platform = $this->createMock(AbstractPlatform::class);
$platform->method('getCompareForeignKeyNames')->willReturn(false);
$comparatorDisabledFkName = new Comparator($platform);
self::assertFalse($comparatorDisabledFkName->diffForeignKey($fk1, $fk2));
}

public function testCompareForeignKeyRestrictNoActionAreTheSame(): void
Expand Down