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

The primary key columns don't have to be in the same order as the table columns #516

Merged
merged 7 commits into from
Mar 25, 2014
Merged

The primary key columns don't have to be in the same order as the table columns #516

merged 7 commits into from
Mar 25, 2014

Conversation

bartv2
Copy link
Contributor

@bartv2 bartv2 commented Jan 17, 2014

This happens when the columns are created in a different order then in the PK. This doesn't happen with Doctrine, as the order is taken from the PK.

@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-793

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

@Ocramius
Copy link
Member

@bartv2 seems like Doctrine\Tests\DBAL\Functional\Schema\SqliteSchemaManagerTest::testListTableIndexes is failing on sqlite now.

Can you also add a test that ensures the order of the keys is kept?

@bartv2
Copy link
Contributor Author

bartv2 commented Jan 26, 2014

Hmm, this change depends on sqlite version 3.7.16. See http://www.sqlite.org/changes.html#version_3_7_16
Enhance the PRAGMA table_info command so that the "pk" column is an increasing integer to show the order of columns in the primary key.
I'm not sure how to solve this now.

@bartv2
Copy link
Contributor Author

bartv2 commented Jan 31, 2014

@beberlei @deeky666 I can add a version check to line 171, but is that acceptable?

@Ocramius
Copy link
Member

@bartv2 if the check is platform-specific, then it probably needs a method in the platform /cc @deeky666

@deeky666
Copy link
Member

@bartv2 @Ocramius I would not add a version check in the schema manager, this seems an ugly hack. I think we had something like this in PostgreSQL and removed it again.
What I would try to do is just accept the fact that you cannot determine the order of PK columns before that version and rather use the order that is given instead.
So you would have to change the implementation not to set the ordinal PK column number as key in the index explicitely but rather just add columns to the index column stack and sort by key afterwards.

@kimhemsoe
Copy link
Member

Agree dekyy

On 31/01/2014, at 20.26, "Steve Müller" notifications@github.com wrote:

@bartv2 https://github.com/bartv2 @Ocramius
https://github.com/OcramiusI would not add a version check in the
schema manager, this seems an ugly
hack. I think we had something like this in PostgreSQL and removed it again.
What I would try to do is just accept the fact that you cannot determine
the order of PK columns before that version and rather use the order that
is given instead.
So you would have to change the implementation not to set the ordinal PK
column number as key in the index explicitely but rather just add columns
to the index column stack and sort by key afterwards.

Reply to this email directly or view it on
GitHubhttps://github.com//pull/516#issuecomment-33833855
.

@deeky666
Copy link
Member

Sort by SQLite's PK index number of course, not array key.

@bartv2
Copy link
Contributor Author

bartv2 commented Feb 7, 2014

How is this, all the tests pass

if ($a['pk'] == $b['pk']) {
return $a['cid'] - $b['cid'];
}
if ($a['pk'] == "0" && $b['pk'] != "0") {
Copy link
Member

Choose a reason for hiding this comment

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

Are this conditional and the one at line 176 required?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right they are not, it 0 columns location doesn't matter

{
$version = \SQLite3::version();
if(version_compare($version['versionString'], '3.7.16', '<')) {
$this->markTestSkipped('This version of sqlite doesn\'t return the order of the Primary Key.');
Copy link
Member

Choose a reason for hiding this comment

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

What does that mean? We get a notice in the SqliteSchemaManager because some keys (pk or cid) do not exist? Or does the usort work as well.

Copy link
Member

Choose a reason for hiding this comment

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

@beberlei The test only works on SQLite versions > 3.7.16 because PK columns order information was added for the PRAGMA TABLE_INFO() statement there. Before that it is not possible to tell in which order the columns in a PK were defined when reverse engineering with PRAGMA TABLE_INFO().
Therefore this test will fail for earlier versions as it retrieves array('id', 'other_id') instead of array('other_id', 'id').

Copy link
Member

Choose a reason for hiding this comment

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

I'm not quite happy with this version constraint. I see the limitation for earlier SQLite versions but IMO we should at least document this limitation in the known vendors issues. I don't want to see any complaints about this issue from users using a SQLite version before this fix.

Copy link
Contributor

Choose a reason for hiding this comment

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

@deeky666 I am not sure @beberlei's comment has been addressed. He is referring to the changes in lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php. If the pk column is not there on older Sqlite versions, the usort($indexArray, function($a, $b) { call will probably throw notices for undefined array index.

Copy link
Member

Choose a reason for hiding this comment

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

@bantu The pk column is there, it's just that it is always 1 on older versions of SQLite and there you cannot determine the order of columns in a composite PK.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, thanks. So the column is there but contains incorrect values in some cases. Maybe this patch can be accepted now.

bartv2 added a commit to owncloud-archive/3rdparty that referenced this pull request Feb 18, 2014
@bartv2
Copy link
Contributor Author

bartv2 commented Feb 19, 2014

Anything stopping this from merging?

@DeepDiver1975
Copy link
Contributor

@beberlei @deeky666 @Ocramius Can this be merged?
In addition: how do your backporting strategies look like? Any chance to get this into 2.4.x?

Thanks a lot!

other_id INTEGER,
PRIMARY KEY(other_id, id)
)
EOS
Copy link
Member

Choose a reason for hiding this comment

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

@bartv2 Is this explicit raw SQL necessary? Can't we just $this->_sm->createTable($table) here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No that function will call Schema\Table->getColumns() which returns a sorted list of columns and that is not what we want here.

@deeky666
Copy link
Member

deeky666 commented Mar 6, 2014

Besides some CS issues and maybe mentioning this particular limitation in the known vendor issues, this PR is okay from my point of view.

@bantu
Copy link
Contributor

bantu commented Mar 10, 2014

On older SQLite versions the schema could be dumped which should also contain the correct order of primary key columns.

@bantu
Copy link
Contributor

bantu commented Mar 24, 2014

Travis CI error seems to be unrelated. psql: could not connect to server: No such file or directory

@deeky666 @beberlei What needs to be done in order to get this merged?

@deeky666
Copy link
Member

Yeah travis seems to have problems sometimes bringing up the PostgreSQL service. Restarted the failing build and it should be okay now.
@beberlei This looks good to merge.

beberlei added a commit that referenced this pull request Mar 25, 2014
The primary key columns don't have to be in the same order as the table columns
@beberlei beberlei merged commit 7c68e80 into doctrine:master Mar 25, 2014
@bartv2 bartv2 deleted the sort-sqlite-pk-columns branch March 25, 2014 16:49
@bantu
Copy link
Contributor

bantu commented Mar 26, 2014

@beberlei Can we get this backported to 2.3 please?

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

Successfully merging this pull request may close these issues.

8 participants