fix: Use last_activity_at instead of updated_at for sorting (#1281)
Co-authored-by: Akash Srivastava <akash.srivastava.1911@gmail.com>
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
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
|
||||
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2020_09_07_094912) do
|
||||
ActiveRecord::Schema.define(version: 2020_09_27_135222) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "pgcrypto"
|
||||
@@ -226,6 +226,7 @@ ActiveRecord::Schema.define(version: 2020_09_07_094912) do
|
||||
t.bigint "contact_inbox_id"
|
||||
t.uuid "uuid", default: -> { "gen_random_uuid()" }, null: false
|
||||
t.string "identifier"
|
||||
t.datetime "last_activity_at", default: -> { "CURRENT_TIMESTAMP" }, null: false
|
||||
t.index ["account_id", "display_id"], name: "index_conversations_on_account_id_and_display_id", unique: true
|
||||
t.index ["account_id"], name: "index_conversations_on_account_id"
|
||||
t.index ["contact_inbox_id"], name: "index_conversations_on_contact_inbox_id"
|
||||
|
||||
Reference in New Issue
Block a user