]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/database-utils.ts
Fix peertube subtitles import
[github/Chocobozzz/PeerTube.git] / server / helpers / database-utils.ts
index ededa79016fb3e3be8c79274eb390d5788805895..b6ba7fd75a846b449cf127ea511f4ef9d31e3946 100644 (file)
@@ -1,23 +1,33 @@
-import * as retry from 'async/retry'
-import * as Bluebird from 'bluebird'
+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 <T, A, B, C, D> (
+  functionToRetry: (arg1: A, arg2: B, arg3: C, arg4: D) => Promise<T>,
+  arg1: A,
+  arg2: B,
+  arg3: C,
+  arg4: D,
+): Promise<T>
+
 function retryTransactionWrapper <T, A, B, C> (
-  functionToRetry: (arg1: A, arg2: B, arg3: C) => Promise<T> | Bluebird<T>,
+  functionToRetry: (arg1: A, arg2: B, arg3: C) => Promise<T>,
   arg1: A,
   arg2: B,
   arg3: C
 ): Promise<T>
 
 function retryTransactionWrapper <T, A, B> (
-  functionToRetry: (arg1: A, arg2: B) => Promise<T> | Bluebird<T>,
+  functionToRetry: (arg1: A, arg2: B) => Promise<T>,
   arg1: A,
   arg2: B
 ): Promise<T>
 
 function retryTransactionWrapper <T, A> (
-  functionToRetry: (arg1: A) => Promise<T> | Bluebird<T>,
+  functionToRetry: (arg1: A) => Promise<T>,
   arg1: A
 ): Promise<T>
 
@@ -26,7 +36,7 @@ function retryTransactionWrapper <T> (
 ): Promise<T>
 
 function retryTransactionWrapper <T> (
-  functionToRetry: (...args: any[]) => Promise<T> | Bluebird<T>,
+  functionToRetry: (...args: any[]) => Promise<T>,
   ...args: any[]
 ): Promise<T> {
   return transactionRetryer<T>(callback => {
@@ -35,7 +45,7 @@ function retryTransactionWrapper <T> (
         .catch(err => callback(err))
   })
   .catch(err => {
-    logger.error('Cannot execute %s with many retries.', functionToRetry.toString(), { err })
+    logger.warn(`Cannot execute ${functionToRetry.name} with many retries.`, { err })
     throw err
   })
 }
@@ -48,7 +58,7 @@ function transactionRetryer <T> (func: (err: any, data: T) => any) {
 
         errorFilter: err => {
           const willRetry = (err.name === 'SequelizeDatabaseError')
-          logger.debug('Maybe retrying the transaction function.', { willRetry, err })
+          logger.debug('Maybe retrying the transaction function.', { willRetry, err, tags: [ 'sql', 'retry' ] })
           return willRetry
         }
       },
@@ -58,18 +68,54 @@ function transactionRetryer <T> (func: (err: any, data: T) => any) {
   })
 }
 
-function updateInstanceWithAnother <T extends Model<T>> (instanceToUpdate: Model<T>, baseInstance: Model<T>) {
-  const obj = baseInstance.toJSON()
+function saveInTransactionWithRetries <T extends Pick<Model, 'save'>> (model: T) {
+  return retryTransactionWrapper(() => {
+    return sequelizeTypescript.transaction(async transaction => {
+      await model.save({ transaction })
+    })
+  })
+}
+
+// ---------------------------------------------------------------------------
+
+function resetSequelizeInstance <T> (instance: Model<T>) {
+  return instance.reload()
+}
+
+function filterNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean }> (
+  fromDatabase: T[],
+  newModels: T[]
+) {
+  return fromDatabase.filter(f => !newModels.find(newModel => newModel.hasSameUniqueKeysThan(f)))
+}
+
+function deleteAllModels <T extends Pick<Model, 'destroy'>> (models: T[], transaction: Transaction) {
+  return Promise.all(models.map(f => f.destroy({ transaction })))
+}
+
+// ---------------------------------------------------------------------------
+
+function runInReadCommittedTransaction <T> (fn: (t: Transaction) => Promise<T>) {
+  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())
 
-  for (const key of Object.keys(obj)) {
-    instanceToUpdate.set(key, obj[key])
-  }
+  return fn()
 }
 
 // ---------------------------------------------------------------------------
 
 export {
+  resetSequelizeInstance,
   retryTransactionWrapper,
   transactionRetryer,
-  updateInstanceWithAnother
+  saveInTransactionWithRetries,
+  afterCommitIfTransaction,
+  filterNonExistingModels,
+  deleteAllModels,
+  runInReadCommittedTransaction
 }