Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

ruby - DataMapper to migrate data from one table to another.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...