tags: fix negative tag counts not being regenerated.
This commit is contained in:
@@ -109,17 +109,16 @@ class Tag < ApplicationRecord
|
|||||||
def regenerate_post_counts!
|
def regenerate_post_counts!
|
||||||
sql = <<~SQL
|
sql = <<~SQL
|
||||||
UPDATE tags
|
UPDATE tags
|
||||||
SET post_count = true_count
|
SET post_count = COALESCE(true_count, 0)
|
||||||
FROM (
|
FROM (
|
||||||
SELECT tag, COUNT(*) AS true_count
|
SELECT tag, COUNT(*) AS true_count
|
||||||
FROM posts, unnest(string_to_array(tag_string, ' ')) AS tag
|
FROM posts, unnest(string_to_array(tag_string, ' ')) AS tag
|
||||||
GROUP BY tag
|
GROUP BY tag
|
||||||
) true_counts, tags AS old_tags
|
) true_counts
|
||||||
WHERE
|
WHERE
|
||||||
tags.name = tag
|
(tags.name = tag AND tags.post_count != true_count)
|
||||||
AND tags.post_count != true_count
|
OR tags.post_count < 0
|
||||||
AND old_tags.id = tags.id
|
RETURNING tags.*
|
||||||
RETURNING tags.*, old_tags.post_count AS old_post_count
|
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
updated_tags = Tag.find_by_sql(sql)
|
updated_tags = Tag.find_by_sql(sql)
|
||||||
|
|||||||
Reference in New Issue
Block a user