Mongoose migrate
The reason of the issue is that you have connection "connected" each time, on every ,migrationThat is why you have to disconnect.The same situation if you replace connect with mongoose.createConnection. you will need to close it.
How to solve?
move
var mongoose = require('mongoose') , Role = require('../models/role') , User = require('../models/user');
into module like db
var mongoose = require('mongoose') , Role = require('../models/role') , User = require('../models/user');module.exports = mongoose
and just require it
var mongoose = require('./db')
So you will have:
- Single connection
- All models loaded in one place
- Clean code inmigrations
You can also try my migrate-mongoose migration framework which provides the mongoose connection right out of the box.
in your up
or down
function you can just access your models like this
this('user').findOne({ name: 'Sergey' });
It also persists your migrations to the database instead of the file system.
You also have east migration framework that is very powerful and it also has mongoDB adaptors:https://github.com/okv/east
Then you will crate migration with command:
east create my_migration_name
And then your migration scripts will look like this:
exports.migrate = function(client, done) { var db = client.db; db......... done();};exports.rollback = function(client, done) { var db = client.db; db......... done();};