Skip to content
This repository was archived by the owner on Jan 20, 2024. It is now read-only.
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
78 changes: 15 additions & 63 deletions bin/flyway.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
'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
const path = require('path');



process.title = 'flyway';
program
Expand All @@ -18,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.');
Expand All @@ -40,66 +40,18 @@ function makeCommand(name, desc) {
program
.command(name)
.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();
.action(cliExec);
}

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();
function cliExec(cmd) {
if(!program.configfile) {
throw new Error('Config file option is required');
}

if(err) {
throw new Error(err);
}
var config = require(path.resolve(program.configfile));

// 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);
});
});
if (typeof config === 'function') {
config = config();
}
return exeCommand(config, cmd).then(code => process.exit(code));
}
63 changes: 63 additions & 0 deletions lib/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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 || {};
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(config, cmd) {
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)
});
}
)});
};

module.exports = {
exeCommand: function(config, cmd) { return exeCommand(config, cmd) },
migrate: function(config) { return exeCommand(config, { _name: 'migrate' } )}
};