PostgreSQL support will now test if main table exists and if not, create all tables and indexes (#4507)

Ylian Saint-Hilaire committed Sep 6, 2022 at 12:48 UTC 8f89665421a67c1386834e29d38f7ba3409292c1
1 file changed +36 -28
db.js
+36 -28
@@ -794,35 +794,13 @@ module.exports.CreateDB = function (parent, func) {
794 Datastore = new Client(connectinArgs);
795 Datastore.connect();
796 if (err == null) {
797 - // Database was created, create the tables
798 - parent.debug('db', 'Creating tables...');
799 - sqlDbBatchExec([
800 - '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)',
801 - '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)',
802 - '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)',
803 - 'CREATE TABLE IF NOT EXISTS serverstats (time TIMESTAMP PRIMARY KEY, expire TIMESTAMP, doc JSON)',
804 - 'CREATE TABLE IF NOT EXISTS power (id SERIAL PRIMARY KEY, time TIMESTAMP, nodeid CHAR(255), doc JSON)',
805 - 'CREATE TABLE IF NOT EXISTS smbios (id CHAR(255) PRIMARY KEY, time TIMESTAMP, expire TIMESTAMP, doc JSON)',
806 - 'CREATE TABLE IF NOT EXISTS plugin (id SERIAL PRIMARY KEY, doc JSON)'
807 - ], function (results) {
808 - parent.debug('db', 'Creating indexes...');
809 - sqlDbExec('CREATE INDEX ndxtypedomainextra ON main (type, domain, extra)', null, function (err, response) { });
810 - sqlDbExec('CREATE INDEX ndxextra ON main (extra)', null, function (err, response) { });
811 - sqlDbExec('CREATE INDEX ndxextraex ON main (extraex)', null, function (err, response) { });
812 - sqlDbExec('CREATE INDEX ndxeventstime ON events(time)', null, function (err, response) { });
813 - sqlDbExec('CREATE INDEX ndxeventsusername ON events(domain, userid, time)', null, function (err, response) { });
814 - sqlDbExec('CREATE INDEX ndxeventsdomainnodeidtime ON events(domain, nodeid, time)', null, function (err, response) { });
815 - sqlDbExec('CREATE INDEX ndxeventids ON eventids(target)', null, function (err, response) { });
816 - sqlDbExec('CREATE INDEX ndxserverstattime ON serverstats (time)', null, function (err, response) { });
817 - sqlDbExec('CREATE INDEX ndxserverstatexpire ON serverstats (expire)', null, function (err, response) { });
818 - sqlDbExec('CREATE INDEX ndxpowernodeidtime ON power (nodeid, time)', null, function (err, response) { });
819 - sqlDbExec('CREATE INDEX ndxsmbiostime ON smbios (time)', null, function (err, response) { });
820 - sqlDbExec('CREATE INDEX ndxsmbiosexpire ON smbios (expire)', null, function (err, response) { });
821 - setupFunctions(func);
822 - });
797 + // Create the tables and indexes
798 + postgreSqlCreateTables(func);
799 } else {
824 - // Database already existed, skip table and index creation
825 - setupFunctions(func);
800 + // Database already existed, perform a test query to see if the main table is present
801 + sqlDbQuery('SELECT doc FROM main WHERE id = $1', ['DatabaseIdentifier'], function (err, docs) {
802 + if (err == null) { setupFunctions(func); } else { postgreSqlCreateTables(func); } // If not present, create the tables and indexes
803 + });
804 }
805 });
806 } else if (parent.args.mongodb) {
@@ -1185,6 +1163,36 @@ module.exports.CreateDB = function (parent, func) {
1163 setupFunctions(func); // Completed setup of NeDB
1164 }
1165
1166 + // Create the PostgreSQL tables
1167 + function postgreSqlCreateTables(func) {
1168 + // Database was created, create the tables
1169 + parent.debug('db', 'Creating tables...');
1170 + sqlDbBatchExec([
1171 + '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)',
1172 + '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)',
1173 + '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)',
1174 + 'CREATE TABLE IF NOT EXISTS serverstats (time TIMESTAMP PRIMARY KEY, expire TIMESTAMP, doc JSON)',
1175 + 'CREATE TABLE IF NOT EXISTS power (id SERIAL PRIMARY KEY, time TIMESTAMP, nodeid CHAR(255), doc JSON)',
1176 + 'CREATE TABLE IF NOT EXISTS smbios (id CHAR(255) PRIMARY KEY, time TIMESTAMP, expire TIMESTAMP, doc JSON)',
1177 + 'CREATE TABLE IF NOT EXISTS plugin (id SERIAL PRIMARY KEY, doc JSON)'
1178 + ], function (results) {
1179 + parent.debug('db', 'Creating indexes...');
1180 + sqlDbExec('CREATE INDEX ndxtypedomainextra ON main (type, domain, extra)', null, function (err, response) { });
1181 + sqlDbExec('CREATE INDEX ndxextra ON main (extra)', null, function (err, response) { });
1182 + sqlDbExec('CREATE INDEX ndxextraex ON main (extraex)', null, function (err, response) { });
1183 + sqlDbExec('CREATE INDEX ndxeventstime ON events(time)', null, function (err, response) { });
1184 + sqlDbExec('CREATE INDEX ndxeventsusername ON events(domain, userid, time)', null, function (err, response) { });
1185 + sqlDbExec('CREATE INDEX ndxeventsdomainnodeidtime ON events(domain, nodeid, time)', null, function (err, response) { });
1186 + sqlDbExec('CREATE INDEX ndxeventids ON eventids(target)', null, function (err, response) { });
1187 + sqlDbExec('CREATE INDEX ndxserverstattime ON serverstats (time)', null, function (err, response) { });
1188 + sqlDbExec('CREATE INDEX ndxserverstatexpire ON serverstats (expire)', null, function (err, response) { });
1189 + sqlDbExec('CREATE INDEX ndxpowernodeidtime ON power (nodeid, time)', null, function (err, response) { });
1190 + sqlDbExec('CREATE INDEX ndxsmbiostime ON smbios (time)', null, function (err, response) { });
1191 + sqlDbExec('CREATE INDEX ndxsmbiosexpire ON smbios (expire)', null, function (err, response) { });
1192 + setupFunctions(func);
1193 + });
1194 + }
1195 +
1196 // Check the object names for a "."
1197 function checkObjectNames(r, tag) {
1198 if (typeof r != 'object') return;