close dbhandles after shell script (missing tableReader)

This commit is contained in:
SPRINX0\prochazka
2024-10-01 10:56:52 +02:00
parent 9910c54aa6
commit ab28a06bef
8 changed files with 141 additions and 101 deletions

View File

@@ -19,32 +19,39 @@ async function dataDuplicator({
systemConnection,
}) {
if (!driver) driver = requireEngineDriver(connection);
const pool = systemConnection || (await connectUtility(driver, connection, 'write'));
const dbhan = systemConnection || (await connectUtility(driver, connection, 'write'));
logger.info(`Connected.`);
try {
logger.info(`Connected.`);
if (!analysedStructure) {
analysedStructure = await driver.analyseFull(pool);
if (!analysedStructure) {
analysedStructure = await driver.analyseFull(dbhan);
}
const dupl = new DataDuplicator(
dbhan,
driver,
analysedStructure,
items.map(item => ({
name: item.name,
operation: item.operation,
matchColumns: item.matchColumns,
openStream:
item.openStream ||
(() => jsonLinesReader({ fileName: path.join(resolveArchiveFolder(archive), `${item.name}.jsonl`) })),
})),
stream,
copyStream,
options
);
await dupl.run();
} finally {
if (!systemConnection) {
await driver.close(dbhan);
}
}
const dupl = new DataDuplicator(
pool,
driver,
analysedStructure,
items.map(item => ({
name: item.name,
operation: item.operation,
matchColumns: item.matchColumns,
openStream:
item.openStream ||
(() => jsonLinesReader({ fileName: path.join(resolveArchiveFolder(archive), `${item.name}.jsonl`) })),
})),
stream,
copyStream,
options
);
await dupl.run();
}
module.exports = dataDuplicator;

View File

@@ -27,15 +27,23 @@ async function dumpDatabase({
logger.info(`Dumping database`);
if (!driver) driver = requireEngineDriver(connection);
const pool = systemConnection || (await connectUtility(driver, connection, 'read', { forceRowsAsObjects: true }));
logger.info(`Connected.`);
const dumper = await driver.createBackupDumper(pool, {
outputFile,
databaseName,
schemaName,
});
await doDump(dumper);
const dbhan = systemConnection || (await connectUtility(driver, connection, 'read', { forceRowsAsObjects: true }));
try {
logger.info(`Connected.`);
const dumper = await driver.createBackupDumper(dbhan, {
outputFile,
databaseName,
schemaName,
});
await doDump(dumper);
} finally {
if (!systemConnection) {
await driver.close(dbhan);
}
}
}
module.exports = dumpDatabase;

View File

@@ -9,12 +9,15 @@ async function executeQuery({ connection = undefined, systemConnection = undefin
if (!driver) driver = requireEngineDriver(connection);
const dbhan = systemConnection || (await connectUtility(driver, connection, 'script'));
logger.info(`Connected.`);
await driver.script(dbhan, sql);
try {
logger.info(`Connected.`);
if (!systemConnection) {
await driver.close(dbhan);
await driver.script(dbhan, sql);
} finally {
if (!systemConnection) {
await driver.close(dbhan);
}
}
}

View File

@@ -22,44 +22,48 @@ async function generateDeploySql({
if (!driver) driver = requireEngineDriver(connection);
const dbhan = systemConnection || (await connectUtility(driver, connection, 'read'));
if (!analysedStructure) {
analysedStructure = await driver.analyseFull(dbhan);
try {
if (!analysedStructure) {
analysedStructure = await driver.analyseFull(dbhan);
}
const deployedModel = generateDbPairingId(
extendDatabaseInfo(loadedDbModel ? databaseInfoFromYamlModel(loadedDbModel) : await importDbModel(modelFolder))
);
const currentModel = generateDbPairingId(extendDatabaseInfo(analysedStructure));
const opts = {
...modelCompareDbDiffOptions,
noDropTable: true,
noDropColumn: true,
noDropConstraint: true,
noDropSqlObject: true,
noRenameTable: true,
noRenameColumn: true,
};
const currentModelPaired = matchPairedObjects(deployedModel, currentModel, opts);
const currentModelPairedPreloaded = await enrichWithPreloadedRows(deployedModel, currentModelPaired, dbhan, driver);
// console.log('currentModelPairedPreloaded', currentModelPairedPreloaded.tables[0]);
// console.log('deployedModel', deployedModel.tables[0]);
// console.log('currentModel', currentModel.tables[0]);
// console.log('currentModelPaired', currentModelPaired.tables[0]);
const res = getAlterDatabaseScript(
currentModelPairedPreloaded,
deployedModel,
opts,
currentModelPairedPreloaded,
deployedModel,
driver
);
return res;
} finally {
if (!systemConnection) {
await driver.close(dbhan);
}
}
const deployedModel = generateDbPairingId(
extendDatabaseInfo(loadedDbModel ? databaseInfoFromYamlModel(loadedDbModel) : await importDbModel(modelFolder))
);
const currentModel = generateDbPairingId(extendDatabaseInfo(analysedStructure));
const opts = {
...modelCompareDbDiffOptions,
noDropTable: true,
noDropColumn: true,
noDropConstraint: true,
noDropSqlObject: true,
noRenameTable: true,
noRenameColumn: true,
};
const currentModelPaired = matchPairedObjects(deployedModel, currentModel, opts);
const currentModelPairedPreloaded = await enrichWithPreloadedRows(deployedModel, currentModelPaired, dbhan, driver);
// console.log('currentModelPairedPreloaded', currentModelPairedPreloaded.tables[0]);
// console.log('deployedModel', deployedModel.tables[0]);
// console.log('currentModel', currentModel.tables[0]);
// console.log('currentModelPaired', currentModelPaired.tables[0]);
const res = getAlterDatabaseScript(
currentModelPairedPreloaded,
deployedModel,
opts,
currentModelPairedPreloaded,
deployedModel,
driver
);
if (!systemConnection) {
await driver.close(dbhan);
}
return res;
}
module.exports = generateDeploySql;

View File

@@ -59,21 +59,27 @@ async function importDatabase({ connection = undefined, systemConnection = undef
if (!driver) driver = requireEngineDriver(connection);
const dbhan = systemConnection || (await connectUtility(driver, connection, 'write'));
logger.info(`Connected.`);
try {
logger.info(`Connected.`);
logger.info(`Input file: ${inputFile}`);
const downloadedFile = await download(inputFile);
logger.info(`Downloaded file: ${downloadedFile}`);
logger.info(`Input file: ${inputFile}`);
const downloadedFile = await download(inputFile);
logger.info(`Downloaded file: ${downloadedFile}`);
const fileStream = fs.createReadStream(downloadedFile, 'utf-8');
const splittedStream = splitQueryStream(fileStream, {
...driver.getQuerySplitterOptions('import'),
returnRichInfo: true,
});
const importStream = new ImportStream(dbhan, driver);
// @ts-ignore
splittedStream.pipe(importStream);
await awaitStreamEnd(importStream);
const fileStream = fs.createReadStream(downloadedFile, 'utf-8');
const splittedStream = splitQueryStream(fileStream, {
...driver.getQuerySplitterOptions('import'),
returnRichInfo: true,
});
const importStream = new ImportStream(dbhan, driver);
// @ts-ignore
splittedStream.pipe(importStream);
await awaitStreamEnd(importStream);
} finally {
if (!systemConnection) {
await driver.close(dbhan);
}
}
}
module.exports = importDatabase;

View File

@@ -9,13 +9,19 @@ async function loadDatabase({ connection = undefined, systemConnection = undefin
logger.info(`Analysing database`);
if (!driver) driver = requireEngineDriver(connection);
const pool = systemConnection || (await connectUtility(driver, connection, 'read', { forceRowsAsObjects: true }));
logger.info(`Connected.`);
const dbhan = systemConnection || (await connectUtility(driver, connection, 'read', { forceRowsAsObjects: true }));
try {
logger.info(`Connected.`);
const dbInfo = await driver.analyseFull(pool);
logger.info(`Analyse finished`);
const dbInfo = await driver.analyseFull(dbhan);
logger.info(`Analyse finished`);
await exportDbModel(dbInfo, outputDir);
await exportDbModel(dbInfo, outputDir);
} finally {
if (!systemConnection) {
await driver.close(dbhan);
}
}
}
module.exports = loadDatabase;

View File

@@ -3,9 +3,9 @@ const requireEngineDriver = require('../utility/requireEngineDriver');
const connectUtility = require('../utility/connectUtility');
const logger = getLogger('tableReader');
async function tableReader({ connection, pureName, schemaName }) {
async function tableReader({ connection, systemConnection, pureName, schemaName }) {
const driver = requireEngineDriver(connection);
const pool = await connectUtility(driver, connection, 'read');
const dbhan = systemConnection || (await connectUtility(driver, connection, 'read'));
logger.info(`Connected.`);
const fullName = { pureName, schemaName };
@@ -14,26 +14,26 @@ async function tableReader({ connection, pureName, schemaName }) {
// @ts-ignore
logger.info(`Reading collection ${fullNameToString(fullName)}`);
// @ts-ignore
return await driver.readQuery(pool, JSON.stringify(fullName));
return await driver.readQuery(dbhan, JSON.stringify(fullName));
}
const table = await driver.analyseSingleObject(pool, fullName, 'tables');
const table = await driver.analyseSingleObject(dbhan, fullName, 'tables');
const query = `select * from ${quoteFullName(driver.dialect, fullName)}`;
if (table) {
// @ts-ignore
logger.info(`Reading table ${fullNameToString(table)}`);
// @ts-ignore
return await driver.readQuery(pool, query, table);
return await driver.readQuery(dbhan, query, table);
}
const view = await driver.analyseSingleObject(pool, fullName, 'views');
const view = await driver.analyseSingleObject(dbhan, fullName, 'views');
if (view) {
// @ts-ignore
logger.info(`Reading view ${fullNameToString(view)}`);
// @ts-ignore
return await driver.readQuery(pool, query, view);
return await driver.readQuery(dbhan, query, view);
}
return await driver.readQuery(pool, query);
return await driver.readQuery(dbhan, query);
}
module.exports = tableReader;

View File

@@ -9,10 +9,16 @@ async function tableWriter({ connection, schemaName, pureName, driver, systemCon
if (!driver) {
driver = requireEngineDriver(connection);
}
const pool = systemConnection || (await connectUtility(driver, connection, 'write'));
const dbhan = systemConnection || (await connectUtility(driver, connection, 'write'));
logger.info(`Connected.`);
return await driver.writeTable(pool, { schemaName, pureName }, options);
try {
logger.info(`Connected.`);
return await driver.writeTable(dbhan, { schemaName, pureName }, options);
} finally {
if (!systemConnection) {
await driver.close(dbhan);
}
}
}
module.exports = tableWriter;