[英]Migrating some objects from one database to another
How can I dump one user with all his associations (comments, posts etc) from one database (development, sqlite) to insert it another (production, mysql). 如何从一个数据库(开发,sqlite)转储一个用户的所有关联(评论,帖子等)以将其插入另一个(生产,mysql)。
Should I dump it into yaml or to sql or something else? 我应该将它转储到yaml或sql或其他东西?
Ok. 好。
God Save the YAML
I've used YAML dumping into file from development and loading this in my production. 我已经在开发中使用YAML转储到文件中并在我的生产中加载它。 There was hack with id, that have changed, due it is auto_increament.
由于它是auto_increament,因此存在已经改变的内容。
development 发展
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) }
...
production 生产
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
...
You can use this gem: https://github.com/ludicast/yaml_db 您可以使用此gem: https : //github.com/ludicast/yaml_db
YamlDb is a database-independent format for dumping and restoring data.
YamlDb是一种与数据库无关的转储和恢复数据格式。 It complements the the database-independent schema format found in db/schema.rb.
它补充了db / schema.rb中的数据库无关模式格式。 The data is saved into db/data.yml.
数据保存到db / data.yml中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.