]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/database-utils.ts
Fix reset sequelize instance
[github/Chocobozzz/PeerTube.git] / server / helpers / database-utils.ts
1 import retry from 'async/retry'
2 import Bluebird from 'bluebird'
3 import { Transaction } from 'sequelize'
4 import { Model } from 'sequelize-typescript'
5 import { sequelizeTypescript } from '@server/initializers/database'
6 import { logger } from './logger'
7
8 function retryTransactionWrapper <T, A, B, C, D> (
9 functionToRetry: (arg1: A, arg2: B, arg3: C, arg4: D) => Promise<T>,
10 arg1: A,
11 arg2: B,
12 arg3: C,
13 arg4: D,
14 ): Promise<T>
15
16 function retryTransactionWrapper <T, A, B, C> (
17 functionToRetry: (arg1: A, arg2: B, arg3: C) => Promise<T>,
18 arg1: A,
19 arg2: B,
20 arg3: C
21 ): Promise<T>
22
23 function retryTransactionWrapper <T, A, B> (
24 functionToRetry: (arg1: A, arg2: B) => Promise<T>,
25 arg1: A,
26 arg2: B
27 ): Promise<T>
28
29 function retryTransactionWrapper <T, A> (
30 functionToRetry: (arg1: A) => Promise<T>,
31 arg1: A
32 ): Promise<T>
33
34 function retryTransactionWrapper <T> (
35 functionToRetry: () => Promise<T> | Bluebird<T>
36 ): Promise<T>
37
38 function retryTransactionWrapper <T> (
39 functionToRetry: (...args: any[]) => Promise<T>,
40 ...args: any[]
41 ): Promise<T> {
42 return transactionRetryer<T>(callback => {
43 functionToRetry.apply(null, args)
44 .then((result: T) => callback(null, result))
45 .catch(err => callback(err))
46 })
47 .catch(err => {
48 logger.warn(`Cannot execute ${functionToRetry.name} with many retries.`, { err })
49 throw err
50 })
51 }
52
53 function transactionRetryer <T> (func: (err: any, data: T) => any) {
54 return new Promise<T>((res, rej) => {
55 retry(
56 {
57 times: 5,
58
59 errorFilter: err => {
60 const willRetry = (err.name === 'SequelizeDatabaseError')
61 logger.debug('Maybe retrying the transaction function.', { willRetry, err, tags: [ 'sql', 'retry' ] })
62 return willRetry
63 }
64 },
65 func,
66 (err, data) => err ? rej(err) : res(data)
67 )
68 })
69 }
70
71 // ---------------------------------------------------------------------------
72
73 function resetSequelizeInstance <T> (instance: Model<T>) {
74 return instance.reload()
75 }
76
77 function filterNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean }> (
78 fromDatabase: T[],
79 newModels: T[]
80 ) {
81 return fromDatabase.filter(f => !newModels.find(newModel => newModel.hasSameUniqueKeysThan(f)))
82 }
83
84 function deleteAllModels <T extends Pick<Model, 'destroy'>> (models: T[], transaction: Transaction) {
85 return Promise.all(models.map(f => f.destroy({ transaction })))
86 }
87
88 // ---------------------------------------------------------------------------
89
90 function runInReadCommittedTransaction <T> (fn: (t: Transaction) => Promise<T>) {
91 const options = { isolationLevel: Transaction.ISOLATION_LEVELS.READ_COMMITTED }
92
93 return sequelizeTypescript.transaction(options, t => fn(t))
94 }
95
96 function afterCommitIfTransaction (t: Transaction, fn: Function) {
97 if (t) return t.afterCommit(() => fn())
98
99 return fn()
100 }
101
102 // ---------------------------------------------------------------------------
103
104 export {
105 resetSequelizeInstance,
106 retryTransactionWrapper,
107 transactionRetryer,
108 afterCommitIfTransaction,
109 filterNonExistingModels,
110 deleteAllModels,
111 runInReadCommittedTransaction
112 }