about summary refs log tree commit diff
path: root/db/migrate
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2022-10-25 21:43:44 +0200
committerGitHub <noreply@github.com>2022-10-25 21:43:44 +0200
commit487d81fb92350974e20ee220553a10b5229f0880 (patch)
tree00ad8445858c85ef56e2e2d2fc08b29c522d0d60 /db/migrate
parent6f01111863bfb15b3574c95c8d60d59ae1d79772 (diff)
Fix IP blocks not having a unique index (#19456)
Diffstat (limited to 'db/migrate')
-rw-r--r--db/migrate/20221025171544_add_index_ip_blocks_on_ip.rb17
1 files changed, 17 insertions, 0 deletions
diff --git a/db/migrate/20221025171544_add_index_ip_blocks_on_ip.rb b/db/migrate/20221025171544_add_index_ip_blocks_on_ip.rb
new file mode 100644
index 000000000..0221369b7
--- /dev/null
+++ b/db/migrate/20221025171544_add_index_ip_blocks_on_ip.rb
@@ -0,0 +1,17 @@
+class AddIndexIpBlocksOnIp < ActiveRecord::Migration[6.1]
+  disable_ddl_transaction!
+
+  def up
+    duplicates = IpBlock.connection.select_all('SELECT string_agg(id::text, \',\') AS ids FROM ip_blocks GROUP BY ip HAVING count(*) > 1').to_ary
+
+    duplicates.each do |row|
+      IpBlock.where(id: row['ids'].split(',')[0...-1]).destroy_all
+    end
+
+    add_index :ip_blocks, :ip, unique: true, algorithm: :concurrently
+  end
+
+  def down
+    remove_index :ip_blocks, :ip, unique: true
+  end
+end