2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
initialize: initialize,
|
|
|
|
|
uninitialize: uninitialize,
|
|
|
|
|
query: query,
|
|
|
|
|
transaction: transaction,
|
|
|
|
|
|
2017-11-22 10:57:56 -08:00
|
|
|
importFromFile: importFromFile,
|
2017-11-24 15:29:23 -08:00
|
|
|
exportToFile: exportToFile,
|
2017-11-22 10:57:56 -08:00
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
_clear: clear
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
|
|
|
|
async = require('async'),
|
2019-12-04 10:29:06 -08:00
|
|
|
BoxError = require('./boxerror.js'),
|
2016-09-20 14:07:39 -07:00
|
|
|
child_process = require('child_process'),
|
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'),
|
2015-07-20 00:09:47 -07:00
|
|
|
mysql = require('mysql'),
|
2016-09-20 14:07:39 -07:00
|
|
|
once = require('once'),
|
2015-07-20 00:09:47 -07:00
|
|
|
util = require('util');
|
|
|
|
|
|
|
|
|
|
var gConnectionPool = null,
|
|
|
|
|
gDefaultConnection = null;
|
|
|
|
|
|
2019-07-25 16:12:37 -07:00
|
|
|
const gDatabase = {
|
|
|
|
|
hostname: '127.0.0.1',
|
|
|
|
|
username: 'root',
|
|
|
|
|
password: 'password',
|
|
|
|
|
port: 3306,
|
|
|
|
|
name: 'box'
|
|
|
|
|
};
|
|
|
|
|
|
2018-03-22 20:34:49 -07:00
|
|
|
function initialize(callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
if (gConnectionPool !== null) return callback(null);
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
gConnectionPool = mysql.createPool({
|
2018-03-22 21:07:06 -07:00
|
|
|
connectionLimit: 5, // this has to be > 1 since we store one connection as 'default'. the rest for transactions
|
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,
|
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) {
|
2019-07-25 16:12:37 -07:00
|
|
|
connection.query('USE ' + gDatabase.name);
|
2016-08-05 12:37:44 +02:00
|
|
|
connection.query('SET SESSION sql_mode = \'strict_all_tables\'');
|
|
|
|
|
});
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
reconnect(callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function uninitialize(callback) {
|
|
|
|
|
if (gConnectionPool) {
|
|
|
|
|
gConnectionPool.end(callback);
|
2020-06-03 16:28:30 -07:00
|
|
|
gDefaultConnection = null;
|
2015-07-20 00:09:47 -07:00
|
|
|
gConnectionPool = null;
|
|
|
|
|
} else {
|
|
|
|
|
callback(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function reconnect(callback) {
|
|
|
|
|
callback = callback ? once(callback) : function () {};
|
|
|
|
|
|
2020-06-03 16:28:30 -07:00
|
|
|
debug('reconnect: connecting to database');
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
gConnectionPool.getConnection(function (error, connection) {
|
|
|
|
|
if (error) {
|
2020-06-04 09:20:28 -07:00
|
|
|
debug(`reconnect: db connection error. ${error.message} fatal:${error.fatal} code:${error.code}. Will retry in 10 seconds`);
|
2020-06-03 16:05:46 -07:00
|
|
|
return setTimeout(reconnect.bind(null, callback), 10000);
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-04 11:27:09 -07:00
|
|
|
debug('reconnect: connected to database');
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
connection.on('error', function (error) {
|
|
|
|
|
// by design, we catch all normal errors by providing callbacks.
|
|
|
|
|
// this function should be invoked only when we have no callbacks pending and we have a fatal error
|
|
|
|
|
assert(error.fatal, 'Non-fatal error on connection object');
|
|
|
|
|
|
2020-06-04 09:20:28 -07:00
|
|
|
debug(`reconnect: db connection error. ${error.message} fatal:${error.fatal} code:${error.code}. Will retry in 10 seconds`);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2020-06-03 16:28:30 -07:00
|
|
|
gDefaultConnection = null;
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
// This is most likely an issue an can cause double callbacks from reconnect()
|
2020-06-03 16:05:46 -07:00
|
|
|
setTimeout(reconnect.bind(null, callback), 10000);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
|
2016-08-05 12:37:44 +02:00
|
|
|
gDefaultConnection = connection;
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-08-05 12:37:44 +02:00
|
|
|
callback(null);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clear(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2019-01-23 16:18:52 +01:00
|
|
|
var cmd = util.format('mysql --host="%s" --user="%s" --password="%s" -Nse "SHOW TABLES" %s | grep -v "^migrations$" | while read table; do mysql --host="%s" --user="%s" --password="%s" -e "SET FOREIGN_KEY_CHECKS = 0; TRUNCATE TABLE $table" %s; done',
|
2019-07-25 16:12:37 -07:00
|
|
|
gDatabase.hostname, gDatabase.username, gDatabase.password, gDatabase.name,
|
|
|
|
|
gDatabase.hostname, gDatabase.username, gDatabase.password, gDatabase.name);
|
2016-09-20 14:07:39 -07:00
|
|
|
|
2020-02-06 16:57:33 +01:00
|
|
|
child_process.exec(cmd, callback);
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function beginTransaction(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2019-12-04 10:29:06 -08:00
|
|
|
if (gConnectionPool === null) return callback(new BoxError(BoxError.DATABASE_ERROR, 'No database connection pool.'));
|
2015-11-27 10:05:57 +01:00
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
gConnectionPool.getConnection(function (error, connection) {
|
2020-06-03 16:08:37 -07:00
|
|
|
if (error) return callback(error);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-08-05 12:37:44 +02:00
|
|
|
connection.beginTransaction(function (error) {
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
2016-08-05 12:37:44 +02:00
|
|
|
return callback(null, connection);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function query() {
|
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
|
var callback = args[args.length - 1];
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2019-12-04 10:29:06 -08:00
|
|
|
if (gDefaultConnection === null) return callback(new BoxError(BoxError.DATABASE_ERROR, 'No connection to database'));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
gDefaultConnection.query.apply(gDefaultConnection, args);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-11 09:50:49 -07:00
|
|
|
function rollback(connection, transactionError, callback) {
|
|
|
|
|
connection.rollback(function (error) {
|
|
|
|
|
if (error) debug('rollback: error when rolling back', error);
|
|
|
|
|
|
|
|
|
|
connection.release();
|
|
|
|
|
callback(transactionError);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
function transaction(queries, callback) {
|
|
|
|
|
assert(util.isArray(queries));
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2020-06-03 16:10:25 -07:00
|
|
|
callback = once(callback);
|
|
|
|
|
|
|
|
|
|
beginTransaction(function (error, connection) {
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
2020-06-03 16:10:25 -07:00
|
|
|
connection.on('error', callback);
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
async.mapSeries(queries, function iterator(query, done) {
|
2020-06-03 16:10:25 -07:00
|
|
|
connection.query(query.query, query.args, done);
|
2015-07-20 00:09:47 -07:00
|
|
|
}, function seriesDone(error, results) {
|
2020-06-11 09:50:49 -07:00
|
|
|
if (error) return rollback(connection, error, callback);
|
|
|
|
|
|
|
|
|
|
connection.commit(function (error) {
|
|
|
|
|
if (error) return rollback(connection, error, callback);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2020-06-11 09:50:49 -07:00
|
|
|
connection.release();
|
|
|
|
|
callback(null, results);
|
|
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-22 10:57:56 -08:00
|
|
|
function importFromFile(file, callback) {
|
|
|
|
|
assert.strictEqual(typeof file, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2019-07-25 16:12:37 -07:00
|
|
|
var cmd = `/usr/bin/mysql -h "${gDatabase.hostname}" -u ${gDatabase.username} -p${gDatabase.password} ${gDatabase.name} < ${file}`;
|
2017-11-22 10:57:56 -08:00
|
|
|
|
|
|
|
|
async.series([
|
|
|
|
|
query.bind(null, 'CREATE DATABASE IF NOT EXISTS box'),
|
|
|
|
|
child_process.exec.bind(null, cmd)
|
|
|
|
|
], callback);
|
|
|
|
|
}
|
2017-11-24 15:29:23 -08:00
|
|
|
|
|
|
|
|
function exportToFile(file, callback) {
|
|
|
|
|
assert.strictEqual(typeof file, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2020-01-31 18:01:13 -08:00
|
|
|
// latest mysqldump enables column stats by default which is not present in MySQL 5.7 server
|
|
|
|
|
// this option must not be set in production cloudrons which still use the old mysqldump
|
2020-02-21 12:17:06 -08:00
|
|
|
const disableColStats = (constants.TEST && process.env.DESKTOP_SESSION !== 'ubuntu') ? '--column-statistics=0' : '';
|
2020-01-31 18:01:13 -08:00
|
|
|
|
|
|
|
|
var cmd = `/usr/bin/mysqldump -h "${gDatabase.hostname}" -u root -p${gDatabase.password} ${disableColStats} --single-transaction --routines --triggers ${gDatabase.name} > "${file}"`;
|
2017-11-24 15:29:23 -08:00
|
|
|
|
|
|
|
|
child_process.exec(cmd, callback);
|
|
|
|
|
}
|