Skip to content

Commit

Permalink
Fix: #540 emoji_reactionsテーブルのuriカラムへのインデックス設定時、重複を削除する処理 (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmycode authored Feb 14, 2024
1 parent eb9d8a8 commit f8b32fa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
9 changes: 0 additions & 9 deletions db/migrate/20240212230358_fix_uri_index_to_emoji_reactions.rb

This file was deleted.

28 changes: 28 additions & 0 deletions db/post_migrate/20240212230358_fix_uri_index_to_emoji_reactions.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f8b32fa

Please sign in to comment.