aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers
diff options
context:
space:
mode:
Diffstat (limited to 'server/initializers')
-rw-r--r--server/initializers/constants.js16
-rw-r--r--server/initializers/database.js1
-rw-r--r--server/initializers/migrations/0005-create-application.js17
-rw-r--r--server/initializers/migrations/0010-users-password.js23
-rw-r--r--server/initializers/migrator.js56
5 files changed, 112 insertions, 1 deletions
diff --git a/server/initializers/constants.js b/server/initializers/constants.js
index 76ebb8681..10ae48e95 100644
--- a/server/initializers/constants.js
+++ b/server/initializers/constants.js
@@ -54,6 +54,18 @@ const FRIEND_SCORE = {
54 MAX: 1000 54 MAX: 1000
55} 55}
56 56
57const MONGO_MIGRATION_SCRIPTS = [
58 {
59 script: '0005-create-application',
60 version: 5
61 },
62 {
63 script: '0010-users-password',
64 version: 10
65 }
66]
67const LAST_MONGO_SCHEMA_VERSION = 10
68
57// Time to wait between requests to the friends (10 min) 69// Time to wait between requests to the friends (10 min)
58let REQUESTS_INTERVAL = 600000 70let REQUESTS_INTERVAL = 600000
59 71
@@ -121,11 +133,13 @@ module.exports = {
121 CONFIG: CONFIG, 133 CONFIG: CONFIG,
122 CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS, 134 CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS,
123 FRIEND_SCORE: FRIEND_SCORE, 135 FRIEND_SCORE: FRIEND_SCORE,
124 REQUESTS_INTERVAL: REQUESTS_INTERVAL, 136 LAST_MONGO_SCHEMA_VERSION: LAST_MONGO_SCHEMA_VERSION,
137 MONGO_MIGRATION_SCRIPTS: MONGO_MIGRATION_SCRIPTS,
125 OAUTH_LIFETIME: OAUTH_LIFETIME, 138 OAUTH_LIFETIME: OAUTH_LIFETIME,
126 PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT, 139 PAGINATION_COUNT_DEFAULT: PAGINATION_COUNT_DEFAULT,
127 PODS_SCORE: PODS_SCORE, 140 PODS_SCORE: PODS_SCORE,
128 REQUESTS_IN_PARALLEL: REQUESTS_IN_PARALLEL, 141 REQUESTS_IN_PARALLEL: REQUESTS_IN_PARALLEL,
142 REQUESTS_INTERVAL: REQUESTS_INTERVAL,
129 REQUESTS_LIMIT: REQUESTS_LIMIT, 143 REQUESTS_LIMIT: REQUESTS_LIMIT,
130 RETRY_REQUESTS: RETRY_REQUESTS, 144 RETRY_REQUESTS: RETRY_REQUESTS,
131 SEARCHABLE_COLUMNS: SEARCHABLE_COLUMNS, 145 SEARCHABLE_COLUMNS: SEARCHABLE_COLUMNS,
diff --git a/server/initializers/database.js b/server/initializers/database.js
index 20dcc056e..1da574631 100644
--- a/server/initializers/database.js
+++ b/server/initializers/database.js
@@ -6,6 +6,7 @@ const constants = require('../initializers/constants')
6const logger = require('../helpers/logger') 6const logger = require('../helpers/logger')
7 7
8// Bootstrap models 8// Bootstrap models
9require('../models/application')
9require('../models/user') 10require('../models/user')
10require('../models/oauth-client') 11require('../models/oauth-client')
11require('../models/oauth-token') 12require('../models/oauth-token')
diff --git a/server/initializers/migrations/0005-create-application.js b/server/initializers/migrations/0005-create-application.js
new file mode 100644
index 000000000..e99dec019
--- /dev/null
+++ b/server/initializers/migrations/0005-create-application.js
@@ -0,0 +1,17 @@
1/*
2 Create the application collection in MongoDB.
3 Used to store the actual MongoDB scheme version.
4*/
5
6const mongoose = require('mongoose')
7
8const Application = mongoose.model('Application')
9
10exports.up = function (callback) {
11 const application = new Application()
12 application.save(callback)
13}
14
15exports.down = function (callback) {
16 throw new Error('Not implemented.')
17}
diff --git a/server/initializers/migrations/0010-users-password.js b/server/initializers/migrations/0010-users-password.js
new file mode 100644
index 000000000..e031fa142
--- /dev/null
+++ b/server/initializers/migrations/0010-users-password.js
@@ -0,0 +1,23 @@
1/*
2 Convert plain user password to encrypted user password.
3*/
4
5const mongoose = require('mongoose')
6
7const User = mongoose.model('User')
8
9exports.up = function (callback) {
10 User.list(function (err, users) {
11 if (err) return callback(err)
12
13 users.forEach(function (user) {
14 user.save()
15 })
16
17 return callback(null)
18 })
19}
20
21exports.down = function (callback) {
22 throw new Error('Not implemented.')
23}
diff --git a/server/initializers/migrator.js b/server/initializers/migrator.js
new file mode 100644
index 000000000..6b31d994f
--- /dev/null
+++ b/server/initializers/migrator.js
@@ -0,0 +1,56 @@
1'use strict'
2
3const eachSeries = require('async/eachSeries')
4const mongoose = require('mongoose')
5const path = require('path')
6
7const constants = require('./constants')
8const logger = require('../helpers/logger')
9
10const Application = mongoose.model('Application')
11
12const migrator = {
13 migrate: migrate
14}
15
16function migrate (callback) {
17 Application.loadMongoSchemaVersion(function (err, actualVersion) {
18 if (err) return callback(err)
19
20 // If there are a new mongo schemas
21 if (!actualVersion || actualVersion < constants.LAST_MONGO_SCHEMA_VERSION) {
22 logger.info('Begin migrations.')
23
24 eachSeries(constants.MONGO_MIGRATION_SCRIPTS, function (entity, callbackEach) {
25 const versionScript = entity.version
26
27 // Do not execute old migration scripts
28 if (versionScript <= actualVersion) return callbackEach(null)
29
30 // Load the migration module and run it
31 const migrationScriptName = entity.script
32 logger.info('Executing %s migration script.', migrationScriptName)
33
34 const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName))
35 migrationScript.up(function (err) {
36 if (err) return callbackEach(err)
37
38 // Update the new mongo version schema
39 Application.updateMongoSchemaVersion(versionScript, callbackEach)
40 })
41 }, function (err) {
42 if (err) return callback(err)
43
44 logger.info('Migrations finished. New mongo version schema: %s', constants.LAST_MONGO_SCHEMA_VERSION)
45 return callback(null)
46 })
47 } else {
48 return callback(null)
49 }
50 })
51}
52
53// ---------------------------------------------------------------------------
54
55module.exports = migrator
56