]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/application.js
Update migrations code
[github/Chocobozzz/PeerTube.git] / server / models / application.js
index 452ac4283ba71efa0b5942269682ead45f30bed8..4114ed76de12b10981994646aee0cdaa4ca6700c 100644 (file)
@@ -1,31 +1,46 @@
-const mongoose = require('mongoose')
+module.exports = function (sequelize, DataTypes) {
+  const Application = sequelize.define('Application',
+    {
+      migrationVersion: {
+        type: DataTypes.INTEGER,
+        defaultValue: 0
+      }
+    },
+    {
+      classMethods: {
+        loadMigrationVersion,
+        updateMigrationVersion
+      }
+    }
+  )
+
+  return Application
+}
 
 // ---------------------------------------------------------------------------
 
-const ApplicationSchema = mongoose.Schema({
-  mongoSchemaVersion: {
-    type: Number,
-    default: 0
+function loadMigrationVersion (callback) {
+  const query = {
+    attributes: [ 'migrationVersion' ]
   }
-})
-
-ApplicationSchema.statics = {
-  loadMongoSchemaVersion,
-  updateMongoSchemaVersion
-}
-
-mongoose.model('Application', ApplicationSchema)
-
-// ---------------------------------------------------------------------------
 
-function loadMongoSchemaVersion (callback) {
-  return this.findOne({}, { mongoSchemaVersion: 1 }, function (err, data) {
-    const version = data ? data.mongoSchemaVersion : 0
+  return this.findOne(query).asCallback(function (err, data) {
+    const version = data ? data.migrationVersion : 0
 
     return callback(err, version)
   })
 }
 
-function updateMongoSchemaVersion (newVersion, callback) {
-  return this.update({}, { mongoSchemaVersion: newVersion }, callback)
+function updateMigrationVersion (newVersion, transaction, callback) {
+  const options = {
+    where: {}
+  }
+
+  if (!callback) {
+    transaction = callback
+  } else {
+    options.transaction = transaction
+  }
+
+  return this.update({ migrationVersion: newVersion }, options).asCallback(callback)
 }