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''

Comments

kalpaitch created an issue. See original summary.

kalpaitch’s picture

It would probably be safer to do a user_save but sticking with a simple query to save resources.

kalpaitch’s picture

Status: Active » Needs review
kalpaitch’s picture

StatusFileSize
new1.06 KB

Oops, help if I include some output right.

WorldFallz’s picture

Status: Needs review » Needs work

Good 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.

WorldFallz’s picture

This should do it. Please take it for a spin and let me know.

kalpaitch’s picture

1) 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.

kalpaitch’s picture

kalpaitch’s picture

Status: Needs work » Needs review
StatusFileSize
new675 bytes

Ignore 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!

kalpaitch’s picture

BUMP. Can someone review this please. Thanks.

  • WorldFallz committed 5860f03 on 7.x-1.x authored by kalpaitch
    Issue #2649830 by kalpaitch, WorldFallz: Cron doesn't check for existing...
WorldFallz’s picture

Status: Needs review » Fixed

This is a no brainer. Lets just get it in. Thanks kalpaitch!

Status: Fixed » Closed (fixed)

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