discourse/plugins/chat/db/migrate/20230411023340_update_thread_reply_counts.rb
Natalie Tay 5b51ed3856
DEV: Promote historic post_deploy migrations (#28128)
This commit promotes all post_deploy migrations which existed in Discourse v3.2.0 (timestamp <= 20240112043325)
2024-07-30 01:14:03 +08:00

23 lines
600 B
Ruby

# frozen_string_literal: true
class UpdateThreadReplyCounts < ActiveRecord::Migration[7.0]
def up
DB.exec <<~SQL
UPDATE chat_threads threads
SET replies_count = subquery.replies_count
FROM (
SELECT COUNT(*) - 1 AS replies_count, thread_id
FROM chat_messages
WHERE chat_messages.deleted_at IS NULL AND thread_id IS NOT NULL
GROUP BY thread_id
) subquery
WHERE threads.id = subquery.thread_id
AND subquery.replies_count != threads.replies_count
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end