X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fdatabase-utils.ts;h=0e6b3550371e721624652166ea03d2435538b5c6;hb=4565774669bc3c1b11cc726d577946953dbe53c5;hp=7befa2c4955b934f397c6ccaf7781fe795e4c8c5;hpb=1c5e49e75284100b7b1fc8b4e73c8ba53fe22e89;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts index 7befa2c49..0e6b35503 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,6 +68,8 @@ function transactionRetryer (func: (err: any, data: T) => any) { }) } +// --------------------------------------------------------------------------- + function updateInstanceWithAnother > (instanceToUpdate: T, baseInstance: U) { const obj = baseInstance.toJSON() @@ -76,37 +78,33 @@ function updateInstanceWithAnother > (instanc } } -function resetSequelizeInstance (instance: Model, savedFields: object) { - Object.keys(savedFields).forEach(key => { - instance[key] = savedFields[key] - }) -} - -function afterCommitIfTransaction (t: Transaction, fn: Function) { - if (t) return t.afterCommit(() => fn()) - - return fn() +function resetSequelizeInstance (instance: Model) { + instance.set(instance.previous()) } -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 +115,7 @@ export { transactionRetryer, updateInstanceWithAnother, afterCommitIfTransaction, - deleteNonExistingModels, - setAsUpdated + filterNonExistingModels, + deleteAllModels, + runInReadCommittedTransaction }