diff --git a/README.MD b/README.MD index e1582fa..4b0e088 100644 --- a/README.MD +++ b/README.MD @@ -115,6 +115,33 @@ module.exports = { } ``` +### Custom templates +You may specify a custom template file as an option. + +```js +migration.init( + connection, + __dirname + '/migrations', + function() {}, + [ + "--template migration_template.js" + ] +); +``` + +Use the `${{ args.up }}` tag to indicate where the `up` content passed on the command line should be placed. + +```js +module.exports = { + 'up' : function (conn, cb) { + conn.query ("${{ args.up }}", function (err, res) { + cb(); + }); + }, + 'down' : "" +} +``` + ## Executing Migrations There are few ways to run migrations. diff --git a/config.js b/config.js index 3dea0db..6b7edc7 100644 --- a/config.js +++ b/config.js @@ -1,4 +1,5 @@ module.exports = { - 'table' : 'mysql_migrations_347ertt3e', - 'migrations_types' : ['up', 'down'] + 'table': 'mysql_migrations_347ertt3e', + 'migrations_types': ['up', 'down'], + 'template': undefined }; diff --git a/core_functions.js b/core_functions.js index b724cdf..afd8186 100644 --- a/core_functions.js +++ b/core_functions.js @@ -4,24 +4,34 @@ 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]); fileFunctions.readFolder(path, function (files) { var file_name = Date.now() + "_" + argv[4]; var file_path = path + '/' + file_name + '.js'; + var content; + + if (config.template) { + content = fs.readFileSync(config.template, { encoding: 'utf-8' }); + let up = ''; + if (argv.length > 5) up = argv[5]; + content = content.replace(/\$\{\{ args\.up \}\}/i, up); + } else { + var sql_json = { + up: '', + down: '' + }; + + if (argv.length > 5) { + sql_json['up'] = argv[5]; + } - var sql_json = { - up : '', - down : '' - }; - - if (argv.length > 5) { - sql_json['up'] = argv[5]; + content = 'module.exports = ' + JSON.stringify(sql_json, null, 4); } - var content = 'module.exports = ' + JSON.stringify(sql_json, null, 4); fs.writeFile(file_path, content, 'utf-8', function (err) { if (err) { throw err; diff --git a/index.js b/index.js index 71e1195..90ac157 100644 --- a/index.js +++ b/index.js @@ -32,6 +32,11 @@ function migration(conn, path, cb, options) { if (options.indexOf("--update-schema") > -1) { updateSchema = true; } + + options.filter(option => option.startsWith('--template ')).forEach(option => { + config.template = option.split(' ', 2)[1]; + fs.accessSync(config.template, fs.constants.F_OK); + }); } queryFunctions.run_query(conn, "CREATE TABLE IF NOT EXISTS `" + table + "` (`timestamp` varchar(254) NOT NULL UNIQUE)", function (res) { @@ -94,6 +99,8 @@ function handle(argv, conn, path, cb) { else { throw new Error('command not found : ' + argv.join(" ")); } + } else { + cb(); } } diff --git a/test/core_functions.js b/test/core_functions.js index 7502daf..2432a17 100644 --- a/test/core_functions.js +++ b/test/core_functions.js @@ -5,15 +5,42 @@ var coreFunctions = require('../core_functions'); var testCommons = require('./test_commons'); var mysql = require('./mysql'); var assert = require('assert'); +var config = require('../config'); var should = chai.should(); describe('core_functions.js', function() { - before(function (done) { + beforeEach(function (done) { testCommons(done); }); context('add_migration', function () { + it('uses custom template with up SQL', function (done) { + config.template = __dirname + '/migrations/test-template.js'; + fs.writeFileSync(config.template, 'Test: ${{ args.up }}', { encoding: 'utf-8' }); + var sql = `SELECT 'MySqlHere'`; + var commands = ['node', 'migration', 'add', 'migration', 'test_custom_template_with_up', sql]; + var path = __dirname + '/migrations'; + coreFunctions.add_migration(commands, path, function () { + var fileName = fs.readdirSync(path)[0]; + var contents = fs.readFileSync(path + '/' + fileName, { encoding: 'utf-8' }); + assert.equal(contents, `Test: ${sql}`, "SQL tag replaced with SQL"); + done(); + }); + }); + it('uses custom template without up SQL', function (done) { + config.template = __dirname + '/migrations/test-template.js'; + fs.writeFileSync(config.template, 'Test: ${{ args.up }}', { encoding: 'utf-8' }); + var commands = ['node', 'migration', 'add', 'migration', 'test_custom_template_no_sql']; + var path = __dirname + '/migrations'; + coreFunctions.add_migration(commands, path, function () { + var fileName = fs.readdirSync(path)[0]; + var contents = fs.readFileSync(path + '/' + fileName, { encoding: 'utf-8' }); + assert.equal(contents, `Test: `, "SQL tag replaced with blank"); + done(); + }); + }); + it('should add migration', function (done) { var commands = ['node', 'migration', 'add', 'migration', 'create_user2']; var path = __dirname + '/migrations'; diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..8d1f7d8 --- /dev/null +++ b/test/index.js @@ -0,0 +1,37 @@ +var migrations = require('../index'); +var testCommons = require('./test_commons'); +var mysql = require('./mysql'); +var assert = require('assert'); +var fs = require('fs'); +var config = require('../config'); + +var path = __dirname + '/migrations'; + +describe('index.js', function () { + before(function (done) { + testCommons(done); + }); + + context('init', function () { + it('reads template option', function (done) { + var name = __dirname + '/migrations/template_17c2387b-38af-4ef6-bf6f-bb72f68255ff.js'; + fs.writeFileSync(name, 'this is a template', { encoding: 'utf-8' }); + mysql.getConnection(function (err, connection) { + if (err) throw err; + migrations.init( + mysql, + path, + function () { + assert.equal(name, config.template); + done(); + }, + [ + `--template ${name}` + ] + ) + }); + + }) + }); + +}); \ No newline at end of file diff --git a/test/test_commons.js b/test/test_commons.js index 6be62c4..3d152e4 100644 --- a/test/test_commons.js +++ b/test/test_commons.js @@ -1,5 +1,6 @@ var mysql = require('./mysql'); var fs = require('fs'); +var config = require('../config'); function deleteFolderRecursive(path) { if (fs.existsSync(path)) { @@ -15,7 +16,8 @@ function deleteFolderRecursive(path) { } module.exports = function(cb) { - mysql.getConnection(function(err, connection) { + config.template = undefined; + mysql.getConnection(function (err, connection) { if (err) { throw err; }