2015-05-09 13:58:18 -04:00
|
|
|
#!/usr/bin/env node
|
2016-09-13 09:31:58 -04:00
|
|
|
/* eslint no-console:0, no-var:0 */
|
2018-11-16 13:23:22 +01:00
|
|
|
const Liftoff = require('liftoff');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const interpret = require('interpret');
|
|
|
|
const path = require('path');
|
|
|
|
const tildify = require('tildify');
|
|
|
|
const commander = require('commander');
|
2018-12-31 01:25:48 +09:00
|
|
|
const color = require('colorette');
|
|
|
|
const argv = require('getopts')(process.argv.slice(2));
|
2018-11-16 13:23:22 +01:00
|
|
|
const fs = Promise.promisifyAll(require('fs'));
|
|
|
|
const cliPkg = require('../package');
|
2018-12-05 08:30:55 +01:00
|
|
|
const {
|
|
|
|
mkConfigObj,
|
|
|
|
tryLoadingDefaultConfiguration,
|
|
|
|
} = require('./utils/cli-config-utils');
|
|
|
|
const { DEFAULT_EXT } = require('./utils/constants');
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
function exit(text) {
|
|
|
|
if (text instanceof Error) {
|
2019-04-28 20:41:49 +02:00
|
|
|
console.error(
|
|
|
|
color.red(`${text.detail ? `${text.detail}\n` : ''}${text.stack}`)
|
|
|
|
);
|
2015-05-09 13:58:18 -04:00
|
|
|
} else {
|
2018-12-31 01:25:48 +09:00
|
|
|
console.error(color.red(text));
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function success(text) {
|
|
|
|
console.log(text);
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkLocalModule(env) {
|
|
|
|
if (!env.modulePath) {
|
2018-07-09 08:10:34 -04:00
|
|
|
console.log(
|
2018-12-31 01:25:48 +09:00
|
|
|
color.red('No local knex install found in:'),
|
|
|
|
color.magenta(tildify(env.cwd))
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
2018-12-05 08:30:55 +01:00
|
|
|
exit('Try running: npm install knex');
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 06:03:18 -03:00
|
|
|
function initKnex(env, opts) {
|
2015-05-09 13:58:18 -04:00
|
|
|
checkLocalModule(env);
|
|
|
|
if (process.cwd() !== env.cwd) {
|
|
|
|
process.chdir(env.cwd);
|
2019-01-31 06:23:05 +01:00
|
|
|
console.log(
|
|
|
|
'Working directory changed to',
|
|
|
|
color.magenta(tildify(env.cwd))
|
|
|
|
);
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
|
2018-12-03 23:14:34 +01:00
|
|
|
if (!opts.knexfile) {
|
2018-12-05 08:30:55 +01:00
|
|
|
const configuration = tryLoadingDefaultConfiguration();
|
|
|
|
env.configuration = configuration || mkConfigObj(opts);
|
2018-12-03 23:14:34 +01:00
|
|
|
}
|
|
|
|
// If knexfile is specified
|
|
|
|
else {
|
|
|
|
const resolvedKnexfilePath = path.resolve(opts.knexfile);
|
2018-12-17 06:51:35 -06:00
|
|
|
const knexfileDir = path.dirname(resolvedKnexfilePath);
|
|
|
|
process.chdir(knexfileDir);
|
2018-12-03 23:14:34 +01:00
|
|
|
env.configuration = require(resolvedKnexfilePath);
|
|
|
|
|
|
|
|
if (!env.configuration) {
|
|
|
|
exit(
|
2018-12-05 08:30:55 +01:00
|
|
|
'Knexfile not found. Specify a path with --knexfile or pass --client and --connection params in commandline'
|
2018-12-03 23:14:34 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 13:23:22 +01:00
|
|
|
let environment = opts.env || process.env.NODE_ENV;
|
|
|
|
const defaultEnv = 'development';
|
|
|
|
|
|
|
|
let config = env.configuration;
|
2015-07-28 02:06:24 -05:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
if (!environment && typeof config[defaultEnv] === 'object') {
|
|
|
|
environment = defaultEnv;
|
|
|
|
}
|
2015-12-17 10:57:11 -06:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
if (environment) {
|
2018-12-31 01:25:48 +09:00
|
|
|
console.log('Using environment:', color.magenta(environment));
|
2015-12-17 10:57:11 -06:00
|
|
|
config = config[environment] || config;
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!config) {
|
2018-12-31 01:25:48 +09:00
|
|
|
console.log(color.red('Warning: unable to read knexfile config'));
|
2015-05-09 13:58:18 -04:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2018-11-22 10:19:06 +01:00
|
|
|
if (argv.debug !== undefined) {
|
|
|
|
config.debug = argv.debug;
|
|
|
|
}
|
|
|
|
|
2018-11-16 13:23:22 +01:00
|
|
|
const knex = require(env.modulePath);
|
2015-05-09 13:58:18 -04:00
|
|
|
return knex(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
function invoke(env) {
|
2018-12-03 23:14:34 +01:00
|
|
|
env.modulePath = env.modulePath || env.knexpath || process.env.KNEX_PATH;
|
|
|
|
|
2018-11-16 13:23:22 +01:00
|
|
|
const filetypes = ['js', 'coffee', 'ts', 'eg', 'ls'];
|
|
|
|
let pending = null;
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
commander
|
|
|
|
.version(
|
2018-12-31 01:25:48 +09:00
|
|
|
color.blue('Knex CLI version: ', color.green(cliPkg.version)) +
|
2018-07-09 08:10:34 -04:00
|
|
|
'\n' +
|
2019-01-31 06:23:05 +01:00
|
|
|
color.blue(
|
|
|
|
'Local Knex version: ',
|
|
|
|
color.green(env.modulePackage.version)
|
|
|
|
) +
|
2018-07-09 08:10:34 -04:00
|
|
|
'\n'
|
2015-05-09 13:58:18 -04:00
|
|
|
)
|
|
|
|
.option('--debug', 'Run with debugging.')
|
|
|
|
.option('--knexfile [path]', 'Specify the knexfile path.')
|
2018-12-03 23:14:34 +01:00
|
|
|
.option('--knexpath [path]', 'Specify the path to knex instance.')
|
2015-05-09 13:58:18 -04:00
|
|
|
.option('--cwd [path]', 'Specify the working directory.')
|
2018-11-06 06:03:18 -03:00
|
|
|
.option('--client [name]', 'Set DB client without a knexfile.')
|
|
|
|
.option('--connection [address]', 'Set DB connection without a knexfile.')
|
|
|
|
.option(
|
|
|
|
'--migrations-directory [path]',
|
|
|
|
'Set migrations directory without a knexfile.'
|
|
|
|
)
|
2019-05-21 19:41:50 -05:00
|
|
|
.option(
|
|
|
|
'--migrations-table-name [path]',
|
|
|
|
'Set migrations table name without a knexfile.'
|
|
|
|
)
|
2018-07-09 08:10:34 -04:00
|
|
|
.option(
|
|
|
|
'--env [name]',
|
|
|
|
'environment, default: process.env.NODE_ENV || development'
|
|
|
|
);
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
commander
|
|
|
|
.command('init')
|
|
|
|
.description(' Create a fresh knexfile.')
|
2018-07-09 08:10:34 -04:00
|
|
|
.option(
|
|
|
|
`-x [${filetypes.join('|')}]`,
|
|
|
|
'Specify the knexfile extension (default js)'
|
|
|
|
)
|
2018-11-16 13:23:22 +01:00
|
|
|
.action(() => {
|
|
|
|
const type = (argv.x || 'js').toLowerCase();
|
2015-05-09 13:58:18 -04:00
|
|
|
if (filetypes.indexOf(type) === -1) {
|
2016-05-17 01:01:34 +10:00
|
|
|
exit(`Invalid filetype specified: ${type}`);
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
2018-11-06 06:03:18 -03:00
|
|
|
if (env.configuration) {
|
2018-12-03 23:14:34 +01:00
|
|
|
exit(`Error: ${env.knexfile} already exists`);
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
checkLocalModule(env);
|
2018-11-16 13:23:22 +01:00
|
|
|
const stubPath = `./knexfile.${type}`;
|
2018-07-09 08:10:34 -04:00
|
|
|
pending = fs
|
|
|
|
.readFileAsync(
|
|
|
|
path.dirname(env.modulePath) +
|
2019-06-04 00:37:17 +02:00
|
|
|
'/src/migrate/stub/knexfile-' +
|
2018-07-09 08:10:34 -04:00
|
|
|
type +
|
|
|
|
'.stub'
|
|
|
|
)
|
2018-11-16 13:23:22 +01:00
|
|
|
.then((code) => {
|
2018-07-09 08:10:34 -04:00
|
|
|
return fs.writeFileAsync(stubPath, code);
|
|
|
|
})
|
2018-11-16 13:23:22 +01:00
|
|
|
.then(() => {
|
2018-12-31 01:25:48 +09:00
|
|
|
success(color.green(`Created ${stubPath}`));
|
2018-07-09 08:10:34 -04:00
|
|
|
})
|
|
|
|
.catch(exit);
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
commander
|
|
|
|
.command('migrate:make <name>')
|
2017-01-11 18:47:24 +05:45
|
|
|
.description(' Create a named migration file.')
|
2018-07-09 08:10:34 -04:00
|
|
|
.option(
|
|
|
|
`-x [${filetypes.join('|')}]`,
|
|
|
|
'Specify the stub extension (default js)'
|
|
|
|
)
|
2018-11-16 13:23:22 +01:00
|
|
|
.action((name) => {
|
|
|
|
const opts = commander.opts();
|
|
|
|
opts.client = opts.client || 'sqlite3'; // We don't really care about client when creating migrations
|
|
|
|
const instance = initKnex(env, opts);
|
2018-12-03 23:14:34 +01:00
|
|
|
const ext = (
|
|
|
|
argv.x ||
|
|
|
|
env.configuration.ext ||
|
|
|
|
DEFAULT_EXT
|
|
|
|
).toLowerCase();
|
2018-07-09 08:10:34 -04:00
|
|
|
pending = instance.migrate
|
|
|
|
.make(name, { extension: ext })
|
2018-11-16 13:23:22 +01:00
|
|
|
.then((name) => {
|
2018-12-31 01:25:48 +09:00
|
|
|
success(color.green(`Created Migration: ${name}`));
|
2018-07-09 08:10:34 -04:00
|
|
|
})
|
|
|
|
.catch(exit);
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
commander
|
|
|
|
.command('migrate:latest')
|
|
|
|
.description(' Run all migrations that have not yet been run.')
|
2018-12-30 18:11:57 +02:00
|
|
|
.option('--verbose', 'verbose')
|
2018-11-16 13:23:22 +01:00
|
|
|
.action(() => {
|
2018-11-06 06:03:18 -03:00
|
|
|
pending = initKnex(env, commander.opts())
|
2018-07-09 08:10:34 -04:00
|
|
|
.migrate.latest()
|
2018-11-16 13:23:22 +01:00
|
|
|
.spread((batchNo, log) => {
|
2018-07-09 08:10:34 -04:00
|
|
|
if (log.length === 0) {
|
2018-12-31 01:25:48 +09:00
|
|
|
success(color.cyan('Already up to date'));
|
2018-07-09 08:10:34 -04:00
|
|
|
}
|
|
|
|
success(
|
2018-12-31 01:25:48 +09:00
|
|
|
color.green(`Batch ${batchNo} run: ${log.length} migrations`) +
|
|
|
|
(argv.verbose ? `\n${color.cyan(log.join('\n'))}` : '')
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch(exit);
|
2019-05-19 07:14:52 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
commander
|
|
|
|
.command('migrate:up')
|
|
|
|
.description(' Run the next migration that has not yet been run.')
|
|
|
|
.action(() => {
|
|
|
|
pending = initKnex(env, commander.opts())
|
|
|
|
.migrate.up()
|
|
|
|
.spread((batchNo, log) => {
|
|
|
|
if (log.length === 0) {
|
|
|
|
success(color.cyan('Already up to date'));
|
|
|
|
}
|
|
|
|
|
|
|
|
success(
|
|
|
|
color.green(
|
|
|
|
`Batch ${batchNo} ran the following migrations:\n${log.join(
|
|
|
|
'\n'
|
|
|
|
)}`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch(exit);
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
commander
|
|
|
|
.command('migrate:rollback')
|
2019-05-11 19:35:51 -04:00
|
|
|
.description(' Rollback the last batch of migrations performed.')
|
|
|
|
.option('--all', 'rollback all completed migrations')
|
2018-12-30 18:11:57 +02:00
|
|
|
.option('--verbose', 'verbose')
|
2019-05-11 19:35:51 -04:00
|
|
|
.action((cmd) => {
|
|
|
|
const { all } = cmd;
|
|
|
|
|
2018-11-06 06:03:18 -03:00
|
|
|
pending = initKnex(env, commander.opts())
|
2019-05-11 19:35:51 -04:00
|
|
|
.migrate.rollback(null, all)
|
2018-11-16 13:23:22 +01:00
|
|
|
.spread((batchNo, log) => {
|
2018-07-09 08:10:34 -04:00
|
|
|
if (log.length === 0) {
|
2018-12-31 01:25:48 +09:00
|
|
|
success(color.cyan('Already at the base migration'));
|
2018-07-09 08:10:34 -04:00
|
|
|
}
|
|
|
|
success(
|
2018-12-31 01:25:48 +09:00
|
|
|
color.green(
|
2018-12-30 18:11:57 +02:00
|
|
|
`Batch ${batchNo} rolled back: ${log.length} migrations`
|
2018-12-31 01:25:48 +09:00
|
|
|
) + (argv.verbose ? `\n${color.cyan(log.join('\n'))}` : '')
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch(exit);
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
|
|
|
|
2019-05-29 18:37:18 -04:00
|
|
|
commander
|
|
|
|
.command('migrate:down')
|
|
|
|
.description(' Undo the last migration performed.')
|
|
|
|
.action(() => {
|
|
|
|
pending = initKnex(env, commander.opts())
|
|
|
|
.migrate.down()
|
|
|
|
.spread((batchNo, log) => {
|
|
|
|
if (log.length === 0) {
|
|
|
|
success(color.cyan('Already at the base migration'));
|
|
|
|
}
|
|
|
|
|
|
|
|
success(
|
|
|
|
color.green(
|
|
|
|
`Batch ${batchNo} rolled back the following migrations:\n${log.join(
|
|
|
|
'\n'
|
|
|
|
)}`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch(exit);
|
|
|
|
});
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
commander
|
|
|
|
.command('migrate:currentVersion')
|
2017-01-11 18:47:24 +05:45
|
|
|
.description(' View the current version for the migration.')
|
2018-11-16 13:23:22 +01:00
|
|
|
.action(() => {
|
2018-11-06 06:03:18 -03:00
|
|
|
pending = initKnex(env, commander.opts())
|
2018-07-09 08:10:34 -04:00
|
|
|
.migrate.currentVersion()
|
2018-11-16 13:23:22 +01:00
|
|
|
.then((version) => {
|
2018-12-31 01:25:48 +09:00
|
|
|
success(color.green('Current Version: ') + color.blue(version));
|
2018-07-09 08:10:34 -04:00
|
|
|
})
|
|
|
|
.catch(exit);
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
commander
|
|
|
|
.command('seed:make <name>')
|
2017-01-11 18:47:24 +05:45
|
|
|
.description(' Create a named seed file.')
|
2018-07-09 08:10:34 -04:00
|
|
|
.option(
|
|
|
|
`-x [${filetypes.join('|')}]`,
|
|
|
|
'Specify the stub extension (default js)'
|
|
|
|
)
|
2018-11-16 13:23:22 +01:00
|
|
|
.action((name) => {
|
|
|
|
const opts = commander.opts();
|
|
|
|
opts.client = opts.client || 'sqlite3'; // We don't really care about client when creating seeds
|
|
|
|
const instance = initKnex(env, opts);
|
2018-12-03 23:14:34 +01:00
|
|
|
const ext = (
|
|
|
|
argv.x ||
|
|
|
|
env.configuration.ext ||
|
|
|
|
DEFAULT_EXT
|
|
|
|
).toLowerCase();
|
2018-07-09 08:10:34 -04:00
|
|
|
pending = instance.seed
|
|
|
|
.make(name, { extension: ext })
|
2018-11-16 13:23:22 +01:00
|
|
|
.then((name) => {
|
2018-12-31 01:25:48 +09:00
|
|
|
success(color.green(`Created seed file: ${name}`));
|
2018-07-09 08:10:34 -04:00
|
|
|
})
|
|
|
|
.catch(exit);
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
commander
|
|
|
|
.command('seed:run')
|
2017-01-11 18:47:24 +05:45
|
|
|
.description(' Run seed files.')
|
2018-12-30 18:11:57 +02:00
|
|
|
.option('--verbose', 'verbose')
|
2018-11-16 13:23:22 +01:00
|
|
|
.action(() => {
|
2018-11-06 06:03:18 -03:00
|
|
|
pending = initKnex(env, commander.opts())
|
2018-07-09 08:10:34 -04:00
|
|
|
.seed.run()
|
2018-11-16 13:23:22 +01:00
|
|
|
.spread((log) => {
|
2018-07-09 08:10:34 -04:00
|
|
|
if (log.length === 0) {
|
2018-12-31 01:25:48 +09:00
|
|
|
success(color.cyan('No seed files exist'));
|
2018-07-09 08:10:34 -04:00
|
|
|
}
|
|
|
|
success(
|
2018-12-31 01:25:48 +09:00
|
|
|
color.green(`Ran ${log.length} seed files`) +
|
|
|
|
(argv.verbose ? `\n${color.cyan(log.join('\n'))}` : '')
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch(exit);
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
commander.parse(process.argv);
|
|
|
|
|
2018-11-16 13:23:22 +01:00
|
|
|
Promise.resolve(pending).then(() => {
|
2018-01-18 23:29:38 +01:00
|
|
|
commander.outputHelp();
|
|
|
|
exit('Unknown command-line options, exiting');
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-16 13:23:22 +01:00
|
|
|
const cli = new Liftoff({
|
2015-05-09 13:58:18 -04:00
|
|
|
name: 'knex',
|
|
|
|
extensions: interpret.jsVariants,
|
2018-07-09 08:10:34 -04:00
|
|
|
v8flags: require('v8flags'),
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
cli.on('require', function(name) {
|
2018-12-31 01:25:48 +09:00
|
|
|
console.log('Requiring external module', color.magenta(name));
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
cli.on('requireFail', function(name) {
|
2018-12-31 01:25:48 +09:00
|
|
|
console.log(color.red('Failed to load external module'), color.magenta(name));
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
cli.launch(
|
|
|
|
{
|
|
|
|
cwd: argv.cwd,
|
2018-12-03 23:14:34 +01:00
|
|
|
knexfile: argv.knexfile,
|
|
|
|
knexpath: argv.knexpath,
|
2018-07-09 08:10:34 -04:00
|
|
|
require: argv.require,
|
|
|
|
completion: argv.completion,
|
|
|
|
},
|
|
|
|
invoke
|
|
|
|
);
|