Skip to content

Commit

Permalink
add ability to change the migrations dir
Browse files Browse the repository at this point in the history
Fixes: #5
  • Loading branch information
seppevs committed Mar 16, 2017
1 parent 37a2cc1 commit f09b37b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 17 deletions.
6 changes: 4 additions & 2 deletions bin/migrate-mongo.js
Expand Up @@ -6,6 +6,7 @@ const Table = require('cli-table');
const migrateMongo = require('../lib/migrate-mongo');
const database = require('../lib/env/database');
const pkgjson = require('../package.json');
const config = require('../lib/env/configFile');

program.version(pkgjson.version);

Expand All @@ -23,10 +24,11 @@ program
.command('create [description]')
.description('create a new database migration with the provided description')
.option('-f --file <file>', 'use a custom config file')
.action((description) =>{
.action((description, options) => {
global.options = options;
migrateMongo.create(description, (err, filename) => {
if (err) return handleError(err);
console.log('Created: migrations/' + filename);
console.log(`Created: ${config.read().migrationsDir}/${filename}`);
});
});

Expand Down
2 changes: 1 addition & 1 deletion lib/actions/create.js
Expand Up @@ -15,7 +15,7 @@ module.exports = function (description, done) {
description = description.split(' ').join('_');
const source = path.join(__dirname, '../../samples/migration.js');
const filename = moment.utc().format('YYYYMMDDHHmmss') + '-' + description + '.js';
const destination = path.join(process.cwd(), 'migrations', filename);
const destination = path.join(migrationsDir.resolve(), filename);
return fs.copy(source, destination, (err) => {
if (err) return taskDone(err);
return taskDone(null, filename);
Expand Down
32 changes: 27 additions & 5 deletions lib/env/migrationsDir.js
Expand Up @@ -2,34 +2,56 @@

const fs = require('fs-extra');
const path = require('path');
const _ = require('lodash');
const configFile = require('./configFile');

const DEFAULT_MIGRATIONS_DIR_NAME = 'migrations';

function resolveMigrationsDirPath() {
let migrationsDir;
try {
migrationsDir = configFile.read().migrationsDir;
} catch(err) {
// config file could not be read, assume default 'migrations' dir
migrationsDir = DEFAULT_MIGRATIONS_DIR_NAME;
}

if (path.isAbsolute(migrationsDir)) {
return migrationsDir;
} else {
return path.join(process.cwd(), migrationsDir);
}
}

module.exports = {

resolve: resolveMigrationsDirPath,

shouldExist(done) {
const migrationsDir = path.join(process.cwd(), 'migrations');
const migrationsDir = resolveMigrationsDirPath();
return fs.stat(migrationsDir, (err) => {
if (err) return done(new Error('migrations directory does not exist: ' + migrationsDir));
return done();
});
},

shouldNotExist(done) {
const migrationsDir = path.join(process.cwd(), 'migrations');
const migrationsDir = resolveMigrationsDirPath();
return fs.stat(migrationsDir, (err) => {
if (err && err.code === 'ENOENT') return done();
return done(new Error('migrations directory already exists: ' + migrationsDir));
});
},

getFileNames(done) {
const migrationsDir = path.join(process.cwd(), 'migrations');
const migrationsDir = resolveMigrationsDirPath();
fs.readdir(migrationsDir, (err, files) => {
if (err) return done(err);
return done(null, files.filter((file) => path.extname(file) === '.js'));
});
},

loadMigration(fileName) {
return require(path.join(process.cwd(), 'migrations', fileName));
return require(path.join(resolveMigrationsDirPath(), fileName));
}
};
};
19 changes: 12 additions & 7 deletions samples/config.js
@@ -1,21 +1,26 @@
'use strict';

// This is where you can configure migrate-mongo
module.exports = {
// In this file you can configure migrate-mongo

// The mongodb collection where the applied changes are stored:
changelogCollectionName: 'changelog',
module.exports = {

mongodb: {
// TODO edit this connection url to your MongoDB database:
// TODO You MUST edit this connection url to your MongoDB database:
url: 'mongodb://localhost:27017/YOURDATABASENAME',

// uncomment and edit to specify Mongo client connect options
// uncomment and edit to specify Mongo client connect options (eg. increase the timeouts)
// see https://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html
//
// options: {
// connectTimeoutMS: 3600000, // 1 hour
// socketTimeoutMS: 3600000, // 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',

};
39 changes: 37 additions & 2 deletions test/env/migrationsDir.test.js
Expand Up @@ -9,11 +9,38 @@ const path = require('path');
describe('migrationsDir', function () {

let migrationsDir;
let fs;
let fs, configFile;

beforeEach(function () {
fs = mockFs();
migrationsDir = proxyquire('../../lib/env/migrationsDir', {'fs-extra': fs});
configFile = mockConfigFile();
migrationsDir = proxyquire('../../lib/env/migrationsDir', {
'fs-extra': fs,
'./configFile': configFile,
});
});

describe('resolve()', function () {

it('should use the configured relative migrations dir when a config file is available', function () {
configFile.read.returns({
migrationsDir: 'custom-migrations-dir'
});
expect(migrationsDir.resolve()).to.equal(path.join(process.cwd(), 'custom-migrations-dir'));
});

it('should use the configured absolute migrations dir when a config file is available', function () {
configFile.read.returns({
migrationsDir: '/absolute/path/to/my/custom-migrations-dir'
});
expect(migrationsDir.resolve()).to.equal('/absolute/path/to/my/custom-migrations-dir');
});

it('should use the default migrations directory when unable to read the config file', function () {
configFile.read.throws(new Error('Cannot read config file'));
expect(migrationsDir.resolve()).to.equal(path.join(process.cwd(), 'migrations'));
});

});

describe('shouldExist()', function () {
Expand Down Expand Up @@ -101,4 +128,12 @@ describe('migrationsDir', function () {
};
}

function mockConfigFile() {
return {
read: sinon.stub().returns({
migrationsDir: 'migrations',
}),
};
}

});

0 comments on commit f09b37b

Please sign in to comment.