From 9c0d5277adfde9f7daee0b0c83d4e68dd331e905 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 8 Feb 2012 14:09:50 +0000 Subject: [PATCH] Combine the address and netmask for an ACL in one column --- app/models/acl.rb | 2 +- ...120208122334_merge_acl_address_and_mask.rb | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20120208122334_merge_acl_address_and_mask.rb diff --git a/app/models/acl.rb b/app/models/acl.rb index 04f04d8f9..8eeb2310a 100644 --- a/app/models/acl.rb +++ b/app/models/acl.rb @@ -1,3 +1,3 @@ class Acl < ActiveRecord::Base - scope :address, lambda { |address| where("? & netmask = address", address) } + scope :address, lambda { |address| where("address >> ?", address) } end diff --git a/db/migrate/20120208122334_merge_acl_address_and_mask.rb b/db/migrate/20120208122334_merge_acl_address_and_mask.rb new file mode 100644 index 000000000..791bf659a --- /dev/null +++ b/db/migrate/20120208122334_merge_acl_address_and_mask.rb @@ -0,0 +1,45 @@ +require "ipaddr" + +class IPAddr + def address + _to_string(@addr) + end + + def netmask + _to_string(@mask_addr) + end +end + +class MergeAclAddressAndMask < ActiveRecord::Migration + def up + Acl.find_each do |acl| + address = IPAddr.new(acl.address) + netmask = IPAddr.new(acl.netmask) + prefix = 0 + + while netmask != "0.0.0.0" + netmask = netmask << 1 + prefix = prefix + 1 + end + + acl.address = "#{address.mask(prefix)}/#{prefix}" + acl.save! + end + + remove_column :acls, :netmask + end + + def down + add_column :acls, :netmask, :inet + + Acl.find_each do |acl| + address = IPAddr.new(acl.address) + + acl.address = address.address + acl.netmask = address.netmask + acl.save! + end + + change_column :acls, :netmask, :inet, :null => false + end +end -- 2.39.5