Skip to content

Commit

Permalink
Merge pull request doctrine#5775 from morozov/remove-comparator-diff-…
Browse files Browse the repository at this point in the history
…table

Remove Comparator::diffTable()
  • Loading branch information
morozov committed Oct 18, 2022
2 parents 26b6351 + d9825af commit 88cfa33
Show file tree
Hide file tree
Showing 28 changed files with 169 additions and 213 deletions.
3 changes: 1 addition & 2 deletions UPGRADE.md
Expand Up @@ -645,7 +645,7 @@ The method `Comparator::compareSchemas()` cannot be called statically anymore.

## Removed `Comparator` methods

The `Comparator::compare()` and `::diffColumn()` methods have been removed.
The `Comparator::compare()`, `::diffTable()` and `::diffColumn()` methods have been removed.

## Removed `ColumnDiff` methods

Expand Down Expand Up @@ -736,7 +736,6 @@ Table columns are no longer indexed by column name. Use the `name` attribute of
## BC BREAK: Changes in the `Doctrine\DBAL\Schema` API

- Method `Doctrine\DBAL\Schema\AbstractSchemaManager::_getPortableViewDefinition()` no longer optionally returns false. It will always return a `Doctrine\DBAL\Schema\View` instance.
- Method `Doctrine\DBAL\Schema\Comparator::diffTable()` now optionally returns null instead of false.
- Property `Doctrine\DBAL\Schema\Table::$_primaryKeyName` is now optionally null instead of false.
- Method `Doctrine\DBAL\Schema\AbstractSchemaManager::tablesExist()` no longer accepts a string. Use `Doctrine\DBAL\Schema\AbstractSchemaManager::tableExists()` instead.
- Method `Doctrine\DBAL\Schema\OracleSchemaManager::createDatabase()` no longer accepts `null` for `$database` argument.
Expand Down
4 changes: 0 additions & 4 deletions psalm.xml.dist
Expand Up @@ -39,10 +39,6 @@
See https://github.com/doctrine/dbal/pull/4317
-->
<file name="tests/Functional/LegacyAPITest.php"/>
<!--
TODO: remove in 4.0.0
-->
<referencedMethod name="Doctrine\DBAL\Schema\Comparator::diffTable"/>
</errorLevel>
</DeprecatedMethod>
<DocblockTypeContradiction>
Expand Down
4 changes: 2 additions & 2 deletions src/Platforms/MySQL/Comparator.php
Expand Up @@ -30,9 +30,9 @@ public function __construct(
parent::__construct($platform);
}

public function diffTable(Table $oldTable, Table $newTable): ?TableDiff
public function compareTables(Table $oldTable, Table $newTable): TableDiff
{
return parent::diffTable(
return parent::compareTables(
$this->normalizeTable($oldTable),
$this->normalizeTable($newTable),
);
Expand Down
4 changes: 2 additions & 2 deletions src/Platforms/SQLServer/Comparator.php
Expand Up @@ -22,15 +22,15 @@ public function __construct(SQLServerPlatform $platform, private readonly string
parent::__construct($platform);
}

public function diffTable(Table $oldTable, Table $newTable): ?TableDiff
public function compareTables(Table $oldTable, Table $newTable): TableDiff
{
$oldTable = clone $oldTable;
$newTable = clone $newTable;

$this->normalizeColumns($oldTable);
$this->normalizeColumns($newTable);

return parent::diffTable($oldTable, $newTable);
return parent::compareTables($oldTable, $newTable);
}

private function normalizeColumns(Table $table): void
Expand Down
4 changes: 2 additions & 2 deletions src/Platforms/SQLite/Comparator.php
Expand Up @@ -24,15 +24,15 @@ public function __construct(SQLitePlatform $platform)
parent::__construct($platform);
}

public function diffTable(Table $oldTable, Table $newTable): ?TableDiff
public function compareTables(Table $oldTable, Table $newTable): TableDiff
{
$oldTable = clone $oldTable;
$newTable = clone $newTable;

$this->normalizeColumns($oldTable);
$this->normalizeColumns($newTable);

return parent::diffTable($oldTable, $newTable);
return parent::compareTables($oldTable, $newTable);
}

private function normalizeColumns(Table $table): void
Expand Down
36 changes: 5 additions & 31 deletions src/Schema/Comparator.php
Expand Up @@ -6,7 +6,6 @@

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\Deprecations\Deprecation;

use function array_map;
use function assert;
Expand All @@ -25,6 +24,8 @@ public function __construct(private readonly AbstractPlatform $platform)

/**
* Returns the differences between the schemas.
*
* @throws Exception
*/
public function compareSchemas(Schema $oldSchema, Schema $newSchema): SchemaDiff
{
Expand Down Expand Up @@ -58,13 +59,13 @@ public function compareSchemas(Schema $oldSchema, Schema $newSchema): SchemaDiff
if (! $oldSchema->hasTable($newTableName)) {
$createdTables[] = $newSchema->getTable($newTableName);
} else {
$tableDifferences = $this->diffTable(
$tableDiff = $this->compareTables(
$oldSchema->getTable($newTableName),
$newSchema->getTable($newTableName),
);

if ($tableDifferences !== null) {
$alteredTables[] = $tableDifferences;
if (! $tableDiff->isEmpty()) {
$alteredTables[] = $tableDiff;
}
}
}
Expand Down Expand Up @@ -140,33 +141,6 @@ public function diffSequence(Sequence $sequence1, Sequence $sequence2): bool
return $sequence1->getInitialValue() !== $sequence2->getInitialValue();
}

/**
* Returns the difference between the tables.
*
* If there are no differences this method returns null.
*
* @deprecated Use {@see compareTables()} and, optionally, {@see TableDiff::isEmpty()} instead.
*
* @throws Exception
*/
public function diffTable(Table $oldTable, Table $newTable): ?TableDiff
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/5770',
'%s is deprecated. Use compareTables() instead.',
__METHOD__,
);

$diff = $this->compareTables($oldTable, $newTable);

if ($diff->isEmpty()) {
return null;
}

return $diff;
}

