check db exists first before creating in postgres (#5968)
Signed-off-by: si458 <simonsmith5521@gmail.com>
This commit is contained in:
parent
8e8cc4b327
commit
102489447d
31
db.js
31
db.js
|
@ -785,20 +785,28 @@ module.exports.CreateDB = function (parent, func) {
|
|||
var dbname = (connectinArgs.database != null) ? connectinArgs.database : 'meshcentral';
|
||||
delete connectinArgs.database;
|
||||
obj.databaseType = 6;
|
||||
const pgtools = require('pgtools');
|
||||
pgtools.createdb(connectinArgs, dbname, function (err, res) {
|
||||
const { Pool, Client } = require('pg');
|
||||
connectinArgs.database = dbname;
|
||||
Datastore = new Client(connectinArgs);
|
||||
Datastore.connect();
|
||||
if (err == null) {
|
||||
// Create the tables and indexes
|
||||
postgreSqlCreateTables(func);
|
||||
} else {
|
||||
// Database already existed, perform a test query to see if the main table is present
|
||||
const { Pool, Client } = require('pg');
|
||||
connectinArgs.database = dbname;
|
||||
Datastore = new Client(connectinArgs);
|
||||
Datastore.connect();
|
||||
sqlDbQuery('SELECT 1 FROM pg_database WHERE datname = $1', [dbname], function (dberr, dbdocs) { // check database exists first before creating
|
||||
if (dberr == null) { // database exists now check tables exists
|
||||
sqlDbQuery('SELECT doc FROM main WHERE id = $1', ['DatabaseIdentifier'], function (err, docs) {
|
||||
if (err == null) { setupFunctions(func); } else { postgreSqlCreateTables(func); } // If not present, create the tables and indexes
|
||||
});
|
||||
} else { // If not present, create the tables and indexes
|
||||
const pgtools = require('pgtools');
|
||||
pgtools.createdb(connectinArgs, dbname, function (err, res) {
|
||||
if (err == null) {
|
||||
// Create the tables and indexes
|
||||
postgreSqlCreateTables(func);
|
||||
} else {
|
||||
// Database already existed, perform a test query to see if the main table is present
|
||||
sqlDbQuery('SELECT doc FROM main WHERE id = $1', ['DatabaseIdentifier'], function (err, docs) {
|
||||
if (err == null) { setupFunctions(func); } else { postgreSqlCreateTables(func); } // If not present, create the tables and indexes
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else if (parent.args.mongodb) {
|
||||
|
@ -1260,7 +1268,6 @@ module.exports.CreateDB = function (parent, func) {
|
|||
} else if (obj.databaseType == 6) { // Postgres SQL
|
||||
Datastore.query(query, args, function (error, results) {
|
||||
if (error != null) {
|
||||
console.log(query, args, error);
|
||||
if (func) try { func(error); } catch (ex) { console.log('SQLERR4', ex); }
|
||||
} else {
|
||||
var docs = [];
|
||||
|
|
Loading…
Reference in New Issue