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

Regression in 2.9 constantly outputting table differences #3427

Closed
cs278 opened this issue Jan 9, 2019 · 8 comments
Closed

Regression in 2.9 constantly outputting table differences #3427

cs278 opened this issue Jan 9, 2019 · 8 comments

Comments

@cs278
Copy link

cs278 commented Jan 9, 2019

Bug Report

Q A
BC Break yes
Version 2.9.*

Summary

I've tried to upgrade a project to DBAL 2.9.2 from 2.8.1, however the Doctrine bundle schema diff command constantly outputs schema changes. This is triggered as we've overloaded the default datetime type with datetime_immutable.

Current behaviour

Schema diff constantly generates the following query:

ALTER TABLE test CHANGE dt dt DATETIME NOT NULL COMMENT '(DC2Type:datetime_immutable)'

How to reproduce

Given schema:

CREATE TABLE test (dt DATETIME NOT NULL COMMENT '(DC2Type:datetime_immutable)') DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;

Running the following, will exit 0 on 2.8.1 and error on 2.9.2.

<?php

use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\DriverManager;

require 'vendor/autoload.php';

Type::overrideType('datetime', \Doctrine\DBAL\Types\DateTimeImmutableType::class);

$conn = DriverManager::getConnection([
    'url' => 'mysql://root@127.0.0.1:3306/main',
]);

$conn->connect();

$platform = $conn->getDatabasePlatform();
$schemaManager = $conn->getSchemaManager();

$schema1 = $schemaManager->createSchema();

$schema2 = new Schema();
$table2 = $schema2->createTable('test');
$table2->addColumn('dt', 'datetime');
$diff = Comparator::compareSchemas($schema1, $schema2)->toSql($platform);

var_dump($diff);

exit($diff === [] ? 0 : 1);

Expected behaviour

No update queries should be generated.

Patch

Cause: dca1465#diff-6a252b2e905db4e03fc6a121a41e6466L415

This fixes the problem, not entirely sure if this is the correct resolution though.

diff --git a/lib/Doctrine/DBAL/Schema/Comparator.php b/lib/Doctrine/DBAL/Schema/Comparator.php
index 8297e05..33ee482 100644
--- a/lib/Doctrine/DBAL/Schema/Comparator.php
+++ b/lib/Doctrine/DBAL/Schema/Comparator.php
@@ -11,6 +11,7 @@
 use function array_shift;
 use function array_unique;
 use function count;
+use function get_class;
 use function strtolower;
 
 /**
@@ -419,7 +420,11 @@ public function diffColumn(Column $column1, Column $column2)
 
         $changedProperties = [];
 
-        foreach (['type', 'notnull', 'unsigned', 'autoincrement'] as $property) {
+        if (get_class($properties1['type']) !== get_class($properties2['type'])) {
+            $changedProperties[] = 'type';
+        }
+
+        foreach (['notnull', 'unsigned', 'autoincrement'] as $property) {
             if ($properties1[$property] === $properties2[$property]) {
                 continue;
             }
@Ocramius
Copy link
Member

@cs278 is it possible to isolate this into a test case?

@cs278
Copy link
Author

cs278 commented Jan 10, 2019

@Ocramius I guess so, as the root cause is that the Type is not the same instance. Does the patch seem reasonable or should I be comparing types in another way?

@Ocramius
Copy link
Member

Does the patch seem reasonable

No idea: I'd need a test first.

@garret-gunter
Copy link
Contributor

I created a merge request for this. #3456

I added two unit tests. Do you want to see a more thorough test case?

@morozov
Copy link
Member

morozov commented Jun 17, 2019

Fixed by #3456.

@SenseException
Copy link
Member

@garret-gunter @morozov Sounds like this issue can be closed now, right?

@morozov
Copy link
Member

morozov commented Jun 21, 2019

@SenseException, yes. Thank you.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants