Updated transaction
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2022-09-08 09:21:05 +02:00
parent ab86ba0652
commit 151e1b52ed
1 changed files with 100 additions and 91 deletions

View File

@ -20,7 +20,11 @@ module.exports = Self => {
const models = Self.app.models;
// Get files checksum
const files = await Self.rawSql('SELECT name, checksum, keyValue FROM edi.fileConfig');
const tx = await Self.beginTransaction({});
try {
const options = {transaction: tx};
const files = await Self.rawSql('SELECT name, checksum, keyValue FROM edi.fileConfig', null, options);
const updatableFiles = [];
for (const file of files) {
@ -51,7 +55,7 @@ module.exports = Self => {
const tables = await Self.rawSql(`
SELECT fileName, toTable, file
FROM edi.tableConfig
WHERE file IN (?)`, [fileNames]);
WHERE file IN (?)`, [fileNames], options);
for (const table of tables) {
const fileName = table.file;
@ -74,7 +78,7 @@ module.exports = Self => {
await extractFile(fileName, tempFile, tempDir);
console.debug(`Updating table ${table.toTable}...`);
await dumpData(tempDir, table);
await dumpData(tempDir, table, options);
}
// Update files checksum
@ -84,7 +88,7 @@ module.exports = Self => {
UPDATE edi.fileConfig
SET checksum = ?
WHERE name = ?`,
[file.checksum, file.name]);
[file.checksum, file.name], options);
}
// Clean files
@ -95,6 +99,11 @@ module.exports = Self => {
if (error.code !== 'ENOENT')
throw e;
}
} catch (error) {
await tx.rollback();
console.log(error);
throw error;
}
return true;
};
@ -197,14 +206,14 @@ module.exports = Self => {
}
}
async function dumpData(tempDir, table) {
async function dumpData(tempDir, table, options) {
const toTable = table.toTable;
const baseName = table.fileName;
console.log(`Starting transaction...`);
// const tx = await Self.beginTransaction({});
console.log(`Started transaction`);
try {
// try {
// const options = {transaction: tx};
console.log(`Emptying table ${toTable}...`);
@ -224,21 +233,21 @@ module.exports = Self => {
const sqlTemplate = await fs.readFile(templatePath, 'utf8');
const filePath = path.join(tempDir, file);
await Self.rawSql(sqlTemplate, [filePath]);
await Self.rawSql(sqlTemplate, [filePath], options);
await Self.rawSql(`
UPDATE edi.tableConfig
SET updated = ?
WHERE fileName = ?
`, [new Date(), baseName]);
`, [new Date(), baseName], options);
}
// await tx.commit();
console.log(`Closed transaction`);
} catch (error) {
// console.log(`Closed transaction`);
// } catch (error) {
// await tx.rollback();
console.log(error);
throw error;
}
// console.log(error);
// throw error;
// }
console.log(`Updated table ${toTable}\n`);
}
};