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

MySQL getListTableForeignKeysSQL: use current database if null passed #856

Merged
merged 2 commits into from
Aug 10, 2015

Conversation

naderman
Copy link
Member

In line with the behavior of getListTableIndexesSQL() the foreign key function should select only foreign keys from the current database if no database name is specified. Otherwise it returns foreign keys of all tables in any database with the given name. This can especially lead to issues if you install different versions of the same schema into multiple databases on the same server.

This function is always called with $database = null in the following chain, which leads to SQL errors when trying to setup/delete a schema in tests on a mysql server that contains another copy of the schema in another database:

Doctrine\ORM\Tools\SchemaTool::dropDatabase()
Doctrine\ORM\Tools\SchemaTool::getDropDatabaseSQL()
Doctrine\DBAL\Schema\AbstractSchemaManager::createSchema()
Doctrine\DBAL\Schema\AbstractSchemaManager::listTables()
Doctrine\DBAL\Schema\AbstractSchemaManager::listTableDetails($tableName)
Doctrine\DBAL\Schema\AbstractSchemaManager::listTableForeignKeys($table, null)
Doctrine\DBAL\Platforms\MySqlPlatform::getListTableForeignKeysSQL($table, null)

I think the $database parameter would ideally be required, and an exception should be thrown if it is null. The AbstractSchemaManager should be modified to consistently pass the database name to the platform in all its calls. But for now this workaround corrects the issue for foreign keys.

@doctrinebot
Copy link

Hello,

thank you for creating this pull request. I have automatically opened an issue
on our Jira Bug Tracker for you. See the issue link:

http://www.doctrine-project.org/jira/browse/DBAL-1232

We use Jira to track the state of pull requests and the versions they got
included in.

@beberlei
Copy link
Member

@deeky666 This is a serious bug, because its very hard to find when it occurs to you.

@deeky666
Copy link
Member

@naderman is it possible to add a functional test to avoid regressions in the future or do you think this is too much effort in this case?

@naderman
Copy link
Member Author

@deeky666 Just haven't looked at doctrine's test set up at all so far, so no idea how much work this would be. Basically just need to create two databases with the same table with a foreign key each with different names and see if a select returns only the one on the right database.

@beberlei
Copy link
Member

The tests still passing is a good indicator that it still works as expected in the scenario we care about (listTableDetails()). I couldn't think of a reliable way to test this with our testing setup without possibly breaking the testsuite. We do have two databases (doctrine_tests and doctrine_tests_tmp) but the later one has to be empty for some of the cleanup tricks we use.

@@ -191,6 +191,8 @@ public function getListTableForeignKeysSQL($table, $database = null)

if ($database) {
$sql .= " AND k.table_schema = '$database' /*!50116 AND c.constraint_schema = '$database' */";
} else {
$sql .= " AND k.table_schema = DATABASE() /*!50116 AND c.constraint_schema = DATABASE() */";
Copy link
Member

Choose a reason for hiding this comment

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

Can we try to avoid SQL duplication here by doing something like this:

$database = null === $database ? 'DATABASE()' : "'$database'";

@naderman
Copy link
Member Author

@zeroedin-bill as we discussed on IRC this is rather hard to test in the current setup as it requires an additional database to be set up.

@naderman
Copy link
Member Author

Did make the change @deeky666 suggested, so this should be mergable?

@naderman
Copy link
Member Author

Test failure is a network issue on the travis box, you can restart those 2 failed builds and it should be fine.

beberlei added a commit that referenced this pull request Aug 10, 2015
MySQL getListTableForeignKeysSQL: use current database if null passed
@beberlei beberlei merged commit 3a698bd into doctrine:master Aug 10, 2015
@naderman
Copy link
Member Author

@beberlei @deeky666 why was this not backported into the 2.5 branch? Kept wondering why this was still broken in 2.5.2 :(

@deeky666
Copy link
Member

deeky666 commented Jan 5, 2016

@naderman backport has been forgotten. Will tag this for 2.5.5 then.

@deeky666 deeky666 added this to the 2.5.5 milestone Jan 5, 2016
@deeky666
Copy link
Member

deeky666 commented Jan 5, 2016

Backported into 2.5 via 43bb31e and e24444e

@naderman
Copy link
Member Author

naderman commented Jan 6, 2016

Thanks @deeky666 !

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

Successfully merging this pull request may close these issues.

5 participants