Skip to content
Open
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
68 changes: 68 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [Setup](#setup)
- [Adding Migrations](#adding-migrations)
- [Executing Migrations](#executing-migrations)
- [Logging](#logging)

## Prerequisites
A node project with [mysql](https://github.com/mysqljs/mysql) used for database.
Expand Down Expand Up @@ -216,6 +217,73 @@ node migration.js load-from-schema

The above command will create all the tables and make entry in the logs table. It is helpful when setting up projects for newbies or environments without running entire migration process.

## Logging

### Custom Logging

By default, logging is done with `console`. You may specify your own logger with the following methods: `debug`, `log`, `info`, `warn`, `error`, and `critical`. Each one expects a string argument as a message.

```js
var customLogger = {
debug: function (message) {
console.debug(message);
},
log: function (message) {
console.log(message);
},
info: function (message) {
console.info(message);
},
warn: function (message) {
console.warn(message);
},
error: function (message) {
console.error(message);
},
critical: function (message) {
console.error(message);
}
};

migration.init(
connection,
__dirname + '/migrations',
function() {},
[
"--logger",
customLogger
]
);
```

### Logging Threshold

The logs can be fairly verbose. You can adjust the `--log-level <threshold>` according to the logs that you want to see.

| Threshold | Logs |
| --- | --- |
| ALL | CRITICAL, ERROR, WARN, INFO, LOG, DEBUG |
| DEBUG | CRITICAL, ERROR, WARN, INFO, LOG, DEBUG |
| LOG | CRITICAL, ERROR, WARN, INFO, LOG |
| INFO | CRITICAL, ERROR, WARN, INFO |
| WARN | CRITICAL, ERROR, WARN |
| ERROR | CRITICAL, ERROR |
| CRITICAL | CRITICAL |
| NONE | *(nothing/silent)* |

```js
migration.init(
connection,
__dirname + '/migrations',
function() {},
[
"--log-level ERROR"
]
);
```

NOTE: The `--log-level` option is not evaluated before calling methods on custom loggers specified via the `--logger` option.

## Pending
>>Test cases: Will pick up when I get time.

Expand Down
4 changes: 3 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = {
'table' : 'mysql_migrations_347ertt3e',
'migrations_types' : ['up', 'down']
'migrations_types': ['up', 'down'],
logger: console,
logLevel: 'ALL'
};
12 changes: 6 additions & 6 deletions core_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ var fs = require("fs");

var fileFunctions = require('./file');
var queryFunctions = require('./query');
var colors = require('colors');
var exec = require('child_process').exec;
var table = require('./config')['table'];
var config = require('./config');
var table = config.table;

function add_migration(argv, path, cb) {
fileFunctions.validate_file_name(argv[4]);
Expand All @@ -27,7 +27,7 @@ function add_migration(argv, path, cb) {
throw err;
}

console.log("Added file " + file_name);
config.logger.info("Added file " + file_name);
cb();
});
});
Expand Down Expand Up @@ -139,7 +139,7 @@ function update_schema(conn, path, cb) {
exec(cmd, function(error, stdout, stderr) {
fs.writeFile(filePath, stdout, function(err) {
if (err) {
console.log(colors.red("Could not save schema file"));
config.logger.error("Could not save schema file");
}
cb();
});
Expand Down Expand Up @@ -172,7 +172,7 @@ function createFromSchema(conn, path, cb) {
cmd = cmd + " < " + filePath;
exec(cmd, function(error, stdout, stderr) {
if (error) {
console.log(colors.red("Could not load from Schema: " + error));
config.logger.error("Could not load from Schema: " + error);
cb();
} else {
var file_paths = [];
Expand All @@ -193,7 +193,7 @@ function createFromSchema(conn, path, cb) {
}
});
} else {
console.log(colors.red("Schema Missing: " + filePath));
config.logger.error("Schema Missing: " + filePath);
cb();
}
}
Expand Down
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var queryFunctions = require('./query');
var config = require('./config');
var table = config['table'];
var migrations_types = config['migrations_types'];
var logger = require('./logger');

var updateSchema = false;
var migrate_all = false;
Expand Down Expand Up @@ -32,6 +33,23 @@ function migration(conn, path, cb, options) {
if (options.indexOf("--update-schema") > -1) {
updateSchema = true;
}

var loggerIndex = options.findIndex(option => option === '--logger');
if (loggerIndex !== -1) {
var customLogger = options[loggerIndex + 1];
logger.checkLogger(customLogger);
config.logger = customLogger;
} else {
config.logger = logger;
}

options
.filter(option => typeof option === "string" && option.startsWith('--log-level '))
.forEach(option => {
var level = option.replace('--log-level ', '').toUpperCase();
logger.checkLevel(level);
config.logLevel = level;
});
}

queryFunctions.run_query(conn, "CREATE TABLE IF NOT EXISTS `" + table + "` (`timestamp` varchar(254) NOT NULL UNIQUE)", function (res) {
Expand Down Expand Up @@ -94,6 +112,8 @@ function handle(argv, conn, path, cb) {
else {
throw new Error('command not found : ' + argv.join(" "));
}
} else {
cb();
}
}

Expand Down
55 changes: 55 additions & 0 deletions logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var colors = require('colors');
var config = require('./config');

var NONE = 'NONE';
var CRITICAL = 'CRITICAL';
var ERROR = 'ERROR';
var WARN = 'WARN';
var LOG = 'LOG';
var INFO = 'INFO';
var DEBUG = 'DEBUG';
var ALL = 'ALL';

var levels = [NONE, CRITICAL, ERROR, WARN, INFO, LOG, DEBUG, ALL];

function canLog(level) {
var threshold = config.logLevel;
if (threshold === undefined) threshold = LOG;
var thresholdIndex = levels.indexOf(threshold);
var levelIndex = levels.indexOf(level);
return levelIndex <= thresholdIndex;
}

function log(method, level, color, message) {
if (canLog(level)) {
console[method](color(message));
}
}

function checkLevel(level) {
if (!levels.includes(level)) {
throw new Error(`Invalid log level '${level}'. Acceptable values are ${levels.join(', ')}`);
}
}

function checkLogger(customLogger) {
var keys = ['critical', 'error', 'warn', 'info', 'log', 'debug'];
keys.forEach(function (key) {
if (typeof customLogger[key] !== 'function') {
throw new Error(`Custom Logger '${key}' is not a function. Expected functions: ${keys.join(', ')}`);
}
});
}

var logger = {
checkLevel,
checkLogger,
critical: log.bind(null, 'error', CRITICAL, colors.red),
error: log.bind(null, 'error', ERROR, colors.red),
warn: log.bind(null, 'warn', WARN, colors.yellow),
info: log.bind(null, 'info', INFO, colors.blue),
log: log.bind(null, 'log', LOG, colors.white),
debug: log.bind(null, 'debug', DEBUG, colors.grey)
}

module.exports = logger;
12 changes: 6 additions & 6 deletions query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var table = require('./config')['table'];
var fileFunctions = require('./file');
var colors = require('colors');
var config = require('./config');
var table = config.table;

function run_query(conn, query, cb, run) {
if (run == null) {
Expand Down Expand Up @@ -36,7 +35,8 @@ function execute_query(conn, path, final_file_paths, type, cb, run) {
var current_file_path = path + "/" + file_name;

var queries = require(current_file_path);
console.info(colors.green("Run: " + run + " Type: " + type.toUpperCase() + ": " +queries[type]));
config.logger.info(`Run: ${run} Type: ${type.toUpperCase()}`);
config.logger.debug(queries[type]);

var timestamp_val = file_name.split("_", 1)[0];
if (typeof(queries[type]) == 'string') {
Expand All @@ -46,7 +46,7 @@ function execute_query(conn, path, final_file_paths, type, cb, run) {
});
}, run);
} else if (typeof(queries[type]) == 'function') {
console.info(`${type.toUpperCase()} Function: "${ queries[type].toString() }"`);
config.logger.info(`${type.toUpperCase()} Function: "${queries[type].toString()}"`);

queries[type](conn, function() {
updateRecords(conn, type, table, timestamp_val, function () {
Expand All @@ -56,7 +56,7 @@ function execute_query(conn, path, final_file_paths, type, cb, run) {
}

} else {
console.info(colors.blue("No more " + type.toUpperCase() + " migrations to run"));
config.logger.info(`No more ${type.toUpperCase()} migrations to run`);
cb();
}
}
Expand Down
65 changes: 65 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var chai = require('chai');

var testCommons = require('./test_commons');
var assert = require('assert');
var logger = require('../logger');
var config = require('../config');
var index = require('../index');
var mysql = require('./mysql');

var should = chai.should();
var path = __dirname + '/migrations/';

describe('index.js', function () {

beforeEach(function (done) {
config.logger = logger;
config.logLevel = 'ALL';
done();
});

describe('.init', function () {
it('assigns --logger to config.logger', function (done) {
function noop() { };

var newLogger = {
critical: noop,
error: noop,
warn: noop,
info: noop,
log: noop,
debug: noop
};
assert.notEqual(config.logger, newLogger);

index.init(
mysql,
path,
function () {
assert.equal(config.logger, newLogger);
done();
},
[
'--logger',
newLogger
]
);
});
it('assigns --log-level to config.logLevel', function (done) {
var newLevel = 'NONE';
assert.notEqual(config.logLevel, newLevel);

index.init(
mysql,
path,
function () {
assert.equal(config.logLevel, newLevel);
done();
},
[
`--log-level ${newLevel}`
]
);
});
});
});
Loading