From e7a2b5f4ff7d709b4d9523ffbffed87cdd61efd8 Mon Sep 17 00:00:00 2001 From: Lewis Moten Date: Mon, 3 Jun 2024 04:31:17 -0400 Subject: [PATCH 1/9] add template setting to config --- config.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 }; From 6d7350141bf51e8aa544b0c950d1180f97b827f8 Mon Sep 17 00:00:00 2001 From: Lewis Moten Date: Mon, 3 Jun 2024 04:31:58 -0400 Subject: [PATCH 2/9] add template flag and check file exists --- index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/index.js b/index.js index 71e1195..e97eeb0 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) { From 7750723c20a77610f0ee4daae93922b3fe664ccb Mon Sep 17 00:00:00 2001 From: Lewis Moten Date: Mon, 3 Jun 2024 04:32:28 -0400 Subject: [PATCH 3/9] don't hang if no arguments passed --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index e97eeb0..90ac157 100644 --- a/index.js +++ b/index.js @@ -99,6 +99,8 @@ function handle(argv, conn, path, cb) { else { throw new Error('command not found : ' + argv.join(" ")); } + } else { + cb(); } } From c7630f1269d14aae3180a175f669daeace989357 Mon Sep 17 00:00:00 2001 From: Lewis Moten Date: Mon, 3 Jun 2024 04:33:27 -0400 Subject: [PATCH 4/9] Use template if provided --- core_functions.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) 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; From cd76368ac1da163c54e3df864b101102815c00ac Mon Sep 17 00:00:00 2001 From: Lewis Moten Date: Mon, 3 Jun 2024 04:34:31 -0400 Subject: [PATCH 5/9] Document --template flag --- README.MD | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) 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. From 4897b77305793051ed422c87296cd320139d6879 Mon Sep 17 00:00:00 2001 From: Lewis Moten Date: Mon, 3 Jun 2024 04:36:00 -0400 Subject: [PATCH 6/9] test custom template is used --- test/core_functions.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/test/core_functions.js b/test/core_functions.js index 7502daf..422cd27 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 = './test-template.js'; + fs.writeFileSync('./test-template.js', '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 = './test-template.js'; + fs.writeFileSync('./test-template.js', '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'; From e90b8b0028664ade867831a76df17929fd7cc6a7 Mon Sep 17 00:00:00 2001 From: Lewis Moten Date: Mon, 3 Jun 2024 04:36:33 -0400 Subject: [PATCH 7/9] test custom template is verified and stored in config --- test/index.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/index.js 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 From 8991a39a8af771b4fbdf4e1783738bd1a2cf31d1 Mon Sep 17 00:00:00 2001 From: Lewis Moten Date: Mon, 3 Jun 2024 04:37:29 -0400 Subject: [PATCH 8/9] reset config.template with each test --- test/test_commons.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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; } From 1cd0780b11486bc503ab13bfd0aff719de3e4648 Mon Sep 17 00:00:00 2001 From: Lewis Moten Date: Mon, 3 Jun 2024 04:40:36 -0400 Subject: [PATCH 9/9] put template in test migrations folder --- test/core_functions.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/core_functions.js b/test/core_functions.js index 422cd27..2432a17 100644 --- a/test/core_functions.js +++ b/test/core_functions.js @@ -16,8 +16,8 @@ describe('core_functions.js', function() { context('add_migration', function () { it('uses custom template with up SQL', function (done) { - config.template = './test-template.js'; - fs.writeFileSync('./test-template.js', 'Test: ${{ args.up }}', { encoding: 'utf-8' }); + 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'; @@ -29,8 +29,8 @@ describe('core_functions.js', function() { }); }); it('uses custom template without up SQL', function (done) { - config.template = './test-template.js'; - fs.writeFileSync('./test-template.js', 'Test: ${{ args.up }}', { encoding: 'utf-8' }); + 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 () {