discourse/app/jobs/scheduled
Régis Hanol b704e338ef
DEV: extract anniversary badge query (#19716)
So it can easily be overwritten in a plugin for example.

### Added more tests to provide better coverage

We previously only had `u.silenced_till IS NULL` but I made it consistent with pretty much every other places where we check for "active" users.

These two new lines do change the query a tiny bit though. 

**Before** 

- You could not get the badge if you were currently silenced (no matter what period is being checked)
- You could get the badge if you were suspended 😬 

**After**

- You can't get the badge if you were silenced during the past year
- You can't get the badge if you were suspended during the past year


### Improved the performance of the query by using `NOT EXISTS` instead of `LEFT JOIN / COUNT() = 0`

There is no difference in behaviour between 

```sql
LEFT JOIN user_badges AS ub ON ub.user_id = u.id AND ...
[...]
HAVING COUNT(ub.*) = 0
```

and

```sql
NOT EXISTS (SELECT 1 FROM user_badges AS ub WHERE ub.user_id = u.id AND ...)
```

The only difference is performance-wise. The `NOT EXISTS` is 10-30% faster on very large databases (aka. posts and users in X millions). I checked on 3 of the largest datasets I could find.
2023-01-16 11:55:00 +01:00
..
about_stats.rb
activation_reminder_emails.rb
auto_expire_user_api_keys.rb
auto_queue_handler.rb
backfill_dominant_colors.rb
badge_grant.rb
bookmark_reminder_notifications.rb
category_stats.rb
check_new_features.rb
check_out_of_date_themes.rb
clean_dismissed_topic_users.rb
clean_up_associated_accounts.rb
clean_up_associated_groups.rb
clean_up_crawler_stats.rb
clean_up_email_change_requests.rb
clean_up_email_logs.rb
clean_up_email_tokens.rb
clean_up_exports.rb
clean_up_inactive_users.rb
clean_up_post_reply_keys.rb
clean_up_search_logs.rb
clean_up_stylesheet_cache.rb
clean_up_unmatched_emails.rb
clean_up_unmatched_ips.rb
clean_up_unsubscribe_keys.rb
clean_up_unused_api_keys.rb
clean_up_unused_staged_users.rb
clean_up_uploads.rb
cleanup_imap_sync_log.rb
create_missing_avatars.rb
create_recent_post_search_indexes.rb
dashboard_stats.rb
destroy_old_deletion_stubs.rb
destroy_old_hidden_posts.rb
directory_refresh_daily.rb
directory_refresh_older.rb
disable_bootstrap_mode.rb
drop_backup_schema.rb
enqueue_digest_emails.rb
enqueue_onceoffs.rb
enqueue_suspect_users.rb
ensure_db_consistency.rb
ensure_s3_uploads_existence.rb
fix_user_usernames_and_groups_names_clash.rb
grant_anniversary_badges.rb DEV: extract anniversary badge query (#19716) 2023-01-16 11:55:00 +01:00
grant_new_user_of_the_month_badges.rb
heartbeat.rb
ignored_users_summary.rb
invalidate_inactive_admins.rb
migrate_upload_scheme.rb
old_keys_reminder.rb
pending_queued_posts_reminder.rb
pending_reviewables_reminder.rb
pending_users_reminder.rb
periodical_updates.rb
poll_mailbox.rb
presence_channel_auto_leave.rb
problem_checks.rb
process_badge_backlog.rb
process_shelved_notifications.rb
process_user_notification_schedules.rb
purge_deleted_uploads.rb
purge_expired_ignored_users.rb
purge_old_web_hook_events.rb
purge_unactivated.rb
regenerate_sitemaps.rb
reindex_search.rb
reviewable_priorities.rb
schedule_backup.rb
tl3_promotions.rb
top_refresh_older.rb
top_refresh_today.rb
topic_timer_enqueuer.rb
unsilence_users.rb
update_animated_uploads.rb
update_heat_settings.rb
version_check.rb
weekly.rb