Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Amauri Bizerra committed Jan 4, 2019
2 parents 42df61a + a3766e6 commit e76c4e4
Show file tree
Hide file tree
Showing 19 changed files with 238 additions and 72 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Master (Unreleased)

# 0.16.3 - 19 Dec, 2018

### Bug fixes:

- @babel/polyfill loaded multiple times #2955
Expand Down
64 changes: 30 additions & 34 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const Liftoff = require('liftoff');
const Promise = require('bluebird');
const interpret = require('interpret');
const path = require('path');
const chalk = require('chalk');
const tildify = require('tildify');
const commander = require('commander');
const argv = require('minimist')(process.argv.slice(2));
const color = require('colorette');
const argv = require('getopts')(process.argv.slice(2));
const fs = Promise.promisifyAll(require('fs'));
const cliPkg = require('../package');
const {
Expand All @@ -18,9 +18,9 @@ const { DEFAULT_EXT } = require('./utils/constants');

function exit(text) {
if (text instanceof Error) {
console.error(chalk.red(text.stack));
console.error(color.red(text.stack));
} else {
console.error(chalk.red(text));
console.error(color.red(text));
}
process.exit(1);
}
Expand All @@ -33,8 +33,8 @@ function success(text) {
function checkLocalModule(env) {
if (!env.modulePath) {
console.log(
chalk.red('No local knex install found in:'),
chalk.magenta(tildify(env.cwd))
color.red('No local knex install found in:'),
color.magenta(tildify(env.cwd))
);
exit('Try running: npm install knex');
}
Expand All @@ -44,10 +44,7 @@ function initKnex(env, opts) {
checkLocalModule(env);
if (process.cwd() !== env.cwd) {
process.chdir(env.cwd);
console.log(
'Working directory changed to',
chalk.magenta(tildify(env.cwd))
);
console.log('Working directory changed to', color.magenta(tildify(env.cwd)));
}

if (!opts.knexfile) {
Expand Down Expand Up @@ -78,12 +75,12 @@ function initKnex(env, opts) {
}

if (environment) {
console.log('Using environment:', chalk.magenta(environment));
console.log('Using environment:', color.magenta(environment));
config = config[environment] || config;
}

if (!config) {
console.log(chalk.red('Warning: unable to read knexfile config'));
console.log(color.red('Warning: unable to read knexfile config'));
process.exit(1);
}

Expand All @@ -103,12 +100,9 @@ function invoke(env) {

commander
.version(
chalk.blue('Knex CLI version: ', chalk.green(cliPkg.version)) +
color.blue('Knex CLI version: ', color.green(cliPkg.version)) +
'\n' +
chalk.blue(
'Local Knex version: ',
chalk.green(env.modulePackage.version)
) +
color.blue('Local Knex version: ', color.green(env.modulePackage.version)) +
'\n'
)
.option('--debug', 'Run with debugging.')
Expand Down Expand Up @@ -154,7 +148,7 @@ function invoke(env) {
return fs.writeFileAsync(stubPath, code);
})
.then(() => {
success(chalk.green(`Created ${stubPath}`));
success(color.green(`Created ${stubPath}`));
})
.catch(exit);
});
Expand All @@ -178,24 +172,25 @@ function invoke(env) {
pending = instance.migrate
.make(name, { extension: ext })
.then((name) => {
success(chalk.green(`Created Migration: ${name}`));
success(color.green(`Created Migration: ${name}`));
})
.catch(exit);
});

commander
.command('migrate:latest')
.description(' Run all migrations that have not yet been run.')
.option('--verbose', 'verbose')
.action(() => {
pending = initKnex(env, commander.opts())
.migrate.latest()
.spread((batchNo, log) => {
if (log.length === 0) {
success(chalk.cyan('Already up to date'));
success(color.cyan('Already up to date'));
}
success(
chalk.green(`Batch ${batchNo} run: ${log.length} migrations \n`) +
chalk.cyan(log.join('\n'))
color.green(`Batch ${batchNo} run: ${log.length} migrations`) +
(argv.verbose ? `\n${color.cyan(log.join('\n'))}` : '')
);
})
.catch(exit);
Expand All @@ -204,17 +199,18 @@ function invoke(env) {
commander
.command('migrate:rollback')
.description(' Rollback the last set of migrations performed.')
.option('--verbose', 'verbose')
.action(() => {
pending = initKnex(env, commander.opts())
.migrate.rollback()
.spread((batchNo, log) => {
if (log.length === 0) {
success(chalk.cyan('Already at the base migration'));
success(color.cyan('Already at the base migration'));
}
success(
chalk.green(
`Batch ${batchNo} rolled back: ${log.length} migrations \n`
) + chalk.cyan(log.join('\n'))
color.green(
`Batch ${batchNo} rolled back: ${log.length} migrations`
) + (argv.verbose ? `\n${color.cyan(log.join('\n'))}` : '')
);
})
.catch(exit);
Expand All @@ -227,7 +223,7 @@ function invoke(env) {
pending = initKnex(env, commander.opts())
.migrate.currentVersion()
.then((version) => {
success(chalk.green('Current Version: ') + chalk.blue(version));
success(color.green('Current Version: ') + color.blue(version));
})
.catch(exit);
});
Expand All @@ -251,25 +247,25 @@ function invoke(env) {
pending = instance.seed
.make(name, { extension: ext })
.then((name) => {
success(chalk.green(`Created seed file: ${name}`));
success(color.green(`Created seed file: ${name}`));
})
.catch(exit);
});

commander
.command('seed:run')
.description(' Run seed files.')
.option('--verbose', 'verbose')
.action(() => {
pending = initKnex(env, commander.opts())
.seed.run()
.spread((log) => {
if (log.length === 0) {
success(chalk.cyan('No seed files exist'));
success(color.cyan('No seed files exist'));
}
success(
chalk.green(
`Ran ${log.length} seed files \n${chalk.cyan(log.join('\n'))}`
)
color.green(`Ran ${log.length} seed files`) +
(argv.verbose ? `\n${color.cyan(log.join('\n'))}` : '')
);
})
.catch(exit);
Expand All @@ -290,11 +286,11 @@ const cli = new Liftoff({
});

cli.on('require', function(name) {
console.log('Requiring external module', chalk.magenta(name));
console.log('Requiring external module', color.magenta(name));
});

cli.on('requireFail', function(name) {
console.log(chalk.red('Failed to load external module'), chalk.magenta(name));
console.log(color.red('Failed to load external module'), color.magenta(name));
});

cli.launch(
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"name": "knex",
"version": "0.16.2",
"version": "0.16.3",
"description": "A batteries-included SQL query & schema builder for Postgres, MySQL and SQLite3 and the Browser",
"main": "knex.js",
"types": "types/knex.d.ts",
"engines": {
"node": ">=6"
},
"dependencies": {
"@babel/polyfill": "^7.0.0",
"@babel/polyfill": "^7.2.5",
"@types/bluebird": "^3.5.25",
"bluebird": "^3.5.3",
"chalk": "2.4.1",
"colorette": "1.0.7",
"commander": "^2.19.0",
"debug": "4.1.0",
"inherits": "~2.0.3",
"interpret": "^1.1.0",
"liftoff": "2.5.0",
"lodash": "^4.17.11",
"minimist": "1.2.0",
"getopts": "2.2.3",
"mkdirp": "^0.5.1",
"pg-connection-string": "2.0.0",
"tarn": "^1.1.4",
"tildify": "1.2.0",
"uuid": "^3.3.2",
"v8flags": "^3.1.1"
"v8flags": "^3.1.2"
},
"lint-staged": {
"*.{js,json}": [
Expand All @@ -33,9 +33,9 @@
]
},
"devDependencies": {
"@babel/cli": "^7.2.0",
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.0",
"@babel/preset-env": "^7.2.3",
"@types/node": "*",
"JSONStream": "^1.3.5",
"async": "^2.6.1",
Expand Down Expand Up @@ -96,7 +96,7 @@
"mssql:init": "docker-compose -f scripts/mssql-docker-compose.yml up --no-start && docker-compose -f scripts/mssql-docker-compose.yml start",
"postmssql:init": "node scripts/wait-for-mssql-server.js && npm run mssql:logs || (npm run mssql:logs;false)",
"mssql:logs": "docker-compose -f scripts/mssql-docker-compose.yml logs",
"mssql:test": "DB=mssql npm test",
"mssql:test": "DB=mssql npm run plaintest",
"mssql:destroy": "docker-compose -f scripts/mssql-docker-compose.yml stop",
"stress:init": "docker-compose -f scripts/stress-test/docker-compose.yml up --no-start && docker-compose -f scripts/stress-test/docker-compose.yml start",
"stress:test": "node scripts/stress-test/knex-stress-test.js | grep -A 5 -B 60 -- '- STATS '",
Expand Down Expand Up @@ -124,9 +124,9 @@
"web": "https://github.com/tgriesser"
},
"browser": {
"./lib/migrate/index.js": "./lib/util/noop.js",
"./lib/migrate/Migrator.js": "./lib/util/noop.js",
"./lib/bin/cli.js": "./lib/util/noop.js",
"./lib/seed/index.js": "./lib/util/noop.js",
"./lib/seed/Seeder.js": "./lib/util/noop.js",
"mssql": false,
"mssql/lib/base": false,
"tedious": false,
Expand Down
8 changes: 4 additions & 4 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint no-console:0 */

import chalk from 'chalk';
import color from 'colorette';
import { isFunction, isNil } from 'lodash';

function log(message, userFn, colorFn) {
Expand Down Expand Up @@ -31,17 +31,17 @@ class Logger {
}

warn(message) {
log(message, this._warn, chalk.yellow);
log(message, this._warn, color.yellow);
}

error(message) {
log(message, this._error, chalk.red);
log(message, this._error, color.red);
}

deprecate(method, alternative) {
const message = `${method} is deprecated, please use ${alternative}`;

log(message, this._deprecate, chalk.yellow);
log(message, this._deprecate, color.yellow);
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/migrate/Migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export default class Migrator {
});
}

// Rollback the last "batch" of migrations that were run.
rollback(config) {
// Rollback the last "batch", or all, of migrations that were run.
rollback(config, all = false) {
return Promise.try(() => {
this.config = getMergedConfig(config, this.config);

Expand All @@ -109,7 +109,7 @@ export default class Migrator {
.tap((value) =>
validateMigrationList(this.config.migrationSource, value)
)
.then((val) => this._getLastBatch(val))
.then((val) => (all ? val[0] : this._getLastBatch(val)))
.then((migrations) => {
return this._runBatch(migrations, 'down');
});
Expand Down Expand Up @@ -423,7 +423,8 @@ export function getMergedConfig(config, currentConfig) {
if (!mergedConfig.migrationSource) {
mergedConfig.migrationSource = new FsMigrations(
mergedConfig.directory,
mergedConfig.sortDirsSeparately
mergedConfig.sortDirsSeparately,
mergedConfig.loadExtensions
);
}

Expand Down
14 changes: 14 additions & 0 deletions src/seed/index.js → src/seed/Seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ Seeder.prototype._waterfallBatch = function(seeds) {
.then(() => seed.seed(knex, Promise))
.then(() => {
log.push(name);
})
.catch((originalError) => {
const error = new Error(
`Error while executing "${name}" seed: ${originalError.message}`
);
error.original = originalError;
error.stack =
error.stack
.split('\n')
.slice(0, 2)
.join('\n') +
'\n' +
originalError.stack;
throw error;
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/util/make-knex.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventEmitter } from 'events';

import Migrator from '../migrate/Migrator';
import Seeder from '../seed';
import Seeder from '../seed/Seeder';
import FunctionHelper from '../functionhelper';
import QueryInterface from '../query/methods';
import { assign } from 'lodash';
Expand Down

0 comments on commit e76c4e4

Please sign in to comment.