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