Skip to content

Commit 9f714a7

Browse files
committed
lib: Fix debug formatting + improve foreign key operation formatting
1 parent 415dd3a commit 9f714a7

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

lib/PoolPlus.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -406,18 +406,17 @@ class PoolPlus extends Pool {
406406
function debugOperations(operations, debuggingError) {
407407
const opTypeNames = Object.keys(Operation.Types);
408408
const header = debuggingError
409-
? '\n====== mysql-plus sync errored on operation: ======\n'
410-
: '\n============= mysql-plus operations: ==============\n';
409+
? '\n====== mysql-plus sync errored on operation: ======\n\n'
410+
: '\n============= mysql-plus operations: ==============\n\n';
411411

412412
console.log(
413413
header +
414414
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'
415+
'type: ' + opTypeNames.find(typeName => Operation.Types[typeName] === operation.type) +
416+
'\nSQL:\n' +
417+
operation.sql
418+
).join('\n\n') +
419+
'\n\n===================================================\n'
421420
);
422421
}
423422

lib/TableDefinition.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ class TableDefinition {
478478
const pool = this._pool;
479479

480480
var sql = 'CONSTRAINT ' + pool.escapeId(this._createKeyName(KeyTypes.FOREIGN, columns)) +
481-
' FOREIGN KEY (' + pool.escapeId(columns) + ')' +
481+
'\n FOREIGN KEY (' + pool.escapeId(columns) + ')' +
482482
' REFERENCES ' + pool.escapeId(foreignKeyData.table) +
483483
' (' + pool.escapeId(foreignKeyData.column) + ')';
484484

test/unit/PoolPlus.test.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -861,24 +861,28 @@ describe('PoolPlus', () => {
861861
const debugPool = new PoolPlus(Object.assign({plusOptions: {debug: true}}, config));
862862

863863
before(() => {
864-
sinon.stub(console, 'log');
865-
});
866-
867-
afterEach(() => {
868-
console.log.reset();
864+
sinon.stub(debugPool, 'query').yieldsAsync(null, []);
869865
});
870866

871867
after(done => {
872-
console.log.restore();
868+
debugPool.query.restore();
873869
debugPool.end(done);
874870
});
875871

876872
it('should log operations to the console when syncing', done => {
877-
debugPool.defineTable('pool_plus_test_table_debug', {
873+
sinon.stub(console, 'log');
874+
sinon.stub(Connection.prototype, 'query').yieldsAsync();
875+
876+
debugPool.defineTable('pool_plus_test_table_debug_a', {
878877
columns: {
879878
id: debugPool.ColTypes.int().unsigned().notNull().primaryKey(),
880879
},
881880
});
881+
debugPool.defineTable('pool_plus_test_table_debug_b', {
882+
columns: {
883+
id: debugPool.ColTypes.char(1),
884+
},
885+
});
882886

883887
debugPool.sync(err => {
884888
if (err) throw err;
@@ -889,20 +893,35 @@ describe('PoolPlus', () => {
889893
'',
890894
'type: CREATE_TABLE',
891895
'SQL:',
892-
'CREATE TABLE `pool_plus_test_table_debug` (',
896+
'CREATE TABLE `pool_plus_test_table_debug_a` (',
893897
' `id` int unsigned NOT NULL,',
894898
' PRIMARY KEY (`id`)',
895899
')',
896900
'',
901+
'type: CREATE_TABLE',
902+
'SQL:',
903+
'CREATE TABLE `pool_plus_test_table_debug_b` (',
904+
' `id` char(1)',
905+
')',
906+
'',
897907
'===================================================',
898908
'',
899909
].join('\n'));
900910

911+
console.log.restore();
912+
Connection.prototype.query.restore();
901913
done();
902914
});
903915
});
904916

905917
it('should log the operation that failed to the console when syncing', done => {
918+
const error = new Error('MOCK ALTER TABLE ERROR');
919+
920+
sinon.stub(console, 'log');
921+
sinon.stub(Connection.prototype, 'query').callsFake((sql, cb) => {
922+
process.nextTick(cb, sql.startsWith('ALTER') ? error : null);
923+
});
924+
906925
debugPool.defineTable('pool_plus_test_table_debug_error', {
907926
columns: {
908927
id: debugPool.ColTypes.int().unsigned().notNull().primaryKey(),
@@ -913,25 +932,23 @@ describe('PoolPlus', () => {
913932
});
914933

915934
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-
});
935+
err.should.equal(error);
921936

922937
console.log.should.have.been.calledWithExactly([
923938
'',
924939
'====== mysql-plus sync errored on operation: ======',
925940
'',
926941
'type: ADD_FOREIGN_KEY',
927942
'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`)',
943+
'ALTER TABLE `pool_plus_test_table_debug_error` ADD CONSTRAINT `fk_pool_plus_test_table_debug_error_id`',
944+
' FOREIGN KEY (`id`) REFERENCES `non_existent_table` (`id`)',
930945
'',
931946
'===================================================',
932947
'',
933948
].join('\n'));
934949

950+
console.log.restore();
951+
Connection.prototype.query.restore();
935952
done();
936953
});
937954
});

0 commit comments

Comments
 (0)