mirror of
https://github.com/discourse/discourse.git
synced 2024-12-02 13:39:36 -06:00
5f64fd0a21
Introduce new patterns for direct sql that are safe and fast. MiniSql is not prone to memory bloat that can happen with direct PG usage. It also has an extremely fast materializer and very a convenient API - DB.exec(sql, *params) => runs sql returns row count - DB.query(sql, *params) => runs sql returns usable objects (not a hash) - DB.query_hash(sql, *params) => runs sql returns an array of hashes - DB.query_single(sql, *params) => runs sql and returns a flat one dimensional array - DB.build(sql) => returns a sql builder See more at: https://github.com/discourse/mini_sql
35 lines
922 B
Ruby
35 lines
922 B
Ruby
require_dependency 'jobs/scheduled/grant_anniversary_badges'
|
|
|
|
module Jobs
|
|
|
|
class FixRetroAnniversary < Jobs::Onceoff
|
|
def execute_onceoff(args)
|
|
return unless SiteSetting.enable_badges
|
|
|
|
users = User.query <<~SQL
|
|
SELECT ub.user_id, MIN(granted_at) AS first_granted_at, COUNT(*)
|
|
FROM user_badges AS ub
|
|
WHERE ub.badge_id = #{Badge::Anniversary}
|
|
GROUP BY ub.user_id
|
|
HAVING COUNT(ub.id) > 1
|
|
SQL
|
|
|
|
users.to_a.each do |u|
|
|
first = u.first_granted_at
|
|
badges = UserBadge.where(
|
|
"badge_id = ? AND user_id = ? AND granted_at > ?",
|
|
Badge::Anniversary,
|
|
u.user_id,
|
|
first
|
|
).order('granted_at')
|
|
|
|
badges.each_with_index do |b, idx|
|
|
award_date = (first + (idx + 1).years)
|
|
UserBadge.where(id: b.id).update_all(["granted_at = ?", award_date])
|
|
end
|
|
end
|
|
|
|
end
|
|
end
|
|
end
|