From 0ab2a8edfd71c89c74cc3d4b13ba7600a4093685 Mon Sep 17 00:00:00 2001 From: Parwat Kunwar Date: Thu, 28 Nov 2019 10:48:05 +0545 Subject: [PATCH 1/4] Added custom ObjectTypeError class to better log mismatched database object type error --- src/sqlRunner.ts | 6 +++++- src/util/ErrorHandler.ts | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 src/util/ErrorHandler.ts diff --git a/src/sqlRunner.ts b/src/sqlRunner.ts index 5d9e9e95..e7ffd189 100644 --- a/src/sqlRunner.ts +++ b/src/sqlRunner.ts @@ -8,6 +8,7 @@ import Mapping from './domain/Mapping'; import SqlCode from './domain/SqlCode'; import * as promise from './util/promise'; import SqlFileInfo from './domain/SqlFileInfo'; +import ObjectTypeError from './util/ErrorHandler'; import DatabaseObjectTypes from './enums/DatabaseObjectTypes'; /** @@ -94,7 +95,10 @@ export function extractSqlFileInfo(filePath: string): SqlFileInfo { export function getDropStatement(type: string, fqon: string): string { const dropStatement = dropStatementsMap[type]; - return dropStatement ? `${dropStatement} ${fqon}` : ''; + if (dropStatement) { return `${dropStatement} ${fqon}`; } + + const message = `Naming convention must be exact as the directory names inside src/`; + throw new ObjectTypeError(`No database object type: ${type}. ${message}`); } /** diff --git a/src/util/ErrorHandler.ts b/src/util/ErrorHandler.ts new file mode 100644 index 00000000..036de439 --- /dev/null +++ b/src/util/ErrorHandler.ts @@ -0,0 +1,17 @@ +/** + * Custom ObjectTypeError which extends Error class. + */ +class ObjectTypeError extends Error { + /** + * Constructor. + * + * @param {string} message + */ + constructor(message: string) { + super(message); + this.message = message; + this.name = 'ObjectTypeError'; + } +} + +export default ObjectTypeError; From 4362954d9efaeabbcb5ede18820c9486fd810c5a Mon Sep 17 00:00:00 2001 From: Parwat Kunwar Date: Thu, 28 Nov 2019 12:31:51 +0545 Subject: [PATCH 2/4] Enhancements in getDropStatement function --- src/sqlRunner.ts | 12 +++++++----- src/util/ErrorHandler.ts | 17 ----------------- 2 files changed, 7 insertions(+), 22 deletions(-) delete mode 100644 src/util/ErrorHandler.ts diff --git a/src/sqlRunner.ts b/src/sqlRunner.ts index e7ffd189..ddb49b97 100644 --- a/src/sqlRunner.ts +++ b/src/sqlRunner.ts @@ -1,5 +1,5 @@ import * as path from 'path'; -import { reverse } from 'ramda'; +import { reverse, keys } from 'ramda'; import * as fs from './util/fs'; import { dbLogger } from './logger'; @@ -8,7 +8,6 @@ import Mapping from './domain/Mapping'; import SqlCode from './domain/SqlCode'; import * as promise from './util/promise'; import SqlFileInfo from './domain/SqlFileInfo'; -import ObjectTypeError from './util/ErrorHandler'; import DatabaseObjectTypes from './enums/DatabaseObjectTypes'; /** @@ -95,10 +94,13 @@ export function extractSqlFileInfo(filePath: string): SqlFileInfo { export function getDropStatement(type: string, fqon: string): string { const dropStatement = dropStatementsMap[type]; - if (dropStatement) { return `${dropStatement} ${fqon}`; } + if (!dropStatement) { + const message = `Naming convention must be one of: ${keys(dropStatementsMap)}.`; - const message = `Naming convention must be exact as the directory names inside src/`; - throw new ObjectTypeError(`No database object type: ${type}. ${message}`); + throw new Error(`No database object type: ${type}. ${message}`); + } + + return `${dropStatement} ${fqon}`; } /** diff --git a/src/util/ErrorHandler.ts b/src/util/ErrorHandler.ts deleted file mode 100644 index 036de439..00000000 --- a/src/util/ErrorHandler.ts +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Custom ObjectTypeError which extends Error class. - */ -class ObjectTypeError extends Error { - /** - * Constructor. - * - * @param {string} message - */ - constructor(message: string) { - super(message); - this.message = message; - this.name = 'ObjectTypeError'; - } -} - -export default ObjectTypeError; From d67e43ba2792477d31d6a0c4e86445ae40afe13c Mon Sep 17 00:00:00 2001 From: Parwat Kunwar Date: Thu, 28 Nov 2019 12:35:16 +0545 Subject: [PATCH 3/4] Test case if the database object type is wrong --- test/sqlRunner.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/sqlRunner.test.ts b/test/sqlRunner.test.ts index 462e30b2..db7d1580 100644 --- a/test/sqlRunner.test.ts +++ b/test/sqlRunner.test.ts @@ -64,5 +64,9 @@ describe('UTIL: sqlRunner', () => { it('should return DROP statement for a view', () => { expect(sqlRunner.getDropStatement('view', 'test.hello_world')).to.equal('DROP VIEW IF EXISTS test.hello_world'); }); + + it('should throw an error if naming convention of view is wrong', () => { + expect(() => sqlRunner.getDropStatement('views', 'test.hello_world')).to.throw(Error); + }); }); }); From f7f915a4be4a7951744afc73e89cd39dc5225e62 Mon Sep 17 00:00:00 2001 From: Kabir Baidhya Date: Thu, 28 Nov 2019 14:37:42 +0545 Subject: [PATCH 4/4] Update sqlRunner.test.ts --- test/sqlRunner.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/sqlRunner.test.ts b/test/sqlRunner.test.ts index db7d1580..2d267c68 100644 --- a/test/sqlRunner.test.ts +++ b/test/sqlRunner.test.ts @@ -65,7 +65,7 @@ describe('UTIL: sqlRunner', () => { expect(sqlRunner.getDropStatement('view', 'test.hello_world')).to.equal('DROP VIEW IF EXISTS test.hello_world'); }); - it('should throw an error if naming convention of view is wrong', () => { + it('should throw an error if naming convention of the object is wrong', () => { expect(() => sqlRunner.getDropStatement('views', 'test.hello_world')).to.throw(Error); }); });