hook_cron doesn't check whether there is already an entry for the user and role in the users_roles table before inserting the record. If for any reason this role has been granted for some other reason, by some other module or by an administrator before this cron runs then we get a duplicate key error:
Error running role_delay_cron: exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '107677-15' for key 'PRIMARY''
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | role_delay-duplicate-key-on-db-insert-2649830-8.patch | 675 bytes | kalpaitch |
| #6 | role_delay-duplicate-key-on-db-insert-2649830-6.patch | 674 bytes | WorldFallz |
| #4 | role_delay-duplicate-key-on-db-insert-2649830-4.patch | 1.06 KB | kalpaitch |
Comments
Comment #2
kalpaitch commentedIt would probably be safer to do a user_save but sticking with a simple query to save resources.
Comment #3
kalpaitch commentedComment #4
kalpaitch commentedOops, help if I include some output right.
Comment #5
WorldFallz commentedGood catch! I'm thinking this is a perfect case for db_merge.
If you get to before I do, just update the status again. Thanks.
Comment #6
WorldFallz commentedThis should do it. Please take it for a spin and let me know.
Comment #7
kalpaitch commented1) Yup, much better than the original patch, which contains a glaring mistake.
2) I had tried to stay away from db_merge() because of incorrectly assumed cross database compatibility issues.
3) Just a small comment, would we really want to update the record if it already exists, me thinks not, do we need to do anything to make this happen, such as set the 'needsUpdate' property or specify only 'insertFields' and not 'fields'?? Will take a look.
Comment #8
kalpaitch commentedComment #9
kalpaitch commentedIgnore my point 3).
There's a very small issue with your patch, the table name needs to be 'users_roles' not 'user_roles'. Attached an updated, tested it myself and I've found it to be working. Over to you sir!
Comment #10
kalpaitch commentedBUMP. Can someone review this please. Thanks.
Comment #12
WorldFallz commentedThis is a no brainer. Lets just get it in. Thanks kalpaitch!