fix: refs #8361 streamline transaction handling in exchangeRateUpdate
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Javi Gallego 2025-01-07 14:53:26 +01:00
parent 786f1fe661
commit dbd8d816c0
2 changed files with 9 additions and 10 deletions

View File

@ -16,12 +16,14 @@ module.exports = Self => {
Self.exchangeRateUpdate = async(options = {}) => { Self.exchangeRateUpdate = async(options = {}) => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {}; const myOptions = {};
Object.assign(myOptions, options); let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
let createdTx = false;
if (!myOptions.transaction) { if (!myOptions.transaction) {
myOptions.transaction = await Self.beginTransaction({}); tx = await Self.beginTransaction({});
createdTx = true; myOptions.transaction = tx;
} }
try { try {
@ -97,12 +99,9 @@ module.exports = Self => {
} }
} }
if (createdTx) if (tx) await tx.commit();
await myOptions.transaction.commit();
} catch (error) { } catch (error) {
if (createdTx) if (tx) await tx.rollback();
await myOptions.transaction.rollback();
throw error; throw error;
} }
}; };

View File

@ -142,7 +142,7 @@ describe('exchangeRateUpdate functionality', function() {
expect(afterCount - beforeCount).toBe(4); expect(afterCount - beforeCount).toBe(4);
}); });
fit('should create entries for day1 and day2 from the feed, and not backfill day3', async function() { it('should create entries for day1 and day2 from the feed, and not backfill day3', async function() {
const lastRate = await models.ReferenceRate.findOne({order: 'dated DESC'}, options); const lastRate = await models.ReferenceRate.findOne({order: 'dated DESC'}, options);
if (!lastRate) return fail('No existing ReferenceRate data in DB'); if (!lastRate) return fail('No existing ReferenceRate data in DB');