Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented comparison of default values as strings regardless of their PHP types #3355

Merged
merged 1 commit into from
Nov 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/Doctrine/DBAL/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,10 @@ 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)
) {
// 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 (($properties1['default'] === null) !== ($properties2['default'] === null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@morozov not sure that's not my problem but with annotation like this:

@ORM\Column(type="boolean", nullable=false, options={"default" = false})

it always generates migration becouse of this fix. compared with old version https://3v4l.org/HBLG7 :

<?php
$changedProperties = [];
$properties1['default'] = false;
$properties2['default'] = 0;

if ($properties1['default'] != $properties2['default'] ||
    (null === $properties1['default'] && null !== $properties2['default']) ||
    (null === $properties2['default'] && null !== $properties1['default'])
) {
    $changedProperties[] = 'default';
}

var_dump($changedProperties);

if (($properties1['default'] === null) !== ($properties2['default'] === null)
    || (string) $properties1['default'] !== (string) $properties2['default']) {
    $changedProperties[] = 'default';
}

var_dump($changedProperties);

result:

array(0) {
}
array(1) {
  [0]=>
  string(7) "default"
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do not support true/false values for boolean type maybe do not allow them?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because ('0' == false) === true but ('0' === (string) false) === false.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Majkl578 yes yes... but should I update my default value to 0 or this will be fixed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll loosen === back to == for the patch release, but later, the missing component here seems to be Type::convertToDatabaseValue(). We're loosely comparing a PHP value with a DB value which may have its own not yet discovered edge cases.

|| (string) $properties1['default'] !== (string) $properties2['default']) {
$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();
Copy link
Member Author

@morozov morozov Nov 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See build #6465. This is needed since a new functional test is added prior to this one. In this case, the platform instance associated with the shared connection has already initialized the type mappings and won't reinitialize them with the newly added type.


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

Expand Down