From 810b052e012eaaef0745d86efd8bb407a70dcef9 Mon Sep 17 00:00:00 2001 From: Naz Date: Wed, 14 Jul 2021 18:23:01 +0400 Subject: [PATCH] Removed use of deprecated new Error() syntax refs https://github.com/TryGhost/Ghost/commit/2f1123d6ca7894fc21499739f46f0ae5c9eda73a refs https://github.com/TryGhost/Ghost/commit/6f1a3e17749e878a379830f12672b266c15bc835 - As per refed commits, we are removing deprecated use of `new Error()` in the codebase - Exposed few internal from commands module methods for easier testing, otherwise it was turning into neverending mocking show --- core/server/data/schema/commands.js | 24 +++++++++++++---- test/unit/data/schema/commands.test.js | 36 ++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 test/unit/data/schema/commands.test.js diff --git a/core/server/data/schema/commands.js b/core/server/data/schema/commands.js index 57efe46fd0..d9809d7008 100644 --- a/core/server/data/schema/commands.js +++ b/core/server/data/schema/commands.js @@ -2,10 +2,17 @@ const _ = require('lodash'); const Promise = require('bluebird'); const i18n = require('../../../shared/i18n'); const logging = require('@tryghost/logging'); +const errors = require('@tryghost/errors'); +const tpl = require('@tryghost/tpl'); const db = require('../db'); const schema = require('./schema'); const clients = require('./clients'); +const messages = { + hasPrimaryKeySQLiteError: 'Must use hasPrimaryKeySQLite on an SQLite3 database', + hasForeignSQLite3: 'Must use hasForeignSQLite3 on an SQLite3 database' +}; + function addTableColumn(tableName, table, columnName, columnSpec = schema[tableName][columnName]) { let column; @@ -132,7 +139,9 @@ async function hasForeignSQLite({fromTable, fromColumn, toTable, toColumn, trans const client = knex.client.config.client; if (client !== 'sqlite3') { - throw new Error('Must use hasForeignSQLite3 on an SQLite3 database'); + throw new errors.GhostError({ + message: tpl(messages.hasForeignSQLite3) + }); } const foreignKeys = await knex.raw(`PRAGMA foreign_key_list('${fromTable}');`); @@ -249,14 +258,16 @@ async function dropForeign({fromTable, fromColumn, toTable, toColumn, transactio * Checks if primary key index exists in a table over the given columns. * * @param {string} tableName - name of the table to check primary key constraint on - * @param {import('knex')} transaction - connnection object containing knex reference + * @param {import('knex')} transaction - connection object containing knex reference */ async function hasPrimaryKeySQLite(tableName, transaction) { const knex = (transaction || db.knex); const client = knex.client.config.client; if (client !== 'sqlite3') { - throw new Error('Must use hasPrimaryKeySQLite on an SQLite3 database'); + throw new errors.GhostError({ + message: tpl(messages.hasPrimaryKeySQLiteError) + }); } const rawConstraints = await knex.raw(`PRAGMA index_list('${tableName}');`); @@ -270,7 +281,7 @@ async function hasPrimaryKeySQLite(tableName, transaction) { * * @param {string} tableName - name of the table to add primaykey constraint to * @param {string|[string]} columns - column(s) to form primary key constraint with - * @param {import('knex')} transaction - connnection object containing knex reference + * @param {import('knex')} transaction - connection object containing knex reference */ async function addPrimaryKey(tableName, columns, transaction) { const isSQLite = db.knex.client.config.client === 'sqlite3'; @@ -406,5 +417,8 @@ module.exports = { addColumn: addColumn, dropColumn: dropColumn, getColumns: getColumns, - createColumnMigration + createColumnMigration, + // NOTE: below are exposed for testing purposes only + _hasForeignSQLite: hasForeignSQLite, + _hasPrimaryKeySQLite: hasPrimaryKeySQLite }; diff --git a/test/unit/data/schema/commands.test.js b/test/unit/data/schema/commands.test.js new file mode 100644 index 0000000000..b8762781bb --- /dev/null +++ b/test/unit/data/schema/commands.test.js @@ -0,0 +1,36 @@ +const should = require('should'); +const errors = require('@tryghost/errors'); + +const commands = require('../../../../core/server/data/schema/commands'); + +describe('schema commands', function () { + it('_hasForeignSQLite throws when knex is nox configured to use sqlite3', async function () { + const Knex = require('knex'); + const knex = Knex({ + client: 'mysql' + }); + + try { + await commands._hasForeignSQLite({transaction: knex}); + should.fail('addForeign did not throw'); + } catch (err) { + should.equal(err instanceof errors.GhostError, true); + err.message.should.equal('Must use hasForeignSQLite3 on an SQLite3 database'); + } + }); + + it('_hasPrimaryKeySQLite throws when knex is configured to use sqlite', async function () { + const Knex = require('knex'); + const knex = Knex({ + client: 'mysql' + }); + + try { + await commands._hasPrimaryKeySQLite(null, knex); + should.fail('hasPrimaryKeySQLite did not throw'); + } catch (err) { + should.equal(err instanceof errors.GhostError, true); + err.message.should.equal('Must use hasPrimaryKeySQLite on an SQLite3 database'); + } + }); +});