2018-10-24 19:44:02 -04:00
|
|
|
class MigrateAccountConversations < ActiveRecord::Migration[5.2]
|
|
|
|
disable_ddl_transaction!
|
|
|
|
|
|
|
|
def up
|
|
|
|
say ''
|
|
|
|
say 'WARNING: This migration may take a *long* time for large instances'
|
|
|
|
say 'It will *not* lock tables for any significant time, but it may run'
|
|
|
|
say 'for a very long time. We will pause for 10 seconds to allow you to'
|
|
|
|
say 'interrupt this migration if you are not ready.'
|
|
|
|
say ''
|
|
|
|
|
|
|
|
10.downto(1) do |i|
|
|
|
|
say "Continuing in #{i} second#{i == 1 ? '' : 's'}...", true
|
|
|
|
sleep 1
|
|
|
|
end
|
|
|
|
|
2018-10-28 01:40:45 -04:00
|
|
|
total = estimate_rows(local_direct_statuses) + estimate_rows(notifications_about_direct_statuses)
|
|
|
|
migrated = 0
|
|
|
|
started_time = Time.zone.now
|
|
|
|
last_time = Time.zone.now
|
|
|
|
|
|
|
|
local_direct_statuses.includes(:account, mentions: :account).find_each do |status|
|
2018-10-24 19:44:02 -04:00
|
|
|
AccountConversation.add_status(status.account, status)
|
2018-10-28 01:40:45 -04:00
|
|
|
migrated += 1
|
|
|
|
|
|
|
|
if Time.zone.now - last_time > 1
|
|
|
|
say_progress(migrated, total, started_time)
|
|
|
|
last_time = Time.zone.now
|
|
|
|
end
|
2018-10-24 19:44:02 -04:00
|
|
|
end
|
|
|
|
|
2018-10-28 01:40:45 -04:00
|
|
|
notifications_about_direct_statuses.includes(:account, mention: { status: [:account, mentions: :account] }).find_each do |notification|
|
2018-10-24 19:44:02 -04:00
|
|
|
AccountConversation.add_status(notification.account, notification.target_status)
|
2018-10-28 01:40:45 -04:00
|
|
|
migrated += 1
|
|
|
|
|
|
|
|
if Time.zone.now - last_time > 1
|
|
|
|
say_progress(migrated, total, started_time)
|
|
|
|
last_time = Time.zone.now
|
|
|
|
end
|
2018-10-24 19:44:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-10-28 01:40:45 -04:00
|
|
|
def estimate_rows(query)
|
|
|
|
result = exec_query("EXPLAIN #{query.to_sql}").first
|
|
|
|
result['QUERY PLAN'].scan(/ rows=([\d]+)/).first&.first&.to_i || 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def say_progress(migrated, total, started_time)
|
|
|
|
status = "Migrated #{migrated} rows"
|
|
|
|
|
|
|
|
percentage = 100.0 * migrated / total
|
|
|
|
status += " (~#{sprintf('%.2f', percentage)}%, "
|
|
|
|
|
|
|
|
remaining_time = (100.0 - percentage) * (Time.zone.now - started_time) / percentage
|
|
|
|
|
|
|
|
status += "#{(remaining_time / 60).to_i}:"
|
|
|
|
status += sprintf('%02d', remaining_time.to_i % 60)
|
|
|
|
status += ' remaining)'
|
|
|
|
|
|
|
|
say status, true
|
|
|
|
end
|
|
|
|
|
2018-10-24 19:44:02 -04:00
|
|
|
def local_direct_statuses
|
2018-10-28 01:40:45 -04:00
|
|
|
Status.unscoped.local.where(visibility: :direct)
|
2018-10-24 19:44:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def notifications_about_direct_statuses
|
2018-10-28 01:40:45 -04:00
|
|
|
Notification.joins(mention: :status).where(activity_type: 'Mention', statuses: { visibility: :direct })
|
2018-10-24 19:44:02 -04:00
|
|
|
end
|
|
|
|
end
|