Skip to content

Commit

Permalink
Merge branch 'master' into remove-lodash-babel-plugin
Browse files Browse the repository at this point in the history
* master:
  Allow overwriting log functions (#2625)
  • Loading branch information
tgriesser committed May 29, 2018
2 parents 2d4932e + 65509c3 commit 6379f1b
Show file tree
Hide file tree
Showing 38 changed files with 187 additions and 130 deletions.
16 changes: 10 additions & 6 deletions src/client.js
@@ -1,5 +1,4 @@
import Promise from 'bluebird';
import * as helpers from './helpers';

import Raw from './raw';
import Ref from './ref';
Expand All @@ -24,6 +23,8 @@ import { EventEmitter } from 'events';
import { makeEscape } from './query/string'
import { assign, uniqueId, cloneDeep, defaults } from 'lodash'

import Logger from './logger';

const debug = require('debug')('knex:client')
const debugQuery = require('debug')('knex:query')
const debugBindings = require('debug')('knex:bindings')
Expand Down Expand Up @@ -51,6 +52,8 @@ function Client(config = {}) {
if (config.useNullAsDefault) {
this.valueForUndefined = null
}

this.logger = new Logger(config);
}
inherits(Client, EventEmitter)

Expand Down Expand Up @@ -197,7 +200,8 @@ assign(Client.prototype, {
try {
this.driver = this._driver()
} catch (e) {
helpers.exit(`Knex: run\n$ npm install ${this.driverName} --save\n${e.stack}`)
this.logger.error(`Knex: run\n$ npm install ${this.driverName} --save\n${e.stack}`)
process.exit(1);
}
},

Expand All @@ -220,7 +224,7 @@ assign(Client.prototype, {
'Promise'
].forEach(option => {
if (option in poolConfig) {
helpers.warn([
this.logger.warn([
`Pool config option "${option}" is no longer supported.`,
`See https://github.com/Vincit/tarn.js for possible pool config options.`
].join(' '))
Expand Down Expand Up @@ -249,7 +253,7 @@ assign(Client.prototype, {

destroy: (connection) => {
if (poolConfig.beforeDestroy) {
helpers.warn(`
this.logger.warn(`
beforeDestroy is deprecated, please open an issue if you use this
to discuss alternative apis
`)
Expand All @@ -264,7 +268,7 @@ assign(Client.prototype, {

validate: (connection) => {
if (connection.__knex__disposed) {
helpers.warn(`Connection Error: ${connection.__knex__disposed}`)
this.logger.warn(`Connection Error: ${connection.__knex__disposed}`)
return false
}

Expand All @@ -275,7 +279,7 @@ assign(Client.prototype, {

initializePool(config) {
if (this.pool) {
helpers.warn('The pool has already been initialized')
this.logger.warn('The pool has already been initialized')
return
}

Expand Down
3 changes: 1 addition & 2 deletions src/dialects/maria/transaction.js
@@ -1,6 +1,5 @@
import Debug from 'debug';
import Transaction from '../../transaction';
import * as helpers from '../../helpers';
import {isUndefined} from 'lodash';

const debug = Debug('knex:tx');
Expand All @@ -11,7 +10,7 @@ export default class Transaction_Maria extends Transaction {
const t = this
const q = this.trxClient.query(conn, sql)
.catch(err => err.code === 1305, () => {
helpers.warn(
this.trxClient.logger.warn(
'Transaction was implicitly committed, do not mix transactions and ' +
'DDL with MariaDB (#805)'
);
Expand Down
9 changes: 4 additions & 5 deletions src/dialects/mssql/schema/columncompiler.js
Expand Up @@ -3,7 +3,6 @@
// -------
import inherits from 'inherits';
import ColumnCompiler from '../../../schema/columncompiler';
import * as helpers from '../../../helpers';

import { assign } from 'lodash'

Expand Down Expand Up @@ -70,7 +69,7 @@ assign(ColumnCompiler_MSSQL.prototype, {

bit(length) {
if (length > 1) {
helpers.warn('Bit field is exactly 1 bit length for MSSQL');
this.client.logger.warn('Bit field is exactly 1 bit length for MSSQL');
}
return 'bit';
},
Expand All @@ -93,18 +92,18 @@ assign(ColumnCompiler_MSSQL.prototype, {
},

first() {
helpers.warn('Column first modifier not available for MSSQL');
this.client.logger.warn('Column first modifier not available for MSSQL');
return '';
},

after(column) {
helpers.warn('Column after modifier not available for MSSQL');
this.client.logger.warn('Column after modifier not available for MSSQL');
return '';
},

comment(comment) {
if (comment && comment.length > 255) {
helpers.warn('Your comment is longer than the max comment length for MSSQL')
this.client.logger.warn('Your comment is longer than the max comment length for MSSQL')
}
return ''
}
Expand Down
2 changes: 1 addition & 1 deletion src/dialects/mssql/schema/tablecompiler.js
Expand Up @@ -26,7 +26,7 @@ assign(TableCompiler_MSSQL.prototype, {

if (this.single.comment) {
const {comment} = this.single;
if (comment.length > 60) helpers.warn('The max length for a table comment is 60 characters');
if (comment.length > 60) this.client.logger.warn('The max length for a table comment is 60 characters');
}

this.pushQuery(sql);
Expand Down
5 changes: 2 additions & 3 deletions src/dialects/mysql/schema/columncompiler.js
Expand Up @@ -3,7 +3,6 @@
// -------
import inherits from 'inherits';
import ColumnCompiler from '../../../schema/columncompiler';
import * as helpers from '../../../helpers';

import { assign } from 'lodash'

Expand All @@ -13,7 +12,7 @@ function supportsPreciseTimestamps(client) {
'your knex configuration. Currently this defaults to 5.5, but in a future ' +
'release it will default to 5.6 which supports high precision timestamps. ' +
'See http://knexjs.org/#Schema-timestamps for more information.'
helpers.warn(message)
client.logger.warn(message)
}

return client.version && parseFloat(client.version) > 5.5
Expand Down Expand Up @@ -113,7 +112,7 @@ assign(ColumnCompiler_MySQL.prototype, {

comment(comment) {
if (comment && comment.length > 255) {
helpers.warn('Your comment is longer than the max comment length for MySQL')
this.client.logger.warn('Your comment is longer than the max comment length for MySQL')
}
return comment && `comment '${comment}'`
},
Expand Down
3 changes: 1 addition & 2 deletions src/dialects/mysql/schema/tablecompiler.js
Expand Up @@ -4,7 +4,6 @@
// -------
import inherits from 'inherits';
import TableCompiler from '../../../schema/tablecompiler';
import * as helpers from '../../../helpers';
import Promise from 'bluebird';

import { assign } from 'lodash'
Expand Down Expand Up @@ -41,7 +40,7 @@ assign(TableCompiler_MySQL.prototype, {

if (this.single.comment) {
const comment = (this.single.comment || '');
if (comment.length > 60) helpers.warn('The max length for a table comment is 60 characters');
if (comment.length > 60) this.client.logger.warn('The max length for a table comment is 60 characters');
sql += ` comment = '${comment}'`;
}

Expand Down
5 changes: 2 additions & 3 deletions src/dialects/mysql/transaction.js
Expand Up @@ -2,7 +2,6 @@
import Transaction from '../../transaction';
import inherits from 'inherits';
import Debug from 'debug';
import * as helpers from '../../helpers';
import { assign, isUndefined } from 'lodash'

const debug = Debug('knex:tx');
Expand All @@ -17,8 +16,8 @@ assign(Transaction_MySQL.prototype, {
query(conn, sql, status, value) {
const t = this
const q = this.trxClient.query(conn, sql)
.catch(err => err.errno === 1305, function() {
helpers.warn(
.catch(err => err.errno === 1305, () => {
this.trxClient.logger.warn(
'Transaction was implicitly committed, do not mix transactions and ' +
'DDL with MySQL (#805)'
)
Expand Down
5 changes: 2 additions & 3 deletions src/dialects/mysql2/transaction.js
Expand Up @@ -2,7 +2,6 @@
import Transaction from '../../transaction';
import inherits from 'inherits';
const debug = require('debug')('knex:tx')
import * as helpers from '../../helpers';

import { assign, isUndefined } from 'lodash'

Expand All @@ -16,8 +15,8 @@ assign(Transaction_MySQL2.prototype, {
query(conn, sql, status, value) {
const t = this
const q = this.trxClient.query(conn, sql)
.catch(err => err.code === 'ER_SP_DOES_NOT_EXIST', function() {
helpers.warn(
.catch(err => err.code === 'ER_SP_DOES_NOT_EXIST', () => {
this.trxClient.logger.warn(
'Transaction was implicitly committed, do not mix transactions and' +
'DDL with MySQL (#805)'
)
Expand Down
3 changes: 1 addition & 2 deletions src/dialects/oracle/query/compiler.js
Expand Up @@ -5,7 +5,6 @@
import { assign, isPlainObject, isEmpty, isString, map, reduce, compact, identity } from 'lodash'
import inherits from 'inherits';
import QueryCompiler from '../../../query/compiler';
import * as helpers from '../../../helpers';
import { ReturningHelper } from '../utils';

const components = [
Expand Down Expand Up @@ -139,7 +138,7 @@ assign(QueryCompiler_Oracle.prototype, {
forShare() {
// lock for share is not directly supported by oracle
// use LOCK TABLE .. IN SHARE MODE; instead
helpers.warn('lock for share is not supported by oracle dialect');
this.client.logger.warn('lock for share is not supported by oracle dialect');
return '';
},

Expand Down
5 changes: 4 additions & 1 deletion src/dialects/oracle/schema/columncompiler.js
Expand Up @@ -21,7 +21,10 @@ assign(ColumnCompiler_Oracle.prototype, {
// TODO Add warning that sequence etc is created
this.pushAdditional(function () {
const tableName = this.tableCompiler.tableNameRaw;
const createTriggerSQL = Trigger.createAutoIncrementTrigger(tableName);
const createTriggerSQL = Trigger.createAutoIncrementTrigger(
this.client.logger,
tableName
);
this.pushQuery(createTriggerSQL);
});
},
Expand Down
8 changes: 6 additions & 2 deletions src/dialects/oracle/schema/compiler.js
Expand Up @@ -13,7 +13,11 @@ inherits(SchemaCompiler_Oracle, SchemaCompiler);

// Rename a table on the schema.
SchemaCompiler_Oracle.prototype.renameTable = function(tableName, to) {
const renameTable = Trigger.renameTableAndAutoIncrementTrigger(tableName, to);
const renameTable = Trigger.renameTableAndAutoIncrementTrigger(
this.client.logger,
tableName,
to
);
this.pushQuery(renameTable);
};

Expand Down Expand Up @@ -45,7 +49,7 @@ SchemaCompiler_Oracle.prototype.dropSequenceIfExists = function (sequenceName) {

SchemaCompiler_Oracle.prototype._dropRelatedSequenceIfExists = function (tableName) {
// removing the sequence that was possibly generated by increments() column
const sequenceName = utils.generateCombinedName('seq', tableName);
const sequenceName = utils.generateCombinedName(this.client.logger, 'seq', tableName);
this.dropSequenceIfExists(sequenceName);
};

Expand Down
4 changes: 2 additions & 2 deletions src/dialects/oracle/schema/tablecompiler.js
Expand Up @@ -43,7 +43,7 @@ assign(TableCompiler_Oracle.prototype, {
renameColumn(from, to) {
// Remove quotes around tableName
const tableName = this.tableName().slice(1, -1)
return this.pushQuery(Trigger.renameColumnTrigger(tableName, from, to));
return this.pushQuery(Trigger.renameColumnTrigger(this.client.logger, tableName, from, to));
},

compileAdd(builder) {
Expand Down Expand Up @@ -84,7 +84,7 @@ assign(TableCompiler_Oracle.prototype, {
},

_indexCommand(type, tableName, columns) {
return this.formatter.wrap(utils.generateCombinedName(type, tableName, columns));
return this.formatter.wrap(utils.generateCombinedName(this.client.logger, type, tableName, columns));
},

primary(columns, constraintName) {
Expand Down
22 changes: 11 additions & 11 deletions src/dialects/oracle/schema/trigger.js
Expand Up @@ -2,9 +2,9 @@ import * as utils from '../utils';

const trigger = {

renameColumnTrigger: function(tableName, columnName, to) {
const triggerName = utils.generateCombinedName('autoinc_trg', tableName);
const sequenceName = utils.generateCombinedName('seq', tableName);
renameColumnTrigger: function(logger, tableName, columnName, to) {
const triggerName = utils.generateCombinedName(logger, 'autoinc_trg', tableName);
const sequenceName = utils.generateCombinedName(logger, 'seq', tableName);
return `DECLARE ` +
`PK_NAME VARCHAR(200); ` +
`IS_AUTOINC NUMBER := 0; ` +
Expand Down Expand Up @@ -38,9 +38,9 @@ const trigger = {
`END;`;
},

createAutoIncrementTrigger: function(tableName) {
const triggerName = utils.generateCombinedName('autoinc_trg', tableName);
const sequenceName = utils.generateCombinedName('seq', tableName);
createAutoIncrementTrigger: function(logger, tableName) {
const triggerName = utils.generateCombinedName(logger, 'autoinc_trg', tableName);
const sequenceName = utils.generateCombinedName(logger, 'seq', tableName);
return `DECLARE ` +
`PK_NAME VARCHAR(200); ` +
`BEGIN` +
Expand Down Expand Up @@ -68,11 +68,11 @@ const trigger = {
`END;`;
},

renameTableAndAutoIncrementTrigger: function(tableName, to) {
const triggerName = utils.generateCombinedName('autoinc_trg', tableName);
const sequenceName = utils.generateCombinedName('seq', tableName);
const toTriggerName = utils.generateCombinedName('autoinc_trg', to);
const toSequenceName = utils.generateCombinedName('seq', to);
renameTableAndAutoIncrementTrigger: function(logger, tableName, to) {
const triggerName = utils.generateCombinedName(logger, 'autoinc_trg', tableName);
const sequenceName = utils.generateCombinedName(logger, 'seq', tableName);
const toTriggerName = utils.generateCombinedName(logger, 'autoinc_trg', to);
const toSequenceName = utils.generateCombinedName(logger, 'seq', to);
return `DECLARE ` +
`PK_NAME VARCHAR(200); ` +
`IS_AUTOINC NUMBER := 0; ` +
Expand Down
7 changes: 2 additions & 5 deletions src/dialects/oracle/utils.js
@@ -1,15 +1,12 @@

import * as helpers from '../../helpers';

function generateCombinedName(postfix, name, subNames) {
function generateCombinedName(logger, postfix, name, subNames) {
const crypto = require('crypto');
const limit = 30;
if (!Array.isArray(subNames)) subNames = subNames ? [subNames] : [];
const table = name.replace(/\.|-/g, '_');
const subNamesPart = subNames.join('_');
let result = `${table}_${subNamesPart.length ? subNamesPart + '_': ''}${postfix}`.toLowerCase();
if (result.length > limit) {
helpers.warn(
logger.warn(
`Automatically generated name "${result}" exceeds ${limit} character ` +
`limit for Oracle. Using base64 encoded sha1 of that name instead.`
);
Expand Down
5 changes: 3 additions & 2 deletions src/dialects/oracledb/index.js
Expand Up @@ -9,7 +9,6 @@ const BlobHelper = require('./utils').BlobHelper;
const ReturningHelper = require('./utils').ReturningHelper;
const Promise = require('bluebird');
const stream = require('stream');
const helpers = require('../../helpers');
const Transaction = require('./transaction');
const Client_Oracle = require('../oracle');
const Oracle_Formatter = require('../oracle/formatter');
Expand Down Expand Up @@ -37,7 +36,9 @@ Client_Oracledb.prototype._driver = function() {
type = type.toUpperCase();
if (oracledb[type]) {
if (type !== 'NUMBER' && type !== 'DATE' && type !== 'CLOB') {
helpers.warn('Only "date", "number" and "clob" are supported for fetchAsString');
this.logger.warn(
'Only "date", "number" and "clob" are supported for fetchAsString'
);
}
client.fetchAsString.push(oracledb[type]);
}
Expand Down
3 changes: 1 addition & 2 deletions src/dialects/postgres/index.js
Expand Up @@ -5,7 +5,6 @@ import { assign, map, extend, isArray, isString, includes } from 'lodash'
import inherits from 'inherits';
import Client from '../../client';
import Promise from 'bluebird';
import {warn} from '../../helpers';

import QueryCompiler from './query/compiler';
import ColumnCompiler from './schema/columncompiler';
Expand Down Expand Up @@ -179,7 +178,7 @@ assign(Client_PG.prototype, {
if(includes(path, ',')) {
const parts = path.split(',');
const arraySyntax = `[${map(parts, (searchPath) => `'${searchPath}'`).join(', ')}]`;
warn(
this.logger.warn(
`Detected comma in searchPath "${path}".`
+
`If you are trying to specify multiple schemas, use Array syntax: ${arraySyntax}`);
Expand Down
3 changes: 1 addition & 2 deletions src/dialects/postgres/schema/columncompiler.js
Expand Up @@ -4,7 +4,6 @@

import inherits from 'inherits';
import ColumnCompiler from '../../../schema/columncompiler';
import * as helpers from '../../../helpers';

import { assign } from 'lodash'

Expand Down Expand Up @@ -51,7 +50,7 @@ assign(ColumnCompiler_PG.prototype, {
floating: 'real',
increments: 'serial primary key',
json(jsonb) {
if (jsonb) helpers.deprecate('json(true)', 'jsonb()')
if (jsonb) this.client.logger.deprecate('json(true)', 'jsonb()')
return jsonColumn(this.client, jsonb);
},
jsonb() {
Expand Down

0 comments on commit 6379f1b

Please sign in to comment.