20180812173710_copy_status_stats.rb 1.6 KB

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