aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/application.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/application.js')
-rw-r--r--server/models/application.js61
1 files changed, 41 insertions, 20 deletions
diff --git a/server/models/application.js b/server/models/application.js
index 452ac4283..46dcfde33 100644
--- a/server/models/application.js
+++ b/server/models/application.js
@@ -1,31 +1,52 @@
1const mongoose = require('mongoose') 1'use strict'
2
3module.exports = function (sequelize, DataTypes) {
4 const Application = sequelize.define('Application',
5 {
6 migrationVersion: {
7 type: DataTypes.INTEGER,
8 defaultValue: 0,
9 allowNull: false,
10 validate: {
11 isInt: true
12 }
13 }
14 },
15 {
16 classMethods: {
17 loadMigrationVersion,
18 updateMigrationVersion
19 }
20 }
21 )
22
23 return Application
24}
2 25
3// --------------------------------------------------------------------------- 26// ---------------------------------------------------------------------------
4 27
5const ApplicationSchema = mongoose.Schema({ 28function loadMigrationVersion (callback) {
6 mongoSchemaVersion: { 29 const query = {
7 type: Number, 30 attributes: [ 'migrationVersion' ]
8 default: 0
9 } 31 }
10})
11
12ApplicationSchema.statics = {
13 loadMongoSchemaVersion,
14 updateMongoSchemaVersion
15}
16
17mongoose.model('Application', ApplicationSchema)
18
19// ---------------------------------------------------------------------------
20 32
21function loadMongoSchemaVersion (callback) { 33 return this.findOne(query).asCallback(function (err, data) {
22 return this.findOne({}, { mongoSchemaVersion: 1 }, function (err, data) { 34 const version = data ? data.migrationVersion : 0
23 const version = data ? data.mongoSchemaVersion : 0
24 35
25 return callback(err, version) 36 return callback(err, version)
26 }) 37 })
27} 38}
28 39
29function updateMongoSchemaVersion (newVersion, callback) { 40function updateMigrationVersion (newVersion, transaction, callback) {
30 return this.update({}, { mongoSchemaVersion: newVersion }, callback) 41 const options = {
42 where: {}
43 }
44
45 if (!callback) {
46 transaction = callback
47 } else {
48 options.transaction = transaction
49 }
50
51 return this.update({ migrationVersion: newVersion }, options).asCallback(callback)
31} 52}