mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2025-02-12 11:01:52 +00:00
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
1 changed files with 19 additions and 12 deletions
31
db.js
31
db.js
|
@ -785,20 +785,28 @@ module.exports.CreateDB = function (parent, func) {
|
||||||
var dbname = (connectinArgs.database != null) ? connectinArgs.database : 'meshcentral';
|
var dbname = (connectinArgs.database != null) ? connectinArgs.database : 'meshcentral';
|
||||||
delete connectinArgs.database;
|
delete connectinArgs.database;
|
||||||
obj.databaseType = 6;
|
obj.databaseType = 6;
|
||||||
const pgtools = require('pgtools');
|
const { Pool, Client } = require('pg');
|
||||||
pgtools.createdb(connectinArgs, dbname, function (err, res) {
|
connectinArgs.database = dbname;
|
||||||
const { Pool, Client } = require('pg');
|
Datastore = new Client(connectinArgs);
|
||||||
connectinArgs.database = dbname;
|
Datastore.connect();
|
||||||
Datastore = new Client(connectinArgs);
|
sqlDbQuery('SELECT 1 FROM pg_database WHERE datname = $1', [dbname], function (dberr, dbdocs) { // check database exists first before creating
|
||||||
Datastore.connect();
|
if (dberr == null) { // database exists now check tables exists
|
||||||
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) {
|
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
|
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) {
|
} else if (parent.args.mongodb) {
|
||||||
|
@ -1260,7 +1268,6 @@ module.exports.CreateDB = function (parent, func) {
|
||||||
} else if (obj.databaseType == 6) { // Postgres SQL
|
} else if (obj.databaseType == 6) { // Postgres SQL
|
||||||
Datastore.query(query, args, function (error, results) {
|
Datastore.query(query, args, function (error, results) {
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
console.log(query, args, error);
|
|
||||||
if (func) try { func(error); } catch (ex) { console.log('SQLERR4', ex); }
|
if (func) try { func(error); } catch (ex) { console.log('SQLERR4', ex); }
|
||||||
} else {
|
} else {
|
||||||
var docs = [];
|
var docs = [];
|
||||||
|
|
Loading…
Reference in a new issue