about summary refs log tree commit diff
path: root/db/migrate/20200721212401_backfill_account_id_on_conversations.rb
diff options
context:
space:
mode:
Diffstat (limited to 'db/migrate/20200721212401_backfill_account_id_on_conversations.rb')
-rw-r--r--db/migrate/20200721212401_backfill_account_id_on_conversations.rb15
1 files changed, 15 insertions, 0 deletions
diff --git a/db/migrate/20200721212401_backfill_account_id_on_conversations.rb b/db/migrate/20200721212401_backfill_account_id_on_conversations.rb
new file mode 100644
index 000000000..595fd8e52
--- /dev/null
+++ b/db/migrate/20200721212401_backfill_account_id_on_conversations.rb
@@ -0,0 +1,15 @@
+class BackfillAccountIdOnConversations < ActiveRecord::Migration[5.2]
+  disable_ddl_transaction!
+
+  def up
+    Rails.logger.info('Backfilling owners of conversation threads...')
+    safety_assured do
+      Conversation.left_outer_joins(:statuses).where(statuses: { id: nil }).in_batches.destroy_all
+      execute('UPDATE conversations SET account_id = s.account_id FROM (SELECT account_id, conversation_id FROM statuses WHERE NOT reply) AS s WHERE conversations.id = s.conversation_id')
+    end
+  end
+
+  def down
+    true
+  end
+end