DataMapper to migrate data from one table to another.

China☆狼群 提交于 2019-12-25 18:44:08

问题


If I am using DataMapper, and I have two databases, is there any way using migration.rb to copy a table for example table person from database 1 to database 2? (same schema and table values).

Referring this:https://github.com/datamapper/dm-migrations/blob/master/examples/sample_migration.rb

It only tells me how to add/modify/drop tables.

Thanks for help.


回答1:


I don't think that's the intention of dm-migrations. I believe the easiest way would be something like this:

DataMapper.setup(:default, db1_config)
DataMapper.setup(:new, db2_config)
class Foo
  include DataMapper::Resource
  property :id, Serial
  property :name, String
  ...
end
DataMapper.finalize

Foo.each do |foo|
  DataMapper.repository(:new) do
    # It may not let you set the "id" attribute here...
    Foo.create(foo.attributes)
  end
end

Edit

In hindsight, I'm not sure if you were asking how to to copy table structure as to opposed to table data. This is obviously copying table data.



来源:https://stackoverflow.com/questions/8807567/datamapper-to-migrate-data-from-one-table-to-another

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!