aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/database-utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/database-utils.ts')
-rw-r--r--server/helpers/database-utils.ts121
1 files changed, 0 insertions, 121 deletions
diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts
deleted file mode 100644
index b6ba7fd75..000000000
--- a/server/helpers/database-utils.ts
+++ /dev/null
@@ -1,121 +0,0 @@
1import retry from 'async/retry'
2import Bluebird from 'bluebird'
3import { Transaction } from 'sequelize'
4import { Model } from 'sequelize-typescript'
5import { sequelizeTypescript } from '@server/initializers/database'
6import { logger } from './logger'
7
8function 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
16function 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
23function retryTransactionWrapper <T, A, B> (
24 functionToRetry: (arg1: A, arg2: B) => Promise<T>,
25 arg1: A,
26 arg2: B
27): Promise<T>
28
29function retryTransactionWrapper <T, A> (
30 functionToRetry: (arg1: A) => Promise<T>,
31 arg1: A
32): Promise<T>
33
34function retryTransactionWrapper <T> (
35 functionToRetry: () => Promise<T> | Bluebird<T>
36): Promise<T>
37
38function 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
53function 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
71function saveInTransactionWithRetries <T extends Pick<Model, 'save'>> (model: T) {
72 return retryTransactionWrapper(() => {
73 return sequelizeTypescript.transaction(async transaction => {
74 await model.save({ transaction })
75 })
76 })
77}
78
79// ---------------------------------------------------------------------------
80
81function resetSequelizeInstance <T> (instance: Model<T>) {
82 return instance.reload()
83}
84
85function filterNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean }> (
86 fromDatabase: T[],
87 newModels: T[]
88) {
89 return fromDatabase.filter(f => !newModels.find(newModel => newModel.hasSameUniqueKeysThan(f)))
90}
91
92function deleteAllModels <T extends Pick<Model, 'destroy'>> (models: T[], transaction: Transaction) {
93 return Promise.all(models.map(f => f.destroy({ transaction })))
94}
95
96// ---------------------------------------------------------------------------
97
98function runInReadCommittedTransaction <T> (fn: (t: Transaction) => Promise<T>) {
99 const options = { isolationLevel: Transaction.ISOLATION_LEVELS.READ_COMMITTED }
100
101 return sequelizeTypescript.transaction(options, t => fn(t))
102}
103
104function afterCommitIfTransaction (t: Transaction, fn: Function) {
105 if (t) return t.afterCommit(() => fn())
106
107 return fn()
108}
109
110// ---------------------------------------------------------------------------
111
112export {
113 resetSequelizeInstance,
114 retryTransactionWrapper,
115 transactionRetryer,
116 saveInTransactionWithRetries,
117 afterCommitIfTransaction,
118 filterNonExistingModels,
119 deleteAllModels,
120 runInReadCommittedTransaction
121}