Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Add a subtask for creating an empty migration #16

Merged
merged 3 commits into from
Sep 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions data/migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = {
up: function(migration, DataTypes, done) {
// add altering commands here, calling 'done' when finished
done();
},
down: function(migration, DataTypes, done) {
// add reverting commands here, calling 'done' when finished
done();
}
};
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@
"coveralls": "rm -rf coverage && ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- -R spec && ./node_modules/.bin/istanbul report --root ./coverage --dir ./coverage lcovonly && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf ./coverage"
},
"devDependencies": {
"grunt-contrib-jshint": "~0.6.0",
"coveralls": "~2.3.0",
"grunt": "~0.4.1",
"grunt-contrib-clean": "~0.4.0",
"grunt-contrib-jshint": "~0.6.0",
"grunt-mocha-test": "~0.7.0",
"grunt": "~0.4.1",
"pg": "~2.8.2",
"istanbul": "~0.1.45",
"lodash": "^2.4.1",
"mocha": "~1.14.0",
"sqlite3": "~2.1.19",
"coveralls": "~2.3.0",
"random-string": "~0.1.1"
"pg": "~2.8.2",
"random-string": "~0.1.1",
"sqlite3": "~2.1.19"
},
"peerDependencies": {
"grunt": "~0.4.1",
Expand Down
24 changes: 23 additions & 1 deletion tasks/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

var Sequelize = require('sequelize');
var _ = Sequelize.Utils._;
var fs = require('fs');

module.exports = function(grunt) {

Expand All @@ -25,7 +26,7 @@ module.exports = function(grunt) {
migrationsPath: __dirname + '/../../../migrations',
logging: false
});

var sequelize = new Sequelize(options.database, options.username, options.password, options);
var migratorOptions = { path: options.migrationsPath };
var migrator = sequelize.getMigrator(migratorOptions);
Expand Down Expand Up @@ -93,6 +94,27 @@ module.exports = function(grunt) {
done();
});

} else if (cmd === 'migration') {
done = this.async();

if ( ! grunt.option('name')) {
grunt.log.error('No migration name specified!');
done(false);
} else {
var pad = function (n) {
return n < 10 ? '0' + n : n;
};

var date = new Date();
var migrationTimestamp = date.getFullYear().toString() + (pad(date.getMonth()+1)).toString() + pad(date.getDate()).toString() + pad(date.getHours()).toString() + pad(date.getMinutes()).toString() + pad(date.getSeconds()).toString();
var migrationFileName = migrationTimestamp + '-' + grunt.option('name') + '.js';

var skeleton = fs.readFileSync(__dirname + '/../data/migration.js').toString();

fs.writeFileSync(options.migrationsPath + '/' + migrationFileName, skeleton);

grunt.log.writeln('Migration created: ' + migrationFileName);
}
} else {
throw new Error('Unknown grunt-sequelize command: ' + cmd);
}
Expand Down
38 changes: 38 additions & 0 deletions test/sequelize_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var assert = require('assert');
var childProcessExec = require('child_process').exec;
var randomString = require('random-string');
var Sequelize = require('sequelize');
var fs = require('fs');
var _ = require('lodash');

var options = {
dialect: 'sqlite',
Expand Down Expand Up @@ -130,6 +132,42 @@ describe('grunt-sequelize', function() {
});
});

describe('sequelize:migration', function() {
describe('without any arguments', function() {
it('should give a warning', function(done) {
exec('grunt sequelize:migration', {cwd: __dirname + '/../'}, function(error) {
assert.notEqual(error, null);
done();
});
});
});

describe('with a name argument', function() {
var filesBefore = fs.readdirSync(__dirname + '/migrations');

it('should create an empty migration with a proper filename', function(done){
exec('grunt sequelize:migration --name testMigration', {cwd: __dirname + '/../'}, function(error) {
assert.equal(error, null, 'There was an error running the command.');

var filesAfter = fs.readdirSync(__dirname + '/migrations');
var newMigrationFilename = _.first(_.xor(filesBefore, filesAfter));
var newMigrationContents = fs.readFileSync(__dirname + '/migrations/' + newMigrationFilename, {encoding: 'utf-8'});
var skeleton = fs.readFileSync(__dirname + '/../data/migration.js', {encoding: 'utf-8'});

assert.equal(newMigrationContents, skeleton, 'The new migration does not contain an empty skeleton migration.');

var pattern = /^[0-9]{14}-testMigration.js$/;
var match = newMigrationFilename.match(pattern);
assert.notEqual(match, null, 'The migration filename does not match the format.');

fs.unlinkSync(__dirname + '/migrations/' + newMigrationFilename);

done();
});
});
});
});


describe('sequelize:undo', function() {
describe('without any arguments', function() {
Expand Down