]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos.js
Merge branch 'postgresql'
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos.js
index 6573b12103efa9c30870b8905e83b6a37434601e..2c4af520e65f62ae2f1c98f4e90b131bbf18e45e 100644 (file)
@@ -231,11 +231,12 @@ function addVideo (req, res, videoFile, callback) {
     }
 
     // Commit transaction
-    t.commit()
+    t.commit().asCallback(function (err) {
+      if (err) return callback(err)
 
-    logger.info('Video with name %s created.', videoInfos.name)
-
-    return callback(null)
+      logger.info('Video with name %s created.', videoInfos.name)
+      return callback(null)
+    })
   })
 }
 
@@ -258,12 +259,13 @@ function updateVideoRetryWrapper (req, res, next) {
 
 function updateVideo (req, res, finalCallback) {
   const videoInstance = res.locals.video
+  const videoFieldsSave = videoInstance.toJSON()
   const videoInfosToUpdate = req.body
 
   waterfall([
 
     function startTransaction (callback) {
-      db.sequelize.transaction().asCallback(function (err, t) {
+      db.sequelize.transaction({ isolationLevel: 'SERIALIZABLE' }).asCallback(function (err, t) {
         return callback(err, t)
       })
     },
@@ -279,12 +281,13 @@ function updateVideo (req, res, finalCallback) {
     },
 
     function updateVideoIntoDB (t, tagInstances, callback) {
-      const options = { transaction: t }
+      const options = {
+        transaction: t
+      }
 
       if (videoInfosToUpdate.name) videoInstance.set('name', videoInfosToUpdate.name)
       if (videoInfosToUpdate.description) videoInstance.set('description', videoInfosToUpdate.description)
 
-      // Add tags association
       videoInstance.save(options).asCallback(function (err) {
         return callback(err, t, tagInstances)
       })
@@ -320,13 +323,24 @@ function updateVideo (req, res, finalCallback) {
       // Abort transaction?
       if (t) t.rollback()
 
+      // Force fields we want to update
+      // If the transaction is retried, sequelize will think the object has not changed
+      // So it will skip the SQL request, even if the last one was ROLLBACKed!
+      Object.keys(videoFieldsSave).forEach(function (key) {
+        const value = videoFieldsSave[key]
+        videoInstance.set(key, value)
+      })
+
       return finalCallback(err)
     }
 
     // Commit transaction
-    t.commit()
+    t.commit().asCallback(function (err) {
+      if (err) return finalCallback(err)
 
-    return finalCallback(null)
+      logger.info('Video with name %s updated.', videoInfosToUpdate.name)
+      return finalCallback(null)
+    })
   })
 }
 
@@ -442,9 +456,12 @@ function reportVideoAbuse (req, res, finalCallback) {
     }
 
     // Commit transaction
-    t.commit()
+    t.commit().asCallback(function (err) {
+      if (err) return finalCallback(err)
 
-    return finalCallback(null)
+      logger.info('Abuse report for video %s created.', videoInstance.name)
+      return finalCallback(null)
+    })
   })
 }