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

Make column and index renaming configurable #6300

Open
wants to merge 2 commits into
base: 4.1.x
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@
class Comparator
{
/** @internal The comparator can be only instantiated by a schema manager. */
public function __construct(private readonly AbstractPlatform $platform)
public function __construct(
private readonly AbstractPlatform $platform,
private ComparatorConfig $config = new ComparatorConfig(),
) {
}

public function setConfig(ComparatorConfig $config): void
{
$this->config = $config;
}

/**
Expand Down Expand Up @@ -146,9 +153,11 @@ public function compareTables(Table $oldTable, Table $newTable): TableDiff
$addedColumns = [];
$modifiedColumns = [];
$droppedColumns = [];
$renamedColumns = [];
$addedIndexes = [];
$modifiedIndexes = [];
$droppedIndexes = [];
$renamedIndexes = [];
$addedForeignKeys = [];
$modifiedForeignKeys = [];
$droppedForeignKeys = [];
Expand Down Expand Up @@ -187,7 +196,9 @@ public function compareTables(Table $oldTable, Table $newTable): TableDiff
$modifiedColumns[] = new ColumnDiff($oldColumn, $newColumn);
}

$renamedColumns = $this->detectRenamedColumns($addedColumns, $droppedColumns);
if ($this->config->getDetectRenamedColumns()) {
$renamedColumns = $this->detectRenamedColumns($addedColumns, $droppedColumns);
}

$oldIndexes = $oldTable->getIndexes();
$newIndexes = $newTable->getIndexes();
Expand Down Expand Up @@ -224,7 +235,9 @@ public function compareTables(Table $oldTable, Table $newTable): TableDiff
$modifiedIndexes[] = $newIndex;
}

$renamedIndexes = $this->detectRenamedIndexes($addedIndexes, $droppedIndexes);
if ($this->config->getDetectRenamedIndexes()) {
$renamedIndexes = $this->detectRenamedIndexes($addedIndexes, $droppedIndexes);
}

$oldForeignKeys = $oldTable->getForeignKeys();
$newForeignKeys = $newTable->getForeignKeys();
Expand Down
32 changes: 32 additions & 0 deletions src/Schema/ComparatorConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Schema;

class ComparatorConfig
{
protected bool $detectRenamedColumns = true;

protected bool $detectRenamedIndexes = true;

public function setDetectRenamedColumns(bool $detectRenamedColumns): void
{
$this->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;
}
}
40 changes: 40 additions & 0 deletions tests/Schema/AbstractComparatorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -395,6 +396,24 @@ 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);
$this->comparator->setConfig($config);
$tableDiff = $this->comparator->compareTables($tableA, $tableB);

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
Expand Down Expand Up @@ -437,6 +456,27 @@ 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);
$this->comparator->setConfig($config);
$tableDiff = $this->comparator->compareTables($table1, $table2);

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
Expand Down