/**
* Compares the tables and returns the difference between them.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/Platform/AddColumnWithDefaultTest.php
Expand Up @@ -26,11 +26,11 @@ public function testAddColumnWithDefault(): void
'default' => 'DEFAULT',
]);

$diff = $schemaManager->createComparator()->diffTable(
$diff = $schemaManager->createComparator()->compareTables(
$schemaManager->introspectTable('add_default_test'),
$table,
);
self::assertNotNull($diff);

$schemaManager->alterTable($diff);

$query = 'SELECT original_field, new_field FROM add_default_test';
Expand Down
3 changes: 1 addition & 2 deletions tests/Functional/Platform/AlterColumnTest.php
Expand Up @@ -25,9 +25,8 @@ public function testColumnPositionRetainedAfterAltering(): void

$sm = $this->connection->createSchemaManager();
$diff = $sm->createComparator()
->diffTable($sm->introspectTable('test_alter'), $table);
->compareTables($sm->introspectTable('test_alter'), $table);

self::assertNotNull($diff);
$sm->alterTable($diff);

$table = $sm->introspectTable('test_alter');
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/Platform/AlterDecimalColumnTest.php
Expand Up @@ -24,8 +24,8 @@ public function testAlterPrecisionAndScale(int $newPrecision, int $newScale): vo
$schemaManager = $this->connection->createSchemaManager();

$diff = $schemaManager->createComparator()
->diffTable($schemaManager->introspectTable('decimal_table'), $table);
self::assertNotNull($diff);
->compareTables($schemaManager->introspectTable('decimal_table'), $table);

$schemaManager->alterTable($diff);

$table = $schemaManager->introspectTable('decimal_table');
Expand Down
3 changes: 1 addition & 2 deletions tests/Functional/Platform/RenameColumnTest.php
Expand Up @@ -23,9 +23,8 @@ public function testColumnPositionRetainedAfterRenaming(string $columnName, stri

$sm = $this->connection->createSchemaManager();
$diff = $sm->createComparator()
->diffTable($sm->introspectTable('test_rename'), $table);
->compareTables($sm->introspectTable('test_rename'), $table);

self::assertNotNull($diff);
$sm->alterTable($diff);

$table = $sm->introspectTable('test_rename');
Expand Down
3 changes: 1 addition & 2 deletions tests/Functional/Schema/ColumnCommentTest.php
Expand Up @@ -93,8 +93,7 @@ public function testAlterColumnComment(string $comment1, string $comment2): void
$schemaManager = $this->connection->createSchemaManager();

$diff = $schemaManager->createComparator()
->diffTable($table1, $table2);
self::assertNotNull($diff);
->compareTables($table1, $table2);

$schemaManager->alterTable($diff);

Expand Down
5 changes: 3 additions & 2 deletions tests/Functional/Schema/ComparatorTest.php
Expand Up @@ -39,9 +39,10 @@ public function testDefaultValueComparison(string $type, mixed $value): void

$onlineTable = $this->schemaManager->introspectTable('default_value');

self::assertNull(
self::assertTrue(
$this->schemaManager->createComparator()
->diffTable($table, $onlineTable),
->compareTables($table, $onlineTable)
->isEmpty(),
);
}

Expand Down
20 changes: 13 additions & 7 deletions tests/Functional/Schema/ComparatorTestUtils.php
Expand Up @@ -19,8 +19,8 @@ public static function diffFromActualToDesiredTable(
AbstractSchemaManager $schemaManager,
Comparator $comparator,
Table $desiredTable,
): ?TableDiff {
return $comparator->diffTable(
): TableDiff {
return $comparator->compareTables(
$schemaManager->introspectTable($desiredTable->getName()),
$desiredTable,
);
Expand All @@ -31,8 +31,8 @@ public static function diffFromDesiredToActualTable(
AbstractSchemaManager $schemaManager,
Comparator $comparator,
Table $desiredTable,
): ?TableDiff {
return $comparator->diffTable(
): TableDiff {
return $comparator->compareTables(
$desiredTable,
$schemaManager->introspectTable($desiredTable->getName()),
);
Expand All @@ -44,11 +44,17 @@ public static function assertDiffNotEmpty(Connection $connection, Comparator $co

$diff = self::diffFromActualToDesiredTable($schemaManager, $comparator, $table);

TestCase::assertNotNull($diff);
TestCase::assertFalse($diff->isEmpty());

$schemaManager->alterTable($diff);

TestCase::assertNull(self::diffFromActualToDesiredTable($schemaManager, $comparator, $table));
TestCase::assertNull(self::diffFromDesiredToActualTable($schemaManager, $comparator, $table));
TestCase::assertTrue(
self::diffFromActualToDesiredTable($schemaManager, $comparator, $table)
->isEmpty(),
);
TestCase::assertTrue(
self::diffFromDesiredToActualTable($schemaManager, $comparator, $table)
->isEmpty(),
);
}
}
24 changes: 12 additions & 12 deletions tests/Functional/Schema/MySQL/ComparatorTest.php
Expand Up @@ -41,17 +41,17 @@ public function testLobLengthIncrementWithinLimit(string $type, int $length): vo
$table = $this->createLobTable($type, $length - 1);
$this->setBlobLength($table, $length);

self::assertNull(ComparatorTestUtils::diffFromActualToDesiredTable(
self::assertTrue(ComparatorTestUtils::diffFromActualToDesiredTable(
$this->schemaManager,
$this->comparator,
$table,
));
)->isEmpty());

self::assertNull(ComparatorTestUtils::diffFromDesiredToActualTable(
self::assertTrue(ComparatorTestUtils::diffFromDesiredToActualTable(
$this->schemaManager,
$this->comparator,
$table,
));
)->isEmpty());
}

/** @dataProvider lobColumnProvider */
Expand Down Expand Up @@ -96,17 +96,17 @@ public function testExplicitDefaultCollation(): void
[$table, $column] = $this->createCollationTable();
$column->setPlatformOption('collation', 'utf8mb4_general_ci');

