about summary refs log tree commit diff
path: root/db/migrate/20200816200239_backfill_root_to_conversations.rb
blob: 2056e076516313088a5b2d0d82c90bd417250ed6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class BackfillRootToConversations < ActiveRecord::Migration[5.2]
  disable_ddl_transaction!

  def up
    Rails.logger.info("Adding URI to statuses without one...")
    Status.where(uri: nil).or(Status.where(uri: '')).find_each do |status|
      status.update(uri: ActivityPub::TagManager.instance.uri_for(status))
    end

    Rails.logger.info('Setting root of all conversations...')
    safety_assured do
      execute('UPDATE conversations SET root = s.uri FROM (SELECT conversation_id, uri FROM statuses WHERE NOT reply) AS s WHERE conversations.id = s.conversation_id')
    end
  end

  def down
    true
  end
end