問題描述
我正在尋找一種為 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;)
這篇關于是否有任何用于 NoSQL 數據庫架構遷移的工具?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!