20181024224956_migrate_account_conversations.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. class MigrateAccountConversations < ActiveRecord::Migration[5.2]
  2. disable_ddl_transaction!
  3. def up
  4. say ''
  5. say 'WARNING: This migration may take a *long* time for large instances'
  6. say 'It will *not* lock tables for any significant time, but it may run'
  7. say 'for a very long time. We will pause for 10 seconds to allow you to'
  8. say 'interrupt this migration if you are not ready.'
  9. say ''
  10. 10.downto(1) do |i|
  11. say "Continuing in #{i} second#{i == 1 ? '' : 's'}...", true
  12. sleep 1
  13. end
  14. migrated = 0
  15. last_time = Time.zone.now
  16. local_direct_statuses.includes(:account, mentions: :account).find_each do |status|
  17. AccountConversation.add_status(status.account, status)
  18. migrated += 1
  19. if Time.zone.now - last_time > 1
  20. say_progress(migrated)
  21. last_time = Time.zone.now
  22. end
  23. end
  24. notifications_about_direct_statuses.includes(:account, mention: { status: [:account, mentions: :account] }).find_each do |notification|
  25. AccountConversation.add_status(notification.account, notification.target_status)
  26. migrated += 1
  27. if Time.zone.now - last_time > 1
  28. say_progress(migrated)
  29. last_time = Time.zone.now
  30. end
  31. end
  32. end
  33. def down
  34. end
  35. private
  36. def say_progress(migrated)
  37. say "Migrated #{migrated} rows", true
  38. end
  39. def local_direct_statuses
  40. Status.unscoped.local.where(visibility: :direct)
  41. end
  42. def notifications_about_direct_statuses
  43. Notification.joins(mention: :status).where(activity_type: 'Mention', statuses: { visibility: :direct })
  44. end
  45. end