Skip to content

Commit

Permalink
Updates for v1.0.8 - ready for manual tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesEggers1 committed Jul 12, 2012
1 parent 29e3bb7 commit 6cc4625
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 76 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,13 @@
Change Log
==============

## 2012-07-11 ##
* Updated to 1.0.8
* Updated CLI to use package.json version.
* Updated Down command to work asynchronously better (added tests to verify).
* Removed excess `console.log()` entries from the status command.
* Renamed HISTORY.md to CHANGELOG.md

## 2012-07-05 ##
* Updated to 1.0.7
* Addressed issue concerning thrown exceptions during synchronous migration scripts.
6 changes: 0 additions & 6 deletions HISTORY.md

This file was deleted.

3 changes: 2 additions & 1 deletion bin/monarch
Expand Up @@ -5,13 +5,14 @@
*/

var program = require('commander')
, pkg = require("../package.json")
, _clog = require("clog")
, _clogConfig = require("../configs/clog-config");

_clogConfig.default();

program
.version('1.0.2')
.version(pkg.version)
.option('-d, --debug', 'Runs the application in debugging mode.');

program
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "monarch",
"version": "1.0.7",
"version": "1.0.8",
"author": "James Eggers <james.r.eggers@gmail.com> (http://www.jamesreggers.com/)",
"description": "A command line utility for generic migrations in Node.js.",
"homepage": "https://github.com/JamesEggers1/node-monarch",
Expand Down
149 changes: 92 additions & 57 deletions src/commands/down.js
Expand Up @@ -5,7 +5,16 @@ module.exports = (function(){
, _clog = require("clog")
, _requiredir = require("requiredir")
, _benchmarkTimer = require("../utils/benchmarkTimer")
, _tracker = require("../utils/migration_version_tracker");
, _tracker = require("../utils/migration_version_tracker")
, _migrationsDirectory
, _callback
, _startTime
, _endTime
, _newVersion
, _migration
, _currentVersion
, _script
, _migrationArray;


/**
Expand Down Expand Up @@ -82,85 +91,111 @@ module.exports = (function(){

return currentVersion;
};

var index = 0;
var _executeMigration = function(idx){
if (index >= _migrationArray.length
|| (_migrationArray[idx].name.substring(0,14) <= _migration.substring(0,14))) {
_finalizeCommand();
return;
}

_script = _migrationArray[idx];
if (_script.name.substring(0,14) <= _currentVersion
&& _script.name.substring(0,14) > _migration.substring(0,14)){
try{
_clog.log("Reverting: " + _script.name);
_script.down(_migrationScriptFinished);
return;
} catch (e) {
_clog.error("*******************************************************");
_clog.error("* " + e.name + ": " + e.message);
_clog.error("* Migrations will be halted.");
_clog.error("*******************************************************");
_finalizeCommand();
return;
}
} else {
_executeMigration(++index);
}
};

/**
* Universal callback for when migration scripts finish.
* @private
* @param {object} err An error that could have been raised during the migration.
*/
var _migrationScriptFinished = function(err){
if (typeof err !== "undefined"){
_clog.error(err);
_finalizeCommand();
}

// emit event to trigger the next migration.
_newVersion = _migrationArray[index].name;
_executeMigration(++index);
};

/**
* Final output logging of the command's events.
* @private
*/
var _finalizeCommand = function(){
if (typeof _newVersion !== "undefined"){
_tracker.setCurrentVersion(_migrationsDirectory, _newVersion);
} else {
_clog.log("No migrations were ran at this time.");
}

_clog.log("Migration Complete!");
_endTime = _benchmarkTimer.getBenchmarkTime();
_clog.log("(" + (_endTime - _startTime) + "ms)");
if (typeof _callback === "function") {_callback();}
};

/**
* Exports a function that will migrate the scripts down.
* @public
* @param {string} migration The required migration file or version to which to migrate.
*/
var _command = function(migration) {
var startTime = _benchmarkTimer.getBenchmarkTime();
var endTime;
var _command = function(migration, callback) {
_startTime = _benchmarkTimer.getBenchmarkTime();
_callback = callback;

_migration = migration;
if (typeof migration === "number"){
migration = migration.toString();
}

console.log('');
var migrationsDirectory = _path.resolve(process.cwd() + "/migrations");
_migration = migration.toString();
}

_migrationsDirectory = _path.resolve(process.cwd() + "/migrations");

if(!_verifyMigrationsDirectory(migrationsDirectory)){
console.log("");
if(!_verifyMigrationsDirectory(_migrationsDirectory)){
_clog.error("Unable to continue proceed.");
endTime = _benchmarkTimer.getBenchmarkTime();
_clog.log("(" + (endTime - startTime) + "ms)");
_endTime = _benchmarkTimer.getBenchmarkTime();
_clog.log("(" + (_endTime - _startTime) + "ms)");
_finalizeCommand("Migration Directory not found.");
return;
}

if(!_verifyMigrationExists(migrationsDirectory, migration)){
console.log("");
if(!_verifyMigrationExists(_migrationsDirectory, migration)){
_clog.error("Unable to continue proceed.");
endTime = _benchmarkTimer.getBenchmarkTime();
_clog.log("(" + (endTime - startTime) + "ms)");
_endTime = _benchmarkTimer.getBenchmarkTime();
_clog.log("(" + (_endTime - _startTime) + "ms)");
_finalizeCommand("Specified migration not found.");
return;
}

var currentVersion = _getCurrentVersion(migrationsDirectory);
_currentVersion = _getCurrentVersion(_migrationsDirectory);
var migrations = _requiredir("./migrations");
var newVersion;
var script;
var i;
var len;

var migrationArray = migrations.toArray()
_migrationArray = migrations.toArray()
.sort(_sortMigrationsDescending);

_clog.log(migrations.length + " migration scripts located.");
_clog.log("Current Version: " + currentVersion);

for (i = 0, len = migrationArray.length; i < len; i++){
script = migrationArray[i];
if (script.name.substring(0,14) > currentVersion){continue;}
if (script.name.substring(0,14) <= migration.substring(0,14)){break;}
if (script.name.substring(0,14) <= currentVersion
&& script.name.substring(0,14) > migration.substring(0,14)){
try{
_clog.log("Reverting: " + script.name);
script.down(_clog.error);
newVersion = script.name;
} catch (e) {
_clog.error("*******************************************************");
_clog.error("* " + e.name + ": " + e.message);
_clog.error("* Migrations will be halted.");
_clog.error("*******************************************************");
break;
}
}
}
_clog.log("Current Version: " + _currentVersion);

if (i === len){newVersion = 0;}

console.log("");
if (typeof newVersion !== "undefined"){
_tracker.setCurrentVersion(migrationsDirectory, newVersion);
} else {
_clog.log("No migrations were ran at this time.");
}

_clog.log("Migration Complete!");
endTime = _benchmarkTimer.getBenchmarkTime();
_clog.log("(" + (endTime - startTime) + "ms)");
console.log('');
index = 0;
_executeMigration(index);
};

return _command;
Expand Down
3 changes: 0 additions & 3 deletions src/commands/status.js
Expand Up @@ -50,11 +50,9 @@ module.exports = (function(){
var startTime = _benchmarkTimer.getBenchmarkTime();
var endTime;

console.log('');
var migrationsDirectory = _path.resolve(process.cwd() + "/migrations");

if(!_verifyMigrationsDirectory(migrationsDirectory)){
console.log("");
_clog.error("Current Version: 0");
endTime = _benchmarkTimer.getBenchmarkTime();
_clog.log("(" + (endTime - startTime) + "ms)");
Expand All @@ -66,7 +64,6 @@ module.exports = (function(){
_clog.log("Current Version: " + currentVersion);
endTime = _benchmarkTimer.getBenchmarkTime();
_clog.log("(" + (endTime - startTime) + "ms)");
console.log('');
};

return _command;
Expand Down
14 changes: 9 additions & 5 deletions src/commands/up.js
Expand Up @@ -148,11 +148,13 @@ module.exports = (function(){
* Final output logging of the command's events.
* @private
*/
var _finalizeCommand = function(){
if (typeof _newVersion !== "undefined"){
_tracker.setCurrentVersion(_migrationsDirectory, _newVersion);
} else {
_clog.log("You are currently at the most recent migration.");
var _finalizeCommand = function(err){
if (typeof err === "undefined"){
if (typeof _newVersion !== "undefined"){
_tracker.setCurrentVersion(_migrationsDirectory, _newVersion);
} else {
_clog.log("You are currently at the most recent migration.");
}
}

_clog.log("Migration Complete!");
Expand Down Expand Up @@ -180,6 +182,7 @@ module.exports = (function(){
_clog.error("Unable to continue proceed.");
_endTime = _benchmarkTimer.getBenchmarkTime();
_clog.log("(" + (_endTime - _startTime) + "ms)");
_finalizeCommand("Migration Directory not found.");
return;
}

Expand All @@ -189,6 +192,7 @@ module.exports = (function(){
_clog.error("Unable to continue proceed.");
_endTime = _benchmarkTimer.getBenchmarkTime();
_clog.log("(" + (_endTime - _startTime) + "ms)");
_finalizeCommand("Specified migration not found.");
return;
} else {
_migrationExists = true;
Expand Down
54 changes: 53 additions & 1 deletion tests/down-tests.js
Expand Up @@ -96,8 +96,60 @@ describe("Down Command", function(){
migrationCounter++;
};

_down("20120630174703.js");
_down("20120630174723.js");
migrationCounter.should.equal(0);
});
});

describe("Asynchronous Down Migration", function(){
var count = 3
, path = "./migrations";

beforeEach(function(done){
_helper.asyncMigrationSetup(path, count);
_helper.createMigrationTrackerFile(path, "20120630174722.js");
done();
});

afterEach(function(done){
_helper.deleteRelativeDirectory(path);
done();
});

it("should migrate to the initial version if 0 is passed in.", function(done){
var migrationCounter = 0;
_clog.test = function(){
migrationCounter++;
};

_down("0", function(){
migrationCounter.should.equal(3);
done();
});
});

it("should migrate to the specified version if less than current version.", function(done){
var migrationCounter = 0;
_clog.test = function(){
migrationCounter++;
};

_down("20120630174720.js", function(){
migrationCounter.should.equal(2);
done();
});
});

it("should not migrate to the specified version if newer than current version.", function(done){
var migrationCounter = 0;
_clog.test = function(){
migrationCounter++;
};

_down("20120630174723.js", function(){
migrationCounter.should.equal(0);
done();
});
});
});
});
4 changes: 2 additions & 2 deletions tests/test-helper.js
Expand Up @@ -160,7 +160,7 @@ module.exports = (function(){
}

for (var i = 0; i < count; i++){
filename = "201206301747E" + i + ".js";
filename = "2012063017471" + i + ".js";
template = _errorTemplate.replace(/\{\{filename\}\}/g, filename);
_fs.writeFileSync(path + '/' + filename, template);
}
Expand All @@ -176,7 +176,7 @@ module.exports = (function(){
}

for (var i = 0; i < count; i++){
filename = "201206301747A" + i + ".js";
filename = "2012063017472" + i + ".js";
template = _asyncTemplate.replace(/\{\{filename\}\}/g, filename);
_fs.writeFileSync(path + '/' + filename, template);
}
Expand Down

0 comments on commit 6cc4625

Please sign in to comment.