discourse/db/migrate/20131210181901_migrate_word_counts.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

36 lines
1.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class MigrateWordCounts < ActiveRecord::Migration[4.2]
2013-12-11 08:52:55 +08:00
disable_ddl_transaction!
def up
2017-07-28 09:20:09 +08:00
post_ids = execute("SELECT id FROM posts WHERE word_count IS NULL LIMIT 500").map { |r| r['id'].to_i }
while post_ids.length > 0
2013-12-11 07:10:52 +08:00
3.times do
begin
execute "UPDATE posts SET word_count = COALESCE(array_length(regexp_split_to_array(raw, ' '),1), 0) WHERE id IN (#{post_ids.join(',')})"
break
rescue PG::Error
# Deadlock. Try again, up to 3 times.
end
end
2017-07-28 09:20:09 +08:00
post_ids = execute("SELECT id FROM posts WHERE word_count IS NULL LIMIT 500").map { |r| r['id'].to_i }
end
2017-07-28 09:20:09 +08:00
topic_ids = execute("SELECT id FROM topics WHERE word_count IS NULL LIMIT 500").map { |r| r['id'].to_i }
while topic_ids.length > 0
2013-12-11 07:10:52 +08:00
3.times do
begin
execute "UPDATE topics SET word_count = COALESCE((SELECT SUM(COALESCE(posts.word_count, 0)) FROM posts WHERE posts.topic_id = topics.id), 0) WHERE topics.id IN (#{topic_ids.join(',')})"
break
rescue PG::Error
# Deadlock. Try again, up to 3 times.
end
end
2017-07-28 09:20:09 +08:00
topic_ids = execute("SELECT id FROM topics WHERE word_count IS NULL LIMIT 500").map { |r| r['id'].to_i }
end
end
end