Chatwoot/db/migrate/20200927135222_add_last_activity_at_to_conversation.rb
Pranav Raj S 399f9e004a
fix: Use last_activity_at instead of updated_at for sorting (#1281)
Co-authored-by: Akash Srivastava <akash.srivastava.1911@gmail.com>
2020-10-05 22:52:43 +05:30

35 lines
1.1 KiB
Ruby

class AddLastActivityAtToConversation < ActiveRecord::Migration[6.0]
def up
add_column :conversations,
:last_activity_at,
:datetime,
default: -> { 'CURRENT_TIMESTAMP' },
index: true
add_last_activity_at_to_conversations
change_column_null(:conversations, :last_activity_at, false)
end
def down
remove_column(:conversations, :last_activity_at)
end
private
def add_last_activity_at_to_conversations
::Conversation.find_in_batches do |conversation_batch|
Rails.logger.info "Migrated till #{conversation_batch.first.id}\n"
conversation_batch.each do |conversation|
# rubocop:disable Rails/SkipsModelValidations
last_activity_at = if conversation.messages.last
conversation.messages.last.created_at
else
conversation.created_at
end
conversation.update_columns(last_activity_at: last_activity_at)
# rubocop:enable Rails/SkipsModelValidations
end
end
end
end