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 1 commit
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
4 changes: 3 additions & 1 deletion src/Platforms/MySQL/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this considered to be a BC break?
If so, should I use func_get_arg() or a setConfig() method instead?
(I’d not be a fan of a setConfig() method as this would introduce state to the Comparator)

Copy link
Contributor

Choose a reason for hiding this comment

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

As I mentioned in the issue, Comparator is indeed an extensible class, so changing the public signature is a breaking change, though likely a very low impact one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, should I add a setConfig() method to the Comparator?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be best yes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in d0c8602

{
return parent::compareTables(
$this->normalizeTable($oldTable),
$this->normalizeTable($newTable),
$config,
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Platforms/SQLServer/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Platforms/SQLite/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
);
}

Expand Down
17 changes: 13 additions & 4 deletions src/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
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;
}
}
38 changes: 38 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,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
Expand Down Expand Up @@ -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
Expand Down