discourse/db/migrate/20140306223522_move_topic_revisions_to_post_revisions.rb
Sam Saffron 30990006a9 DEV: enable frozen string literal on all files
This reduces chances of errors where consumers of strings mutate inputs
and reduces memory usage of the app.

Test suite passes now, but there may be some stuff left, so we will run
a few sites on a branch prior to merging
2019-05-13 09:31:32 +08:00

46 lines
963 B
Ruby

# frozen_string_literal: true
class MoveTopicRevisionsToPostRevisions < ActiveRecord::Migration[4.2]
def up
execute <<SQL
INSERT INTO post_revisions(user_id, post_id, modifications, number, created_at, updated_at)
SELECT tr.user_id, p.id, tr.modifications, tr.number, tr.created_at, tr.updated_at
FROM topic_revisions tr
JOIN topics t ON t.id = tr.topic_id
JOIN posts p ON p.topic_id = t.id AND p.post_number = 1
SQL
execute <<SQL
UPDATE post_revisions r SET number = 2 + (
SELECT COUNT(*) FROM post_revisions r2
WHERE r2.post_id = r.post_id AND r2.created_at < r.created_at
)
SQL
execute <<SQL
UPDATE posts p SET version = 1 + (
SELECT COUNT(*) FROM post_revisions r
WHERE r.post_id = p.id
)
SQL
execute <<SQL
DROP TABLE topic_revisions
SQL
end
def down
# strictly, we could reverse this, but not implemented
raise ActiveRecord::IrreversibleMigration
end
end