FEATURE: ensure consistency of post revisions

This commit is contained in:
Régis Hanol 2014-10-15 21:09:08 +02:00
parent a967d4dfba
commit bb59798066
2 changed files with 25 additions and 0 deletions

View File

@ -9,6 +9,7 @@ module Jobs
Notification.ensure_consistency!
UserAction.ensure_consistency!
TopicFeaturedUsers.ensure_consistency!
PostRevision.ensure_consistency!
UserStat.update_view_counts(13.hours.ago)
end
end

View File

@ -6,6 +6,30 @@ class PostRevision < ActiveRecord::Base
serialize :modifications, Hash
def self.ensure_consistency!
# 1 - fix the numbers
sql = <<-SQL
UPDATE post_revisions
SET number = pr.rank
FROM (SELECT id, ROW_NUMBER() OVER (PARTITION BY post_id ORDER BY number, created_at, updated_at) AS rank FROM post_revisions) AS pr
WHERE post_revisions.id = pr.id
AND post_revisions.number <> pr.rank
SQL
PostRevision.exec_sql(sql)
# 2 - fix the versions on the posts
sql = <<-SQL
UPDATE posts
SET version = pv.version
FROM (SELECT post_id, MAX(number) AS version FROM post_revisions GROUP BY post_id) AS pv
WHERE posts.id = pv.post_id
AND posts.version <> pv.version
SQL
PostRevision.exec_sql(sql)
end
def body_changes
cooked_diff = DiscourseDiff.new(previous("cooked"), current("cooked"))
raw_diff = DiscourseDiff.new(previous("raw"), current("raw"))