Added auto-create tables/indexes for MySQL/MariaDB databases.

Ylian Saint-Hilaire committed Mar 10, 2021 at 14:48 UTC 746fe62fd1a63bdfd52df495c46f6d0736a29105
1 file changed +28 -2
db.js
+28 -2
@@ -451,7 +451,33 @@ module.exports.CreateDB = function (parent, func) {
451 //sqlDbQuery('DROP DATABASE MeshCentral', null, function (err, docs) { console.log('DROP'); }); return;
452 sqlDbQuery('USE meshcentral', null, function (err, docs) {
453 if (err != null) { console.log(err); parent.debug('db', 'ERROR: USE meshcentral: ' + err); }
454 - if (err == null) { setupFunctions(func); } else {
454 + if (err == null) {
455 + parent.debug('db', 'Checking tables...');
456 + sqlDbBatchExec([
457 + 'CREATE TABLE IF NOT EXISTS meshcentral.main (id VARCHAR(256) NOT NULL, type CHAR(32), domain CHAR(64), extra CHAR(255), extraex CHAR(255), doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
458 + 'CREATE TABLE IF NOT EXISTS meshcentral.events(id INT NOT NULL AUTO_INCREMENT, time DATETIME, domain CHAR(64), action CHAR(255), nodeid CHAR(255), userid CHAR(255), doc JSON, PRIMARY KEY(id), CHECK(json_valid(doc)))',
459 + 'CREATE TABLE IF NOT EXISTS meshcentral.eventids(fkid INT NOT NULL, target CHAR(255), CONSTRAINT fk_eventid FOREIGN KEY (fkid) REFERENCES events (id) ON DELETE CASCADE ON UPDATE RESTRICT)',
460 + 'CREATE TABLE IF NOT EXISTS meshcentral.serverstats (time DATETIME, expire DATETIME, doc JSON, PRIMARY KEY(time), CHECK (json_valid(doc)))',
461 + 'CREATE TABLE IF NOT EXISTS meshcentral.power (id INT NOT NULL AUTO_INCREMENT, time DATETIME, nodeid CHAR(255), doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
462 + 'CREATE TABLE IF NOT EXISTS meshcentral.smbios (id CHAR(255), time DATETIME, expire DATETIME, doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
463 + 'CREATE TABLE IF NOT EXISTS meshcentral.plugin (id INT NOT NULL AUTO_INCREMENT, doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))'
464 + ], function (err) {
465 + parent.debug('db', 'Checking indexes...');
466 + sqlDbExec('CREATE INDEX ndxtypedomainextra ON meshcentral.main (type, domain, extra)', null, function (err, response) { });
467 + sqlDbExec('CREATE INDEX ndxextra ON meshcentral.main (extra)', null, function (err, response) { });
468 + sqlDbExec('CREATE INDEX ndxextraex ON meshcentral.main (extraex)', null, function (err, response) { });
469 + sqlDbExec('CREATE INDEX ndxeventstime ON meshcentral.events(time)', null, function (err, response) { });
470 + sqlDbExec('CREATE INDEX ndxeventsusername ON meshcentral.events(domain, userid, time)', null, function (err, response) { });
471 + sqlDbExec('CREATE INDEX ndxeventsdomainnodeidtime ON meshcentral.events(domain, nodeid, time)', null, function (err, response) { });
472 + sqlDbExec('CREATE INDEX ndxeventids ON meshcentral.eventids(target)', null, function (err, response) { });
473 + sqlDbExec('CREATE INDEX ndxserverstattime ON meshcentral.serverstats (time)', null, function (err, response) { });
474 + sqlDbExec('CREATE INDEX ndxserverstatexpire ON meshcentral.serverstats (expire)', null, function (err, response) { });
475 + sqlDbExec('CREATE INDEX ndxpowernodeidtime ON meshcentral.power (nodeid, time)', null, function (err, response) { });
476 + sqlDbExec('CREATE INDEX ndxsmbiostime ON meshcentral.smbios (time)', null, function (err, response) { });
477 + sqlDbExec('CREATE INDEX ndxsmbiosexpire ON meshcentral.smbios (expire)', null, function (err, response) { });
478 + setupFunctions(func);
479 + });
480 + } else {
481 parent.debug('db', 'Creating database...');
482 sqlDbBatchExec([
483 'CREATE DATABASE meshcentral',
@@ -890,7 +916,7 @@ module.exports.CreateDB = function (parent, func) {
916 }).catch(function (err) { if (func) { try { func(err); } catch (ex) { console.log(ex); } } });
917 } else if (obj.databaseType == 5) { // MySQL
918 Datastore.query(query, args, function (error, results, fields) {
893 - if (func) try { func(error, results[0]); } catch (ex) { console.log(ex); }
919 + if (func) try { func(error, results?results[0]:null); } catch (ex) { console.log(ex); }
920 });
921 }
922 }