From f8b32facb42080a9017d120e64a52c1aab8abd77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?KMY=EF=BC=88=E9=9B=AA=E3=81=82=E3=81=99=E3=81=8B=EF=BC=89?= Date: Wed, 14 Feb 2024 09:58:45 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20#540=20emoji=5Freactions=E3=83=86?= =?UTF-8?q?=E3=83=BC=E3=83=96=E3=83=AB=E3=81=AEuri=E3=82=AB=E3=83=A9?= =?UTF-8?q?=E3=83=A0=E3=81=B8=E3=81=AE=E3=82=A4=E3=83=B3=E3=83=87=E3=83=83?= =?UTF-8?q?=E3=82=AF=E3=82=B9=E8=A8=AD=E5=AE=9A=E6=99=82=E3=80=81=E9=87=8D?= =?UTF-8?q?=E8=A4=87=E3=82=92=E5=89=8A=E9=99=A4=E3=81=99=E3=82=8B=E5=87=A6?= =?UTF-8?q?=E7=90=86=20(#543)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...230358_fix_uri_index_to_emoji_reactions.rb | 9 ------ ...230358_fix_uri_index_to_emoji_reactions.rb | 28 +++++++++++++++++++ 2 files changed, 28 insertions(+), 9 deletions(-) delete mode 100644 db/migrate/20240212230358_fix_uri_index_to_emoji_reactions.rb create mode 100644 db/post_migrate/20240212230358_fix_uri_index_to_emoji_reactions.rb diff --git a/db/migrate/20240212230358_fix_uri_index_to_emoji_reactions.rb b/db/migrate/20240212230358_fix_uri_index_to_emoji_reactions.rb deleted file mode 100644 index 1523f8adbb6ae2..00000000000000 --- a/db/migrate/20240212230358_fix_uri_index_to_emoji_reactions.rb +++ /dev/null @@ -1,9 +0,0 @@ -# frozen_string_literal: true - -class FixUriIndexToEmojiReactions < ActiveRecord::Migration[7.1] - disable_ddl_transaction! - - def change - add_index :emoji_reactions, :uri, unique: true, algorithm: :concurrently - end -end diff --git a/db/post_migrate/20240212230358_fix_uri_index_to_emoji_reactions.rb b/db/post_migrate/20240212230358_fix_uri_index_to_emoji_reactions.rb new file mode 100644 index 00000000000000..2a5721d814360b --- /dev/null +++ b/db/post_migrate/20240212230358_fix_uri_index_to_emoji_reactions.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +class FixUriIndexToEmojiReactions < ActiveRecord::Migration[7.1] + disable_ddl_transaction! + + class EmojiReaction < ApplicationRecord + end + + def up + # Remove duplications (very old kmyblue code [2023/03-04] maybe made some duplications) + duplications = EmojiReaction.where('uri IN (SELECT uri FROM emoji_reactions GROUP BY uri HAVING COUNT(*) > 1)') + .to_a.group_by(&:uri).to_h + + if duplications.any? + EmojiReaction.transaction do + duplications.each do |h| + h[1].drop(1).each(&:destroy) + end + end + end + + add_index :emoji_reactions, :uri, unique: true, algorithm: :concurrently + end + + def down + remove_index :emoji_reactions, :uri + end +end