self::assertNull(ComparatorTestUtils::diffFromActualToDesiredTable(
self::assertTrue(ComparatorTestUtils::diffFromActualToDesiredTable(
$this->schemaManager,
$this->comparator,
$table,
));
)->isEmpty());

self::assertNull(ComparatorTestUtils::diffFromDesiredToActualTable(
self::assertTrue(ComparatorTestUtils::diffFromDesiredToActualTable(
$this->schemaManager,
$this->comparator,
$table,
));
)->isEmpty());
}

public function testChangeColumnCharsetAndCollation(): void
Expand Down Expand Up @@ -142,17 +142,17 @@ public function testTableAndColumnOptions(array $tableOptions, array $columnOpti

$this->dropAndCreateTable($table);

self::assertNull(ComparatorTestUtils::diffFromActualToDesiredTable(
self::assertTrue(ComparatorTestUtils::diffFromActualToDesiredTable(
$this->schemaManager,
$this->comparator,
$table,
));
)->isEmpty());

self::assertNull(ComparatorTestUtils::diffFromDesiredToActualTable(
self::assertTrue(ComparatorTestUtils::diffFromDesiredToActualTable(
$this->schemaManager,
$this->comparator,
$table,
));
)->isEmpty());
}

/** @return iterable<string,array{array<string,string>,array<string,string>}> */
Expand Down

0 comments on commit 88cfa33

Please sign in to comment.