X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fdatabase-utils.ts;h=422774022ac549168faca01afa2a3b247c373c5d;hb=764b1a14fc494f2cfd7ea590d2f07b01df65c7ad;hp=b4adaf9cc2179bd4696bb4d6eef952173f39fe5e;hpb=94a5ff8a4a75d75bb9df542a39ce8769e7a7e6a4;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts index b4adaf9cc..422774022 100644 --- a/server/helpers/database-utils.ts +++ b/server/helpers/database-utils.ts @@ -1,53 +1,150 @@ import * as retry from 'async/retry' import * as Bluebird from 'bluebird' +import { BindOrReplacements, 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: (arg1: A, arg2: B, arg3: C, arg4: D) => Promise, + arg1: A, + arg2: B, + arg3: C, + arg4: D, +): Promise + +function retryTransactionWrapper ( + functionToRetry: (arg1: A, arg2: B, arg3: C) => Promise, + arg1: A, + arg2: B, + arg3: C +): Promise + +function retryTransactionWrapper ( + functionToRetry: (arg1: A, arg2: B) => Promise, + arg1: A, + arg2: B +): Promise + +function retryTransactionWrapper ( + functionToRetry: (arg1: A) => Promise, + arg1: A +): Promise + function retryTransactionWrapper ( - functionToRetry: (...args) => Promise | Bluebird, - options: RetryTransactionWrapperOptions -): Promise { - const args = options.arguments ? options.arguments : [] + functionToRetry: () => Promise | Bluebird +): Promise +function retryTransactionWrapper ( + functionToRetry: (...args: any[]) => Promise, + ...args: any[] +): Promise { return transactionRetryer(callback => { - functionToRetry.apply(this, args) + functionToRetry.apply(null, args) .then((result: T) => callback(null, result)) .catch(err => callback(err)) }) .catch(err => { - console.error(err) - 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 }) - return willRetry - } - }, func, (err, data) => err ? rej(err) : res(data)) + retry( + { + times: 5, + + errorFilter: err => { + const willRetry = (err.name === 'SequelizeDatabaseError') + logger.debug('Maybe retrying the transaction function.', { willRetry, err, tags: [ 'sql', 'retry' ] }) + return willRetry + } + }, + func, + (err, data) => err ? rej(err) : res(data) + ) }) } -function updateInstanceWithAnother (instanceToUpdate: Model, baseInstance: Model) { +// --------------------------------------------------------------------------- + +function updateInstanceWithAnother > (instanceToUpdate: T, baseInstance: U) { const obj = baseInstance.toJSON() for (const key of Object.keys(obj)) { - instanceToUpdate.set(key, obj[key]) + instanceToUpdate[key] = obj[key] } } +function resetSequelizeInstance (instance: Model, savedFields: object) { + Object.keys(savedFields).forEach(key => { + instance[key] = savedFields[key] + }) +} + +function filterNonExistingModels ( + fromDatabase: T[], + newModels: T[] +) { + return fromDatabase.filter(f => !newModels.find(newModel => newModel.hasSameUniqueKeysThan(f))) +} + +function deleteAllModels > (models: T[], transaction: Transaction) { + return Promise.all(models.map(f => f.destroy({ transaction }))) +} + +// 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 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() +} + +// --------------------------------------------------------------------------- + +function doesExist (query: string, bind?: BindOrReplacements) { + const options = { + type: QueryTypes.SELECT as QueryTypes.SELECT, + bind, + raw: true + } + + return sequelizeTypescript.query(query, options) + .then(results => results.length === 1) +} + // --------------------------------------------------------------------------- export { + resetSequelizeInstance, retryTransactionWrapper, transactionRetryer, - updateInstanceWithAnother + updateInstanceWithAnother, + afterCommitIfTransaction, + filterNonExistingModels, + deleteAllModels, + setAsUpdated, + runInReadCommittedTransaction, + doesExist }