X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fdatabase-utils.ts;h=b5dc70c17dbb33ae9871baf9754628259f95a185;hb=a6a12dae10658a1fa7120ad39702c07137143954;hp=7befa2c4955b934f397c6ccaf7781fe795e4c8c5;hpb=5b1a6d45b5d6e50102e1c7c8c845401b76b11b4d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts index 7befa2c49..b5dc70c17 100644 --- a/server/helpers/database-utils.ts +++ b/server/helpers/database-utils.ts @@ -58,7 +58,7 @@ function transactionRetryer (func: (err: any, data: T) => any) { errorFilter: err => { const willRetry = (err.name === 'SequelizeDatabaseError') - logger.debug('Maybe retrying the transaction function.', { willRetry, err }) + logger.debug('Maybe retrying the transaction function.', { willRetry, err, tags: [ 'sql', 'retry' ] }) return willRetry } }, @@ -68,6 +68,8 @@ function transactionRetryer (func: (err: any, data: T) => any) { }) } +// --------------------------------------------------------------------------- + function updateInstanceWithAnother > (instanceToUpdate: T, baseInstance: U) { const obj = baseInstance.toJSON() @@ -82,12 +84,6 @@ function resetSequelizeInstance (instance: Model, savedFields: object) { }) } -function afterCommitIfTransaction (t: Transaction, fn: Function) { - if (t) return t.afterCommit(() => fn()) - - return fn() -} - function deleteNonExistingModels > ( fromDatabase: T[], newModels: T[], @@ -111,6 +107,20 @@ function setAsUpdated (table: string, id: number, transaction?: Transaction) { // --------------------------------------------------------------------------- +function runInReadCommittedTransaction (fn: (t: Transaction) => Promise) { + const options = { isolationLevel: Transaction.ISOLATION_LEVELS.READ_COMMITTED } + + return sequelizeTypescript.transaction(options, t => fn(t)) +} + +function afterCommitIfTransaction (t: Transaction, fn: Function) { + if (t) return t.afterCommit(() => fn()) + + return fn() +} + +// --------------------------------------------------------------------------- + export { resetSequelizeInstance, retryTransactionWrapper, @@ -118,5 +128,6 @@ export { updateInstanceWithAnother, afterCommitIfTransaction, deleteNonExistingModels, - setAsUpdated + setAsUpdated, + runInReadCommittedTransaction }