]> git.openstreetmap.org Git - rails.git/commitdiff
Combine the address and netmask for an ACL in one column
authorTom Hughes <tom@compton.nu>
Wed, 8 Feb 2012 14:09:50 +0000 (14:09 +0000)
committerTom Hughes <tom@compton.nu>
Wed, 8 Feb 2012 14:09:50 +0000 (14:09 +0000)
app/models/acl.rb
db/migrate/20120208122334_merge_acl_address_and_mask.rb [new file with mode: 0644]

index 04f04d8f96d2b93ae8eb94eb0a6d36dc133a855f..8eeb2310a77674545fa68c2c1d5d8f7deb75e7e0 100644 (file)
@@ -1,3 +1,3 @@
 class Acl < ActiveRecord::Base
 class Acl < ActiveRecord::Base
-  scope :address, lambda { |address| where("? & netmask = address", address) }
+  scope :address, lambda { |address| where("address >> ?", address) }
 end
 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 (file)
index 0000000..791bf65
--- /dev/null
@@ -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