# Bignums, integers and migrations

**URL:** <https://www.ruby-forum.com/t/bignums-integers-and-migrations/133362>\
**Category:** Rails\
**Created:** [March 30, 2008, 11:33pm UTC](https://www.ruby-forum.com/t/bignums-integers-and-migrations/133362 "2008-03-30T23:33:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 30, 2008, 11:33pm UTC](https://www.ruby-forum.com/t/bignums-integers-and-migrations/133362/1 "2008-03-30T23:33:42Z")

</div>

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 😉  
Anyone know of a workaround?

Thanks!

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 31, 2008, 12:48am UTC](https://www.ruby-forum.com/t/bignums-integers-and-migrations/133362/2 "2008-03-31T00:48:15Z")

</div>

Gu stav wrote:

> I’m using MySQL, with the integer-columns migrated as, yes, integers 😉  
> 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.

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 31, 2008, 1:03am UTC](https://www.ruby-forum.com/t/bignums-integers-and-migrations/133362/3 "2008-03-31T01:03:28Z")

</div>

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

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?

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 31, 2008, 1:12am UTC](https://www.ruby-forum.com/t/bignums-integers-and-migrations/133362/4 "2008-03-31T01:12:05Z")

</div>

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