简体   繁体   English

Rails-将数据迁移到具有不同ID的另一个DB

[英]Rails - Migrating data to another DB with different IDs

I have a scenario in which I need to dump/transfer data of one user from my Rails App to another database of same configuration and tables. 我有一种情况,我需要将一个用户的数据从我的Rails应用程序转储/传输到另一个具有相同配置和表的数据库。 For instance, The application is built as 例如,应用程序构建为

  class Company < ActiveRecord::Base
    has_many :depots
    has_many :users
  end

  class Depot < ActiveRecord::Base
    belongs_to :company
    has_many :products
  end

  class User < ActiveRecord::Base
    belongs_to :company
  end

  class Product < ActiveRecord::Base
    belongs_to :depot
  end

My requirement is, if companyA stops paying, I want to dump their data into another DB (databse2 for instance) to keep my actual DB clean and once they come back and start paying, I want this data back. 我的要求是,如果companyA停止付款,我想将其数据转储到另一个数据库(例如,databse2)中,以保持我的实际数据库清洁,一旦他们回来并开始付款,我希望将此数据退回。

Second requirement is, database2 can already have some data in it. 第二个要求是,database2中已经可以包含一些数据。 So I need to retain all the records and I want to change the IDs of companyA (as there can already be a company with the same ID) while saving in to database2 keeping associations intact. 因此,我需要保留所有记录,并且要更改companyA的ID(因为已经存在具有相同ID的公司),同时保存到database2中以保持关联完整。 It might seem silly to do this, but that is my requirement. 这样做似乎很愚蠢,但这是我的要求。 I am using Postgres as my application DB. 我正在使用Postgres作为我的应用程序数据库。 Any helps??? 有帮助吗???

You have a few options here worth investigating: 您有一些值得研究的选项:

  1. Output everything in a singular JSON file that encodes everything the client had in the database, complete with ID fields. 将所有内容输出到单个JSON文件中,该文件对客户端在数据库中拥有的所有内容进行编码,并带有ID字段。
  2. Dump out a series of CSV files that can be imported on the destination server. 转储一系列可以在目标服务器上导入的CSV文件。
  3. Dump out a single .sql file that will properly restore the data simply by running it. 转储出一个.sql文件,只需运行它即可正确还原数据。

The first option is the most elegant, but probably requires the most work. 第一个选择是最优雅的,但可能需要最多的工作。 It gives you the ability to archive your data in a neat, tidy file that's easily parsed. 它使您能够将数据归档到易于分析的整洁文件中。

The second option might be fine or could be severely ugly depending on the sorts of data you have. 第二种选择可能很好,也可能很难看,具体取决于您拥有的数据种类。 If there's any binary data involved that's probably not going to work, but for clean, text-only columns and tabular data it's usually fairly efficient. 如果涉及到任何二进制数据,则可能行不通,但对于纯文本列和表格数据而言,它通常是相当有效的。 The advantage here is you can selectively load in parts of your data without having to commit to parsing all of it. 这样做的好处是您可以有选择地加载部分数据,而不必致力于解析所有数据。

The third option isn't easily parsed, you need to restore it to be able to use it, but it does make insertion really, really simple. 第三个选项不容易解析,您需要还原它才能使用它,但是它确实使插入变得非常非常简单。 You will only have to write an archiver, no specific restoration tool is required. 您只需要编写一个存档器,就不需要特定的还原工具。

Whatever approach you take you'll need to be absolutely certain that ID numbers are never, ever reissued. 无论采用哪种方法,都需要绝对确定ID号永远不会重发。 Do not reset your sequence generators to fill in holes, and when moving databases port these over as well and test that they're set correctly. 不要重置序列生成器以填补漏洞,并且在移动数据库时也将它们移植过来并测试它们的设置是否正确。 The last thing you need is ID conflicts. 您需要做的最后一件事是ID冲突。

If you're really worried about ID conflicts you might want to switch to non-numeric IDs, like use a UUID for everything where conflicts are basically irrelevant, though this does not come without a cost. 如果您真的很担心ID冲突,则可能希望切换到非数字ID,例如对于基本上不涉及冲突的所有内容都使用UUID,尽管这并非没有代价。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM