createDb, dropDb - catch errors

This commit is contained in:
Jan Prochazka
2024-08-29 19:50:46 +02:00
parent 967daf3bb6
commit b747c750e8
3 changed files with 38 additions and 33 deletions

View File

@@ -59,7 +59,7 @@ module.exports = {
if (connection.useRedirectDbLogin) {
throw new MissingCredentialsError({ conid, redirectToDbLogin: true });
}
const subprocess = fork(
const subprocess = fork(
global['API_PACKAGE'] || process.argv[1],
[
'--is-forked-api',
@@ -184,22 +184,29 @@ module.exports = {
return { status: 'ok' };
},
createDatabase_meta: true,
async createDatabase({ conid, name }, req) {
async sendDatabaseOp({ conid, msgtype, name }, req) {
testConnectionPermission(conid, req);
const opened = await this.ensureOpened(conid);
if (opened.connection.isReadOnly) return false;
opened.subprocess.send({ msgtype: 'createDatabase', name });
return { status: 'ok' };
const res = await this.sendRequest(opened, { msgtype, name });
if (res.errorMessage) {
console.error(res.errorMessage);
return {
apiErrorMessage: res.errorMessage,
};
}
return res.result || null;
},
createDatabase_meta: true,
async createDatabase({ conid, name }, req) {
return this.sendDatabaseOp({ conid, msgtype: 'createDatabase', name }, req);
},
dropDatabase_meta: true,
async dropDatabase({ conid, name }, req) {
testConnectionPermission(conid, req);
const opened = await this.ensureOpened(conid);
if (opened.connection.isReadOnly) return false;
opened.subprocess.send({ msgtype: 'dropDatabase', name });
return { status: 'ok' };
return this.sendDatabaseOp({ conid, msgtype: 'dropDatabase', name }, req);
},
sendRequest(conn, message) {

View File

@@ -105,18 +105,24 @@ function handlePing() {
lastPing = new Date().getTime();
}
async function handleDatabaseOp(op, { name }) {
const driver = requireEngineDriver(storedConnection);
systemConnection = await connectUtility(driver, storedConnection, 'app');
if (driver[op]) {
await driver[op](systemConnection, name);
} else {
const dmp = driver.createDumper();
dmp[op](name);
logger.info({ sql: dmp.s }, 'Running script');
await driver.query(systemConnection, dmp.s);
async function handleDatabaseOp(op, { msgid, name }) {
try {
const driver = requireEngineDriver(storedConnection);
systemConnection = await connectUtility(driver, storedConnection, 'app');
if (driver[op]) {
await driver[op](systemConnection, name);
} else {
const dmp = driver.createDumper();
dmp[op](name);
logger.info({ sql: dmp.s }, 'Running script');
await driver.query(systemConnection, dmp.s);
}
await handleRefresh();
process.send({ msgtype: 'response', msgid, status: 'ok' });
} catch (err) {
process.send({ msgtype: 'response', msgid, errorMessage: err.message });
}
await handleRefresh();
}
async function handleDriverDataCore(msgid, callMethod) {