There's a whole bunch of bugs caused by 'duplicate key' errors http://goo.gl/f9YdV

Coming from #505812: PDO uses inconsistant error codes between DBs

There's a 2 approaches in core introduced in #364348: OpenID throws an error when two accounts try to use the same OpenID
1) check existence by key in form validation level - most of core uses this
2) try...catch with exception handling in API level (_save)

First one could lead to some drift in multi-db config (master-slave replication)
Second has no consistency in constraint violation detection

I think we should use second approach and unify this all over core

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

c960657’s picture

First one could lead to some drift in multi-db config (master-slave replication)

True. Even in a single-db setup it would not be bullet-proof unless access to the table was restricted by semaphores.

I think we should use second approach and unify this all over core

I agree. I suggest we create a separate exception class for all 23xxx Integrity Constraint Violation errors.
http://www.postgresql.org/docs/8.1/static/errcodes-appendix.html

c960657’s picture

Status: Active » Needs review
FileSize
4.23 KB

How about this?

Status: Needs review » Needs work

The last submitted patch, integrity-constraint-violation-1.patch, failed testing.

c960657’s picture

Status: Needs work » Needs review
FileSize
5 KB

I couldn't reproduce all the test errors reported by the test bot (it complained about fatal errors in line numbers that were containts the initial "function testFoo()" method declaration). But I fixed one bug in lock.inc that might have caused problems elsewhere.

andypost’s picture

Looks good, are you sure that all places are fixed?

c960657’s picture

I looked through the code (especially places where we catch PDOExceptions) but couldn't find any other places where we should catch the new exception (I wonder why our own database exceptions don't inherit from PDOException? Then changes like this would not break existing code - though of course we should still use the more specific exception class when appropriate).

This updated patch contains an update to the pgsql driver also. This has been tested on Postgres 8.4 (I ran all database tests — two tests were failing with and without the patch). I had to patch core in two places in order to even install Drupal on Postgres?! This is not included in the patch (I assume this problem is being tracked elsewhere, though I couldn't find the ticket).

The patch was also tested successfully with the sqlite driver (I ran all database tests — one test were failing with and without the patch). I fixed a test failure (database_test line 3186, “The whole transaction is rolled back when a duplicate key insert occurs.”) by adding a missing "use Exception" to core/lib/Drupal/Core/Database/Query/Insert.php.

I'm a bit surprised about the problems with the pgsql and sqlite drivers. Don't we have test bots running tests on with engines?

c960657’s picture

I created a separate issue for the Postgres install troubles: #1491526: Cannot install Drupal on PostgreSQL

c960657’s picture

c960657’s picture

Reroll.

c960657’s picture

Reroll.

Status: Needs review » Needs work

The last submitted patch, integrity-constraint-violation-6.patch, failed testing.

c960657’s picture

Bad patch.

c960657’s picture

Status: Needs work » Needs review
c960657’s picture

Bah, the last patch was also bad (it included a fix for some other issue also). This is better, I hope.

mikl’s picture

Status: Needs review » Reviewed & tested by the community

Looks great, it will make it a lot easier to catch these errors specifically.

Dries’s picture

This patch needs a re-roll. Asking for a re-test.

Dries’s picture

Issue tags: -API clean-up

Status: Reviewed & tested by the community » Needs work
Issue tags: +API clean-up

The last submitted patch, integrity-constraint-violation-8.patch, failed testing.

c960657’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
6.96 KB

Reroll.

Status: Reviewed & tested by the community » Needs work
Issue tags: -API clean-up

The last submitted patch, integrity-constraint-violation-9.patch, failed testing.

c960657’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work
Issue tags: +API clean-up

The last submitted patch, integrity-constraint-violation-9.patch, failed testing.

c960657’s picture

Status: Needs work » Needs review
FileSize
6.21 KB

Status: Needs review » Needs work

The last submitted patch, integrity-constraint-violation-10.patch, failed testing.

c960657’s picture

c960657’s picture

Status: Needs work » Needs review
c960657’s picture

Status: Needs review » Reviewed & tested by the community
catch’s picture

Status: Reviewed & tested by the community » Fixed

Not sure why this is listed as a feature. Committed/pushed to 8.x.

daften’s picture

Status: Fixed » Needs work
Issue tags: +Needs backport to D7

I'm guessing this is nice to have/necessary for drupal7 as well, unless this proves impossible/too big of a task.

c960657’s picture

Version: 8.x-dev » 7.x-dev

  • catch committed 7e8d99b on 8.3.x
    Issue #1376778 by c960657 | andypost: Added Consistent 'duplicate key'...

  • catch committed 7e8d99b on 8.3.x
    Issue #1376778 by c960657 | andypost: Added Consistent 'duplicate key'...

  • catch committed 7e8d99b on 8.4.x
    Issue #1376778 by c960657 | andypost: Added Consistent 'duplicate key'...

  • catch committed 7e8d99b on 8.4.x
    Issue #1376778 by c960657 | andypost: Added Consistent 'duplicate key'...
alexpott’s picture

Version: 7.x-dev » 8.0.x-dev
Issue summary: View changes
Status: Needs work » Fixed

Gonna mark this as fixed against 8.x dev because there's been no movement on the 7.x work for 7 years.

alexpott’s picture

Version: 8.0.x-dev » 8.3.x-dev

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.