Bignums, integers and migrations

Hi. I’m using a migration to extract data from one table, to another. In
the process I also convert ip-adresses to integers, for insertion in the
new table.

Problem is that migrations doesn’t seem to handle large numbers very
well. The conversion works fine from the script/console, but when
running it from migrations only very low IP-ranges “make it thru”.
Integer-based IPs seem to be capped around 2150000000.

def self.up
require ‘ipaddr’
@ips = GeoIpDatabase.find(:all)
@ips.each do |ip|
start_ip = IPAddr.new(ip.start_ip)
end_ip = IPAddr.new(ip.end_ip)
new_ip = Ip.new( :start_ip => start_ip.to_s, :end_ip =>
end_ip.to_s, :start_ip_integer => start_ip.to_i, :end_ip_integer =>
end_ip.to_i )
new_ip.save
end
end

I’m using MySQL, with the integer-columns migrated as, yes, integers :wink:
Anyone know of a workaround?

Thanks!

Gu stav wrote:

I’m using MySQL, with the integer-columns migrated as, yes, integers :wink:
Anyone know of a workaround?

You are probably running into the limitation of the integer type of the
database. Try storing them as strings instead of integers and use the
model to generate integers from them when you need them. That is, have
db attributes :start_ip_intstring and :end_ip_intstring (say) and
virtual attributes :start_ip_integer and :end_ip_integer which do the
conversion.

You are probably running into the limitation of the integer type of the
database. Try storing them as strings instead of integers and use the
model to generate integers from them when you need them. That is, have
db attributes :start_ip_intstring and :end_ip_intstring (say) and
virtual attributes :start_ip_integer and :end_ip_integer which do the
conversion.

Problem is that I’m performing math stuff on them to see if an IP-adress
is in the range in between:

Ip.find(:all, :conditions => ["start_ip_integer <= ? AND end_ip_integer

= ?", @request_ip, @request_ip])

So converting them one by one would be painfully slow i suppose?
Specially since I have about 1 million ranges :wink:

The problem seems to be corrected when I change the int column to bigint
in the MySQL table. However, I don’t know if I can set it to bigint in
the migrations directly. Can I?

Problem solved, simply by changin :integer to :bigint in the migrations.
Thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs