Skip to content

Commit

Permalink
Merge pull request #5220 from greg0ire/platform-aware-comparison-thro…
Browse files Browse the repository at this point in the history
…ugh-compare-schemas-magic

Allow dynamic call to compareSchemas
  • Loading branch information
greg0ire committed Jan 30, 2022
2 parents 9ddb464 + 1898f74 commit 5b6eb6c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
8 changes: 8 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,13 @@ parameters:
# Type check for legacy implementations of the Connection interface
# TODO: remove in 4.0.0
- "~Call to function method_exists\\(\\) with Doctrine\\\\DBAL\\\\Driver\\\\Connection and 'getNativeConnection' will always evaluate to true\\.~"

# TODO: remove in 4.0.0
-
message: '~^Call to an undefined method.*compareSchemas.*$~'
paths:
- src/Schema/AbstractSchemaManager.php
- src/Schema/Comparator.php
- src/Schema/Schema.php
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
41 changes: 35 additions & 6 deletions src/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\DBAL\Schema;

use BadMethodCallException;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types;
Expand All @@ -16,10 +17,13 @@
use function assert;
use function count;
use function get_class;
use function sprintf;
use function strtolower;

/**
* Compares two Schemas and return an instance of SchemaDiff.
*
* @method SchemaDiff compareSchemas(Schema $fromSchema, Schema $toSchema)
*/
class Comparator
{
Expand All @@ -44,6 +48,32 @@ public function __construct(?AbstractPlatform $platform = null)
$this->platform = $platform;
}

/**
* @param list<mixed> $args
*/
public function __call(string $method, array $args): SchemaDiff
{
if ($method !== 'compareSchemas') {
throw new BadMethodCallException(sprintf('Unknown method "%s"', $method));
}

return $this->doCompareSchemas(...$args);
}

/**
* @param list<mixed> $args
*/
public static function __callStatic(string $method, array $args): SchemaDiff
{
if ($method !== 'compareSchemas') {
throw new BadMethodCallException(sprintf('Unknown method "%s"', $method));
}

$comparator = new self();

return $comparator->doCompareSchemas(...$args);
}

/**
* Returns a SchemaDiff object containing the differences between the schemas $fromSchema and $toSchema.
*
Expand All @@ -53,11 +83,10 @@ public function __construct(?AbstractPlatform $platform = null)
*
* @throws SchemaException
*/
public static function compareSchemas(
private function doCompareSchemas(
Schema $fromSchema,
Schema $toSchema
) {
$comparator = new self();
$diff = new SchemaDiff();
$diff->fromSchema = $fromSchema;

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

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

Expand Down
5 changes: 5 additions & 0 deletions tests/Platforms/SQLite/ComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ protected function setUp(): void
{
$this->comparator = new Comparator(new SqlitePlatform());
}

public function testCompareChangedBinaryColumn(): void
{
self::markTestSkipped('Binary columns are BLOB in SQLite and do not support $length or $fixed.');
}
}

0 comments on commit 5b6eb6c

Please sign in to comment.