Skip to content

Commit

Permalink
fix(major): use mysql2 library if present
Browse files Browse the repository at this point in the history
fixes TryGhost/Toolbox#306

- this fixes an issue whereby newer versions of Ghost ship with mysql2
  and not mysql, but to maintain compatibility, the config uses `mysql`
- this means knex would attempt to use the `mysql` lib, but the library
  wouldn't exist
- this commit adds a check to see if `mysql2` exists and override the
  client we connect with if so
  • Loading branch information
daniellockyer committed Apr 21, 2022
1 parent ae8177b commit 098ca92
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/tasks/major-update/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,27 @@ module.exports = async function getData({dir, database, versionFolder, version})

const knexPath = path.resolve(dir, versionFolder, 'node_modules', 'knex');
const gscanPath = path.resolve(dir, versionFolder, 'node_modules', 'gscan');
const mysql2Path = path.resolve(dir, versionFolder, 'node_modules', 'mysql2');

const knex = require(knexPath);
const gscan = require(gscanPath);

try {
// If we're using MySQL, check to see if the `mysql2` lib exists because we
// need to change the `client` to maintain backwards compatibility with config
if (database.client === 'mysql') {
require(mysql2Path);
database.client = 'mysql2';
}
} catch (err) {
// `mysql2` doesn't exist, so we can stay with `mysql`
}

const connection = knex({...database, useNullAsDefault: true});
const query = async (sql) => {
const response = await connection.raw(sql);

if (database.client === 'mysql') {
if (['mysql', 'mysql2'].includes(database.client)) {
return response[0][0];
}

Expand Down

0 comments on commit 098ca92

Please sign in to comment.