From c2a733a95a422837d517a991de04f8462119cc5e Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Fri, 23 Dec 2022 10:23:57 +0800 Subject: [PATCH] DEV: Make recent creation of chat index idempotent (#19603) --- ...20221219082223_add_index_to_chat_messages.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/plugins/chat/db/migrate/20221219082223_add_index_to_chat_messages.rb b/plugins/chat/db/migrate/20221219082223_add_index_to_chat_messages.rb index 90a660dc1fb..f63d6dfae5b 100644 --- a/plugins/chat/db/migrate/20221219082223_add_index_to_chat_messages.rb +++ b/plugins/chat/db/migrate/20221219082223_add_index_to_chat_messages.rb @@ -3,7 +3,20 @@ class AddIndexToChatMessages < ActiveRecord::Migration[7.0] disable_ddl_transaction! - def change - add_index :chat_messages, [:chat_channel_id, :id], where: "deleted_at IS NULL", algorithm: :concurrently + def up + # Transaction has been disabled so we need to clean up the invalid index if index creation timeout + execute <<~SQL + DROP INDEX CONCURRENTLY IF EXISTS index_chat_messages_on_chat_channel_id_and_id + SQL + + execute <<~SQL + CREATE INDEX CONCURRENTLY index_chat_messages_on_chat_channel_id_and_id + ON chat_messages (chat_channel_id,id) + WHERE deleted_at IS NOT NULL + SQL + end + + def down + raise ActiveRecord::IrreversibleMigration end end