[solved] Badges 'update daily' won't trigger

AI-generated summary

In a Discourse 1.3.0.beta1 installation, jebwizoscar reported that badges configured to ‘Update daily’ were failing to trigger automatically, despite the query preview indicating five badges were ready for assignment. riking suggested checking the Sidekiq scheduler and logs to identify if the Jobs::BadgeGrant job was failing. Upon investigation, jebwizoscar discovered that the scheduler showed the job as failed with a PG::UniqueViolation error: “duplicate key value violates unique constraint index_user_badges_on_badge_id_and_user_id_and_seq”. The error indicated a conflict with an existing key for badge ID 104 and user ID 1. The issue was resolved by modifying the underlying SQL query to include DISTINCT ON for the user_id of the conflicting badge, preventing the duplicate entry error.

In my installation of 1.3.0.beta1, Badge that ‘Update daily’ won’t trigger automatically.

SQL:

SELECT u.id AS user_id, MIN(u.created_at + interval '8 day') AS granted_at
      FROM users u
      JOIN posts p ON p.user_id = u.id
     WHERE u.id > 0
       AND u.active
       AND NOT u.blocked
       AND u.created_at + interval '8 day' < now()
       AND p.deleted_at IS NULL
       AND NOT p.hidden
       AND p.created_at + interval '8 day' > now()
       AND (:backfill OR u.id IN (:user_ids))
     GROUP BY u.id
     HAVING COUNT(p.id) > 0

but Preview granted badges | Preview with query plan are working normally.

Badge Query Preview
5 badges to be assigned.

Sample:

… at Feb 21, 2015 8:44 am
… at Feb 21, 2015 12:03 am
… at Feb 20, 2015 9:03 pm
… at Feb 22, 2015 10:40 am
… at Feb 20, 2015 8:52 pm

Settings:
Trigger: Update daily
Allow badge to be used as a title
Show badge on the public badges page
Enable badge

What does /sidekiq/scheduler and /logs show? Is the Jobs::BadgeGrant failing?

/sidekiq/scheduler : Jobs::BadgeGrant FAILED
/logs:
Job exception: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint “index_user_badges_on_badge_id_and_user_id_and_seq”
DETAIL: Key (badge_id, user_id, seq)=(104, 1, 0) already exists.

Thanks, problem solved (by adding DISTINCT ON for user_id of another badge)