Skip to content

Commit

Permalink
Make column and index renaming configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Feb 6, 2024
1 parent ac75c3c commit 82624c0
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 7 deletions.
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
{
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

0 comments on commit 82624c0

Please sign in to comment.