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

Comparator does not detect renamed foreign keys #2360

Open
yakobe opened this issue Apr 15, 2016 · 9 comments
Open

Comparator does not detect renamed foreign keys #2360

yakobe opened this issue Apr 15, 2016 · 9 comments

Comments

@yakobe
Copy link

yakobe commented Apr 15, 2016

The origin of this problem is here: doctrine/orm#4783
The comparator does not detect a change for names of foreign keys. It works fine for indexes, but foreign keys are just ignored.

A test for this change might look something like this (to match the index test):
https://github.com/doctrine/dbal/blob/master/tests/Doctrine/Tests/DBAL/Schema/ComparatorTest.php#L628

public function testCompareForeignKeyBasedOnPropertiesNotName()
    {
        $tableA = new Table("foo");
        $tableA->addColumn('id', 'integer');
        $tableA->addNamedForeignKeyConstraint('foo_constraint', 'bar', array('id'), array('id'));

        $tableB = new Table("foo");
        $tableB->addColumn('ID', 'integer');
        $tableB->addNamedForeignKeyConstraint('bar_constraint', 'bar', array('id'), array('id'));

        $c = new Comparator();
        $tableDiff = $c->diffTable($tableA, $tableB);

        $tableDiff = new TableDiff('foo');
        $tableDiff->fromTable = $tableA;
        $tableDiff->renamedForeignKeys['foo_constraint'] = new ForeignKeyConstraint(array('id'),'bar', array('id'), 'bar_constraint');

        $this->assertEquals(
            $tableDiff,
            $c->diffTable($tableA, $tableB)
        );
    }
@senaria
Copy link

senaria commented May 4, 2016

Is there any news about this? This possible solution looks like it would really help us out. Would be great if you could have a closer look.

@yakobe
Copy link
Author

yakobe commented Nov 7, 2016

Is it possible to get some feedback about this issue? Is it a confirmed bug? Is there any possibility of it being resolved? Can I or other members of the community help in any way or is it not a desired change?
It would be really useful to get some sort of status / opinion here.

@Ocramius
Copy link
Member

Ocramius commented Nov 7, 2016

As far as I know, this would currently just drop and recreate a FK. Not sure if worth fixing atm.

@yakobe
Copy link
Author

yakobe commented Nov 7, 2016

It causes a problem because it does not detect if a foreign key has been renamed. This means that it does not show up when creating automatic migrations, so the key keeps the old name after migrations.

If something more major then changes and want to drop this key, the new name is used in the generated migration. This new name is then not recognised when running the migration in production and everything crashes. Hard.

Normal indexes are handled correctly, but these are not. It's casuing us all manor of problems when preparing migrations. 😞

@Ocramius
Copy link
Member

Ocramius commented Nov 7, 2016

when running the migration in production and everything crashes. Hard.

You use doctrine/migrations or a similar tool for production handling, I hope?

@yakobe picking up the issue yourself is going to be the simplest way to get this fixed - unable to do it myself atm.

@yakobe
Copy link
Author

yakobe commented Nov 7, 2016

You use doctrine/migrations or a similar tool for production handling, I hope?

Yes, we are using doctrine migrations.

Just so I understand, this is would be an acceptable change? If so, I will look a bit deeper into the code, perhaps i can create a PR.

@Ocramius
Copy link
Member

Ocramius commented Nov 7, 2016

Yes, although you must make sure that cascade operations are also equivalent.

@yakobe
Copy link
Author

yakobe commented Nov 7, 2016

Yes, although you must make sure that cascade operations are also equivalent.

I would just try to fix it in a similar way to how the indexes work (and make the example test above work). What are the cascade operations that you are referring to?

@Ocramius
Copy link
Member

Ocramius commented Nov 7, 2016

What are the cascade operations that you are referring to?

Foreign keys may define cascade operation types - these must match in order for a rename to be suitable. If a cascade is changed, then a drop + recreate is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants