X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fdatabase-utils.ts;h=b6ba7fd75a846b449cf127ea511f4ef9d31e3946;hb=472170b4f923a52cceb595221864eab61d624d5b;hp=b5dc70c17dbb33ae9871baf9754628259f95a185;hpb=cf21b2cbef61929177b9c09b5e017c3b7eb8535d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts index b5dc70c17..b6ba7fd75 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 }) } @@ -68,41 +68,29 @@ function transactionRetryer (func: (err: any, data: T) => any) { }) } -// --------------------------------------------------------------------------- - -function updateInstanceWithAnother > (instanceToUpdate: T, baseInstance: U) { - const obj = baseInstance.toJSON() - - for (const key of Object.keys(obj)) { - instanceToUpdate[key] = obj[key] - } +function saveInTransactionWithRetries > (model: T) { + return retryTransactionWrapper(() => { + return sequelizeTypescript.transaction(async transaction => { + await model.save({ transaction }) + }) + }) } -function resetSequelizeInstance (instance: Model, savedFields: object) { - Object.keys(savedFields).forEach(key => { - instance[key] = savedFields[key] - }) +// --------------------------------------------------------------------------- + +function resetSequelizeInstance (instance: Model) { + return instance.reload() } -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 }))) } // --------------------------------------------------------------------------- @@ -125,9 +113,9 @@ export { resetSequelizeInstance, retryTransactionWrapper, transactionRetryer, - updateInstanceWithAnother, + saveInTransactionWithRetries, afterCommitIfTransaction, - deleteNonExistingModels, - setAsUpdated, + filterNonExistingModels, + deleteAllModels, runInReadCommittedTransaction }