Browse Source

Fix IP blocks not having a unique index (#19456)

closed-social-glitch-2
Eugen Rochko 2 years ago
committed by GitHub
parent
commit
487d81fb92
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 2 deletions
  1. +1
    -1
      app/controllers/admin/ip_blocks_controller.rb
  2. +1
    -0
      app/models/ip_block.rb
  3. +17
    -0
      db/migrate/20221025171544_add_index_ip_blocks_on_ip.rb
  4. +2
    -1
      db/schema.rb

+ 1
- 1
app/controllers/admin/ip_blocks_controller.rb View File

@ -5,7 +5,7 @@ module Admin
def index def index
authorize :ip_block, :index? authorize :ip_block, :index?
@ip_blocks = IpBlock.page(params[:page])
@ip_blocks = IpBlock.order(ip: :asc).page(params[:page])
@form = Form::IpBlockBatch.new @form = Form::IpBlockBatch.new
end end

+ 1
- 0
app/models/ip_block.rb View File

@ -25,6 +25,7 @@ class IpBlock < ApplicationRecord
} }
validates :ip, :severity, presence: true validates :ip, :severity, presence: true
validates :ip, uniqueness: true
after_commit :reset_cache after_commit :reset_cache

+ 17
- 0
db/migrate/20221025171544_add_index_ip_blocks_on_ip.rb View File

@ -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

+ 2
- 1
db/schema.rb View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_10_21_055441) do
ActiveRecord::Schema.define(version: 2022_10_25_171544) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -521,6 +521,7 @@ ActiveRecord::Schema.define(version: 2022_10_21_055441) do
t.inet "ip", default: "0.0.0.0", null: false t.inet "ip", default: "0.0.0.0", null: false
t.integer "severity", default: 0, null: false t.integer "severity", default: 0, null: false
t.text "comment", default: "", null: false t.text "comment", default: "", null: false
t.index ["ip"], name: "index_ip_blocks_on_ip", unique: true
end end
create_table "list_accounts", force: :cascade do |t| create_table "list_accounts", force: :cascade do |t|

Loading…
Cancel
Save