前言
在版本迭代過程中,存在數(shù)據(jù)庫變更的幾率,如增加某張表某個(gè)字段,刪除某個(gè)字段等;
缺乏變更歷史的記錄,要么在升級多套不同版本環(huán)境時(shí),需要耗費(fèi)大量時(shí)間去尋找數(shù)據(jù)庫變更記錄以及執(zhí)行腳本;
該方案提供一種數(shù)據(jù)庫變更記錄方式;
使用到開源庫:migrate, 目前已經(jīng)支持多種不同類型數(shù)據(jù)庫(可視化目前使用的為mongodb,已經(jīng)支持);
使用
migrate提供兩種方式進(jìn)行變更操作:1. 命令行方式 2. SDK方式(使用Go)
在本方案中使用的為命令行方式(官方文檔)。
安裝
通過GitHub下載最新二進(jìn)制文件包: github.com/golang-migr…
直接解壓則可以使用;
簡單使用
每一次數(shù)據(jù)庫的變更都分為兩種情況:up(升級版本)和down(降級版本);
所以每一次變更記錄的編寫都需要兩個(gè)文件,
{version}_{title}.up.{extension}
{version}_{title}.down.{extension}
version為當(dāng)前的版本號,可使用順序序號或者時(shí)間戳進(jìn)行表示,程序會(huì)對第一個(gè)'_'前的字符當(dāng)成版本號;
title用于記錄當(dāng)前的變更的主題,主要是對用戶有較好的提示作用;
extension: 表示文件擴(kuò)展名;
舉例如mongodb數(shù)據(jù)庫:
0001_init_database.down.json
0001_init_database.up.json
在創(chuàng)建遷移文件時(shí),可以使用migrate create 命令進(jìn)行創(chuàng)建,如
# migrate create [-ext E] [-dir D] [-seq] [-digits N] [-format] NAME
$ migrate create -ext json -dir migrations -seq init_database
~/migrations/000001_init_database.up.json
~/migrations/000001_init_database.down.json
參數(shù)介紹:
- ext: 文件擴(kuò)展名;
- dir: 創(chuàng)建的目錄;
- seq: 是否以序號模式進(jìn)行創(chuàng)建;
- digits: 序號長度(默認(rèn)為6);
- format:時(shí)間格式。
使用該文件對mirations數(shù)據(jù)庫中的test集合,進(jìn)行初始化操作;
$ cat migrations/000001_init_database.up.json
[
{
"insert": "test",
"documents": [{"name": "aaa"}]
}
]
$migrate -verbose -source file://migrations --database mongodb://root:pwd@數(shù)據(jù)庫IP>:27017/migrations?authSource=admin up
2021/08/31 14:26:06 Start buffering 1/u init_database
2021/08/31 14:26:08 Read and execute 1/u init_database
2021/08/31 14:26:08 Finished 1/u init_database (read 1.516025172s, ran 75.143261ms)
2021/08/31 14:26:08 Finished after 1.654028624s
2021/08/31 14:26:08 Closing source and database
參數(shù)解釋:
- verbose: 打印當(dāng)前變更日志
- source: 變更文件存放目錄,最好進(jìn)入本項(xiàng)目的migrations目錄下執(zhí)行, 使用ls可查看當(dāng)前所有數(shù)據(jù)庫
- database: 為monogdb連接uri
- up: 升級 (使用 down 降級)
查看數(shù)據(jù)庫,發(fā)現(xiàn)數(shù)據(jù)已經(jīng)插入到了數(shù)據(jù)庫中:
mongos> use migrations;
switched to db migrations
mongos> show collections;
migrate_advisory_lock
schema_migrations
test
mongos> db.test.find()
{ "_id" : ObjectId("612e3f5febb6de55cdeec1de"), "name" : "aaa" }
# 多生成了兩張表,其中schema_migrations為遷移數(shù)據(jù)記錄;
mongos> db.schema_migrations.find();
{ "_id" : ObjectId("612dcb8023fbb5b85368b874"), "version" : 1, "dirty" : false }
修改version能夠控制migrate的升級版本;當(dāng)本身數(shù)據(jù)庫的版本已經(jīng)高于最新版本時(shí),可以使用force命令,修改當(dāng)前數(shù)據(jù)庫遷移的版本號;
$ migrate -verbose -source file://migrations --database mongodb://root:pwd@數(shù)據(jù)庫IP>:27017/migrations?authSource=admin force 000004
2021/08/31 14:34:52 Finished after 89.470244ms
2021/08/31 14:34:52 Closing source and database
# 查看數(shù)據(jù)庫版本已經(jīng)修改到了4版本,再次進(jìn)行up操作則會(huì)被告知no change;
mongos> db.schema_migrations.find();
{ "_id" : ObjectId("612dcd8c1e88c95afcb426fe"), "version" : 4, "dirty" : false }
后續(xù)
在項(xiàng)目中維護(hù)一個(gè)數(shù)據(jù)庫/配置文件變更歷史,將項(xiàng)目重新部署時(shí),能夠通過變更歷史,完成所有改動(dòng)的變更;無需耗費(fèi)人力去找相關(guān)開發(fā);
到此這篇關(guān)于mongodb數(shù)據(jù)庫遷移變更的文章就介紹到這了,更多相關(guān)mongodb遷移變更內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 記一次MongoDB性能問題(從MySQL遷移到MongoDB)