mirror of
https://github.com/discourse/discourse.git
synced 2024-12-16 05:33:54 +08: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
22 lines
814 B
Ruby
22 lines
814 B
Ruby
module Jobs
|
|
class RetroRecentTimeRead < Jobs::Onceoff
|
|
def execute_onceoff(args)
|
|
# update past records by evenly distributing total time reading among each post read
|
|
sql = <<~SQL
|
|
UPDATE user_visits uv1
|
|
SET time_read = (
|
|
SELECT (
|
|
uv1.posts_read
|
|
/ (SELECT CAST(sum(uv2.posts_read) AS FLOAT) FROM user_visits uv2 where uv2.user_id = uv1.user_id)
|
|
* COALESCE((SELECT us.time_read FROM user_stats us WHERE us.user_id = uv1.user_id), 0)
|
|
)
|
|
)
|
|
WHERE EXISTS (SELECT 1 FROM user_stats stats WHERE stats.user_id = uv1.user_id AND stats.posts_read_count > 0 LIMIT 1)
|
|
AND EXISTS (SELECT 1 FROM user_visits visits WHERE visits.user_id = uv1.user_id AND visits.posts_read > 0 LIMIT 1)
|
|
SQL
|
|
|
|
DB.exec(sql)
|
|
end
|
|
end
|
|
end
|