A platform for community discussion. Free, open, simple.
DEV: Do one query per month when exporting chat messages (#22746)
Andrei Prigorshnevad05924bdf64
diff --git a/plugins/chat/lib/chat/messages_exporter.rb b/plugins/chat/lib/chat/messages_exporter.rbindex f81784233b4..b627049f9ef 100644--- a/plugins/chat/lib/chat/messages_exporter.rb+++ b/plugins/chat/lib/chat/messages_exporter.rb@@ -4,10 +4,44 @@ module Chatmodule MessagesExporterLIMIT = 10_000- def chat_message_export+ def chat_message_export(&block)+ # perform 1 db query per month:+ now = Time.now+ from = 6.months.ago+ while from <= now+ export(from, from + 1.month, &block)+ from = from + 1.month+ end+ end++ def get_header(entity)+ if entity === "chat_message"+ %w[+ id+ chat_channel_id+ chat_channel_name+ user_id+ username+ message+ cooked+ created_at+ updated_at+ deleted_at+ in_reply_to_id+ last_editor_id+ last_editor_username+ ]+ else+ super+ end+ end++ private++ def export(from, to)Chat::Message.unscoped- .where(created_at: 6.months.ago..Time.current)+ .where(created_at: from..to).includes(:chat_channel).includes(:user).includes(:last_editor)@@ -32,27 +66,5 @@ module Chat)endend-- def get_header(entity)- if entity === "chat_message"- %w[- id- chat_channel_id- chat_channel_name- user_id- username- message- cooked- created_at- updated_at- deleted_at- in_reply_to_id- last_editor_id- last_editor_username- ]- else- super- end- endendenddiff --git a/plugins/chat/spec/system/admin/csv_exports_spec.rb b/plugins/chat/spec/system/admin/csv_exports_spec.rbindex 012b948d59a..339f9b61da0 100644--- a/plugins/chat/spec/system/admin/csv_exports_spec.rb+++ b/plugins/chat/spec/system/admin/csv_exports_spec.rb@@ -11,7 +11,10 @@ RSpec.describe "Chat CSV exports", type: :system doendxit "exports chat messages" do- message = Fabricate(:chat_message)+ message_1 = Fabricate(:chat_message, created_at: 12.months.ago)+ message_2 = Fabricate(:chat_message, created_at: 6.months.ago)+ message_3 = Fabricate(:chat_message, created_at: 1.months.ago)+ message_4 = Fabricate(:chat_message, created_at: Time.now)visit "/admin/plugins/chat"click_button "Create export"@@ -39,8 +42,17 @@ RSpec.describe "Chat CSV exports", type: :system do],)+ assert_message(exported_data.second, message_1)+ assert_message(exported_data.third, message_2)+ assert_message(exported_data.fourth, message_3)+ assert_message(exported_data.fifth, message_4)+ ensure+ csv_export_pm_page.clear_downloads+ end++ def assert_message(exported_message, message)time_format = "%Y-%m-%d %H:%M:%S UTC"- expect(exported_data.second).to eq(+ expect(exported_message).to eq([message.id.to_s,message.chat_channel.id.to_s,@@ -57,7 +69,5 @@ RSpec.describe "Chat CSV exports", type: :system domessage.last_editor.username,],)- ensure- csv_export_pm_page.clear_downloadsendend