Skip to content

Commit

Permalink
Deprecate inconsistent Comparator API
Browse files Browse the repository at this point in the history
This commit deprecates:
- calls to compare(), and
- static calls to compareSchemas().
  • Loading branch information
trompette committed Jul 12, 2021
1 parent 304bae4 commit f8eb927
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 27 deletions.
9 changes: 9 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ parameters:
paths:
- %currentWorkingDirectory%/src/Platforms/AbstractPlatform.php

# See https://github.com/doctrine/dbal/pull/4707
# TODO: remove in 4.0.0
-
message: '~^Dynamic call to static method Doctrine\\DBAL\\Schema\\Comparator::compareSchemas\(\)\.$~'
paths:
- %currentWorkingDirectory%/src/Schema/AbstractSchemaManager.php
- %currentWorkingDirectory%/src/Schema/Comparator.php
- %currentWorkingDirectory%/src/Schema/Schema.php

# We're checking for invalid invalid input
-
message: "#^Strict comparison using \\!\\=\\= between null and null will always evaluate to false\\.$#"
Expand Down
7 changes: 6 additions & 1 deletion psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@
-->
<file name="src/Schema/AbstractSchemaManager.php" />
<file name="src/Schema/PostgreSQLSchemaManager.php" />
<directory name="tests" />
<!--
See https://github.com/doctrine/dbal/pull/4707
TODO: remove in 4.0.0
-->
<file name="src/Schema/Comparator.php" />
<directory name="tests" />
<referencedMethod name="Doctrine\DBAL\Statement::execute"/>
</errorLevel>
</DeprecatedMethod>
Expand Down
4 changes: 3 additions & 1 deletion src/Schema/AbstractSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,9 @@ public function alterSchema(SchemaDiff $schemaDiff): void
*/
public function migrateSchema(Schema $toSchema): void
{
$this->alterSchema(Comparator::compareSchemas($this->createSchema(), $toSchema));
$schemaDiff = (new Comparator())->compareSchemas($this->createSchema(), $toSchema);

$this->alterSchema($schemaDiff);
}

/* alterTable() Methods */
Expand Down
46 changes: 26 additions & 20 deletions src/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Schema;

use Doctrine\DBAL\Types;
use Doctrine\Deprecations\Deprecation;

use function array_intersect_key;
use function array_key_exists;
Expand All @@ -20,31 +21,18 @@
*/
class Comparator
{
/**
* @return SchemaDiff
*
* @throws SchemaException
*/
public static function compareSchemas(Schema $fromSchema, Schema $toSchema)
{
$c = new self();

return $c->compare($fromSchema, $toSchema);
}

/**
* Returns a SchemaDiff object containing the differences between the schemas $fromSchema and $toSchema.
*
* The returned differences are returned in such a way that they contain the
* operations to change the schema stored in $fromSchema to the schema that is
* stored in $toSchema.
* This method should be called non-statically since it will be declared as non-static in the next major release.
*
* @return SchemaDiff
*
* @throws SchemaException
*/
public function compare(Schema $fromSchema, Schema $toSchema)
public static function compareSchemas(Schema $fromSchema, Schema $toSchema)
{
$comparator = new self();
$diff = new SchemaDiff();
$diff->fromSchema = $fromSchema;

Expand All @@ -71,7 +59,7 @@ public function compare(Schema $fromSchema, Schema $toSchema)
if (! $fromSchema->hasTable($tableName)) {
$diff->newTables[$tableName] = $toSchema->getTable($tableName);
} else {
$tableDifferences = $this->diffTable(
$tableDifferences = $comparator->diffTable(
$fromSchema->getTable($tableName),
$toSchema->getTable($tableName)
);
Expand Down Expand Up @@ -134,18 +122,18 @@ public function compare(Schema $fromSchema, Schema $toSchema)
foreach ($toSchema->getSequences() as $sequence) {
$sequenceName = $sequence->getShortestName($toSchema->getName());
if (! $fromSchema->hasSequence($sequenceName)) {
if (! $this->isAutoIncrementSequenceInSchema($fromSchema, $sequence)) {
if (! $comparator->isAutoIncrementSequenceInSchema($fromSchema, $sequence)) {
$diff->newSequences[] = $sequence;
}
} else {
if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) {
if ($comparator->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) {
$diff->changedSequences[] = $toSchema->getSequence($sequenceName);
}
}
}

foreach ($fromSchema->getSequences() as $sequence) {
if ($this->isAutoIncrementSequenceInSchema($toSchema, $sequence)) {
if ($comparator->isAutoIncrementSequenceInSchema($toSchema, $sequence)) {
continue;
}

Expand All @@ -161,6 +149,24 @@ public function compare(Schema $fromSchema, Schema $toSchema)
return $diff;
}

/**
* @deprecated Use non-static call to {@link compareSchemas()} instead.
*
* @return SchemaDiff
*
* @throws SchemaException
*/
public function compare(Schema $fromSchema, Schema $toSchema)
{
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4707',
'Method compare() is deprecated. Use a non-static call to compareSchemas() instead.'
);

return $this->compareSchemas($fromSchema, $toSchema);
}

/**
* @param Schema $schema
* @param Sequence $sequence
Expand Down
6 changes: 2 additions & 4 deletions src/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,7 @@ public function toDropSql(AbstractPlatform $platform)
*/
public function getMigrateToSql(Schema $toSchema, AbstractPlatform $platform)
{
$comparator = new Comparator();
$schemaDiff = $comparator->compare($this, $toSchema);
$schemaDiff = (new Comparator())->compareSchemas($this, $toSchema);

return $schemaDiff->toSql($platform);
}
Expand All @@ -450,8 +449,7 @@ public function getMigrateToSql(Schema $toSchema, AbstractPlatform $platform)
*/
public function getMigrateFromSql(Schema $fromSchema, AbstractPlatform $platform)
{
$comparator = new Comparator();
$schemaDiff = $comparator->compare($fromSchema, $this);
$schemaDiff = (new Comparator())->compareSchemas($fromSchema, $this);

return $schemaDiff->toSql($platform);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Schema/SchemaDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use function array_merge;

/**
* Schema Diff.
* Differences between two schemas.
*
* The object contains the operations to change the schema stored in $fromSchema
* to a target schema.
*/
class SchemaDiff
{
Expand Down

0 comments on commit f8eb927

Please sign in to comment.