FYI, you can currently store IPv4 in the database using a signed field.

MIN_INT4_UNSIGNED = -2147483648

I have a method like this that converts an ip string to an integer,  
then adds the offset above to make it signed.

   def self.ip_to_int4(an_ip)
     an_ip.split('.').inject(0){|sum, i| (sum << 8) + i.to_i } +  
Integer::MIN_INT4_UNSIGNED
   end

To convert it back you subtract the offset when you load it from the db.

   def self.int4_to_ip(int4)
     int4 -= Integer::MIN_INT4_UNSIGNED
     r = []
     4.times{
       r << int4 % 256
       int4 >>= 8
     }
     r.reverse.join('.')
   end

I still support the unsigned integer idea though.  Perhaps a setting  
in environment.rb could explicitly turn it on, then projects could  
decide whether or not they wanted to use it.  This would be handy for  
both existing apps (not to break anything) and new ones (which can  
rake db:migrate:reset without penalty).

- Steve

On 4-Jun-08, at 7:09 AM, Tarmo Tänav wrote:

>
> On K, 2008-06-04 at 06:25 -0700, rob-twf wrote:
>> As a practical example: I originally started work on this because I
>> wanted to store IPv4 addresses in my database, and to do that I  
>> really
>> needed an unsigned 32 bit integer.
>>
>> Hope this helps...
>
> Yes, I agree that for IPv4 it is indeed useful.
> But I do have a few more questions.
>
> 1. If unsigned was made default and a project that was built before  
> this
> started using the newer migration code that would mean that part of
> their database uses unsigned and part uses signed integers.
>
> 2. Does mysql support changing the type of a column (including a  
> primary
> key column) from signed to unsigned? Would rails' change_column be  
> able
> to do it?
>
> 3. I think mysql does not support FK's between different integer
> types, so wouldn't making unsigned the default cause problems
> for all new FK fields (and their constraints) that refer to existing
> tables (with signed int primary keys)?
>
> Apart from the possibility of storing IPv4 addresses the usefulness
> of this seems quite limited, most applications probably do not have
> tables that touch the limit of 32bit signed integers and even if they
> did a two-fold increase in the number of keys would not be a good
> long-term solution.
>
> So basically I'm concerned about the fact that making this the
> default seems to not give great benefit yet unless I'm wrong about
> the above things could cause issues if done so for an existing
> project. So perhaps the default should be configurable and at
> least for older applications should not be changed?
>
>
> Thank you,
>
> -- 
> Tarmo Tänav <[EMAIL PROTECTED]>
>
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to