尝试将现有IPS转换为整数

时间:2015-03-21 22:04:00

标签: ruby-on-rails database-migration

我有一个users表,其中包含一串字符串格式的IPS,现在我们决定开始将IPS存储为整数,因此我将这段代码添加到我的user.rb模型中:

class User < ActiveRecord::Base
  [:current_sign_in_ip, :last_sign_in_ip].each do |field|
    define_method(field) do
      ip = read_attribute(field)
      return nil unless ip
      ip += 4_294_967_296 if ip < 0 # Convert from 2's complement
      "#{(ip & 0xFF000000) >> 24}.#{(ip & 0x00FF0000) >> 16}.#{(ip & 0x0000FF00) >> 8}.#{ip & 0x000000FF}"
    end

    define_method("#{field}=") do |value|
      quads = value.split('.')
      if quads.length == 4
        as_int = (quads[0].to_i * (2**24)) + (quads[1].to_i * (2**16)) + (quads[2].to_i * (2**8)) + quads[3].to_i
        as_int -= 4_294_967_296 if as_int > 2147483647 # Convert to 2's complement
      else
        as_int = nil
      end
      write_attribute(field, as_int)
    end

  end
end

此代码工作正常,当新用户注册时,他/她的IP将存储为整数。现在我需要创建一个迁移,将IP列的类型从字符串更改为整数:

class ConvertStringIpsToIntegers&lt; ActiveRecord的::迁移

def up

  change_column :users, :current_sign_in_ip, :integer
  change_column :users, :last_sign_in_ip,    :integer
end

def down
  change_column :users, :current_sign_in_ip, :string
  change_column :users, :last_sign_in_ip,    :string
end

然而,此迁移将破坏存储为字符串&#34; 127.0.0.1&#34;的旧IPS。迁移后将变为127。 知道如何在运行迁移之前将所有现有的IPS从字符串转换为整数吗?

由于

1 个答案:

答案 0 :(得分:1)

您可以在迁移中包含转换代码。在单独的转换中将字符串实际转换为整数。

def up
  User.all.each do
    [:current_sign_in_ip, :last_sign_in_ip].each do |field|
      quads = user.read_attribute(field).split('.')
      if quads.length == 4
        as_int = (quads[0].to_i * (2**24)) + (quads[1].to_i * (2**16)) + (quads[2].to_i * (2**8)) + quads[3].to_i
        as_int -= 4_294_967_296 if as_int > 2147483647 # Convert to 2's complement
      else
        as_int = nil
      end
      user.write_attribute(field, "#{as_int}")
    end
  end
end

def down
  User.all.each do |user|
    [:current_sign_in_ip, :last_sign_in_ip].each do |field|
      ip = user.read_attribute(field).to_i
      return nil unless ip
      ip += 4_294_967_296 if ip < 0 # Convert from 2's complement
      user.write_attribute(field, "#{(ip & 0xFF000000) >> 24}.#{(ip & 0x00FF0000) >> 16}.#{(ip & 0x0000FF00) >> 8}.#{ip & 0x000000FF}")
    end
  end
end