2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2021-05-01 11:21:09 -07:00
|
|
|
initialize,
|
|
|
|
|
uninitialize,
|
|
|
|
|
query,
|
|
|
|
|
transaction,
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2021-05-01 11:21:09 -07:00
|
|
|
importFromFile,
|
|
|
|
|
exportToFile,
|
2017-11-22 10:57:56 -08:00
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
_clear: clear
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-02 21:12:38 -07:00
|
|
|
const assert = require('assert'),
|
2015-07-20 00:09:47 -07:00
|
|
|
async = require('async'),
|
2020-07-03 13:47:56 -07:00
|
|
|
BoxError = require('./boxerror.js'),
|
2019-07-25 16:12:37 -07:00
|
|
|
constants = require('./constants.js'),
|
2020-06-03 16:14:05 -07:00
|
|
|
debug = require('debug')('box:database'),
|
2023-07-11 16:32:28 +05:30
|
|
|
fs = require('fs'),
|
2015-07-20 00:09:47 -07:00
|
|
|
mysql = require('mysql'),
|
2021-09-16 13:59:03 -07:00
|
|
|
safe = require('safetydance'),
|
2024-10-14 19:10:31 +02:00
|
|
|
shell = require('./shell.js')('database');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2021-08-27 09:52:24 -07:00
|
|
|
let gConnectionPool = null;
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2019-07-25 16:12:37 -07:00
|
|
|
const gDatabase = {
|
|
|
|
|
hostname: '127.0.0.1',
|
2024-06-03 10:35:50 +02:00
|
|
|
username: 'root',
|
2019-07-25 16:12:37 -07:00
|
|
|
password: 'password',
|
|
|
|
|
port: 3306,
|
2024-06-03 10:35:50 +02:00
|
|
|
name: 'box'
|
2019-07-25 16:12:37 -07:00
|
|
|
};
|
|
|
|
|
|
2021-08-22 17:14:00 -07:00
|
|
|
async function initialize() {
|
|
|
|
|
if (gConnectionPool !== null) return;
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2019-07-25 16:12:37 -07:00
|
|
|
if (constants.TEST) {
|
|
|
|
|
// see setupTest script how the mysql-server is run
|
|
|
|
|
gDatabase.hostname = require('child_process').execSync('docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" mysql-server').toString().trim();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-03 13:07:39 -07:00
|
|
|
// https://github.com/mysqljs/mysql#pool-options
|
2015-07-20 00:09:47 -07:00
|
|
|
gConnectionPool = mysql.createPool({
|
2020-07-03 13:07:39 -07:00
|
|
|
connectionLimit: 5,
|
2021-06-25 16:46:49 -07:00
|
|
|
acquireTimeout: 60000,
|
|
|
|
|
connectTimeout: 60000,
|
2019-07-25 16:12:37 -07:00
|
|
|
host: gDatabase.hostname,
|
|
|
|
|
user: gDatabase.username,
|
|
|
|
|
password: gDatabase.password,
|
|
|
|
|
port: gDatabase.port,
|
|
|
|
|
database: gDatabase.name,
|
2015-07-20 00:09:47 -07:00
|
|
|
multipleStatements: false,
|
2020-07-03 13:07:39 -07:00
|
|
|
waitForConnections: true, // getConnection() will wait until a connection is avaiable
|
2019-03-22 15:12:30 -07:00
|
|
|
ssl: false,
|
|
|
|
|
timezone: 'Z' // mysql follows the SYSTEM timezone. on Cloudron, this is UTC
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
|
2016-08-05 12:37:44 +02:00
|
|
|
gConnectionPool.on('connection', function (connection) {
|
2020-07-02 15:10:05 -07:00
|
|
|
// 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()
|
2020-07-03 13:07:39 -07:00
|
|
|
connection.on('error', (error) => debug(`Connection ${connection.threadId} error: ${error.message} ${error.code}`));
|
2020-07-02 15:10:05 -07:00
|
|
|
|
2022-06-22 17:54:52 -07:00
|
|
|
connection.query(`USE ${gDatabase.name}`);
|
2016-08-05 12:37:44 +02:00
|
|
|
connection.query('SET SESSION sql_mode = \'strict_all_tables\'');
|
2024-10-09 19:12:53 +02:00
|
|
|
// 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');
|
2016-08-05 12:37:44 +02:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-22 17:14:00 -07:00
|
|
|
async function uninitialize() {
|
|
|
|
|
if (!gConnectionPool) return;
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2021-08-22 17:14:00 -07:00
|
|
|
gConnectionPool.end();
|
2020-07-03 13:07:39 -07:00
|
|
|
gConnectionPool = null;
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
async function clear() {
|
2023-07-11 16:32:28 +05:30
|
|
|
await fs.promises.writeFile('/tmp/extra.cnf', `[client]\nhost=${gDatabase.hostname}\nuser=${gDatabase.username}\npassword=${gDatabase.password}\ndatabase=${gDatabase.name}`, 'utf8');
|
2016-09-20 14:07:39 -07:00
|
|
|
|
2023-07-11 16:32:28 +05:30
|
|
|
const cmd = 'mysql --defaults-extra-file=/tmp/extra.cnf -Nse "SHOW TABLES" | grep -v "^migrations$" | while read table; do mysql --defaults-extra-file=/tmp/extra.cnf -e "SET FOREIGN_KEY_CHECKS = 0; TRUNCATE TABLE $table"; done';
|
2024-10-16 10:25:07 +02:00
|
|
|
await shell.bash(cmd, {});
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-03 18:10:04 -07:00
|
|
|
async function query() {
|
2021-05-02 21:12:38 -07:00
|
|
|
assert.notStrictEqual(gConnectionPool, null);
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2024-10-14 19:10:31 +02:00
|
|
|
const args = Array.prototype.slice.call(arguments);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2021-05-02 21:12:38 -07:00
|
|
|
args.push(function queryCallback(error, result) {
|
2021-11-02 23:03:12 -07:00
|
|
|
if (error) return reject(new BoxError(BoxError.DATABASE_ERROR, error, { code: error.code, sqlMessage: error.sqlMessage || null }));
|
2020-07-03 13:47:56 -07:00
|
|
|
|
2021-09-03 18:10:04 -07:00
|
|
|
resolve(result);
|
2021-05-02 21:12:38 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
gConnectionPool.query.apply(gConnectionPool, args); // this is same as getConnection/query/release
|
|
|
|
|
});
|
2020-06-11 09:50:49 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-03 18:10:04 -07:00
|
|
|
async function transaction(queries) {
|
2021-05-02 11:26:08 -07:00
|
|
|
assert(Array.isArray(queries));
|
2020-06-03 16:10:25 -07:00
|
|
|
|
2021-06-01 15:37:42 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
gConnectionPool.getConnection(function (error, connection) {
|
2021-09-03 18:10:04 -07:00
|
|
|
if (error) return reject(new BoxError(BoxError.DATABASE_ERROR, error, { code: error.code, sqlMessage: error.sqlMessage }));
|
2020-07-03 13:07:39 -07:00
|
|
|
|
2021-06-01 15:37:42 -07:00
|
|
|
const releaseConnection = (error) => {
|
|
|
|
|
connection.release();
|
2021-11-02 23:03:12 -07:00
|
|
|
reject(new BoxError(BoxError.DATABASE_ERROR, error, { code: error.code, sqlMessage: error.sqlMessage || null }));
|
2021-06-01 15:37:42 -07:00
|
|
|
};
|
2020-07-03 13:07:39 -07:00
|
|
|
|
2021-06-01 15:37:42 -07:00
|
|
|
connection.beginTransaction(function (error) {
|
|
|
|
|
if (error) return releaseConnection(error);
|
2020-06-11 09:50:49 -07:00
|
|
|
|
2021-06-01 15:37:42 -07:00
|
|
|
async.mapSeries(queries, function iterator(query, done) {
|
|
|
|
|
connection.query(query.query, query.args, done);
|
|
|
|
|
}, function seriesDone(error, results) {
|
2020-07-03 13:07:39 -07:00
|
|
|
if (error) return connection.rollback(() => releaseConnection(error));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2021-06-01 15:37:42 -07:00
|
|
|
connection.commit(function (error) {
|
|
|
|
|
if (error) return connection.rollback(() => releaseConnection(error));
|
|
|
|
|
|
|
|
|
|
connection.release();
|
2020-07-02 11:59:45 -07:00
|
|
|
|
2021-09-03 18:10:04 -07:00
|
|
|
resolve(results);
|
2021-06-01 15:37:42 -07:00
|
|
|
});
|
2020-07-03 13:07:39 -07:00
|
|
|
});
|
2020-06-11 09:50:49 -07:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-16 13:59:03 -07:00
|
|
|
async function importFromFile(file) {
|
2017-11-22 10:57:56 -08:00
|
|
|
assert.strictEqual(typeof file, 'string');
|
|
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
const cmd = `/usr/bin/mysql -h "${gDatabase.hostname}" -u ${gDatabase.username} -p${gDatabase.password} ${gDatabase.name} < ${file}`;
|
2021-09-16 13:59:03 -07:00
|
|
|
await query('CREATE DATABASE IF NOT EXISTS box');
|
2024-10-16 10:25:07 +02:00
|
|
|
const [error] = await safe(shell.bash(cmd, {}));
|
2021-09-16 13:59:03 -07:00
|
|
|
if (error) throw new BoxError(BoxError.DATABASE_ERROR, error);
|
2017-11-22 10:57:56 -08:00
|
|
|
}
|
2017-11-24 15:29:23 -08:00
|
|
|
|
2021-09-16 13:59:03 -07:00
|
|
|
async function exportToFile(file) {
|
2017-11-24 15:29:23 -08:00
|
|
|
assert.strictEqual(typeof file, 'string');
|
|
|
|
|
|
2022-04-27 15:39:49 -07:00
|
|
|
// latest mysqldump enables column stats by default which is not present in 5.7 util
|
2024-10-15 10:10:15 +02:00
|
|
|
const mysqlDumpHelp = await shell.spawn('/usr/bin/mysqldump', ['--help'], { encoding: 'utf8' });
|
2022-04-27 15:39:49 -07:00
|
|
|
const hasColStats = mysqlDumpHelp.includes('column-statistics');
|
|
|
|
|
const colStats = hasColStats ? '--column-statistics=0' : '';
|
2020-01-31 18:01:13 -08:00
|
|
|
|
2021-09-16 17:00:15 -07:00
|
|
|
const cmd = `/usr/bin/mysqldump -h "${gDatabase.hostname}" -u root -p${gDatabase.password} ${colStats} --single-transaction --routines --triggers ${gDatabase.name} > "${file}"`;
|
2017-11-24 15:29:23 -08:00
|
|
|
|
2024-10-16 10:25:07 +02:00
|
|
|
const [error] = await safe(shell.bash(cmd, {}));
|
2021-09-16 13:59:03 -07:00
|
|
|
if (error) throw new BoxError(BoxError.DATABASE_ERROR, error);
|
2017-11-24 15:29:23 -08:00
|
|
|
}
|