Skip to content

Commit

Permalink
FEATURE: ensure consistency of post revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
ZogStriP committed Oct 15, 2014
1 parent a967d4d commit bb59798
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/jobs/scheduled/ensure_db_consistency.rb
Expand Up @@ -9,6 +9,7 @@ def execute(args)
Notification.ensure_consistency!
UserAction.ensure_consistency!
TopicFeaturedUsers.ensure_consistency!
PostRevision.ensure_consistency!
UserStat.update_view_counts(13.hours.ago)
end
end
Expand Down
24 changes: 24 additions & 0 deletions app/models/post_revision.rb
Expand Up @@ -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"))
Expand Down

0 comments on commit bb59798

Please sign in to comment.