Skip to content

Commit c9aacb2

Browse files
committed
lib: Change how debugging logs to the console and test debugging code
This changes the format of SQL that is logged slightly and logs to the console using a single console.log(). Also, console.log() is now always used (console.error() is no longer used to log an operation that failed).
1 parent db32bec commit c9aacb2

File tree

4 files changed

+120
-42
lines changed

4 files changed

+120
-42
lines changed

lib/PoolPlus.js

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ class PoolPlus extends Pool {
378378
}
379379
}
380380

381-
/* istanbul ignore if - Ignore debugging */
382381
if (this._debug) {
383382
for (let i = 0; i < opsLeft; i++) {
384383
connection.query(operations[i].sql, err => { // eslint-disable-line no-loop-func
@@ -398,41 +397,32 @@ class PoolPlus extends Pool {
398397
// Sort the operations while the connection is being fetched
399398
operations.sort(Operation.sorter);
400399

401-
/* istanbul ignore if - Ignore debugging */
402400
if (this._debug) {
403401
debugOperations(operations);
404402
}
405403
}
406404
}
407405

408-
/* istanbul ignore next - Ignore debugging */
409-
function debugOperations(operations) {
410-
console.log();
411-
console.log('============= mysql-plus operations: ==============');
412-
const opTypes = require('./Operation').Types; // eslint-disable-line global-require
413-
const opTypeNames = Object.keys(opTypes);
414-
operations.forEach(operation => {
415-
console.log();
416-
console.log('type:', opTypeNames.find(typeName => opTypes[typeName] === operation.type));
417-
console.log('SQL:', operation.sql);
418-
});
419-
console.log();
420-
console.log('===================================================');
421-
console.log();
406+
function debugOperations(operations, debuggingError) {
407+
const opTypeNames = Object.keys(Operation.Types);
408+
const header = debuggingError
409+
? '\n====== mysql-plus sync errored on operation: ======\n'
410+
: '\n============= mysql-plus operations: ==============\n';
411+
412+
console.log(
413+
header +
414+
operations.map(operation =>
415+
'\n' +
416+
'type: ' + opTypeNames.find(typeName => Operation.Types[typeName] === operation.type) + '\n' +
417+
'SQL:\n' +
418+
operation.sql + '\n'
419+
) +
420+
'\n===================================================\n'
421+
);
422422
}
423423

424-
/* istanbul ignore next - Ignore debugging */
425424
function debugSyncErrorOperation(operation) {
426-
console.error();
427-
console.error('====== mysql-plus sync errored on operation: ======');
428-
const opTypes = require('./Operation').Types; // eslint-disable-line global-require
429-
const opTypeNames = Object.keys(opTypes);
430-
console.error();
431-
console.error('type:', opTypeNames.find(typeName => opTypes[typeName] === operation.type));
432-
console.error('SQL:', operation.sql);
433-
console.error();
434-
console.error('===================================================');
435-
console.error();
425+
debugOperations([operation], true);
436426
}
437427

438428
/**

lib/TableDefinition.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ class TableDefinition {
9696
const operations = this._getMigrateForeignKeysOperations(oldSchema, alterOperations);
9797

9898
if (alterOperations.length) {
99-
const spacer = '\n ';
99+
const spacer = '\n ';
100+
100101
alterOperations.sort(Operation.sorter);
101102
operations.push(Operation.create(
102103
Operation.Types.ALTER_TABLE,
@@ -345,7 +346,7 @@ class TableDefinition {
345346
_schemaToSQL() {
346347
const pool = this._pool;
347348
const schema = this._schema;
348-
const spacer = '\n ';
349+
const spacer = '\n ';
349350
const separator = ',' + spacer;
350351

351352
var sql = 'CREATE TABLE ' + this._escapedName + ' (' + spacer;
@@ -382,7 +383,7 @@ class TableDefinition {
382383
// referenced haven't been created yet
383384

384385
// Slice off the trailing ',' and close the CREATE TABLE statement
385-
sql = sql.slice(0, -separator.length) + '\n )';
386+
sql = sql.slice(0, -separator.length) + '\n)';
386387

387388
if (schema.engine) {
388389
sql += ' ENGINE=' + schema.engine;

test/unit/PoolPlus.test.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,4 +855,87 @@ describe('PoolPlus', () => {
855855

856856
});
857857

858+
859+
describe('with debugging enabled', () => {
860+
861+
const debugPool = new PoolPlus(Object.assign({plusOptions: {debug: true}}, config));
862+
863+
before(() => {
864+
sinon.stub(console, 'log');
865+
});
866+
867+
afterEach(() => {
868+
console.log.reset();
869+
});
870+
871+
after(done => {
872+
console.log.restore();
873+
debugPool.end(done);
874+
});
875+
876+
it('should log operations to the console when syncing', done => {
877+
debugPool.defineTable('pool_plus_test_table_debug', {
878+
columns: {
879+
id: debugPool.ColTypes.int().unsigned().notNull().primaryKey(),
880+
},
881+
});
882+
883+
debugPool.sync(err => {
884+
if (err) throw err;
885+
886+
console.log.should.have.been.calledWithExactly([
887+
'',
888+
'============= mysql-plus operations: ==============',
889+
'',
890+
'type: CREATE_TABLE',
891+
'SQL:',
892+
'CREATE TABLE `pool_plus_test_table_debug` (',
893+
' `id` int unsigned NOT NULL,',
894+
' PRIMARY KEY (`id`)',
895+
')',
896+
'',
897+
'===================================================',
898+
'',
899+
].join('\n'));
900+
901+
done();
902+
});
903+
});
904+
905+
it('should log the operation that failed to the console when syncing', done => {
906+
debugPool.defineTable('pool_plus_test_table_debug_error', {
907+
columns: {
908+
id: debugPool.ColTypes.int().unsigned().notNull().primaryKey(),
909+
},
910+
foreignKeys: {
911+
id: 'non_existent_table.id',
912+
},
913+
});
914+
915+
debugPool.sync(err => {
916+
err.should.be.an.Error().and.match({
917+
message: /ER_CANNOT_ADD_FOREIGN/,
918+
code: 'ER_CANNOT_ADD_FOREIGN',
919+
errno: 1215,
920+
});
921+
922+
console.log.should.have.been.calledWithExactly([
923+
'',
924+
'====== mysql-plus sync errored on operation: ======',
925+
'',
926+
'type: ADD_FOREIGN_KEY',
927+
'SQL:',
928+
'ALTER TABLE `pool_plus_test_table_debug_error` ADD CONSTRAINT `fk_pool_plus_test_table_debug_error_id`' +
929+
' FOREIGN KEY (`id`) REFERENCES `non_existent_table` (`id`)',
930+
'',
931+
'===================================================',
932+
'',
933+
].join('\n'));
934+
935+
done();
936+
});
937+
});
938+
939+
});
940+
858941
});

test/unit/TableDefinition.test.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ describe('TableDefinition', () => {
7979
const expectedOperations = [
8080
{
8181
type: Operation.Types.CREATE_TABLE,
82-
sql: 'CREATE TABLE `' + tableName + '` (\n' +
83-
' `id` int unsigned NOT NULL,\n' +
84-
' PRIMARY KEY (`id`)\n' +
85-
' )',
82+
sql:
83+
'CREATE TABLE `' + tableName + '` (\n' +
84+
' `id` int unsigned NOT NULL,\n' +
85+
' PRIMARY KEY (`id`)\n' +
86+
')',
8687
},
8788
];
8889

@@ -136,10 +137,11 @@ describe('TableDefinition', () => {
136137
const expectedOperations = [
137138
{
138139
type: Operation.Types.ALTER_TABLE,
139-
sql: 'ALTER TABLE `' + existingTableName + '`\n' +
140-
' DROP PRIMARY KEY,\n' +
141-
' MODIFY COLUMN `id` int unsigned NOT NULL FIRST,\n' +
142-
' ADD COLUMN `newCol` tinyint AFTER `id`',
140+
sql:
141+
'ALTER TABLE `' + existingTableName + '`\n' +
142+
' DROP PRIMARY KEY,\n' +
143+
' MODIFY COLUMN `id` int unsigned NOT NULL FIRST,\n' +
144+
' ADD COLUMN `newCol` tinyint AFTER `id`',
143145
columns: undefined,
144146
},
145147
];
@@ -162,12 +164,14 @@ describe('TableDefinition', () => {
162164
},
163165
{
164166
type: Operation.Types.CREATE_TABLE,
165-
sql: 'CREATE TABLE `' + existingTableName + '` (\n' +
166-
' `id` int unsigned NOT NULL,\n' +
167-
' `newCol` tinyint\n' +
168-
' )',
167+
sql:
168+
'CREATE TABLE `' + existingTableName + '` (\n' +
169+
' `id` int unsigned NOT NULL,\n' +
170+
' `newCol` tinyint\n' +
171+
')',
169172
},
170173
];
174+
171175
new TableDefinition(existingTableName, newSchema, mockPool, 'drop')
172176
.genSyncOperations((err, operations) => {
173177
if (err) throw err;

0 commit comments

Comments
 (0)