mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Add last_message_id to channel and thread (#22488)
Initial migration and changes to models as well as changing the following services to update last_message_id: * Chat::MessageCreator * Chat::RestoreMessage * Chat::TrashMessage The data migration will set the `last_message_id` for all existing threads and channels in the database. When we query the thread list as well as the channel, we look at the last message ID for the following: * Channel - Sorting DM channels, and channel metadata for the list of channels * Thread - Last reply details for thread indicators and thread list
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class AddLastMessageIdToChannelAndThread < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
add_column :chat_channels, :last_message_id, :bigint, null: true
|
||||
add_column :chat_threads, :last_message_id, :bigint, null: true
|
||||
|
||||
add_index :chat_channels, :last_message_id
|
||||
add_index :chat_threads, :last_message_id
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,35 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class BackfillChatChannelAndThreadLastMessageIds < ActiveRecord::Migration[7.0]
|
||||
def up
|
||||
execute <<-SQL
|
||||
UPDATE chat_channels
|
||||
SET last_message_id = (
|
||||
SELECT cm.id
|
||||
FROM chat_messages cm
|
||||
LEFT JOIN chat_threads ON chat_threads.original_message_id = cm.id
|
||||
WHERE cm.chat_channel_id = chat_channels.id
|
||||
AND cm.deleted_at IS NULL
|
||||
AND (cm.thread_id IS NULL OR chat_threads.id IS NOT NULL)
|
||||
ORDER BY cm.created_at DESC, cm.id DESC
|
||||
LIMIT 1
|
||||
);
|
||||
SQL
|
||||
|
||||
execute <<-SQL
|
||||
UPDATE chat_threads
|
||||
SET last_message_id = (
|
||||
SELECT cm.id
|
||||
FROM chat_messages cm
|
||||
WHERE cm.thread_id = chat_threads.id
|
||||
AND cm.deleted_at IS NULL
|
||||
ORDER BY cm.created_at DESC, cm.id DESC
|
||||
LIMIT 1
|
||||
);
|
||||
SQL
|
||||
end
|
||||
|
||||
def down
|
||||
raise ActiveRecord::IrreversibleMigration
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,29 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class BackfillChatChannelAndThreadLastMessageIdsPostMigrate < ActiveRecord::Migration[7.0]
|
||||
execute <<-SQL
|
||||
UPDATE chat_channels
|
||||
SET last_message_id = (
|
||||
SELECT cm.id
|
||||
FROM chat_messages cm
|
||||
LEFT JOIN chat_threads ON chat_threads.original_message_id = cm.id
|
||||
WHERE cm.chat_channel_id = chat_channels.id
|
||||
AND cm.deleted_at IS NULL
|
||||
AND (cm.thread_id IS NULL OR chat_threads.id IS NOT NULL)
|
||||
ORDER BY cm.created_at DESC, cm.id DESC
|
||||
LIMIT 1
|
||||
);
|
||||
SQL
|
||||
|
||||
execute <<-SQL
|
||||
UPDATE chat_threads
|
||||
SET last_message_id = (
|
||||
SELECT cm.id
|
||||
FROM chat_messages cm
|
||||
WHERE cm.thread_id = chat_threads.id
|
||||
AND cm.deleted_at IS NULL
|
||||
ORDER BY cm.created_at DESC, cm.id DESC
|
||||
LIMIT 1
|
||||
);
|
||||
SQL
|
||||
end
|
||||
Reference in New Issue
Block a user