是否有任何用于 NoSQL 数据库架构迁移的工具?
问题描述
我正在寻找一种为 MongoDB 或 CouchDB 等数据库自动迁移架构的方法.
I'm looking a way to automate schema migration for such databases like MongoDB or CouchDB.
这个工具最好是用python写的,其他任何语言都可以.
Preferably, this instument should be written in python, but any other language is ok.
解决方案
由于 nosql 数据库可以包含大量数据,因此您无法在常规 rdbms 中迁移它.实际上,一旦您的数据超过某个大小阈值,您就无法为 rdbms 执行此操作.让您的网站停工一天以将字段添加到现有表是不切实际的,因此使用 rdbms 您最终会做丑陋的补丁,例如仅为该字段添加新表并进行连接以获取数据.在 nosql 世界中你可以做几件事.
Since a nosql database can contain huge amounts of data you can not migrate it in the regular rdbms sence. Actually you can't do it for rdbms as well as soon as your data passes some size threshold. It is impractical to bring your site down for a day to add a field to an existing table, and so with rdbms you end up doing ugly patches like adding new tables just for the field and doing joins to get to the data. In nosql world you can do several things.
- 正如其他人建议的那样,您可以编写代码,以便处理可能架构的不同版本".这通常比看起来更简单.许多类型的模式更改对代码来说都是微不足道的.例如,如果您想向架构中添加一个新字段,您只需将其添加到所有新记录中,所有旧记录上它都将为空(您不会收到字段不存在"错误或任何内容;).如果您需要旧记录中的字段的默认"值,则在代码中太简单了.
- 另一个选项,实际上唯一明智的选择是在字段重命名和结构更改等非平凡的架构更改中存储 schema_version 在每个记录中,并使用代码将数据从任何版本迁移到 上的下一个版本阅读.即,如果您当前的架构版本是 10,并且您从数据库中读取版本为 7 的记录,那么您的数据库层应该调用 migrate_8、migrate_9 和 migrate_10.这样访问的数据会逐渐迁移到新版本.如果它没有被访问,那么谁在乎它是哪个版本;)
- As others suggested you can write your code so that it will handle different 'versions' of the possible schema. this is usually simpler then it looks. Many kinds of schema changes are trivial to code around. for example if you want to add a new field to the schema, you just add it to all new records and it will be empty on the all old records (you will not get "field doesn't exist" errors or anything ;). if you need a 'default' value for the field in the old records it is too trivially done in code.
- Another option and actually the only sane option going forward with non-trivial schema changes like field renames and structural changes is to store schema_version in EACH record, and to have code to migrate data from any version to the next on READ. i.e. if your current schema version is 10 and you read a record from the database with the version of 7, then your db layer should call migrate_8, migrate_9, and migrate_10. This way the data that is accessed will be gradually migrated to the new version. and if it is not accessed, then who cares which version is it;)
相关文章