diff options
Diffstat (limited to 'server/helpers/database-utils.ts')
-rw-r--r-- | server/helpers/database-utils.ts | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts index f9cb33aca..b5dc70c17 100644 --- a/server/helpers/database-utils.ts +++ b/server/helpers/database-utils.ts | |||
@@ -58,7 +58,7 @@ function transactionRetryer <T> (func: (err: any, data: T) => any) { | |||
58 | 58 | ||
59 | errorFilter: err => { | 59 | errorFilter: err => { |
60 | const willRetry = (err.name === 'SequelizeDatabaseError') | 60 | const willRetry = (err.name === 'SequelizeDatabaseError') |
61 | logger.debug('Maybe retrying the transaction function.', { willRetry, err }) | 61 | logger.debug('Maybe retrying the transaction function.', { willRetry, err, tags: [ 'sql', 'retry' ] }) |
62 | return willRetry | 62 | return willRetry |
63 | } | 63 | } |
64 | }, | 64 | }, |
@@ -68,7 +68,9 @@ function transactionRetryer <T> (func: (err: any, data: T) => any) { | |||
68 | }) | 68 | }) |
69 | } | 69 | } |
70 | 70 | ||
71 | function updateInstanceWithAnother <T extends Model<T>> (instanceToUpdate: Model<T>, baseInstance: Model<T>) { | 71 | // --------------------------------------------------------------------------- |
72 | |||
73 | function updateInstanceWithAnother <M, T extends U, U extends Model<M>> (instanceToUpdate: T, baseInstance: U) { | ||
72 | const obj = baseInstance.toJSON() | 74 | const obj = baseInstance.toJSON() |
73 | 75 | ||
74 | for (const key of Object.keys(obj)) { | 76 | for (const key of Object.keys(obj)) { |
@@ -82,13 +84,7 @@ function resetSequelizeInstance (instance: Model<any>, savedFields: object) { | |||
82 | }) | 84 | }) |
83 | } | 85 | } |
84 | 86 | ||
85 | function afterCommitIfTransaction (t: Transaction, fn: Function) { | 87 | function deleteNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean } & Pick<Model, 'destroy'>> ( |
86 | if (t) return t.afterCommit(() => fn()) | ||
87 | |||
88 | return fn() | ||
89 | } | ||
90 | |||
91 | function deleteNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean } & Model<T>> ( | ||
92 | fromDatabase: T[], | 88 | fromDatabase: T[], |
93 | newModels: T[], | 89 | newModels: T[], |
94 | t: Transaction | 90 | t: Transaction |
@@ -111,6 +107,20 @@ function setAsUpdated (table: string, id: number, transaction?: Transaction) { | |||
111 | 107 | ||
112 | // --------------------------------------------------------------------------- | 108 | // --------------------------------------------------------------------------- |
113 | 109 | ||
110 | function runInReadCommittedTransaction <T> (fn: (t: Transaction) => Promise<T>) { | ||
111 | const options = { isolationLevel: Transaction.ISOLATION_LEVELS.READ_COMMITTED } | ||
112 | |||
113 | return sequelizeTypescript.transaction(options, t => fn(t)) | ||
114 | } | ||
115 | |||
116 | function afterCommitIfTransaction (t: Transaction, fn: Function) { | ||
117 | if (t) return t.afterCommit(() => fn()) | ||
118 | |||
119 | return fn() | ||
120 | } | ||
121 | |||
122 | // --------------------------------------------------------------------------- | ||
123 | |||
114 | export { | 124 | export { |
115 | resetSequelizeInstance, | 125 | resetSequelizeInstance, |
116 | retryTransactionWrapper, | 126 | retryTransactionWrapper, |
@@ -118,5 +128,6 @@ export { | |||
118 | updateInstanceWithAnother, | 128 | updateInstanceWithAnother, |
119 | afterCommitIfTransaction, | 129 | afterCommitIfTransaction, |
120 | deleteNonExistingModels, | 130 | deleteNonExistingModels, |
121 | setAsUpdated | 131 | setAsUpdated, |
132 | runInReadCommittedTransaction | ||
122 | } | 133 | } |