X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fdatabase-utils.ts;h=7befa2c4955b934f397c6ccaf7781fe795e4c8c5;hb=8ee37c5f38b0f9b7e97239197d5590109c163250;hp=dcc9e257714cbdf25667a151770e219d908c7b08;hpb=eb08047657e739bcd9e592d76307befa3998482b;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts index dcc9e2577..7befa2c49 100644 --- a/server/helpers/database-utils.ts +++ b/server/helpers/database-utils.ts @@ -1,40 +1,122 @@ -// TODO: import from ES6 when retry typing file will include errorFilter function import * as retry from 'async/retry' - +import * as Bluebird from 'bluebird' +import { QueryTypes, Transaction } from 'sequelize' +import { Model } from 'sequelize-typescript' +import { sequelizeTypescript } from '@server/initializers/database' import { logger } from './logger' -type RetryTransactionWrapperOptions = { errorMessage: string, arguments?: any[] } -function retryTransactionWrapper (functionToRetry: (... args) => Promise, options: RetryTransactionWrapperOptions) { - const args = options.arguments ? options.arguments : [] +function retryTransactionWrapper ( + functionToRetry: (arg1: A, arg2: B, arg3: C, arg4: D) => Promise | Bluebird, + arg1: A, + arg2: B, + arg3: C, + arg4: D, +): Promise + +function retryTransactionWrapper ( + functionToRetry: (arg1: A, arg2: B, arg3: C) => Promise | Bluebird, + arg1: A, + arg2: B, + arg3: C +): Promise + +function retryTransactionWrapper ( + functionToRetry: (arg1: A, arg2: B) => Promise | Bluebird, + arg1: A, + arg2: B +): Promise + +function retryTransactionWrapper ( + functionToRetry: (arg1: A) => Promise | Bluebird, + arg1: A +): Promise + +function retryTransactionWrapper ( + functionToRetry: () => Promise | Bluebird +): Promise - return transactionRetryer(callback => { - functionToRetry.apply(this, args) - .then(result => callback(null, result)) +function retryTransactionWrapper ( + functionToRetry: (...args: any[]) => Promise | Bluebird, + ...args: any[] +): Promise { + return transactionRetryer(callback => { + functionToRetry.apply(null, args) + .then((result: T) => callback(null, result)) .catch(err => callback(err)) }) .catch(err => { - // Do not throw the error, continue the process - logger.error(options.errorMessage, err) + logger.error(`Cannot execute ${functionToRetry.name} with many retries.`, { err }) + throw err + }) +} + +function transactionRetryer (func: (err: any, data: T) => any) { + return new Promise((res, rej) => { + retry( + { + times: 5, + + errorFilter: err => { + const willRetry = (err.name === 'SequelizeDatabaseError') + logger.debug('Maybe retrying the transaction function.', { willRetry, err }) + return willRetry + } + }, + func, + (err, data) => err ? rej(err) : res(data) + ) }) } -function transactionRetryer (func: Function) { - return new Promise((res, rej) => { - retry({ - times: 5, +function updateInstanceWithAnother > (instanceToUpdate: T, baseInstance: U) { + const obj = baseInstance.toJSON() - errorFilter: err => { - const willRetry = (err.name === 'SequelizeDatabaseError') - logger.debug('Maybe retrying the transaction function.', { willRetry }) - return willRetry - } - }, func, err => err ? rej(err) : res()) + for (const key of Object.keys(obj)) { + instanceToUpdate[key] = obj[key] + } +} + +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 deleteNonExistingModels > ( + fromDatabase: T[], + newModels: T[], + t: Transaction +) { + 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 + } + ) +} + // --------------------------------------------------------------------------- export { + resetSequelizeInstance, retryTransactionWrapper, - transactionRetryer + transactionRetryer, + updateInstanceWithAnother, + afterCommitIfTransaction, + deleteNonExistingModels, + setAsUpdated }