简体   繁体   English

将一些对象从一个数据库迁移到另一个数据库

[英]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.

相关问题 mysql 将数据从一个数据库迁移到另一个数据库 - Migrating data from one database to another database in mysql 使用talend将数据从一个mysql数据库迁移到另一个 - Migrating data from one mysql database to another using talend 使用LEFT JOIN将数据从一个数据库迁移到另一个数据库 - Migrating data from one database to another using LEFT JOIN 从一张桌子迁移到另一张桌子 - Migrating from one table to another 将数据库从一个PXC环境迁移到另一个环境 - Migrating databases from One PXC environment to another 将MySQL服务器从一个盒子迁移到另一个盒子 - Migrating a MySQL server from one box to another 在将数据库从一个数据库迁移到另一个数据库时,保持数据库一致性的最佳方法是什么 - What is the best way to maintain consistency in database while migrating it from one to another 如何将数据从一个数据库迁移到另一个具有不同结构的数据库? - How can I go about migrating data from one database to another with different structure? 将行从一个数据库复制到另一个数据库并更改一些值 - Copying row from one database to another and changing some values 将表数据从MySQL数据库迁移到另一个MySQL数据库 - Migrating table data from a MySQL database to another MySQL database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM