kunklejr/node-db-migrate — pull request #160 by tuliomonteazul (2014-03-23 23:14:19+00:00)
Title: New option to auto generate sql files

Add the option `--sql-file` that can be passed with the `db-migrate` command or set by the `database.json` config file.
I don't know if someone else is interested in this feature, but in my job we prefer to set all the up and down statements on a .sql file.

So, when creating a new migration as:
`db-migrate create migration --sql-file`

These two sql files are also created:
`migrations/sqls/20140323230607-migration-up.sql`
`migrations/sqls/20140323230607-migration-down.sql`

And the file `migrations/20140323230607-migration.js`, instead of having the empty `exports.up` and `exports.down`, has the auto-generated code that load the sql files:
````
var dbm = require('db-migrate');
var type = dbm.dataType;
var fs = require('fs');
var path = require('path');

exports.up = function(db, callback) {
var filePath = path.join(__dirname + '/sqls/20140323230607-migration-up.sql');
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return console.log(err);
console.log('received data: ' + data);

db.runSql(data, function(err) {
if (err) return console.log(err);
callback();
});
});
};

exports.down = function(db, callback) {
var filePath = path.join(__dirname + '/sqls/20140323230607-migration-down.sql');
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return console.log(err);
console.log('received data: ' + data);

db.runSql(data, function(err) {
if (err) return console.log(err);
callback();
});
});
};
````

I added some unit tests for the changes I made on migrations.js and integration tests for the use of this new option.