replace mysql module with mysql2
mysql is deprecated since years now
This commit is contained in:
@@ -13,12 +13,11 @@ exports = module.exports = {
|
||||
};
|
||||
|
||||
const assert = require('assert'),
|
||||
async = require('async'),
|
||||
BoxError = require('./boxerror.js'),
|
||||
constants = require('./constants.js'),
|
||||
debug = require('debug')('box:database'),
|
||||
execSync = require('child_process').execSync,
|
||||
mysql = require('mysql'),
|
||||
mysql = require('mysql2/promise'),
|
||||
safe = require('safetydance'),
|
||||
shell = require('./shell.js')('database');
|
||||
|
||||
@@ -43,7 +42,6 @@ async function initialize() {
|
||||
// https://github.com/mysqljs/mysql#pool-options
|
||||
gConnectionPool = mysql.createPool({
|
||||
connectionLimit: 5,
|
||||
acquireTimeout: 60000,
|
||||
connectTimeout: 60000,
|
||||
host: gDatabase.hostname,
|
||||
user: gDatabase.username,
|
||||
@@ -54,30 +52,34 @@ async function initialize() {
|
||||
waitForConnections: true, // getConnection() will wait until a connection is avaiable
|
||||
ssl: false,
|
||||
timezone: 'Z', // mysql follows the SYSTEM timezone. on Cloudron, this is UTC
|
||||
charset: 'utf8mb4'
|
||||
charset: 'utf8mb4',
|
||||
jsonStrings: true, // for JSON types, JSONARRAYAGG will return string instead of JSON
|
||||
});
|
||||
|
||||
gConnectionPool.on('connection', function (connection) {
|
||||
debug('connected');
|
||||
// run one time setup commands on new connections. connections are reused and so we cannot use 'acquire' event of the pool
|
||||
gConnectionPool.on('connection', async function (connection) { // https://github.com/sidorares/node-mysql2/issues/565, 567
|
||||
debug('new connection');
|
||||
|
||||
// connection objects are re-used. so we have to attach to the event here (once) to prevent crash
|
||||
// note the pool also has an 'acquire' event but that is called whenever we do a getConnection()
|
||||
connection.on('error', (error) => debug(`Connection ${connection.threadId} error: ${error.message} ${error.code}`));
|
||||
const conn = connection.promise(); // convert PoolConnection to PromisePoolConnection
|
||||
|
||||
connection.query(`USE ${gDatabase.name}`);
|
||||
connection.query('SET SESSION sql_mode = \'strict_all_tables\'');
|
||||
// GROUP_CONCAT has only 1024 default. we use it in the groups API which doesn't support pagination yet
|
||||
// a uuid v4 is 36 in length. so the value below provides for roughly 10k users
|
||||
connection.query('SET SESSION group_concat_max_len = 360000');
|
||||
try {
|
||||
// await connection.query('SET NAMES utf8mb4 COLLATE utf8mb4_bin');
|
||||
await conn.query('SET SESSION sql_mode = \'strict_all_tables\''); // disable type coercion etc
|
||||
// GROUP_CONCAT has only 1024 default. we use it in the groups API which doesn't support pagination yet
|
||||
// a uuid v4 is 36 in length. so the value below provides for roughly 10k users
|
||||
await conn.query('SET SESSION group_concat_max_len = 360000');
|
||||
} catch (error) {
|
||||
debug(`failed to init new db connection ${connection.threadId}:`, error); // only log. we will let the app handle the exception when it calls query()/transaction()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function uninitialize() {
|
||||
if (!gConnectionPool) return;
|
||||
|
||||
gConnectionPool.end();
|
||||
await safe(gConnectionPool.end(), { debug });
|
||||
gConnectionPool = null;
|
||||
debug('connection closed');
|
||||
debug('pool closed');
|
||||
}
|
||||
|
||||
async function clear() {
|
||||
@@ -89,53 +91,35 @@ async function clear() {
|
||||
await transaction(queries);
|
||||
}
|
||||
|
||||
async function query() {
|
||||
async function query(...args) {
|
||||
assert.notStrictEqual(gConnectionPool, null, 'Database connection is already closed');
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const args = Array.prototype.slice.call(arguments);
|
||||
|
||||
args.push(function queryCallback(error, result) {
|
||||
if (error) return reject(new BoxError(BoxError.DATABASE_ERROR, error, { code: error.code, sqlMessage: error.sqlMessage || null }));
|
||||
|
||||
resolve(result);
|
||||
});
|
||||
|
||||
gConnectionPool.query.apply(gConnectionPool, args); // this is same as getConnection/query/release
|
||||
});
|
||||
const [error, result] = await safe(gConnectionPool.query(...args)); // this is same as getConnection/query/release
|
||||
if (error) throw new BoxError(BoxError.DATABASE_ERROR, error, { code: error.code, sqlMessage: error.sqlMessage || null });
|
||||
return result[0]; // the promise version returns a tuple of [rows, fields]
|
||||
}
|
||||
|
||||
async function transaction(queries) {
|
||||
assert(Array.isArray(queries));
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
gConnectionPool.getConnection(function (error, connection) {
|
||||
if (error) return reject(new BoxError(BoxError.DATABASE_ERROR, error, { code: error.code, sqlMessage: error.sqlMessage }));
|
||||
const [error, connection] = await safe(gConnectionPool.getConnection());
|
||||
if (error) throw new BoxError(BoxError.DATABASE_ERROR, error, { code: error.code, sqlMessage: error.sqlMessage });
|
||||
|
||||
const releaseConnection = (error) => {
|
||||
connection.release();
|
||||
reject(new BoxError(BoxError.DATABASE_ERROR, error, { code: error.code, sqlMessage: error.sqlMessage || null }));
|
||||
};
|
||||
|
||||
connection.beginTransaction(function (error) {
|
||||
if (error) return releaseConnection(error);
|
||||
|
||||
async.mapSeries(queries, function iterator(query, done) {
|
||||
connection.query(query.query, query.args, done);
|
||||
}, function seriesDone(error, results) {
|
||||
if (error) return connection.rollback(() => releaseConnection(error));
|
||||
|
||||
connection.commit(function (error) {
|
||||
if (error) return connection.rollback(() => releaseConnection(error));
|
||||
|
||||
connection.release();
|
||||
|
||||
resolve(results);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
try {
|
||||
await connection.beginTransaction();
|
||||
const results = [];
|
||||
for (const query of queries) {
|
||||
const [rows /*, fields */] = await connection.query(query.query, query.args);
|
||||
results.push(rows);
|
||||
}
|
||||
await connection.commit();
|
||||
connection.release(); // no await!
|
||||
return results;
|
||||
} catch (error) {
|
||||
await safe(connection.rollback(), { debug });
|
||||
connection.release(); // no await!
|
||||
throw new BoxError(BoxError.DATABASE_ERROR, error, { code: error.code, sqlMessage: error.sqlMessage || null });
|
||||
}
|
||||
}
|
||||
|
||||
async function importFromFile(file) {
|
||||
|
||||
Reference in New Issue
Block a user