From 0e989a5b8f98f6c607fc9a727910e4e693bf8abd Mon Sep 17 00:00:00 2001
From: Eugen Rochko <eugen@zeonfederated.com>
Date: Sun, 6 Jan 2019 23:53:46 +0100
Subject: [PATCH] Fix unique violation in downcase custom emoji domain
 migration (#9733)

Fix #9727
---
 .../20181207011115_downcase_custom_emoji_domains.rb    | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/db/migrate/20181207011115_downcase_custom_emoji_domains.rb b/db/migrate/20181207011115_downcase_custom_emoji_domains.rb
index c9db3800da..1fce2cb379 100644
--- a/db/migrate/20181207011115_downcase_custom_emoji_domains.rb
+++ b/db/migrate/20181207011115_downcase_custom_emoji_domains.rb
@@ -1,7 +1,15 @@
 class DowncaseCustomEmojiDomains < ActiveRecord::Migration[5.2]
   disable_ddl_transaction!
 
-  def change
+  def up
+    duplicates = CustomEmoji.connection.select_all('SELECT string_agg(id::text, \',\') AS ids FROM custom_emojis GROUP BY lower(domain) HAVING count(*) > 1').to_hash
+
+    duplicates.each do |row|
+      CustomEmoji.where(id: row['ids'].split(',')[0...-1]).destroy_all
+    end
+
     CustomEmoji.in_batches.update_all('domain = lower(domain)')
   end
+
+  def down; end
 end