]> git.openstreetmap.org Git - rails.git/blob - db/migrate/20120208122334_merge_acl_address_and_mask.rb
Make OSM::GreatCircle#bounds return a BoundingBox object
[rails.git] / db / migrate / 20120208122334_merge_acl_address_and_mask.rb
1 require "ipaddr"
2
3 class IPAddr
4   def address
5     _to_string(@addr)
6   end
7
8   def netmask
9     _to_string(@mask_addr)
10   end
11 end
12
13 class MergeAclAddressAndMask < ActiveRecord::Migration[5.0]
14   def up
15     Acl.find_each do |acl|
16       address = IPAddr.new(acl.address)
17       netmask = IPAddr.new(acl.netmask)
18       prefix = 0
19
20       while netmask != "0.0.0.0"
21         netmask = netmask << 1
22         prefix += 1
23       end
24
25       acl.address = "#{address.mask(prefix)}/#{prefix}"
26       acl.save!
27     end
28
29     remove_column :acls, :netmask
30   end
31
32   def down
33     add_column :acls, :netmask, :inet
34
35     Acl.find_each do |acl|
36       address = IPAddr.new(acl.address)
37
38       acl.address = address.address
39       acl.netmask = address.netmask
40       acl.save!
41     end
42
43     change_column :acls, :netmask, :inet, :null => false
44   end
45 end