PostgreSQL now skips table and index creation when DB already exists, #3487

Ylian Saint-Hilaire committed Jan 19, 2022 at 12:21 UTC 8acbfd2a308c5a591777f0d6e50a1860d44ec553
1 file changed +31 -25
db.js
+31 -25
@@ -686,32 +686,38 @@ module.exports.CreateDB = function (parent, func) {
686 const { Pool, Client } = require('pg');
687 connectinArgs.database = dbname;
688 Datastore = new Client(connectinArgs);
689 - Datastore.connect()
690 - parent.debug('db', 'Checking tables...');
691 - sqlDbBatchExec([
692 - 'CREATE TABLE IF NOT EXISTS main (id VARCHAR(256) PRIMARY KEY NOT NULL, type CHAR(32), domain CHAR(64), extra CHAR(255), extraex CHAR(255), doc JSON)',
693 - 'CREATE TABLE IF NOT EXISTS events(id SERIAL PRIMARY KEY, time TIMESTAMP, domain CHAR(64), action CHAR(255), nodeid CHAR(255), userid CHAR(255), doc JSON)',
694 - 'CREATE TABLE IF NOT EXISTS eventids(fkid INT NOT NULL, target CHAR(255), CONSTRAINT fk_eventid FOREIGN KEY (fkid) REFERENCES events (id) ON DELETE CASCADE ON UPDATE RESTRICT)',
695 - 'CREATE TABLE IF NOT EXISTS serverstats (time TIMESTAMP PRIMARY KEY, expire TIMESTAMP, doc JSON)',
696 - 'CREATE TABLE IF NOT EXISTS power (id SERIAL PRIMARY KEY, time TIMESTAMP, nodeid CHAR(255), doc JSON)',
697 - 'CREATE TABLE IF NOT EXISTS smbios (id CHAR(255) PRIMARY KEY, time TIMESTAMP, expire TIMESTAMP, doc JSON)',
698 - 'CREATE TABLE IF NOT EXISTS plugin (id SERIAL PRIMARY KEY, doc JSON)'
699 - ], function (results) {
700 - parent.debug('db', 'Checking indexes...');
701 - sqlDbExec('CREATE INDEX ndxtypedomainextra ON main (type, domain, extra)', null, function (err, response) { });
702 - sqlDbExec('CREATE INDEX ndxextra ON main (extra)', null, function (err, response) { });
703 - sqlDbExec('CREATE INDEX ndxextraex ON main (extraex)', null, function (err, response) { });
704 - sqlDbExec('CREATE INDEX ndxeventstime ON events(time)', null, function (err, response) { });
705 - sqlDbExec('CREATE INDEX ndxeventsusername ON events(domain, userid, time)', null, function (err, response) { });
706 - sqlDbExec('CREATE INDEX ndxeventsdomainnodeidtime ON events(domain, nodeid, time)', null, function (err, response) { });
707 - sqlDbExec('CREATE INDEX ndxeventids ON eventids(target)', null, function (err, response) { });
708 - sqlDbExec('CREATE INDEX ndxserverstattime ON serverstats (time)', null, function (err, response) { });
709 - sqlDbExec('CREATE INDEX ndxserverstatexpire ON serverstats (expire)', null, function (err, response) { });
710 - sqlDbExec('CREATE INDEX ndxpowernodeidtime ON power (nodeid, time)', null, function (err, response) { });
711 - sqlDbExec('CREATE INDEX ndxsmbiostime ON smbios (time)', null, function (err, response) { });
712 - sqlDbExec('CREATE INDEX ndxsmbiosexpire ON smbios (expire)', null, function (err, response) { });
689 + Datastore.connect();
690 + if (err == null) {
691 + // Database was created, create the tables
692 + parent.debug('db', 'Creating tables...');
693 + sqlDbBatchExec([
694 + 'CREATE TABLE IF NOT EXISTS main (id VARCHAR(256) PRIMARY KEY NOT NULL, type CHAR(32), domain CHAR(64), extra CHAR(255), extraex CHAR(255), doc JSON)',
695 + 'CREATE TABLE IF NOT EXISTS events(id SERIAL PRIMARY KEY, time TIMESTAMP, domain CHAR(64), action CHAR(255), nodeid CHAR(255), userid CHAR(255), doc JSON)',
696 + 'CREATE TABLE IF NOT EXISTS eventids(fkid INT NOT NULL, target CHAR(255), CONSTRAINT fk_eventid FOREIGN KEY (fkid) REFERENCES events (id) ON DELETE CASCADE ON UPDATE RESTRICT)',
697 + 'CREATE TABLE IF NOT EXISTS serverstats (time TIMESTAMP PRIMARY KEY, expire TIMESTAMP, doc JSON)',
698 + 'CREATE TABLE IF NOT EXISTS power (id SERIAL PRIMARY KEY, time TIMESTAMP, nodeid CHAR(255), doc JSON)',
699 + 'CREATE TABLE IF NOT EXISTS smbios (id CHAR(255) PRIMARY KEY, time TIMESTAMP, expire TIMESTAMP, doc JSON)',
700 + 'CREATE TABLE IF NOT EXISTS plugin (id SERIAL PRIMARY KEY, doc JSON)'
701 + ], function (results) {
702 + parent.debug('db', 'Creating indexes...');
703 + sqlDbExec('CREATE INDEX ndxtypedomainextra ON main (type, domain, extra)', null, function (err, response) { });
704 + sqlDbExec('CREATE INDEX ndxextra ON main (extra)', null, function (err, response) { });
705 + sqlDbExec('CREATE INDEX ndxextraex ON main (extraex)', null, function (err, response) { });
706 + sqlDbExec('CREATE INDEX ndxeventstime ON events(time)', null, function (err, response) { });
707 + sqlDbExec('CREATE INDEX ndxeventsusername ON events(domain, userid, time)', null, function (err, response) { });
708 + sqlDbExec('CREATE INDEX ndxeventsdomainnodeidtime ON events(domain, nodeid, time)', null, function (err, response) { });
709 + sqlDbExec('CREATE INDEX ndxeventids ON eventids(target)', null, function (err, response) { });
710 + sqlDbExec('CREATE INDEX ndxserverstattime ON serverstats (time)', null, function (err, response) { });
711 + sqlDbExec('CREATE INDEX ndxserverstatexpire ON serverstats (expire)', null, function (err, response) { });
712 + sqlDbExec('CREATE INDEX ndxpowernodeidtime ON power (nodeid, time)', null, function (err, response) { });
713 + sqlDbExec('CREATE INDEX ndxsmbiostime ON smbios (time)', null, function (err, response) { });
714 + sqlDbExec('CREATE INDEX ndxsmbiosexpire ON smbios (expire)', null, function (err, response) { });
715 + setupFunctions(func);
716 + });
717 + } else {
718 + // Database already existed, skip table and index creation
719 setupFunctions(func);
714 - });
720 + }
721 });
722 } else if (parent.args.mongodb) {
723 // Use MongoDB