~cytrogen/masto-fe

ref: bb98d970e3f3333f85d454bc1fd26a1b582b14d2 masto-fe/db/migrate/20221025171544_add_index_ip_blocks_on_ip.rb -rw-r--r-- 523 bytes
bb98d970 — Claire Merge pull request #2291 from ClearlyClaire/glitch-soc/merge-upstream 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# frozen_string_literal: true

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