简体   繁体   English

将postgresql数据库迁移到uuid rails 3.1

[英]Migrate postgresql database to uuid rails 3.1

I am using rails 3.1 and ruby 1.9.3,Now i want to use uuid concept in rails 3 so i did like :- 我正在使用rails 3.1和ruby 1.9.3,现在我想在rails 3中使用uuid概念,所以我喜欢: -

create_table :posts, :id => false do |t|
 t.string :uuid, :limit => 36, :primary => true
end

ActiveRecord::Base.class_eval do
 set_primary_key 'uuid'
 before_create :generate_uuid
 def generate_uuid
 self.id = UUIDTools::UUID.random_create.to_s
 end
end

This is working for new data,now i want to migrate existing data with relation.for uuid they are using datatype as string,in postgresql the data type used for primary_key and foreign key is integer ,so if i am trying to change foreign key integer to string it is throwing error. 这适用于新数据,现在我想用关系迁移现有数据。因为uuid他们使用数据类型作为字符串,在postgresql中用于primary_key和外键的数据类型是整数,所以如果我试图更改外键整数字符串它是抛出错误。

Could you please tell me some example,how to do this. 你能告诉我一些例子,如何做到这一点。

kingston.s kingston.s

First of all, to use UUIDs in ActiveRecord you need to enable the uuid-ossp extension. 首先,要在ActiveRecord中使用UUID,您需要启用uuid-ossp扩展。 Create a new migration. 创建新的迁移。

class EnableUuidOssp < ActiveRecord::Migration
  def change
    enable_extension 'uuid-ossp'
  end
end

Second, you do do not need to use string type in your migrations, there is a uuid type. 其次,您不需要在迁移中使用字符串类型,而是存在uuid类型。 When creating a new table: 创建新表时:

create_table :posts, id: :uuid do |t|
end

This will automatically generate a UUID in the same way that an incremental Integer ID is normally generated. 这将自动生成UUID,其方式与通常生成增量Integer ID的方式相同。 When you want to add a UUID field to an existing table: 如果要将UUID字段添加到现有表:

change_table :posts do |t|
  t.uuid :uuid, default: 'uuid_generate_v4()'
end

The default: 'uuid_generate_v4()' will ensure that a new UUID is generated for you by Postgres. 默认值:'uuid_generate_v4()'将确保Postgres为您生成新的UUID。

Third, to actually migrate existing data I guess you would need to create migrations that 1) add UUID fields to all of the models 2) create new UUID foreign keys 3) associate the models using the UUID foreign keys 4) remove the old foreign keys 5) rename the new foreign keys: 第三,要实际迁移现有数据,我猜你需要创建迁移1)向所有模型添加UUID字段2)创建新的UUID外键3)使用UUID外键关联模型4)删除旧外键5)重命名新的外键:

class AddUuidToPosts < ActiveRecord::Migration
  def change
    change_table :posts do |t|
      t.uuid :uuid, default: 'uuid_generate_v4()'
    end
  end
end

# assuming you have a comments table that belongs to posts
class AddUuidToComments < ActiveRecord::Migration
  def change
    change_table :comments do |t|
      t.uuid :uuid, default: 'uuid_generate_v4()'
    end
  end
end

class AssociateCommentsWithPostings < ActiveRecord::Migration
  def change
    # Add a uuid_posting_id field to comments relate comments to postings
    # through the posting UUID
    change_table :comments do |t|
      t.uuid :uuid_posting_id
    end

    # Loop through all existing postings and update all associated comments
    # new foreign key field
    Posting.all.each do |posting|
      # Not sure about this, but you might need to touch the posting to generate
      # a UUID
      posting.touch 

      Comment.where(posting_id: posting.id).
        update_all(uuid_posting_id: posting.uuid)
    end

    remove_column :comments, :posting_id
    rename_column :comments, :uuid_posting_id, :posting_id
  end
end

# You don't need to override ActiveRecord::Base to set the primary key to :uuid.
# Only do this in existing models that you migrated to UUIDs. In any new tables
# that you create, the id column will be a UUID, as long as you use the migration
# format that I showed you at the top.
class Posting < ActiveRecord::Base
  set_primary_key :uuid
end

You should probably go the extra mile and actually remove the old Integer id fields and rename the new UUID ids to "id" but I'm not sure how to do that off the top of my head. 你应该加倍努力,实际上删除旧的整数id字段并将新的UUID id重命名为“id”,但我不知道如何做到这一点。 Anyway, I think this approach should work. 无论如何,我认为这种方法应该有效。 There could be a couple of errors or bits missing though, it's a bit late over here. 可能会有一些错误或位丢失,但这里有点晚了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM