将某些对象从一个数据库迁移到另一个数据库 [英] Migrating some objects from one database to another

查看:73
本文介绍了将某些对象从一个数据库迁移到另一个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从一个数据库(开发,sqlite)中将一个用户及其所有关联(评论,帖子等)转储到另一个数据库(生产,mysql)中.

How can I dump one user with all his associations (comments, posts etc) from one database (development, sqlite) to insert it another (production, mysql).

我应该将其转储到yaml还是sql中?

Should I dump it into yaml or to sql or something else?

推荐答案

好.

God Save the YAML

我已经使用YAML从开发中转储到文件中并将其加载到我的产品中.由于ID为auto_increament,因此发生了ID更改的黑客事件.

I've used YAML dumping into file from development and loading this in my production. There was hack with id, that have changed, due it is auto_increament.

发展

user     = User.find X
posts    = user.posts
comments = user.comments
...
File.open("user.yml", "w")    { |f| f << YAML::dump(user) }
File.open("comments.yml", "w"){ |f| f << YAML::dump(comments) }
File.open("posts.yml", "w")   { |f| f << YAML::dump(posts) }
...

生产

user     = YAML::load_file("user.yml")
posts    = YAML::load_file("posts.yml")
comments = YAML::load_file("comments.yml")
new_user = user.clone.save # we should clone our object, because it isn't exist
posts.each do |p|
  post = p.clone
  post.user = new_user
  post.save
end
...

这篇关于将某些对象从一个数据库迁移到另一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