Skip to content

Commit

Permalink
Implemented comparison of default values as strings regardless of the…
Browse files Browse the repository at this point in the history
…ir PHP types

Fixes doctrine#3336.
  • Loading branch information
morozov committed Nov 24, 2018
1 parent 296b0e5 commit a5a472f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/Doctrine/DBAL/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use function array_shift;
use function array_unique;
use function count;
use function strcmp;
use function strtolower;

/**
Expand Down Expand Up @@ -432,12 +433,12 @@ public function diffColumn(Column $column1, Column $column2)
$changedProperties[] = 'comment';
}

if ($properties1['default'] !== $properties2['default'] ||
// Null values need to be checked additionally as they tell whether to create or drop a default value.
// null != 0, null != false, null != '' etc. This affects platform's table alteration SQL generation.
($properties1['default'] === null && $properties2['default'] !== null) ||
($properties2['default'] === null && $properties1['default'] !== null)
) {
$default1IsNull = $properties1['default'] === null;
$default2IsNull = $properties2['default'] === null;

// Null values need to be checked additionally as they tell whether to create or drop a default value.
// null != 0, null != false, null != '' etc. This affects platform's table alteration SQL generation.
if ($default1IsNull !== $default2IsNull || strcmp($properties1['default'], $properties2['default']) !== 0) {
$changedProperties[] = 'default';
}

Expand Down
39 changes: 39 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/Schema/ComparatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\DBAL\Functional\Schema;

use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Table;
use Doctrine\Tests\DbalFunctionalTestCase;

class ComparatorTest extends DbalFunctionalTestCase
{
/** @var AbstractSchemaManager */
private $schemaManager;

/** @var Comparator */
private $comparator;

protected function setUp()
{
parent::setUp();

$this->schemaManager = $this->connection->getSchemaManager();
$this->comparator = new Comparator();
}

public function testDefaultValueComparison()
{
$table = new Table('default_value');
$table->addColumn('id', 'integer', ['default' => 1]);

$this->schemaManager->createTable($table);

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

self::assertFalse($this->comparator->diffTable($table, $onlineTable));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ protected function setUp()
return;
}

$this->resetSharedConn();

Type::addType('point', MySqlPointType::class);
}

Expand Down

0 comments on commit a5a472f

Please sign in to comment.