From 2abc1e70a8e572beffc0e0e1fb1a73964d872699 Mon Sep 17 00:00:00 2001 From: Sameer Brenn Date: Fri, 16 Jun 2023 11:27:04 -0600 Subject: [PATCH 1/5] move exeCommand into subdir --- bin/flyway.js | 68 ++---------------------------------------------- lib/exec.js | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 66 deletions(-) create mode 100644 lib/exec.js diff --git a/bin/flyway.js b/bin/flyway.js index 20e8211..98f7e5e 100755 --- a/bin/flyway.js +++ b/bin/flyway.js @@ -3,12 +3,9 @@ 'use strict'; const program = require('commander'); -const path = require('path'); -const spawn = require('child_process').spawn; -const fs = require('fs'); -const os = require('os'); const pkg = require('../package.json'); -const download = require('../lib/download'); +const exeCommand = require('../lib/exec').exeCommand + process.title = 'flyway'; program @@ -42,64 +39,3 @@ function makeCommand(name, desc) { .description(desc) .action(exeCommand); } - -function configFlywayArgs(config) { - const flywayArgs = config.flywayArgs || {}; - const flywayArgsKeys = Object.keys(flywayArgs); - - return flywayArgsKeys.map(function(key) { - return `-${key}=${flywayArgs[key]}`; - }); -} - -function binIsFile(path) { - const stats = fs.statSync(path); - - return !!stats && stats.isFile(); -} - -function exeCommand(cmd) { - if(!program.configfile) { - throw new Error('Config file option is required'); - } - - var config = require(path.resolve(program.configfile)); - - if (typeof config === 'function') { - config = config(); - } - - download.ensureArtifacts(config, function(err, flywayBin) { - const workingDir = process.cwd(); - - if(err) { - throw new Error(err); - } - - // Ensure that the flywayBin is a file, helps with security risk of having - // shell true in the spawn call below - if (!binIsFile(flywayBin)) { - throw new Error('Flyway bin was not found at "' + flywayBin + '"'); - } - - const args = configFlywayArgs(config) - .concat([cmd._name]); - - // Fix problem with spaces on windows OS - // https://github.com/nodejs/node/issues/7367 - const isWindowsAndHasSpace = !!(flywayBin.match(/\s/) && os.platform() === 'win32'); - const safeSpawnBin = isWindowsAndHasSpace ? '"' + flywayBin + '"' : flywayBin; - - const child = spawn(safeSpawnBin, args, { - env: Object.assign({}, process.env, config.env), - cwd: workingDir, - stdio: 'inherit', - windowsVerbatimArguments: true, // Super Weird, https://github.com/nodejs/node/issues/5060 - shell: isWindowsAndHasSpace, - }); - - child.on('close', code => { - process.exit(code); - }); - }); -} diff --git a/lib/exec.js b/lib/exec.js new file mode 100644 index 0000000..5888827 --- /dev/null +++ b/lib/exec.js @@ -0,0 +1,72 @@ +const download = require('./download'); +const path = require('path'); +const spawn = require('child_process').spawn; +const fs = require('fs'); +const os = require('os'); +const program = require('commander'); + + +function configFlywayArgs(config) { + const flywayArgs = config.flywayArgs || {}; + const flywayArgsKeys = Object.keys(flywayArgs); + + return flywayArgsKeys.map(function(key) { + return `-${key}=${flywayArgs[key]}`; + }); +} + +function binIsFile(path) { + const stats = fs.statSync(path); + + return !!stats && stats.isFile(); +} + +function exeCommand(cmd) { + if(!program.configfile) { + throw new Error('Config file option is required'); + } + + var config = require(path.resolve(program.configfile)); + + if (typeof config === 'function') { + config = config(); + } + + download.ensureArtifacts(config, function(err, flywayBin) { + const workingDir = process.cwd(); + + if(err) { + throw new Error(err); + } + + // Ensure that the flywayBin is a file, helps with security risk of having + // shell true in the spawn call below + if (!binIsFile(flywayBin)) { + throw new Error('Flyway bin was not found at "' + flywayBin + '"'); + } + + const args = configFlywayArgs(config) + .concat([cmd._name]); + + // Fix problem with spaces on windows OS + // https://github.com/nodejs/node/issues/7367 + const isWindowsAndHasSpace = !!(flywayBin.match(/\s/) && os.platform() === 'win32'); + const safeSpawnBin = isWindowsAndHasSpace ? '"' + flywayBin + '"' : flywayBin; + + const child = spawn(safeSpawnBin, args, { + env: Object.assign({}, process.env, config.env), + cwd: workingDir, + stdio: 'inherit', + windowsVerbatimArguments: true, // Super Weird, https://github.com/nodejs/node/issues/5060 + shell: isWindowsAndHasSpace, + }); + + child.on('close', code => { + process.exit(code); + }); + }); +} + +module.exports = { + exeCommand: function(cmd) { exeCommand(cmd) } +} \ No newline at end of file From dc388f315a7c132df9251648d0e9e31fafbd3d18 Mon Sep 17 00:00:00 2001 From: Sameer Brenn Date: Fri, 16 Jun 2023 13:49:21 -0600 Subject: [PATCH 2/5] wip --- bin/flyway.js | 3 ++- lib/exec.js | 15 +++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bin/flyway.js b/bin/flyway.js index 98f7e5e..8dc8f80 100755 --- a/bin/flyway.js +++ b/bin/flyway.js @@ -7,6 +7,7 @@ const pkg = require('../package.json'); const exeCommand = require('../lib/exec').exeCommand + process.title = 'flyway'; program .version(pkg.version) @@ -37,5 +38,5 @@ function makeCommand(name, desc) { program .command(name) .description(desc) - .action(exeCommand); + .action(exeCommand(program.configfile)); } diff --git a/lib/exec.js b/lib/exec.js index 5888827..8542e0c 100644 --- a/lib/exec.js +++ b/lib/exec.js @@ -1,10 +1,8 @@ const download = require('./download'); -const path = require('path'); const spawn = require('child_process').spawn; const fs = require('fs'); const os = require('os'); -const program = require('commander'); - +const path = require('path'); function configFlywayArgs(config) { const flywayArgs = config.flywayArgs || {}; @@ -21,12 +19,12 @@ function binIsFile(path) { return !!stats && stats.isFile(); } -function exeCommand(cmd) { - if(!program.configfile) { +function exeCommand(configfile) { (cmd) => { + if(!configfile) { throw new Error('Config file option is required'); } - var config = require(path.resolve(program.configfile)); + var config = require(path.resolve(configfile)); if (typeof config === 'function') { config = config(); @@ -66,7 +64,8 @@ function exeCommand(cmd) { }); }); } +}; module.exports = { - exeCommand: function(cmd) { exeCommand(cmd) } -} \ No newline at end of file + exeCommand: function(config) { exeCommand(config) } +}; \ No newline at end of file From a1b110addb6b30a0c7c07970b37b0c78a42b822c Mon Sep 17 00:00:00 2001 From: Sameer Brenn Date: Fri, 16 Jun 2023 14:10:37 -0600 Subject: [PATCH 3/5] wip --- bin/flyway.js | 17 ++++++++++++++++- lib/exec.js | 14 +------------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/bin/flyway.js b/bin/flyway.js index 8dc8f80..e4bc90b 100755 --- a/bin/flyway.js +++ b/bin/flyway.js @@ -5,6 +5,7 @@ const program = require('commander'); const pkg = require('../package.json'); const exeCommand = require('../lib/exec').exeCommand +const path = require('path'); @@ -16,6 +17,7 @@ program console.log(' See Flyway\'s configuration options at https://flywaydb.org/documentation/commandline/'); }); + makeCommand('migrate', 'Migrates the schema to the latest version. Flyway will create the metadata table automatically if it doesn\'t exist.'); makeCommand('clean', 'Drops all objects (tables, views, procedures, triggers, ...) in the configured schemas. The schemas are cleaned in the order specified by the schemas property.'); makeCommand('info', 'Prints the details and status information about all the migrations.'); @@ -38,5 +40,18 @@ function makeCommand(name, desc) { program .command(name) .description(desc) - .action(exeCommand(program.configfile)); + .action(cliExec); +} + +function cliExec(cmd) { + if(!program.configfile) { + throw new Error('Config file option is required'); + } + + var config = require(path.resolve(program.configfile)); + + if (typeof config === 'function') { + config = config(); + } + return exeCommand(config, cmd); } diff --git a/lib/exec.js b/lib/exec.js index 8542e0c..46d7c61 100644 --- a/lib/exec.js +++ b/lib/exec.js @@ -2,7 +2,6 @@ const download = require('./download'); const spawn = require('child_process').spawn; const fs = require('fs'); const os = require('os'); -const path = require('path'); function configFlywayArgs(config) { const flywayArgs = config.flywayArgs || {}; @@ -19,17 +18,7 @@ function binIsFile(path) { return !!stats && stats.isFile(); } -function exeCommand(configfile) { (cmd) => { - if(!configfile) { - throw new Error('Config file option is required'); - } - - var config = require(path.resolve(configfile)); - - if (typeof config === 'function') { - config = config(); - } - +function exeCommand(config, cmd) { download.ensureArtifacts(config, function(err, flywayBin) { const workingDir = process.cwd(); @@ -63,7 +52,6 @@ function exeCommand(configfile) { (cmd) => { process.exit(code); }); }); -} }; module.exports = { From 41f8f52a486dd1d25720e5b78c9504c389a63682 Mon Sep 17 00:00:00 2001 From: Sameer Brenn Date: Fri, 16 Jun 2023 15:52:17 -0600 Subject: [PATCH 4/5] returna promise, add migrat command --- bin/flyway.js | 3 ++- lib/exec.js | 70 +++++++++++++++++++++++++++------------------------ 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/bin/flyway.js b/bin/flyway.js index e4bc90b..e38d265 100755 --- a/bin/flyway.js +++ b/bin/flyway.js @@ -44,6 +44,7 @@ function makeCommand(name, desc) { } function cliExec(cmd) { + console.log(cmd); if(!program.configfile) { throw new Error('Config file option is required'); } @@ -53,5 +54,5 @@ function cliExec(cmd) { if (typeof config === 'function') { config = config(); } - return exeCommand(config, cmd); + return exeCommand(config, cmd).then(code => process.exit(code)); } diff --git a/lib/exec.js b/lib/exec.js index 46d7c61..af671d6 100644 --- a/lib/exec.js +++ b/lib/exec.js @@ -2,6 +2,7 @@ const download = require('./download'); const spawn = require('child_process').spawn; const fs = require('fs'); const os = require('os'); +const { resolve } = require('path'); function configFlywayArgs(config) { const flywayArgs = config.flywayArgs || {}; @@ -19,41 +20,44 @@ function binIsFile(path) { } function exeCommand(config, cmd) { - download.ensureArtifacts(config, function(err, flywayBin) { - const workingDir = process.cwd(); - - if(err) { - throw new Error(err); - } - - // Ensure that the flywayBin is a file, helps with security risk of having - // shell true in the spawn call below - if (!binIsFile(flywayBin)) { - throw new Error('Flyway bin was not found at "' + flywayBin + '"'); + return new Promise((resolve, _) => { + download.ensureArtifacts(config, function(err, flywayBin) { + const workingDir = process.cwd(); + + if(err) { + throw new Error(err); + } + + // Ensure that the flywayBin is a file, helps with security risk of having + // shell true in the spawn call below + if (!binIsFile(flywayBin)) { + throw new Error('Flyway bin was not found at "' + flywayBin + '"'); + } + + const args = configFlywayArgs(config) + .concat([cmd._name]); + + // Fix problem with spaces on windows OS + // https://github.com/nodejs/node/issues/7367 + const isWindowsAndHasSpace = !!(flywayBin.match(/\s/) && os.platform() === 'win32'); + const safeSpawnBin = isWindowsAndHasSpace ? '"' + flywayBin + '"' : flywayBin; + + const child = spawn(safeSpawnBin, args, { + env: Object.assign({}, process.env, config.env), + cwd: workingDir, + stdio: 'inherit', + windowsVerbatimArguments: true, // Super Weird, https://github.com/nodejs/node/issues/5060 + shell: isWindowsAndHasSpace, + }); + + child.on('close', code => { + resolve(code) + }); } - - const args = configFlywayArgs(config) - .concat([cmd._name]); - - // Fix problem with spaces on windows OS - // https://github.com/nodejs/node/issues/7367 - const isWindowsAndHasSpace = !!(flywayBin.match(/\s/) && os.platform() === 'win32'); - const safeSpawnBin = isWindowsAndHasSpace ? '"' + flywayBin + '"' : flywayBin; - - const child = spawn(safeSpawnBin, args, { - env: Object.assign({}, process.env, config.env), - cwd: workingDir, - stdio: 'inherit', - windowsVerbatimArguments: true, // Super Weird, https://github.com/nodejs/node/issues/5060 - shell: isWindowsAndHasSpace, - }); - - child.on('close', code => { - process.exit(code); - }); - }); + )}); }; module.exports = { - exeCommand: function(config) { exeCommand(config) } + exeCommand: function(config, cmd) { return exeCommand(config, cmd) }, + migrate: function(config) { return exeCommand(config, { _name: 'migrate' } )} }; \ No newline at end of file From c66bb8f53e0fc7d1765dce2df923cc9536871fca Mon Sep 17 00:00:00 2001 From: Sameer Brenn Date: Fri, 16 Jun 2023 16:02:25 -0600 Subject: [PATCH 5/5] remove excess log --- bin/flyway.js | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/flyway.js b/bin/flyway.js index e38d265..d8b46c0 100755 --- a/bin/flyway.js +++ b/bin/flyway.js @@ -44,7 +44,6 @@ function makeCommand(name, desc) { } function cliExec(cmd) { - console.log(cmd); if(!program.configfile) { throw new Error('Config file option is required'); }