Connect without db in connection obj
Noah Zalev committed
Apr 22, 2021 at 20:06 UTC
4ca9c391a266eb4f9f7e9c0c2a554268dc83edef
1 file changed
+9
-38
db.js
+9
-38
@@ -440,16 +440,19 @@ module.exports.CreateDB = function (parent, func) {
440
441
if (parent.args.mariadb || parent.args.mysql) {
442
var connectinArgs = (parent.args.mariadb) ? parent.args.mariadb : parent.args.mysql;
443
+ var dbname = (connectinArgs.database != null) ? connectinArgs.database : 'meshcentral';
444
+
445
+ // Including the db name in the connection obj will cause a connection failure if it does not exist
446
var connectionObject = {
447
'host': connectinArgs.host,
448
'port': connectinArgs.port,
449
'user': connectinArgs.user,
450
'password': connectinArgs.password,
448
- 'connectionLimit': null,
449
- 'database': null
451
+ 'connectionLimit': null
452
};
453
if (connectinArgs.connectionLimit != null) connectionObject.connectionLimit = connectinArgs.connectionLimit;
452
- connectionObject.database = (connectinArgs.database != null) ? connectinArgs.database : 'meshcentral';
454
+
455
+
456
if (parent.args.mariadb) {
457
// Use MariaDB
458
obj.databaseType = 4;
@@ -459,8 +462,10 @@ module.exports.CreateDB = function (parent, func) {
462
Datastore = require('mysql').createConnection(connectionObject);
463
obj.databaseType = 5;
464
}
465
+ sqlDbQuery('CREATE DATABASE IF NOT EXISTS ' + dbname);
466
463
- var useDatabase = 'USE ' + connectionObject.database;
467
+ // Set the default database for the rest of this connections lifetime
468
+ var useDatabase = 'USE ' + dbname;
469
sqlDbQuery(useDatabase, null, function (err, docs) {
470
if (err != null) { console.log(err); parent.debug('db', 'ERROR: ' + useDatabase + ': ' + err); }
471
if (err == null) {
@@ -489,40 +494,6 @@ module.exports.CreateDB = function (parent, func) {
494
sqlDbExec('CREATE INDEX ndxsmbiosexpire ON smbios (expire)', null, function (err, response) { });
495
setupFunctions(func);
496
});
492
- } else {
493
- parent.debug('db', 'Creating database...');
494
- sqlDbBatchExec([
495
- 'CREATE DATABASE ' + connectionObject.database,
496
- // Main table
497
- 'CREATE TABLE 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)))',
498
- 'CREATE INDEX ndxtypedomainextra ON main (type, domain, extra)',
499
- 'CREATE INDEX ndxextra ON main (extra)',
500
- 'CREATE INDEX ndxextraex ON main (extraex)',
501
- // Events table
502
- 'CREATE TABLE 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)))',
503
- 'CREATE INDEX ndxeventstime ON events(time)',
504
- 'CREATE INDEX ndxeventsusername ON events(domain, userid, time)',
505
- 'CREATE INDEX ndxeventsdomainnodeidtime ON events(domain, nodeid, time)',
506
- // Events ID table
507
- 'CREATE TABLE eventids(fkid INT NOT NULL, target CHAR(255), CONSTRAINT fk_eventid FOREIGN KEY (fkid) REFERENCES events (id) ON DELETE CASCADE ON UPDATE RESTRICT)',
508
- 'CREATE INDEX ndxeventids ON eventids(target)',
509
- // Server stats table
510
- 'CREATE TABLE serverstats (time DATETIME, expire DATETIME, doc JSON, PRIMARY KEY(time), CHECK (json_valid(doc)))',
511
- 'CREATE INDEX ndxserverstattime ON serverstats (time)',
512
- 'CREATE INDEX ndxserverstatexpire ON serverstats (expire)',
513
- // Power events table
514
- 'CREATE TABLE power (id INT NOT NULL AUTO_INCREMENT, time DATETIME, nodeid CHAR(255), doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
515
- 'CREATE INDEX ndxpowernodeidtime ON power (nodeid, time)',
516
- // SMBIOS table
517
- 'CREATE TABLE smbios (id CHAR(255), time DATETIME, expire DATETIME, doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))',
518
- 'CREATE INDEX ndxsmbiostime ON smbios (time)',
519
- 'CREATE INDEX ndxsmbiosexpire ON smbios (expire)',
520
- // Plugins table
521
- 'CREATE TABLE plugin (id INT NOT NULL AUTO_INCREMENT, doc JSON, PRIMARY KEY(id), CHECK (json_valid(doc)))'
522
- ], function (err) {
523
- if (err != null) { parent.debug('db', 'BatchSetupDb: ' + err); }
524
- setupFunctions(func);
525
- });
497
}
498
});
499
} else if (parent.args.mongodb) {