X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fdatabase-utils.ts;h=62738108607f650af0ca63b25de506f4fb4de03f;hb=91a4893063402d7beabb3104f9b989b8f88b6038;hp=f9cb33acafbb4729b4b997e603e9e5805ad4c9a4;hpb=ad35265d743e621d86f3f0796dd9d8795c599dca;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts index f9cb33aca..627381086 100644 --- a/server/helpers/database-utils.ts +++ b/server/helpers/database-utils.ts @@ -1,12 +1,12 @@ -import * as retry from 'async/retry' -import * as Bluebird from 'bluebird' -import { QueryTypes, Transaction } from 'sequelize' +import retry from 'async/retry' +import Bluebird from 'bluebird' +import { Transaction } from 'sequelize' import { Model } from 'sequelize-typescript' import { sequelizeTypescript } from '@server/initializers/database' import { logger } from './logger' function retryTransactionWrapper ( - functionToRetry: (arg1: A, arg2: B, arg3: C, arg4: D) => Promise | Bluebird, + functionToRetry: (arg1: A, arg2: B, arg3: C, arg4: D) => Promise, arg1: A, arg2: B, arg3: C, @@ -14,20 +14,20 @@ function retryTransactionWrapper ( ): Promise function retryTransactionWrapper ( - functionToRetry: (arg1: A, arg2: B, arg3: C) => Promise | Bluebird, + functionToRetry: (arg1: A, arg2: B, arg3: C) => Promise, arg1: A, arg2: B, arg3: C ): Promise function retryTransactionWrapper ( - functionToRetry: (arg1: A, arg2: B) => Promise | Bluebird, + functionToRetry: (arg1: A, arg2: B) => Promise, arg1: A, arg2: B ): Promise function retryTransactionWrapper ( - functionToRetry: (arg1: A) => Promise | Bluebird, + functionToRetry: (arg1: A) => Promise, arg1: A ): Promise @@ -36,7 +36,7 @@ function retryTransactionWrapper ( ): Promise function retryTransactionWrapper ( - functionToRetry: (...args: any[]) => Promise | Bluebird, + functionToRetry: (...args: any[]) => Promise, ...args: any[] ): Promise { return transactionRetryer(callback => { @@ -45,7 +45,7 @@ function retryTransactionWrapper ( .catch(err => callback(err)) }) .catch(err => { - logger.error(`Cannot execute ${functionToRetry.name} with many retries.`, { err }) + logger.warn(`Cannot execute ${functionToRetry.name} with many retries.`, { err }) throw err }) } @@ -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,7 +68,9 @@ function transactionRetryer (func: (err: any, data: T) => any) { }) } -function updateInstanceWithAnother > (instanceToUpdate: Model, baseInstance: Model) { +// --------------------------------------------------------------------------- + +function updateInstanceWithAnother > (instanceToUpdate: T, baseInstance: U) { const obj = baseInstance.toJSON() for (const key of Object.keys(obj)) { @@ -82,31 +84,29 @@ function resetSequelizeInstance (instance: Model, savedFields: object) { }) } -function afterCommitIfTransaction (t: Transaction, fn: Function) { - if (t) return t.afterCommit(() => fn()) - - return fn() -} - -function deleteNonExistingModels > ( +function filterNonExistingModels ( fromDatabase: T[], - newModels: T[], - t: Transaction + newModels: T[] ) { return fromDatabase.filter(f => !newModels.find(newModel => newModel.hasSameUniqueKeysThan(f))) - .map(f => f.destroy({ transaction: t })) } -// Sequelize always skip the update if we only update updatedAt field -function setAsUpdated (table: string, id: number, transaction?: Transaction) { - return sequelizeTypescript.query( - `UPDATE "${table}" SET "updatedAt" = :updatedAt WHERE id = :id`, - { - replacements: { table, id, updatedAt: new Date() }, - type: QueryTypes.UPDATE, - transaction - } - ) +function deleteAllModels > (models: T[], transaction: Transaction) { + return Promise.all(models.map(f => f.destroy({ 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() } // --------------------------------------------------------------------------- @@ -117,6 +117,7 @@ export { transactionRetryer, updateInstanceWithAnother, afterCommitIfTransaction, - deleteNonExistingModels, - setAsUpdated + filterNonExistingModels, + deleteAllModels, + runInReadCommittedTransaction }