migrate-mongo is a database migration tool for MongoDB running in Node.js
$ npm install -g migrate-mongo
$ migrate-mongo
Usage: migrate-mongo [options] [command]
Commands:
init initialize a new migration project
create [description] create a new database migration with the provided description
up [options] run all unapplied database migrations
down [options] undo the last applied database migration
status [options] print the changelog of the database
Options:
-h, --help output usage information
-V, --version output the version number
Make sure you have Node.js 10 (or higher) installed.
Create a directory where you want to store your migrations for your mongo database (eg. 'albums' here) and cd into it
$ mkdir albums-migrations
$ cd albums-migrations
Initialize a new migrate-mongo project
$ migrate-mongo init
Initialization successful. Please edit the generated migrate-mongo-config.js file
The above command did two things:
Edit the migrate-mongo-config.js file. An object or promise can be returned. Make sure you change the mongodb url:
// In this file you can configure migrate-mongo
module.exports = {
mongodb: {
// TODO Change (or review) the url to your MongoDB:
url: "mongodb://localhost:27017",
// TODO Change this to your database name:
databaseName: "YOURDATABASENAME",
options: {
useNewUrlParser: true // removes a deprecation warning when connecting
// connectTimeoutMS: 3600000, // increase connection timeout to 1 hour
// socketTimeoutMS: 3600000, // increase socket timeout to 1 hour
}
},
// The migrations dir, can be an relative or absolute path. Only edit this when really necessary.
migrationsDir: "migrations",
// The mongodb collection where the applied changes are stored. Only edit this when really necessary.
changelogCollectionName: "changelog",
// The file extension to create migrations and search for in migration dir
migrationFileExtension: ".js"
// Enable the algorithm to create a checksum of the file contents and use that in the comparison to determin
// if the file should be run. Requires that scripts are coded to be run multiple times.
useFileHash: false
};
Alternatively, you can also encode your database name in the url (and leave out the databaseName
property):
url: "mongodb://localhost:27017/YOURDATABASE",
To create a new database migration script, just run the migrate-mongo create [description]
command.
For example:
$ migrate-mongo create blacklist_the_beatles
Created: migrations/20160608155948-blacklist_the_beatles.js
A new migration file is created in the 'migrations' directory:
module.exports = {
up(db, client) {
// TODO write your migration here. Return a Promise (and/or use async & await).
// See https://github.com/seppevs/migrate-mongo/#creating-a-new-migration-script
// Example:
// return db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: true}});
},
down(db, client) {
// TODO write the statements to rollback your migration (if possible)
// Example:
// return db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: false}});
}
};
Edit this content so it actually performs changes to your database. Don't forget to write the down part as well.The db
object contains the official MongoDB db objectThe client
object is a MongoClient instance (which you can omit if you don't use it).
There are 3 options to implement the up
and down
functions of your migration:
Always make sure the implementation matches the function signature:
function up(db, client) { /* */ }
should return Promise
function async up(db, client) { /* */ }
should contain await
keyword(s) and return Promise
function up(db, client, next) { /* */ }
should callback next
module.exports = {
up(db) {
return db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: true}});
},
down(db) {
return db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: false}});
}
};
Async & await is especially useful if you want to perform multiple operations against your MongoDB in one migration.
module.exports = {
async up(db) {
await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: true}});
await db.collection('albums').updateOne({artist: 'The Doors'}, {$set: {stars: 5}});
},
async down(db) {
await db.collection('albums').updateOne({artist: 'The Doors'}, {$set: {stars: 0}});
await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: false}});
},
};
Callbacks are supported for backwards compatibility.New migration scripts should be written using Promises and/or async & await. It's easier to read and write.
module.exports = {
up(db, callback) {
return db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: true}}, callback);
},
down(db, callback) {
return db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: false}}, callback);
}
};
To override the content of the sample migration that will be created by the create
command,create a file sample-migration.js
in the migrations directory.
At any time, you can check which migrations are applied (or not)
$ migrate-mongo status
┌─────────────────────────────────────────┬────────────┐
│ Filename │ Applied At │
├─────────────────────────────────────────┼────────────┤
│ 20160608155948-blacklist_the_beatles.js │ PENDING │
└─────────────────────────────────────────┴────────────┘
This command will apply all pending migrations
$ migrate-mongo up
MIGRATED UP: 20160608155948-blacklist_the_beatles.js
If an an error occurred, it will stop and won't continue with the rest of the pending migrations
If we check the status again, we can see the last migration was successfully applied:
$ migrate-mongo status
┌─────────────────────────────────────────┬──────────────────────────┐
│ Filename │ Applied At │
├─────────────────────────────────────────┼──────────────────────────┤
│ 20160608155948-blacklist_the_beatles.js │ 2016-06-08T20:13:30.415Z │
└─────────────────────────────────────────┴──────────────────────────┘
With this command, migrate-mongo will revert (only) the last applied migration
$ migrate-mongo down
MIGRATED DOWN: 20160608155948-blacklist_the_beatles.js
If we check the status again, we see that the reverted migration is pending again:
$ migrate-mongo status
┌─────────────────────────────────────────┬────────────┐
│ Filename │ Applied At │
├─────────────────────────────────────────┼────────────┤
│ 20160608155948-blacklist_the_beatles.js │ PENDING │
└─────────────────────────────────────────┴────────────┘
All actions (except init
) accept an optional -f
or --file
option to specify a path to a custom config file.By default, migrate-mongo will look for a migrate-mongo-config.js
config file in of the current directory.
$ migrate-mongo status -f '~/configs/albums-migrations.js'
┌─────────────────────────────────────────┬────────────┐
│ Filename │ Applied At │
├─────────────────────────────────────────┼────────────┤
│ 20160608155948-blacklist_the_beatles.js │ PENDING │
└─────────────────────────────────────────┴────────────┘
You can use use Node.js modules (or require other modules) in your migration scripts.It's even possible to use npm modules, just provide a package.json
file in the root of your migration project:
$ cd albums-migrations
$ npm init --yes
Now you have a package.json file, and you can install your favorite npm modules that might help you in your migration scripts.For example, one of the very useful promise-fun npm modules.
You can make use of the MongoDB Transaction API in your migration scripts.
Note: this requires both:
migrate-mongo will call your migration up
and down
function with a second argument: client
.This client
argument is an MongoClient instance, it gives you access to the startSession
function.
Example:
module.exports = {
async up(db, client) {
const session = client.startSession();
try {
await session.withTransaction(async () => {
await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: true}});
await db.collection('albums').updateOne({artist: 'The Doors'}, {$set: {stars: 5}});
});
} finally {
await session.endSession();
}
},
async down(db, client) {
const session = client.startSession();
try {
await session.withTransaction(async () => {
await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: false}});
await db.collection('albums').updateOne({artist: 'The Doors'}, {$set: {stars: 0}});
});
} finally {
await session.endSession();
}
},
};
There are use cases where it may make sense to not treat scripts as immutable items. An example would be a simple collection with lookup values where you just can wipe and recreate the entire collection all at the same time.
useFileHash: true
Set this config value to will enable tracking a hash of the file contents and will run a file with the same name again as long as the file contents have changes. Setting this flag changes the behavior for every script and if this is enabled each script needs to be written in a manner where it can be re-run safefly. A script of the same name and hash will not be executed again, only if the hash changes.
Now the status will also include the file hash in the output
┌────────────────────────────────────────┬──────────────────────────────────────────────────────────────────┬──────────────────────────┐
│ Filename │ Hash │ Applied At │
├────────────────────────────────────────┼──────────────────────────────────────────────────────────────────┼──────────────────────────┤
│ 20160608155948-blacklist_the_beatles.js│ 7625a0220d552dbeb42e26fdab61d8c7ef54ac3a052254588c267e42e9fa876d │ 2021-03-04T15:40:22.732Z │
└────────────────────────────────────────┴──────────────────────────────────────────────────────────────────┴──────────────────────────┘
To know which version of migrate-mongo you're running, just pass the version
option:
$ migrate-mongo version
const {
init,
create,
database,
config,
up,
down,
status
} = require('migrate-mongo');
init() → Promise
Initialize a new migrate-mongo project
await init();
The above command did two things:
migrate-mongo-config.js
file andmigrations
directoryEdit the migrate-mongo-config.js
file. Make sure you change the mongodb url.
create(description) → Promise<fileName>
For example:
const fileName = await create('blacklist_the_beatles');
console.log('Created:', fileName);
A new migration file is created in the migrations
directory.
database.connect() → Promise<{db: MongoDb, client: MongoClient}>
Connect to a mongo database using the connection settings from the migrate-mongo-config.js
file.
const { db, client } = await database.connect();
config.read() → Promise<JSON>
Read connection settings from the migrate-mongo-config.js
file.
const mongoConnectionSettings = await config.read();
config.set(yourConfigObject)
Tell migrate-mongo NOT to use the migrate-mongo-config.js
file, but instead use the config object passed as the first argument of this function.When using this feature, please do this at the very beginning of your program.
Example:
const { config, up } = require('../lib/migrate-mongo');
const myConfig = {
mongodb: {
url: "mongodb://localhost:27017/mydatabase",
options: { useNewUrlParser: true }
},
migrationsDir: "migrations",
changelogCollectionName: "changelog",
migrationFileExtension: ".js"
};
config.set(myConfig);
// then, use the API as you normally would, eg:
await up();
up(MongoDb, MongoClient) → Promise<Array<fileName>>
Apply all pending migrations
const { db, client } = await database.connect();
const migrated = await up(db, client);
migrated.forEach(fileName => console.log('Migrated:', fileName));
If an an error occurred, the promise will reject and won't continue with the rest of the pending migrations.
down(MongoDb, MongoClient) → Promise<Array<fileName>>
Revert (only) the last applied migration
const { db, client } = await database.connect();
const migratedDown = await down(db, client);
migratedDown.forEach(fileName => console.log('Migrated Down:', fileName));
status(MongoDb) → Promise<Array<{ fileName, appliedAt }>>
Check which migrations are applied (or not.
const { db } = await database.connect();
const migrationStatus = await status(db);
migrationStatus.forEach(({ fileName, appliedAt }) => console.log(fileName, ':', appliedAt));
client.close() → Promise
Close the database connection
const { db, client } = await database.connect();
await client.close();
功能 将一个集群,复制集或者单机 mongoDB 中的一个集合的数据,迁移到另一个集群,复制集或者单机 mongoDB. 如果源集合使用了分片,会在目标自动分片,并按照源集合的 chunk 进行 split 与随机 movechunk. 如果源集合存在索引,会拷贝索引. 如果源使用了复制集,会使用 oplog 做增量迁移. 参数 --src "ip:host" 源地址 --srcDB "db na
在某些业务场景,我们或许只迁移部分表的数据,大部分都是迁移表结构和索引,这种如何实现,如下: #coding=utf-8 import subprocess import pymongo path_indexs = '/home/indexs/' path_table_structure = '/home/table_structure/' mongo_client = pymongo.Mong
1 如何在线修改chunk大小 https://docs.mongodb.com/manual/tutorial/modify-chunk-size-in-sharded-cluster/ 2 chunk size影响到了chunk的迁移 https://docs.mongodb.com/manual/reference/limits/#Maximum-Number-of-Documents-Pe
redis 优势:缓存中间件,基于内存,速度快,适用于秒杀的库存扣件等状况 数据结构:string、hash、set、list、sortedset、、HyperLogLog、Geo、Pub/Sub 备份:开启aof备份,定期备份数据转移 数据恢复:在数据安全丢失的情况下,基于rdb冷备,如何完美的恢复数据,同时还保持aof和rdb的双开: 停止redis,关闭aof,删除aof文件,拷贝rdb备份
MYSQL setting配置如下 DATABASES = { "default": { "ENGINE": "django.db.backends.mysql", "NAME": "pay", "USER": "root", "PASSWORD": "123456", "HOST": "10.23.xxx.x
MIGRATE host port key destination-db timeout 将 key 原子性地从当前实例传送到目标实例的指定数据库上,一旦传送成功, key 保证会出现在目标实例上,而当前实例上的 key 会被删除。 这个命令是一个原子操作,它在执行的时候会阻塞进行迁移的两个实例,直到以下任意结果发生:迁移成功,迁移失败,等到超时。 命令的内部实现是这样的:它在当前实例对给定 ke
migrate Database migrations written in Go. Use as CLI or import as library. DEPRECATED mattes/migrate is now golang-migrate/migrate Please open issues and pull requests in the new repository.
MIGRATE host port key destination-db timeout [COPY] [REPLACE] 将 key 原子性地从当前实例传送到目标实例的指定数据库上,一旦传送成功, key 保证会出现在目标实例上,而当前实例上的 key 会被删除。 这个命令是一个原子操作,它在执行的时候会阻塞进行迁移的两个实例,直到以下任意结果发生:迁移成功,迁移失败,等到超时。 命令的内部实现
sql-migrate 是一个 Go 语言的数据库 Schema 移植工具。 特性: 可作为 CLI 命令行工具或者开发库使用 支持 SQLite, PostgreSQL, MySQL, MSSQL 和 Oracle 数据库 (使用 gorp) 可嵌入到应用程序 使用 SQL 定义移植过程 原子迁移 可对移植过程进行撤回 在一个项目中支持多种数据库类型
Flask-Migrate Flask-Migrate is an extension that handles SQLAlchemy database migrations for Flask applications using Alembic. The database operations are provided as command-line arguments under the f
Migrate 类别能让你从你的控制器运行、穿越与恢复 迁移。 迁移支援应用程序、模组和套件。 current($name = 'default', $type = 'app') 迁移到配置里的 current 架构设定。 静态 是 参数 参数 预设 描述 $name 'default' 套件或模组的名称。在应用程序的情况下,使用 'default'。 $type 'app' 迁移的类型。有效的值