From ccae97945192c34b0839f896bd53e97a4b1048ec Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Wed, 25 Nov 2020 15:12:10 +0100 Subject: [PATCH] ESLint autofixes --- gulpfile.js | 2 +- src/errors/check-constraint-violation.js | 88 +++++++++--------- src/errors/enum.js | 72 +++++++-------- .../foreign-key-constraint-violation.js | 44 ++++----- src/errors/invalid-type.js | 76 ++++++++-------- src/errors/not-null-constraint-violation.js | 42 ++++----- src/errors/undefined-column.js | 70 +++++++-------- src/errors/unique-constraint-violation.js | 90 +++++++++---------- src/get-columns.js | 20 ++--- src/get-table.js | 12 +-- src/get-values.js | 12 +-- src/index.js | 6 +- .../check-constraint-violation.js | 20 ++--- .../composite-unique-constraint-violation.js | 24 ++--- test/create-tables/enum.js | 36 ++++---- .../foreign-key-constraint-violation.js | 44 ++++----- test/create-tables/index.js | 56 ++++++------ test/create-tables/invalid-type.js | 22 ++--- .../not-null-constraint-violation.js | 20 ++--- .../unique-constraint-violation.js | 20 ++--- test/test.js | 34 +++---- .../check-constraint-violation-insert.js | 14 +-- .../check-constraint-violation-update.js | 24 ++--- .../composite-unique-constraint-violation.js | 34 +++---- test/testcase/enum-insert.js | 14 +-- test/testcase/enum-update.js | 24 ++--- .../foreign-key-constraint-violation.js | 40 ++++----- test/testcase/invalid-type-boolean-inverse.js | 26 +++--- test/testcase/invalid-type-boolean.js | 26 +++--- test/testcase/invalid-type-integer.js | 26 +++--- .../testcase/not-null-constraint-violation.js | 16 ++-- test/testcase/undefined-column.js | 12 +-- test/testcase/unique-constraint-violation.js | 20 ++--- 33 files changed, 543 insertions(+), 543 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 1769501..97b9199 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,7 +1,7 @@ var gulp = require("gulp"); var presetES2015 = require("@joepie91/gulp-preset-es2015"); -var source = ["src/**/*.js"] +var source = ["src/**/*.js"]; gulp.task('babel', function() { return gulp.src(source) diff --git a/src/errors/check-constraint-violation.js b/src/errors/check-constraint-violation.js index bc0c69c..d886747 100644 --- a/src/errors/check-constraint-violation.js +++ b/src/errors/check-constraint-violation.js @@ -12,48 +12,48 @@ let CheckConstraintViolationError = create("CheckConstraintViolationError", { in let messageRegex = /^(.+) - new row for relation "([^"]+)" violates check constraint "([^"]+)"$/; module.exports = { - error: CheckConstraintViolationError, - errorName: "CheckConstraintViolationError", - check: function checkType(error) { - return ( - // PostgreSQL (via `pg`): - (error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "23514") - ) - }, - convert: function convertError(error) { - let messageMatch = messageRegex.exec(error.message); - - if (messageMatch == null) { - throw new Error("Encountered unknown error format"); - } - - let [_, query, table, constraint] = messageMatch; - - let columns = getColumns(query); - - let offendingColumn, message; - - if (columns != null) { - /* This is the naming convention that Knex uses for .enum() in PostgreSQL */ - offendingColumn = columns.find(column => { - return error.constraint === `${error.table}_${column}_check` - }); - - message = `Value violates the '${error.constraint}' constraint for the '${offendingColumn}' column in the '${error.table}' table`; - } else { - message = `Value violates the '${error.constraint}' constraint for the '${error.table}' table`; - } - - return new CheckConstraintViolationError(message, { - originalError: error, - pgCode: error.code, - code: pgErrorCodes[error.code], - query: query, - schema: error.schema, - table: error.table, - column: offendingColumn, - constraint: error.constraint, - values: getValues(error.detail) - }); - } + error: CheckConstraintViolationError, + errorName: "CheckConstraintViolationError", + check: function checkType(error) { + return ( + // PostgreSQL (via `pg`): + (error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "23514") + ); + }, + convert: function convertError(error) { + let messageMatch = messageRegex.exec(error.message); + + if (messageMatch == null) { + throw new Error("Encountered unknown error format"); + } + + let [_, query, table, constraint] = messageMatch; + + let columns = getColumns(query); + + let offendingColumn, message; + + if (columns != null) { + /* This is the naming convention that Knex uses for .enum() in PostgreSQL */ + offendingColumn = columns.find(column => { + return error.constraint === `${error.table}_${column}_check`; + }); + + message = `Value violates the '${error.constraint}' constraint for the '${offendingColumn}' column in the '${error.table}' table`; + } else { + message = `Value violates the '${error.constraint}' constraint for the '${error.table}' table`; + } + + return new CheckConstraintViolationError(message, { + originalError: error, + pgCode: error.code, + code: pgErrorCodes[error.code], + query: query, + schema: error.schema, + table: error.table, + column: offendingColumn, + constraint: error.constraint, + values: getValues(error.detail) + }); + } }; diff --git a/src/errors/enum.js b/src/errors/enum.js index 4596353..f26da28 100644 --- a/src/errors/enum.js +++ b/src/errors/enum.js @@ -11,40 +11,40 @@ let EnumError = create("EnumError", { inheritsFrom: DatabaseError }); let messageRegex = /^(.+) - invalid input value for enum ([^:]+): "([^"]+)"$/; module.exports = { - error: EnumError, - errorName: "EnumError", - check: function checkType(error) { - return ( - // PostgreSQL (via `pg`): - (error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "22P02" && error.message.includes("invalid input value for enum")) - ) - }, - convert: function convertError(error) { - let messageMatch = messageRegex.exec(error.message); - - if (messageMatch == null) { - throw new Error("Encountered unknown error format"); - } - - let [_, query, enumType, value] = messageMatch; - let table = getTable(query); - - let message; - - if (table != null) { - message = `Value '${value}' is not an allowed value for the ENUM type '${enumType}' (in table '${table}')`; - } else { - message = `Value '${value}' is not an allowed value for the ENUM type '${enumType}'`; - } - - return new EnumError(message, { - originalError: error, - pgCode: error.code, - code: pgErrorCodes[error.code], - query: query, - table: table, - enumType: enumType, - value: value - }); - } + error: EnumError, + errorName: "EnumError", + check: function checkType(error) { + return ( + // PostgreSQL (via `pg`): + (error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "22P02" && error.message.includes("invalid input value for enum")) + ); + }, + convert: function convertError(error) { + let messageMatch = messageRegex.exec(error.message); + + if (messageMatch == null) { + throw new Error("Encountered unknown error format"); + } + + let [_, query, enumType, value] = messageMatch; + let table = getTable(query); + + let message; + + if (table != null) { + message = `Value '${value}' is not an allowed value for the ENUM type '${enumType}' (in table '${table}')`; + } else { + message = `Value '${value}' is not an allowed value for the ENUM type '${enumType}'`; + } + + return new EnumError(message, { + originalError: error, + pgCode: error.code, + code: pgErrorCodes[error.code], + query: query, + table: table, + enumType: enumType, + value: value + }); + } }; diff --git a/src/errors/foreign-key-constraint-violation.js b/src/errors/foreign-key-constraint-violation.js index 3862569..3340678 100644 --- a/src/errors/foreign-key-constraint-violation.js +++ b/src/errors/foreign-key-constraint-violation.js @@ -9,27 +9,27 @@ let ForeignKeyConstraintViolationError = create("ForeignKeyConstraintViolationEr let detailsRegex = /^Key \(([^\)]+)\)=\(([^\)]+)\) is not present in table "([^"]+)"\.$/; module.exports = { - error: ForeignKeyConstraintViolationError, - errorName: "ForeignKeyConstraintViolationError", - check: function checkType(error) { - return ( - // PostgreSQL (via `pg`): - (error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "23503") - ) - }, - convert: function convertError(error) { - let [_, column, value, foreignTable] = detailsRegex.exec(error.detail); + error: ForeignKeyConstraintViolationError, + errorName: "ForeignKeyConstraintViolationError", + check: function checkType(error) { + return ( + // PostgreSQL (via `pg`): + (error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "23503") + ); + }, + convert: function convertError(error) { + let [_, column, value, foreignTable] = detailsRegex.exec(error.detail); - return new ForeignKeyConstraintViolationError(`Value for column '${column}' in table '${error.table}' refers to a non-existent key '${value}' in table '${foreignTable}'`, { - originalError: error, - pgCode: error.code, - code: pgErrorCodes[error.code], - schema: error.schema, - table: error.table, - foreignTable: foreignTable, - column: column, - value: value, - constraint: error.constraint - }); - } + return new ForeignKeyConstraintViolationError(`Value for column '${column}' in table '${error.table}' refers to a non-existent key '${value}' in table '${foreignTable}'`, { + originalError: error, + pgCode: error.code, + code: pgErrorCodes[error.code], + schema: error.schema, + table: error.table, + foreignTable: foreignTable, + column: column, + value: value, + constraint: error.constraint + }); + } }; diff --git a/src/errors/invalid-type.js b/src/errors/invalid-type.js index b1f2633..1fcd00e 100644 --- a/src/errors/invalid-type.js +++ b/src/errors/invalid-type.js @@ -12,42 +12,42 @@ let InvalidTypeError = create("InvalidTypeError", { inheritsFrom: DatabaseError let messageRegex = /^(?:(.+) - )?invalid input syntax for(?: type)? ([^:]+)(?:: "(.+)")?$/; module.exports = { - error: InvalidTypeError, - errorName: "InvalidTypeError", - check: function checkType(error) { - return ( - // PostgreSQL (via `pg`): - (error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "22P02" && error.message.includes("invalid input syntax for")) - ) - }, - convert: function convertError(error) { - let messageMatch = messageRegex.exec(error.message); - - if (messageMatch == null) { - /* TODO: Update other error parsing modules to display the original error message here as well. */ - throw new Error(`Encountered unknown error format for error message: ${error.message}`); - } - - let [_, query, expectedType, value] = messageMatch; - let table = getTable(query); - - let message; - - /* TODO: `value` can be undefined! */ - if (table != null) { - message = `Value <${value}> is of the wrong type for a '${expectedType}'-type column (in table '${table}')`; - } else { - message = `Value <${value}> is of the wrong type for a '${expectedType}'-type column`; - } - - return new InvalidTypeError(message, { - originalError: error, - pgCode: error.code, - code: pgErrorCodes[error.code], - query: query, - table: table, - expectedType: expectedType, - value: value, - }); - } + error: InvalidTypeError, + errorName: "InvalidTypeError", + check: function checkType(error) { + return ( + // PostgreSQL (via `pg`): + (error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "22P02" && error.message.includes("invalid input syntax for")) + ); + }, + convert: function convertError(error) { + let messageMatch = messageRegex.exec(error.message); + + if (messageMatch == null) { + /* TODO: Update other error parsing modules to display the original error message here as well. */ + throw new Error(`Encountered unknown error format for error message: ${error.message}`); + } + + let [_, query, expectedType, value] = messageMatch; + let table = getTable(query); + + let message; + + /* TODO: `value` can be undefined! */ + if (table != null) { + message = `Value <${value}> is of the wrong type for a '${expectedType}'-type column (in table '${table}')`; + } else { + message = `Value <${value}> is of the wrong type for a '${expectedType}'-type column`; + } + + return new InvalidTypeError(message, { + originalError: error, + pgCode: error.code, + code: pgErrorCodes[error.code], + query: query, + table: table, + expectedType: expectedType, + value: value, + }); + } }; diff --git a/src/errors/not-null-constraint-violation.js b/src/errors/not-null-constraint-violation.js index fdae567..cdfd6fb 100644 --- a/src/errors/not-null-constraint-violation.js +++ b/src/errors/not-null-constraint-violation.js @@ -11,26 +11,26 @@ let NotNullConstraintViolationError = create(DatabaseError, "NotNullConstraintVi let messageRegex = /^(.+) - null value in column "([^"]+)" violates not-null constraint$/; module.exports = { - error: NotNullConstraintViolationError, - errorName: "NotNullConstraintViolationError", - check: function checkType(error) { - return ( - // PostgreSQL (via `pg`): - (error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "23502") - ) - }, - convert: function convertError(error) { - let [_, query, column] = messageRegex.exec(error.message); + error: NotNullConstraintViolationError, + errorName: "NotNullConstraintViolationError", + check: function checkType(error) { + return ( + // PostgreSQL (via `pg`): + (error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "23502") + ); + }, + convert: function convertError(error) { + let [_, query, column] = messageRegex.exec(error.message); - return new NotNullConstraintViolationError(`Missing required value for column '${error.column}' in table '${error.table}'`, { - originalError: error, - pgCode: error.code, - code: pgErrorCodes[error.code], - schema: error.schema, - table: error.table, - column: error.column, - values: getValues(error.detail), - query: query - }); - } + return new NotNullConstraintViolationError(`Missing required value for column '${error.column}' in table '${error.table}'`, { + originalError: error, + pgCode: error.code, + code: pgErrorCodes[error.code], + schema: error.schema, + table: error.table, + column: error.column, + values: getValues(error.detail), + query: query + }); + } }; diff --git a/src/errors/undefined-column.js b/src/errors/undefined-column.js index d737c44..457ef2d 100644 --- a/src/errors/undefined-column.js +++ b/src/errors/undefined-column.js @@ -11,39 +11,39 @@ let UndefinedColumnError = create("UndefinedColumnError", { inheritsFrom: Databa let messageRegex = /^(.+) - column "([^"]+)" (?:of relation "([^"]+)" )?does not exist$/; module.exports = { - error: UndefinedColumnError, - errorName: "UndefinedColumnError", - check: function checkType(error) { - return ( - // PostgreSQL (via `pg`): - (error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "42703") - ) - }, - convert: function convertError(error) { - let messageMatch = messageRegex.exec(error.message); - - if (messageMatch == null) { - throw new Error("Encountered unknown error format"); - } - - let [_, query, column, table] = messageMatch; - - let errorMessage; - - if (table != null) { - errorMessage = `The '${column}' column does not exist in the '${table}' table`; - } else { - /* TODO: Maybe try to extract this from the query... somehow? */ - errorMessage = `The '${column}' column does not exist in (unknown table)`; - } - - return new UndefinedColumnError(errorMessage, { - originalError: error, - pgCode: error.code, - code: pgErrorCodes[error.code], - query: query, - table: table, - column: column, - }); - } + error: UndefinedColumnError, + errorName: "UndefinedColumnError", + check: function checkType(error) { + return ( + // PostgreSQL (via `pg`): + (error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "42703") + ); + }, + convert: function convertError(error) { + let messageMatch = messageRegex.exec(error.message); + + if (messageMatch == null) { + throw new Error("Encountered unknown error format"); + } + + let [_, query, column, table] = messageMatch; + + let errorMessage; + + if (table != null) { + errorMessage = `The '${column}' column does not exist in the '${table}' table`; + } else { + /* TODO: Maybe try to extract this from the query... somehow? */ + errorMessage = `The '${column}' column does not exist in (unknown table)`; + } + + return new UndefinedColumnError(errorMessage, { + originalError: error, + pgCode: error.code, + code: pgErrorCodes[error.code], + query: query, + table: table, + column: column, + }); + } }; diff --git a/src/errors/unique-constraint-violation.js b/src/errors/unique-constraint-violation.js index 09dd35f..992e78c 100644 --- a/src/errors/unique-constraint-violation.js +++ b/src/errors/unique-constraint-violation.js @@ -10,49 +10,49 @@ let UniqueConstraintViolationError = create("UniqueConstraintViolationError", { let detailsRegex = /Key \(([^\)]+)\)=\(([^\)]+)\) already exists\./; module.exports = { - error: UniqueConstraintViolationError, - errorName: "UniqueConstraintViolationError", - check: function checkType(error) { - return ( - // PostgreSQL (via `pg`): - (error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "23505") - ) - }, - convert: function convertError(error) { - let [_, columnValue, valueValue] = detailsRegex.exec(error.detail); - - let column, columns, value, values, messageColumn, messageValue, isComposite; - - if (columnValue.includes(",")) { - columns = splitValues(columnValue); - messageColumn = `columns [${columns.map(column => `'${column}'`).join(", ")}]`; - isComposite = true; - } else { - column = columnValue; - messageColumn = `column '${column}'`; - isComposite = false; - } - - if (valueValue.includes(",")) { - values = splitValues(valueValue); - messageValue = `Values [${values.map(value => `'${value}'`).join(", ")}] already exist`; - } else { - value = valueValue; - messageValue = `Value '${value}' already exists`; - } - - return new UniqueConstraintViolationError(`${messageValue} for ${messageColumn} in table '${error.table}'`, { - originalError: error, - pgCode: error.code, - code: pgErrorCodes[error.code], - schema: error.schema, - table: error.table, - column: column, - columns: columns, - value: value, - values: values, - isComposite: isComposite, - constraint: error.constraint - }); - } + error: UniqueConstraintViolationError, + errorName: "UniqueConstraintViolationError", + check: function checkType(error) { + return ( + // PostgreSQL (via `pg`): + (error.length != null && error.file != null && error.line != null && error.routine != null && error.code === "23505") + ); + }, + convert: function convertError(error) { + let [_, columnValue, valueValue] = detailsRegex.exec(error.detail); + + let column, columns, value, values, messageColumn, messageValue, isComposite; + + if (columnValue.includes(",")) { + columns = splitValues(columnValue); + messageColumn = `columns [${columns.map(column => `'${column}'`).join(", ")}]`; + isComposite = true; + } else { + column = columnValue; + messageColumn = `column '${column}'`; + isComposite = false; + } + + if (valueValue.includes(",")) { + values = splitValues(valueValue); + messageValue = `Values [${values.map(value => `'${value}'`).join(", ")}] already exist`; + } else { + value = valueValue; + messageValue = `Value '${value}' already exists`; + } + + return new UniqueConstraintViolationError(`${messageValue} for ${messageColumn} in table '${error.table}'`, { + originalError: error, + pgCode: error.code, + code: pgErrorCodes[error.code], + schema: error.schema, + table: error.table, + column: column, + columns: columns, + value: value, + values: values, + isComposite: isComposite, + constraint: error.constraint + }); + } }; diff --git a/src/get-columns.js b/src/get-columns.js index a72d895..0b4678c 100644 --- a/src/get-columns.js +++ b/src/get-columns.js @@ -5,15 +5,15 @@ let updateRegex = /^update "[^"]+" set (.+) where/; let updateColumnRegex = /^"([^"]+)"/; module.exports = function getColumns(query) { - let match, columns; + let match, columns; - if (match = insertRegex.exec(query)) { - return match[1].split(",").map((columnName) => { - return columnName.trim().slice(1, -1) - }); - } else if (match = updateRegex.exec(query)) { - return match[1].split(",").map((statement) => { - return updateColumnRegex.exec(statement.trim())[1]; - }); - } + if (match = insertRegex.exec(query)) { + return match[1].split(",").map((columnName) => { + return columnName.trim().slice(1, -1); + }); + } else if (match = updateRegex.exec(query)) { + return match[1].split(",").map((statement) => { + return updateColumnRegex.exec(statement.trim())[1]; + }); + } }; diff --git a/src/get-table.js b/src/get-table.js index 3db3970..9cff5f2 100644 --- a/src/get-table.js +++ b/src/get-table.js @@ -4,11 +4,11 @@ let insertRegex = /^insert into "([^"]+)"/; let updateRegex = /^update "([^"]+)"/; module.exports = function getTable(query) { - let match; + let match; - if (match = insertRegex.exec(query)) { - return match[1]; - } else if (match = updateRegex.exec(query)) { - return match[1]; - } + if (match = insertRegex.exec(query)) { + return match[1]; + } else if (match = updateRegex.exec(query)) { + return match[1]; + } }; diff --git a/src/get-values.js b/src/get-values.js index 275903d..9a58813 100644 --- a/src/get-values.js +++ b/src/get-values.js @@ -3,13 +3,13 @@ let detailsRegex = /^Failing row contains \((.+)\).$/; module.exports = function getValues(detail) { - let detailsMatch = detailsRegex.exec(detail); + let detailsMatch = detailsRegex.exec(detail); - if (detailsMatch == null) { - throw new Error("Could not determine values for query"); - } + if (detailsMatch == null) { + throw new Error("Could not determine values for query"); + } - let [_, valueList] = detailsMatch; + let [_, valueList] = detailsMatch; - return valueList.split(", "); + return valueList.split(", "); }; diff --git a/src/index.js b/src/index.js index 43c0a49..a871ebf 100644 --- a/src/index.js +++ b/src/index.js @@ -15,13 +15,13 @@ let handlers = [ require("./errors/invalid-type"), require("./errors/undefined-column"), require("./errors/not-null-constraint-violation"), -] +]; function convertError(error) { let handler = handlers.find(handler => handler.check(error)); if (handler != null) { - debug(`Converting error with message: ${error.message}`) + debug(`Converting error with message: ${error.message}`); return handler.convert(error); } else { throw new UnknownError("The specified error is not of a recognized type"); @@ -51,4 +51,4 @@ module.exports = Object.assign({ rethrow: rethrowBetterError, UnknownError: UnknownError, DatabaseError: DatabaseError -}, errorTypes) +}, errorTypes); diff --git a/test/create-tables/check-constraint-violation.js b/test/create-tables/check-constraint-violation.js index 406b4d3..24d7809 100644 --- a/test/create-tables/check-constraint-violation.js +++ b/test/create-tables/check-constraint-violation.js @@ -3,14 +3,14 @@ const Promise = require("bluebird"); module.exports = { - up: function createCheckConstraintViolationTable(knex, errorHandler) { - return knex.schema.createTable("check_constraint_violation", (table) => { - table.increments("id"); - table.enum("number_value", ["one", "two", "three"]); - table.text("name"); - }).catch(errorHandler); - }, - down: function dropCheckConstraintViolationTable(knex, errorHandler) { - return knex.schema.dropTable("check_constraint_violation").catch(errorHandler); - } + up: function createCheckConstraintViolationTable(knex, errorHandler) { + return knex.schema.createTable("check_constraint_violation", (table) => { + table.increments("id"); + table.enum("number_value", ["one", "two", "three"]); + table.text("name"); + }).catch(errorHandler); + }, + down: function dropCheckConstraintViolationTable(knex, errorHandler) { + return knex.schema.dropTable("check_constraint_violation").catch(errorHandler); + } }; diff --git a/test/create-tables/composite-unique-constraint-violation.js b/test/create-tables/composite-unique-constraint-violation.js index fcb06bd..e1021c2 100644 --- a/test/create-tables/composite-unique-constraint-violation.js +++ b/test/create-tables/composite-unique-constraint-violation.js @@ -1,16 +1,16 @@ 'use strict'; module.exports = { - up: function createUniqueConstraintViolationTable(knex, errorHandler) { - return knex.schema.createTable("composite_unique_constraint_violation", (table) => { - table.increments("id"); - table.text("email"); - table.text("username"); - table.text("name"); - table.unique(["email", "username"]); - }).catch(errorHandler); - }, - down: function dropUniqueConstraintViolationTable(knex, errorHandler) { - return knex.schema.dropTable("composite_unique_constraint_violation").catch(errorHandler); - } + up: function createUniqueConstraintViolationTable(knex, errorHandler) { + return knex.schema.createTable("composite_unique_constraint_violation", (table) => { + table.increments("id"); + table.text("email"); + table.text("username"); + table.text("name"); + table.unique(["email", "username"]); + }).catch(errorHandler); + }, + down: function dropUniqueConstraintViolationTable(knex, errorHandler) { + return knex.schema.dropTable("composite_unique_constraint_violation").catch(errorHandler); + } }; diff --git a/test/create-tables/enum.js b/test/create-tables/enum.js index c106d40..bd96f9a 100644 --- a/test/create-tables/enum.js +++ b/test/create-tables/enum.js @@ -3,22 +3,22 @@ const Promise = require("bluebird"); module.exports = { - up: function createEnumViolationTable(knex, errorHandler) { - return Promise.try(() => { - return knex.raw("CREATE TYPE number AS ENUM ('one', 'two', 'three')").catch(errorHandler); - }).then(() => { - return knex.schema.createTable("enum_violation", (table) => { - table.increments("id"); - table.specificType("number_value", "number"); - table.text("name"); - }).catch(errorHandler); - }); - }, - down: function dropEnumViolationTable(knex, errorHandler) { - return Promise.try(() => { - return knex.schema.dropTable("enum_violation").catch(errorHandler); - }).then(() => { - return knex.raw("DROP TYPE number").catch(errorHandler); - }); - } + up: function createEnumViolationTable(knex, errorHandler) { + return Promise.try(() => { + return knex.raw("CREATE TYPE number AS ENUM ('one', 'two', 'three')").catch(errorHandler); + }).then(() => { + return knex.schema.createTable("enum_violation", (table) => { + table.increments("id"); + table.specificType("number_value", "number"); + table.text("name"); + }).catch(errorHandler); + }); + }, + down: function dropEnumViolationTable(knex, errorHandler) { + return Promise.try(() => { + return knex.schema.dropTable("enum_violation").catch(errorHandler); + }).then(() => { + return knex.raw("DROP TYPE number").catch(errorHandler); + }); + } }; diff --git a/test/create-tables/foreign-key-constraint-violation.js b/test/create-tables/foreign-key-constraint-violation.js index 1a5f049..5d95de6 100644 --- a/test/create-tables/foreign-key-constraint-violation.js +++ b/test/create-tables/foreign-key-constraint-violation.js @@ -3,26 +3,26 @@ const Promise = require("bluebird"); module.exports = { - up: function createForeignKeyConstraintViolationTable(knex, errorHandler) { - return Promise.try(() => { - return knex.schema.createTable("foreign_key_constraint_users", (table) => { - table.increments("id"); - table.text("username"); - table.text("name"); - }).catch(errorHandler); - }).then(() => { - return knex.schema.createTable("foreign_key_constraint_posts", (table) => { - table.increments("id"); - table.integer("user_id").references("id").inTable("foreign_key_constraint_users"); - table.text("body"); - }).catch(errorHandler); - }); - }, - down: function dropForeignKeyConstraintViolationTable(knex, errorHandler) { - return Promise.try(() => { - return knex.schema.dropTable("foreign_key_constraint_posts").catch(errorHandler); - }).then(() => { - return knex.schema.dropTable("foreign_key_constraint_users").catch(errorHandler); - }); - } + up: function createForeignKeyConstraintViolationTable(knex, errorHandler) { + return Promise.try(() => { + return knex.schema.createTable("foreign_key_constraint_users", (table) => { + table.increments("id"); + table.text("username"); + table.text("name"); + }).catch(errorHandler); + }).then(() => { + return knex.schema.createTable("foreign_key_constraint_posts", (table) => { + table.increments("id"); + table.integer("user_id").references("id").inTable("foreign_key_constraint_users"); + table.text("body"); + }).catch(errorHandler); + }); + }, + down: function dropForeignKeyConstraintViolationTable(knex, errorHandler) { + return Promise.try(() => { + return knex.schema.dropTable("foreign_key_constraint_posts").catch(errorHandler); + }).then(() => { + return knex.schema.dropTable("foreign_key_constraint_users").catch(errorHandler); + }); + } }; diff --git a/test/create-tables/index.js b/test/create-tables/index.js index e109b1b..2134ba9 100644 --- a/test/create-tables/index.js +++ b/test/create-tables/index.js @@ -3,40 +3,40 @@ const Promise = require("bluebird"); let tables = [ - require("./unique-constraint-violation"), - require("./composite-unique-constraint-violation"), - require("./check-constraint-violation"), - require("./foreign-key-constraint-violation"), - require("./enum"), - require("./invalid-type"), - require("./not-null-constraint-violation") + require("./unique-constraint-violation"), + require("./composite-unique-constraint-violation"), + require("./check-constraint-violation"), + require("./foreign-key-constraint-violation"), + require("./enum"), + require("./invalid-type"), + require("./not-null-constraint-violation") ]; let noop = function noop(err) { - // Do nothing. -} + // Do nothing. +}; let rethrow = function rethrowError(err) { - throw err; -} + throw err; +}; module.exports = { - up: function createTables(knex) { - return Promise.map(tables, (table) => { - return table.up(knex, rethrow); - }); - }, - down: function dropTables(knex, ignoreErrors) { - let errorHandler; + up: function createTables(knex) { + return Promise.map(tables, (table) => { + return table.up(knex, rethrow); + }); + }, + down: function dropTables(knex, ignoreErrors) { + let errorHandler; - if (ignoreErrors) { - errorHandler = noop; - } else { - errorHandler = rethrow; - } + if (ignoreErrors) { + errorHandler = noop; + } else { + errorHandler = rethrow; + } - return Promise.map(tables, (table) => { - return table.down(knex, errorHandler); - }); - } -} + return Promise.map(tables, (table) => { + return table.down(knex, errorHandler); + }); + } +}; diff --git a/test/create-tables/invalid-type.js b/test/create-tables/invalid-type.js index fc42448..63fffd1 100644 --- a/test/create-tables/invalid-type.js +++ b/test/create-tables/invalid-type.js @@ -1,15 +1,15 @@ 'use strict'; module.exports = { - up: function createInvalidTypeTable(knex, errorHandler) { - return knex.schema.createTable("invalid_type", (table) => { - table.increments("id"); - table.integer("age"); - table.text("name"); - table.boolean("active"); - }).catch(errorHandler); - }, - down: function dropInvalidTypeTable(knex, errorHandler) { - return knex.schema.dropTable("invalid_type").catch(errorHandler); - } + up: function createInvalidTypeTable(knex, errorHandler) { + return knex.schema.createTable("invalid_type", (table) => { + table.increments("id"); + table.integer("age"); + table.text("name"); + table.boolean("active"); + }).catch(errorHandler); + }, + down: function dropInvalidTypeTable(knex, errorHandler) { + return knex.schema.dropTable("invalid_type").catch(errorHandler); + } }; diff --git a/test/create-tables/not-null-constraint-violation.js b/test/create-tables/not-null-constraint-violation.js index 3f4fecf..789b270 100644 --- a/test/create-tables/not-null-constraint-violation.js +++ b/test/create-tables/not-null-constraint-violation.js @@ -1,14 +1,14 @@ 'use strict'; module.exports = { - up: function createNotNullConstraintViolationTable(knex, errorHandler) { - return knex.schema.createTable("not_null_violation", (table) => { - table.increments("id"); - table.text("email").notNull(); - table.text("name"); - }).catch(errorHandler); - }, - down: function dropNotNullConstraintViolationTable(knex, errorHandler) { - return knex.schema.dropTable("not_null_violation").catch(errorHandler); - } + up: function createNotNullConstraintViolationTable(knex, errorHandler) { + return knex.schema.createTable("not_null_violation", (table) => { + table.increments("id"); + table.text("email").notNull(); + table.text("name"); + }).catch(errorHandler); + }, + down: function dropNotNullConstraintViolationTable(knex, errorHandler) { + return knex.schema.dropTable("not_null_violation").catch(errorHandler); + } }; diff --git a/test/create-tables/unique-constraint-violation.js b/test/create-tables/unique-constraint-violation.js index b94aa6a..e9a81c6 100644 --- a/test/create-tables/unique-constraint-violation.js +++ b/test/create-tables/unique-constraint-violation.js @@ -1,14 +1,14 @@ 'use strict'; module.exports = { - up: function createUniqueConstraintViolationTable(knex, errorHandler) { - return knex.schema.createTable("unique_constraint_violation", (table) => { - table.increments("id"); - table.text("email").unique(); - table.text("name"); - }).catch(errorHandler); - }, - down: function dropUniqueConstraintViolationTable(knex, errorHandler) { - return knex.schema.dropTable("unique_constraint_violation").catch(errorHandler); - } + up: function createUniqueConstraintViolationTable(knex, errorHandler) { + return knex.schema.createTable("unique_constraint_violation", (table) => { + table.increments("id"); + table.text("email").unique(); + table.text("name"); + }).catch(errorHandler); + }, + down: function dropUniqueConstraintViolationTable(knex, errorHandler) { + return knex.schema.dropTable("unique_constraint_violation").catch(errorHandler); + } }; diff --git a/test/test.js b/test/test.js index 9412b5a..7156f45 100644 --- a/test/test.js +++ b/test/test.js @@ -10,29 +10,29 @@ const path = require("path"); const createTables = require("./create-tables"); let db = knex({ - client: "pg", - connection: { - host: "localhost", - user: "sven", - password: "password", - database: "database_error" - }, - debug: true + client: "pg", + connection: { + host: "localhost", + user: "sven", + password: "password", + database: "database_error" + }, + debug: true }); return Promise.try(() => { - return createTables.down(db, true); + return createTables.down(db, true); }).then(() => { - return createTables.up(db); + return createTables.up(db); }).then(() => { - let testcase = require(path.join(process.cwd(), process.argv[2])); - return testcase(db); + let testcase = require(path.join(process.cwd(), process.argv[2])); + return testcase(db); }).catch(databaseError.rethrow).catch((err) => { - console.log("__________________\n"); - console.error(util.inspect(err, {colors: true, depth: 1})); - console.log("__________________\n"); + console.log("__________________\n"); + console.error(util.inspect(err, {colors: true, depth: 1})); + console.log("__________________\n"); }).finally(() => { - return createTables.down(db); + return createTables.down(db); }).finally(() => { - return db.destroy(); + return db.destroy(); }); diff --git a/test/testcase/check-constraint-violation-insert.js b/test/testcase/check-constraint-violation-insert.js index 03dc78b..2756f4c 100644 --- a/test/testcase/check-constraint-violation-insert.js +++ b/test/testcase/check-constraint-violation-insert.js @@ -1,11 +1,11 @@ 'use strict'; module.exports = function attemptCheckConstraintViolation(knex) { - return knex("check_constraint_violation").insert([{ - number_value: "one", - name: "Joe" - }, { - number_value: "four", - name: "Jane" - }]).returning("*"); + return knex("check_constraint_violation").insert([{ + number_value: "one", + name: "Joe" + }, { + number_value: "four", + name: "Jane" + }]).returning("*"); }; diff --git a/test/testcase/check-constraint-violation-update.js b/test/testcase/check-constraint-violation-update.js index cdb53a9..cb9f136 100644 --- a/test/testcase/check-constraint-violation-update.js +++ b/test/testcase/check-constraint-violation-update.js @@ -3,16 +3,16 @@ const Promise = require("bluebird"); module.exports = function attemptCheckConstraintViolation(knex) { - return Promise.try(() => { - return knex("check_constraint_violation").insert({ - number_value: "one", - name: "Joe" - }).returning("id"); - }).then((id) => { - return knex("check_constraint_violation").update({ - number_value: "four" - }).where({ - id: id[0] - }); - }); + return Promise.try(() => { + return knex("check_constraint_violation").insert({ + number_value: "one", + name: "Joe" + }).returning("id"); + }).then((id) => { + return knex("check_constraint_violation").update({ + number_value: "four" + }).where({ + id: id[0] + }); + }); }; diff --git a/test/testcase/composite-unique-constraint-violation.js b/test/testcase/composite-unique-constraint-violation.js index 06df69a..bb5698a 100644 --- a/test/testcase/composite-unique-constraint-violation.js +++ b/test/testcase/composite-unique-constraint-violation.js @@ -1,21 +1,21 @@ 'use strict'; module.exports = function attemptCompositeUniqueConstraintViolation(knex) { - return knex("composite_unique_constraint_violation").insert([{ - email: "foo@bar.com", - username: "foo", - name: "Joe" - }, { - email: "baz@qux.com", - username: "bar", - name: "Jane" - }, { - email: "foo@bar.com", - username: "baz", - name: "Pete" - }, { - email: "foo@bar.com", - username: "foo", - name: "Jill" - }]).returning("*"); + return knex("composite_unique_constraint_violation").insert([{ + email: "foo@bar.com", + username: "foo", + name: "Joe" + }, { + email: "baz@qux.com", + username: "bar", + name: "Jane" + }, { + email: "foo@bar.com", + username: "baz", + name: "Pete" + }, { + email: "foo@bar.com", + username: "foo", + name: "Jill" + }]).returning("*"); }; diff --git a/test/testcase/enum-insert.js b/test/testcase/enum-insert.js index 5097da6..57828b7 100644 --- a/test/testcase/enum-insert.js +++ b/test/testcase/enum-insert.js @@ -1,11 +1,11 @@ 'use strict'; module.exports = function attemptEnumViolation(knex) { - return knex("enum_violation").insert([{ - number_value: "one", - name: "Joe" - }, { - number_value: "four", - name: "Jane" - }]).returning("*"); + return knex("enum_violation").insert([{ + number_value: "one", + name: "Joe" + }, { + number_value: "four", + name: "Jane" + }]).returning("*"); }; diff --git a/test/testcase/enum-update.js b/test/testcase/enum-update.js index 36dfeba..2e03447 100644 --- a/test/testcase/enum-update.js +++ b/test/testcase/enum-update.js @@ -3,16 +3,16 @@ const Promise = require("bluebird"); module.exports = function attemptEnumViolation(knex) { - return Promise.try(() => { - return knex("enum_violation").insert({ - number_value: "one", - name: "Joe" - }).returning("id"); - }).then((id) => { - return knex("enum_violation").update({ - number_value: "four" - }).where({ - id: id[0] - }); - }); + return Promise.try(() => { + return knex("enum_violation").insert({ + number_value: "one", + name: "Joe" + }).returning("id"); + }).then((id) => { + return knex("enum_violation").update({ + number_value: "four" + }).where({ + id: id[0] + }); + }); }; diff --git a/test/testcase/foreign-key-constraint-violation.js b/test/testcase/foreign-key-constraint-violation.js index 34c4c17..530d9fa 100644 --- a/test/testcase/foreign-key-constraint-violation.js +++ b/test/testcase/foreign-key-constraint-violation.js @@ -3,24 +3,24 @@ const Promise = require("bluebird"); module.exports = function attemptForeignKeyConstraintViolation(knex) { - return Promise.try(() => { - return knex("foreign_key_constraint_users").insert([{ - name: "Joe", - username: "joe" - }, { - name: "Jane", - username: "jane" - }]).returning("id"); - }).then((ids) => { - return knex("foreign_key_constraint_posts").insert([{ - user_id: ids[0], - body: "Foo" - }, { - user_id: ids[1], - body: "Bar" - }, { - user_id: 567213, - body: "Baz" - }]) - }); + return Promise.try(() => { + return knex("foreign_key_constraint_users").insert([{ + name: "Joe", + username: "joe" + }, { + name: "Jane", + username: "jane" + }]).returning("id"); + }).then((ids) => { + return knex("foreign_key_constraint_posts").insert([{ + user_id: ids[0], + body: "Foo" + }, { + user_id: ids[1], + body: "Bar" + }, { + user_id: 567213, + body: "Baz" + }]); + }); }; diff --git a/test/testcase/invalid-type-boolean-inverse.js b/test/testcase/invalid-type-boolean-inverse.js index 8928226..8e87f6d 100644 --- a/test/testcase/invalid-type-boolean-inverse.js +++ b/test/testcase/invalid-type-boolean-inverse.js @@ -1,17 +1,17 @@ 'use strict'; module.exports = function attemptInvalidTypeBooleanInverse(knex) { - return knex("invalid_type").insert([{ - name: "Joe", - age: 29, - active: true - }, { - name: "Jane", - age: 42, - active: true - }, { - name: true, - age: 24, - active: false - }]).returning("*"); + return knex("invalid_type").insert([{ + name: "Joe", + age: 29, + active: true + }, { + name: "Jane", + age: 42, + active: true + }, { + name: true, + age: 24, + active: false + }]).returning("*"); }; diff --git a/test/testcase/invalid-type-boolean.js b/test/testcase/invalid-type-boolean.js index 9d56849..aa92af7 100644 --- a/test/testcase/invalid-type-boolean.js +++ b/test/testcase/invalid-type-boolean.js @@ -1,17 +1,17 @@ 'use strict'; module.exports = function attemptInvalidTypeBoolean(knex) { - return knex("invalid_type").insert([{ - name: "Joe", - age: 29, - active: true - }, { - name: "Jane", - age: 42, - active: true - }, { - name: "Pete", - age: 24, - active: "foo bar" - }]).returning("*"); + return knex("invalid_type").insert([{ + name: "Joe", + age: 29, + active: true + }, { + name: "Jane", + age: 42, + active: true + }, { + name: "Pete", + age: 24, + active: "foo bar" + }]).returning("*"); }; diff --git a/test/testcase/invalid-type-integer.js b/test/testcase/invalid-type-integer.js index ff6346f..d01571d 100644 --- a/test/testcase/invalid-type-integer.js +++ b/test/testcase/invalid-type-integer.js @@ -1,17 +1,17 @@ 'use strict'; module.exports = function attemptInvalidTypeInteger(knex) { - return knex("invalid_type").insert([{ - name: "Joe", - age: 29, - active: true - }, { - name: "Jane", - age: 42, - active: true - }, { - name: "Pete", - age: "twenty-four", - active: false - }]).returning("*"); + return knex("invalid_type").insert([{ + name: "Joe", + age: 29, + active: true + }, { + name: "Jane", + age: 42, + active: true + }, { + name: "Pete", + age: "twenty-four", + active: false + }]).returning("*"); }; diff --git a/test/testcase/not-null-constraint-violation.js b/test/testcase/not-null-constraint-violation.js index 6e12706..5977b4c 100644 --- a/test/testcase/not-null-constraint-violation.js +++ b/test/testcase/not-null-constraint-violation.js @@ -1,12 +1,12 @@ 'use strict'; module.exports = function attemptNotNullConstraintViolation(knex) { - return knex("not_null_violation").insert([{ - email: "foo@bar.com", - name: "Joe" - }, { - email: "baz@qux.com" - }, { - name: "Pete" - }]).returning("*"); + return knex("not_null_violation").insert([{ + email: "foo@bar.com", + name: "Joe" + }, { + email: "baz@qux.com" + }, { + name: "Pete" + }]).returning("*"); }; diff --git a/test/testcase/undefined-column.js b/test/testcase/undefined-column.js index d938d1c..b00e8d3 100644 --- a/test/testcase/undefined-column.js +++ b/test/testcase/undefined-column.js @@ -1,10 +1,10 @@ 'use strict'; module.exports = function attemptUndefinedColumn(knex) { - return knex("invalid_type").insert([{ - name: "Joe", - age: 29, - active: true, - nonexistentColumn: "Hello!" - }]).returning("*"); + return knex("invalid_type").insert([{ + name: "Joe", + age: 29, + active: true, + nonexistentColumn: "Hello!" + }]).returning("*"); }; diff --git a/test/testcase/unique-constraint-violation.js b/test/testcase/unique-constraint-violation.js index a86a6a9..9724669 100644 --- a/test/testcase/unique-constraint-violation.js +++ b/test/testcase/unique-constraint-violation.js @@ -1,14 +1,14 @@ 'use strict'; module.exports = function attemptUniqueConstraintViolation(knex) { - return knex("unique_constraint_violation").insert([{ - email: "foo@bar.com", - name: "Joe" - }, { - email: "baz@qux.com", - name: "Jane" - }, { - email: "foo@bar.com", - name: "Pete" - }]).returning("*"); + return knex("unique_constraint_violation").insert([{ + email: "foo@bar.com", + name: "Joe" + }, { + email: "baz@qux.com", + name: "Jane" + }, { + email: "foo@bar.com", + name: "Pete" + }]).returning("*"); };