Skip to content

Commit

Permalink
Use setter method instead of config parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Mar 11, 2024
1 parent 82624c0 commit d0c8602
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
4 changes: 1 addition & 3 deletions src/Platforms/MySQL/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

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 @@ -31,12 +30,11 @@ public function __construct(
parent::__construct($platform);
}

public function compareTables(Table $oldTable, Table $newTable, ?ComparatorConfig $config = null): TableDiff
public function compareTables(Table $oldTable, Table $newTable): TableDiff
{
return parent::compareTables(
$this->normalizeTable($oldTable),
$this->normalizeTable($newTable),
$config,
);
}

Expand Down
4 changes: 1 addition & 3 deletions src/Platforms/SQLServer/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

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 @@ -23,12 +22,11 @@ public function __construct(SQLServerPlatform $platform, private readonly string
parent::__construct($platform);
}

public function compareTables(Table $oldTable, Table $newTable, ?ComparatorConfig $config = null): TableDiff
public function compareTables(Table $oldTable, Table $newTable): TableDiff
{
return parent::compareTables(
$this->normalizeColumns($oldTable),
$this->normalizeColumns($newTable),
$config,
);
}

Expand Down
4 changes: 1 addition & 3 deletions src/Platforms/SQLite/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

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 @@ -25,12 +24,11 @@ public function __construct(SQLitePlatform $platform)
parent::__construct($platform);
}

public function compareTables(Table $oldTable, Table $newTable, ?ComparatorConfig $config = null): TableDiff
public function compareTables(Table $oldTable, Table $newTable): TableDiff
{
return parent::compareTables(
$this->normalizeColumns($oldTable),
$this->normalizeColumns($newTable),
$config,
);
}

Expand Down
20 changes: 12 additions & 8 deletions src/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@
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;
}

/**
* Returns the differences between the schemas.
*/
public function compareSchemas(Schema $oldSchema, Schema $newSchema, ?ComparatorConfig $config = null): SchemaDiff
public function compareSchemas(Schema $oldSchema, Schema $newSchema): SchemaDiff
{
$createdSchemas = [];
$droppedSchemas = [];
Expand Down Expand Up @@ -59,7 +66,6 @@ public function compareSchemas(Schema $oldSchema, Schema $newSchema, ?Comparator
$tableDiff = $this->compareTables(
$oldSchema->getTable($newTableName),
$newSchema->getTable($newTableName),
$config,
);

if (! $tableDiff->isEmpty()) {
Expand Down Expand Up @@ -142,10 +148,8 @@ public function diffSequence(Sequence $sequence1, Sequence $sequence2): bool
/**
* Compares the tables and returns the difference between them.
*/
public function compareTables(Table $oldTable, Table $newTable, ?ComparatorConfig $config = null): TableDiff
public function compareTables(Table $oldTable, Table $newTable): TableDiff
{
$config ??= new ComparatorConfig();

$addedColumns = [];
$modifiedColumns = [];
$droppedColumns = [];
Expand Down Expand Up @@ -192,7 +196,7 @@ public function compareTables(Table $oldTable, Table $newTable, ?ComparatorConfi
$modifiedColumns[] = new ColumnDiff($oldColumn, $newColumn);
}

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

Expand Down Expand Up @@ -231,7 +235,7 @@ public function compareTables(Table $oldTable, Table $newTable, ?ComparatorConfi
$modifiedIndexes[] = $newIndex;
}

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

Expand Down
6 changes: 4 additions & 2 deletions tests/Schema/AbstractComparatorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@ public function testDetectRenameColumnDisabled(): void

$config = new ComparatorConfig();
$config->setDetectRenamedColumns(false);
$tableDiff = $this->comparator->compareTables($tableA, $tableB, $config);
$this->comparator->setConfig($config);
$tableDiff = $this->comparator->compareTables($tableA, $tableB);

self::assertCount(1, $tableDiff->getAddedColumns());
self::assertCount(1, $tableDiff->getDroppedColumns());
Expand Down Expand Up @@ -468,7 +469,8 @@ public function testDetectRenameIndexDisabled(): void

$config = new ComparatorConfig();
$config->setDetectRenamedIndexes(false);
$tableDiff = $this->comparator->compareTables($table1, $table2, $config);
$this->comparator->setConfig($config);
$tableDiff = $this->comparator->compareTables($table1, $table2);

self::assertCount(1, $tableDiff->getAddedIndexes());
self::assertCount(1, $tableDiff->getDroppedIndexes());
Expand Down

0 comments on commit d0c8602

Please sign in to comment.