20221025171544_add_index_ip_blocks_on_ip.rb 523 B

12345678910111213141516171819
  1. # frozen_string_literal: true
  2. class AddIndexIpBlocksOnIp < ActiveRecord::Migration[6.1]
  3. disable_ddl_transaction!
  4. def up
  5. duplicates = IpBlock.connection.select_all('SELECT string_agg(id::text, \',\') AS ids FROM ip_blocks GROUP BY ip HAVING count(*) > 1').to_ary
  6. duplicates.each do |row|
  7. IpBlock.where(id: row['ids'].split(',')[0...-1]).destroy_all
  8. end
  9. add_index :ip_blocks, :ip, unique: true, algorithm: :concurrently
  10. end
  11. def down
  12. remove_index :ip_blocks, :ip, unique: true
  13. end
  14. end