diff --git a/src/Platforms/MySQL/Comparator.php b/src/Platforms/MySQL/Comparator.php index ebe025dc2a9..6be36be67a9 100644 --- a/src/Platforms/MySQL/Comparator.php +++ b/src/Platforms/MySQL/Comparator.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; use Doctrine\DBAL\Schema\Comparator as BaseComparator; +use Doctrine\DBAL\Schema\ComparatorConfig; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\TableDiff; @@ -30,11 +31,12 @@ public function __construct( parent::__construct($platform); } - public function compareTables(Table $oldTable, Table $newTable): TableDiff + public function compareTables(Table $oldTable, Table $newTable, ?ComparatorConfig $config = null): TableDiff { return parent::compareTables( $this->normalizeTable($oldTable), $this->normalizeTable($newTable), + $config, ); } diff --git a/src/Platforms/SQLServer/Comparator.php b/src/Platforms/SQLServer/Comparator.php index aa8d9fb5b62..5135b508b4f 100644 --- a/src/Platforms/SQLServer/Comparator.php +++ b/src/Platforms/SQLServer/Comparator.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Platforms\SQLServerPlatform; use Doctrine\DBAL\Schema\Comparator as BaseComparator; +use Doctrine\DBAL\Schema\ComparatorConfig; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\TableDiff; @@ -22,11 +23,12 @@ public function __construct(SQLServerPlatform $platform, private readonly string parent::__construct($platform); } - public function compareTables(Table $oldTable, Table $newTable): TableDiff + public function compareTables(Table $oldTable, Table $newTable, ?ComparatorConfig $config = null): TableDiff { return parent::compareTables( $this->normalizeColumns($oldTable), $this->normalizeColumns($newTable), + $config, ); } diff --git a/src/Platforms/SQLite/Comparator.php b/src/Platforms/SQLite/Comparator.php index f27e1b4571c..4c1f0475c97 100644 --- a/src/Platforms/SQLite/Comparator.php +++ b/src/Platforms/SQLite/Comparator.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\DBAL\Schema\Comparator as BaseComparator; +use Doctrine\DBAL\Schema\ComparatorConfig; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\TableDiff; @@ -24,11 +25,12 @@ public function __construct(SQLitePlatform $platform) parent::__construct($platform); } - public function compareTables(Table $oldTable, Table $newTable): TableDiff + public function compareTables(Table $oldTable, Table $newTable, ?ComparatorConfig $config = null): TableDiff { return parent::compareTables( $this->normalizeColumns($oldTable), $this->normalizeColumns($newTable), + $config, ); } diff --git a/src/Schema/Comparator.php b/src/Schema/Comparator.php index 4c60c0770a3..88710f84f6e 100644 --- a/src/Schema/Comparator.php +++ b/src/Schema/Comparator.php @@ -24,7 +24,7 @@ public function __construct(private readonly AbstractPlatform $platform) /** * Returns the differences between the schemas. */ - public function compareSchemas(Schema $oldSchema, Schema $newSchema): SchemaDiff + public function compareSchemas(Schema $oldSchema, Schema $newSchema, ?ComparatorConfig $config = null): SchemaDiff { $createdSchemas = []; $droppedSchemas = []; @@ -59,6 +59,7 @@ public function compareSchemas(Schema $oldSchema, Schema $newSchema): SchemaDiff $tableDiff = $this->compareTables( $oldSchema->getTable($newTableName), $newSchema->getTable($newTableName), + $config, ); if (! $tableDiff->isEmpty()) { @@ -141,14 +142,18 @@ public function diffSequence(Sequence $sequence1, Sequence $sequence2): bool /** * Compares the tables and returns the difference between them. */ - public function compareTables(Table $oldTable, Table $newTable): TableDiff + public function compareTables(Table $oldTable, Table $newTable, ?ComparatorConfig $config = null): TableDiff { + $config ??= new ComparatorConfig(); + $addedColumns = []; $modifiedColumns = []; $droppedColumns = []; + $renamedColumns = []; $addedIndexes = []; $modifiedIndexes = []; $droppedIndexes = []; + $renamedIndexes = []; $addedForeignKeys = []; $modifiedForeignKeys = []; $droppedForeignKeys = []; @@ -187,7 +192,9 @@ public function compareTables(Table $oldTable, Table $newTable): TableDiff $modifiedColumns[] = new ColumnDiff($oldColumn, $newColumn); } - $renamedColumns = $this->detectRenamedColumns($addedColumns, $droppedColumns); + if ($config->getDetectRenamedColumns()) { + $renamedColumns = $this->detectRenamedColumns($addedColumns, $droppedColumns); + } $oldIndexes = $oldTable->getIndexes(); $newIndexes = $newTable->getIndexes(); @@ -224,7 +231,9 @@ public function compareTables(Table $oldTable, Table $newTable): TableDiff $modifiedIndexes[] = $newIndex; } - $renamedIndexes = $this->detectRenamedIndexes($addedIndexes, $droppedIndexes); + if ($config->getDetectRenamedIndexes()) { + $renamedIndexes = $this->detectRenamedIndexes($addedIndexes, $droppedIndexes); + } $oldForeignKeys = $oldTable->getForeignKeys(); $newForeignKeys = $newTable->getForeignKeys(); diff --git a/src/Schema/ComparatorConfig.php b/src/Schema/ComparatorConfig.php new file mode 100644 index 00000000000..a2e6d83f42e --- /dev/null +++ b/src/Schema/ComparatorConfig.php @@ -0,0 +1,32 @@ +detectRenamedColumns = $detectRenamedColumns; + } + + public function getDetectRenamedColumns(): bool + { + return $this->detectRenamedColumns; + } + + public function setDetectRenamedIndexes(bool $detectRenamedIndexes): void + { + $this->detectRenamedIndexes = $detectRenamedIndexes; + } + + public function getDetectRenamedIndexes(): bool + { + return $this->detectRenamedIndexes; + } +} diff --git a/tests/Schema/AbstractComparatorTestCase.php b/tests/Schema/AbstractComparatorTestCase.php index ed9faafb6f0..cdc5740742e 100644 --- a/tests/Schema/AbstractComparatorTestCase.php +++ b/tests/Schema/AbstractComparatorTestCase.php @@ -8,6 +8,7 @@ use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\ColumnDiff; use Doctrine\DBAL\Schema\Comparator; +use Doctrine\DBAL\Schema\ComparatorConfig; use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Schema; @@ -395,6 +396,23 @@ public function testDetectRenameColumn(): void self::assertEquals('bar', $renamedColumns['foo']->getName()); } + public function testDetectRenameColumnDisabled(): void + { + $tableA = new Table('foo'); + $tableA->addColumn('foo', Types::INTEGER); + + $tableB = new Table('foo'); + $tableB->addColumn('bar', Types::INTEGER); + + $config = new ComparatorConfig(); + $config->setDetectRenamedColumns(false); + $tableDiff = $this->comparator->compareTables($tableA, $tableB, $config); + + self::assertCount(1, $tableDiff->getAddedColumns()); + self::assertCount(1, $tableDiff->getDroppedColumns()); + self::assertCount(0, $tableDiff->getRenamedColumns()); + } + /** * You can easily have ambiguities in the column renaming. If these * are detected no renaming should take place, instead adding and dropping @@ -437,6 +455,26 @@ public function testDetectRenameIndex(): void self::assertEquals('idx_bar', $renamedIndexes['idx_foo']->getName()); } + public function testDetectRenameIndexDisabled(): void + { + $table1 = new Table('foo'); + $table1->addColumn('foo', Types::INTEGER); + + $table2 = clone $table1; + + $table1->addIndex(['foo'], 'idx_foo'); + + $table2->addIndex(['foo'], 'idx_bar'); + + $config = new ComparatorConfig(); + $config->setDetectRenamedIndexes(false); + $tableDiff = $this->comparator->compareTables($table1, $table2, $config); + + self::assertCount(1, $tableDiff->getAddedIndexes()); + self::assertCount(1, $tableDiff->getDroppedIndexes()); + self::assertCount(0, $tableDiff->getRenamedIndexes()); + } + /** * You can easily have ambiguities in the index renaming. If these * are detected no renaming should take place, instead adding and dropping