FEATURE: Add post edits count to user activity (#13495)

This commit is contained in:
Jean
2021-08-02 10:15:53 -04:00
committed by GitHub
parent 7b56325f89
commit e7b8e75583
22 changed files with 210 additions and 10 deletions

View File

@@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddUserIdIndexToPostRevisions < ActiveRecord::Migration[6.1]
def change
add_index :post_revisions, :user_id
end
end

View File

@@ -0,0 +1,31 @@
# frozen_string_literal: true
class AddPostEditsCountToUserStats < ActiveRecord::Migration[6.1]
disable_ddl_transaction!
BATCH_SIZE = 30_000
def up
add_column :user_stats, :post_edits_count, :integer
loop do
count = DB.exec(<<~SQL, batch_size: BATCH_SIZE)
UPDATE user_stats us
SET post_edits_count = editor.edits_count
FROM (
SELECT COUNT(editor.id) AS edits_count, editor.id AS id
FROM post_revisions pr JOIN users editor ON editor.id = pr.user_id
JOIN user_stats us ON us.user_id = editor.id
WHERE us.post_edits_count IS NULL AND pr.user_id IS NOT NULL
GROUP BY editor.id
LIMIT :batch_size
) editor
WHERE editor.id = us.user_id;
SQL
break if count == 0
end
end
def down
raise ActiveRecord::IrreversibleMigration
end
end