問題描述
我正在尋找一種為 MongoDB 或 CouchDB 等數(shù)據(jù)庫自動(dòng)遷移架構(gòu)的方法.
I'm looking a way to automate schema migration for such databases like MongoDB or CouchDB.
這個(gè)工具最好是用python寫的,其他任何語言都可以.
Preferably, this instument should be written in python, but any other language is ok.
推薦答案
由于 nosql 數(shù)據(jù)庫可以包含大量數(shù)據(jù),因此您無法在常規(guī) rdbms 中遷移它.實(shí)際上,一旦您的數(shù)據(jù)超過某個(gè)大小閾值,您就無法為 rdbms 執(zhí)行此操作.讓您的網(wǎng)站停工一天以將字段添加到現(xiàn)有表是不切實(shí)際的,因此使用 rdbms 您最終會做丑陋的補(bǔ)丁,例如僅為該字段添加新表并進(jìn)行連接以獲取數(shù)據(jù).在 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.
- 正如其他人建議的那樣,您可以編寫代碼,以便處理可能架構(gòu)的不同版本".這通常比看起來更簡單.許多類型的模式更改對代碼來說都是微不足道的.例如,如果您想向架構(gòu)中添加一個(gè)新字段,您只需將其添加到所有新記錄中,所有舊記錄上它都將為空(您不會收到字段不存在"錯(cuò)誤或任何內(nèi)容;).如果您需要舊記錄中的字段的默認(rèn)"值,則在代碼中太簡單了.
- 另一個(gè)選項(xiàng),實(shí)際上唯一明智的選擇是在字段重命名和結(jié)構(gòu)更改等非平凡的架構(gòu)更改中存儲 schema_version 在每個(gè)記錄中,并使用代碼將數(shù)據(jù)從任何版本遷移到 上的下一個(gè)版本閱讀.即,如果您當(dāng)前的架構(gòu)版本是 10,并且您從數(shù)據(jù)庫中讀取版本為 7 的記錄,那么您的數(shù)據(jù)庫層應(yīng)該調(diào)用 migrate_8、migrate_9 和 migrate_10.這樣訪問的數(shù)據(jù)會逐漸遷移到新版本.如果它沒有被訪問,那么誰在乎它是哪個(gè)版本;)
- 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;)
這篇關(guān)于是否有任何用于 NoSQL 數(shù)據(jù)庫架構(gòu)遷移的工具?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!