简体   繁体   English

Rails 3 数据库移植

[英]Rails 3 database porting

Had to quickly slap something together to move a table (approx 30k records) from one db to another with different names.必须快速将某些东西放在一起以将表(大约 30k 记录)从一个数据库移动到另一个具有不同名称的数据库。

My hack solution using RoR was this:我使用 RoR 的 hack 解决方案是这样的:

for old_line in SaleItemsOld.where(:sale_id => old_sale.id)
    line = LineItems.new
        line.sale_id = new_sale.id
        ...
    line.save
end

I had the thought that this should be sped up by constructing all these new lines as SQL queries in a single string and then executing that in one go.我认为应该通过将所有这些新行构造为 SQL 在单个字符串中查询,然后在一个 go 中执行来加快速度。

Is there a better Rails solution for this?有没有更好的 Rails 解决方案?

Usually you can do this sort of thing with a migration and some carefully constructed SQL.通常你可以通过迁移和一些精心构建的 SQL 来做这种事情。 Generally the idea is do to something along the lines of:一般来说,这个想法是按照以下方式做一些事情:

INSERT INTO line_items (sale_id) SELECT id FROM sale_items_old

You can add other values as required both to the insert list and the select specification.您可以根据需要将其他值添加到插入列表和 select 规范中。

In a migration you can run arbitrary SQL by placing it in your up method:在迁移中,您可以通过将任意 SQL 放在up方法中来运行它:

def self.up
  execute("...")
end

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

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