@@ -290,33 +290,34 @@ def self.skip_db?
290
290
end
291
291
292
292
query = <<~SQL
293
+ UPDATE directory_items di
294
+ SET solutions = 0
295
+ WHERE di.period_type = :period_type AND di.solutions IS NOT NULL;
296
+
293
297
WITH x AS (
294
- SELECT users.id AS user_id,
295
- COUNT(DISTINCT st.topic_id) FILTER (WHERE
296
- st.topic_id IS NOT NULL AND
297
- t.id IS NOT NULL AND
298
- t.archetype <> 'private_message' AND
299
- t.deleted_at IS NULL AND
300
- p.deleted_at IS NULL
301
- ) AS solutions
302
- FROM users
303
- LEFT JOIN posts p ON p.user_id = users.id
304
- LEFT JOIN discourse_solved_solved_topics st
305
- ON st.answer_post_id = p.id
306
- AND st.created_at >= :since
307
- LEFT JOIN topics t ON t.id = st.topic_id
308
- WHERE users.id > 0
309
- AND users.active
310
- AND users.silenced_till IS NULL
311
- AND users.suspended_till IS NULL
312
- GROUP BY users.id
298
+ SELECT p.user_id, COUNT(DISTINCT st.id) AS solutions
299
+ FROM discourse_solved_solved_topics AS st
300
+ JOIN posts AS p
301
+ ON p.id = st.answer_post_id
302
+ AND COALESCE(st.created_at, :since) > :since
303
+ AND p.deleted_at IS NULL
304
+ JOIN topics AS t
305
+ ON t.id = st.topic_id
306
+ AND t.archetype <> 'private_message'
307
+ AND t.deleted_at IS NULL
308
+ JOIN users AS u
309
+ ON u.id = p.user_id
310
+ WHERE u.id > 0
311
+ AND u.active
312
+ AND u.silenced_till IS NULL
313
+ AND u.suspended_till IS NULL
314
+ GROUP BY p.user_id
313
315
)
314
316
UPDATE directory_items di
315
- SET solutions = x.solutions
316
- FROM x
317
- WHERE x.user_id = di.user_id
318
- AND di.period_type = :period_type
319
- AND di.solutions <> x.solutions
317
+ SET solutions = x.solutions
318
+ FROM x
319
+ WHERE x.user_id = di.user_id
320
+ AND di.period_type = :period_type;
320
321
SQL
321
322
322
323
add_directory_column ( "solutions" , query :)
0 commit comments