20181116173541_copy_account_stats.rb 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # frozen_string_literal: true
  2. class CopyAccountStats < ActiveRecord::Migration[5.2]
  3. disable_ddl_transaction!
  4. class MigrationAccount < ApplicationRecord
  5. self.table_name = :accounts
  6. end
  7. def up
  8. safety_assured do
  9. if supports_upsert?
  10. up_fast
  11. else
  12. up_slow
  13. end
  14. end
  15. end
  16. def down
  17. # Nothing
  18. end
  19. private
  20. def supports_upsert?
  21. version = select_one("SELECT current_setting('server_version_num') AS v")['v'].to_i
  22. version >= 90_500
  23. end
  24. def up_fast
  25. say 'Upsert is available, importing counters using the fast method'
  26. MigrationAccount.unscoped.select('id').find_in_batches(batch_size: 5_000) do |accounts|
  27. execute <<-SQL.squish
  28. INSERT INTO account_stats (account_id, statuses_count, following_count, followers_count, created_at, updated_at)
  29. SELECT id, statuses_count, following_count, followers_count, created_at, updated_at
  30. FROM accounts
  31. WHERE id IN (#{accounts.map(&:id).join(', ')})
  32. ON CONFLICT (account_id) DO UPDATE
  33. SET statuses_count = EXCLUDED.statuses_count, following_count = EXCLUDED.following_count, followers_count = EXCLUDED.followers_count
  34. SQL
  35. end
  36. end
  37. def up_slow
  38. say 'Upsert is not available in PostgreSQL below 9.5, falling back to slow import of counters'
  39. # We cannot use bulk INSERT or overarching transactions here because of possible
  40. # uniqueness violations that we need to skip over
  41. MigrationAccount.unscoped.select('id, statuses_count, following_count, followers_count, created_at, updated_at').find_each do |account|
  42. params = [account.id, account[:statuses_count], account[:following_count], account[:followers_count], account.created_at, account.updated_at]
  43. exec_insert('INSERT INTO account_stats (account_id, statuses_count, following_count, followers_count, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6)', nil, params)
  44. rescue ActiveRecord::RecordNotUnique
  45. next
  46. end
  47. end
  48. end