mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-19 08:31:43 +03:00
bf6f607f42
refs https://github.com/TryGhost/Toolbox/issues/174 - this commit switches Ghost from using the `mysql` library to the `mysql2` one - we've done this for several reasons: - `mysql2` is more actively maintained - `mysql2` natively supports the default auth plugin on MySQL 8 - `mysql2` is fasterrrr - there have been various other commits refactoring the groundwork for this commit but this commit should be short and sweet: - alias `mysql` to `mysql2` client so we maintain backwards compatibility with all configs who use `"client": "mysql"` - enabled `decimalNumbers` so we maintain the same functionality as `mysql` - replaced the dependencies and updated `knex-migrator` - hardcoded the newer authentication plugin in MySQL 8 CI. Before switching to `mysql2`, this would break because it didn't support this
62 lines
2.3 KiB
JavaScript
62 lines
2.3 KiB
JavaScript
const knex = require('knex');
|
|
const config = require('../../../shared/config');
|
|
let knexInstance;
|
|
|
|
// @TODO:
|
|
// - if you require this file before config file was loaded,
|
|
// - then this file is cached and you have no chance to connect to the db anymore
|
|
// - bring dynamic into this file (db.connect())
|
|
function configure(dbConfig) {
|
|
const client = dbConfig.client;
|
|
|
|
if (client === 'sqlite3') {
|
|
// Backwards compatibility with old knex behaviour
|
|
dbConfig.useNullAsDefault = Object.prototype.hasOwnProperty.call(dbConfig, 'useNullAsDefault') ? dbConfig.useNullAsDefault : true;
|
|
|
|
// Enables foreign key checks and delete on cascade
|
|
dbConfig.pool = {
|
|
afterCreate(conn, cb) {
|
|
conn.run('PRAGMA foreign_keys = ON', cb);
|
|
|
|
// These two are meant to improve performance at the cost of reliability
|
|
// Should be safe for tests. We add them here and leave them on
|
|
if (config.get('env').startsWith('testing')) {
|
|
conn.run('PRAGMA synchronous = OFF;');
|
|
conn.run('PRAGMA journal_mode = TRUNCATE;');
|
|
}
|
|
}
|
|
};
|
|
|
|
// Force bthreads to use child_process backend until a worker_thread-compatible version of sqlite3 is published
|
|
// https://github.com/mapbox/node-sqlite3/issues/1386
|
|
process.env.BTHREADS_BACKEND = 'child_process';
|
|
}
|
|
|
|
if (client === 'mysql2') {
|
|
dbConfig.connection.timezone = 'Z';
|
|
dbConfig.connection.charset = 'utf8mb4';
|
|
dbConfig.connection.decimalNumbers = true;
|
|
|
|
// NOTE: disabled so that worker processes can use the db without
|
|
// requiring logging and causing file desriptor leaks.
|
|
// See https://github.com/TryGhost/Ghost/issues/12496
|
|
//
|
|
// const logging = require('@tryghost/logging');
|
|
// const errors = require('@tryghost/errors');
|
|
// dbConfig.connection.loggingHook = function loggingHook(err) {
|
|
// logging.error(new errors.InternalServerError({
|
|
// code: 'MYSQL_LOGGING_HOOK',
|
|
// err: err
|
|
// }));
|
|
// };
|
|
}
|
|
|
|
return dbConfig;
|
|
}
|
|
|
|
if (!knexInstance && config.get('database') && config.get('database').client) {
|
|
knexInstance = knex(configure(config.get('database')));
|
|
}
|
|
|
|
module.exports = knexInstance;
|