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

This commit is contained in:
Ylian Saint-Hilaire 2022-01-19 12:21:50 -08:00
parent c8ef09f94c
commit 8acbfd2a30

12
db.js
View File

@ -686,8 +686,10 @@ module.exports.CreateDB = function (parent, func) {
const { Pool, Client } = require('pg');
connectinArgs.database = dbname;
Datastore = new Client(connectinArgs);
Datastore.connect()
parent.debug('db', 'Checking tables...');
Datastore.connect();
if (err == null) {
// Database was created, create the tables
parent.debug('db', 'Creating tables...');
sqlDbBatchExec([
'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)',
'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)',
@ -697,7 +699,7 @@ module.exports.CreateDB = function (parent, func) {
'CREATE TABLE IF NOT EXISTS smbios (id CHAR(255) PRIMARY KEY, time TIMESTAMP, expire TIMESTAMP, doc JSON)',
'CREATE TABLE IF NOT EXISTS plugin (id SERIAL PRIMARY KEY, doc JSON)'
], function (results) {
parent.debug('db', 'Checking indexes...');
parent.debug('db', 'Creating indexes...');
sqlDbExec('CREATE INDEX ndxtypedomainextra ON main (type, domain, extra)', null, function (err, response) { });
sqlDbExec('CREATE INDEX ndxextra ON main (extra)', null, function (err, response) { });
sqlDbExec('CREATE INDEX ndxextraex ON main (extraex)', null, function (err, response) { });
@ -712,6 +714,10 @@ module.exports.CreateDB = function (parent, func) {
sqlDbExec('CREATE INDEX ndxsmbiosexpire ON smbios (expire)', null, function (err, response) { });
setupFunctions(func);
});
} else {
// Database already existed, skip table and index creation
setupFunctions(func);
}
});
} else if (parent.args.mongodb) {
// Use MongoDB