diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-09-26 22:36:36 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-09-26 22:39:58 +0200 |
commit | 00d6b0dda4b1964ab11127851c0fc7106cc0f912 (patch) | |
tree | 3759ff8c8724f85f2554c28edf5bcf58a60085cf /server/initializers | |
parent | 44c5275e1bdd1d4b4c23f7c423034e4293c927b3 (diff) | |
download | PeerTube-00d6b0dda4b1964ab11127851c0fc7106cc0f912.tar.gz PeerTube-00d6b0dda4b1964ab11127851c0fc7106cc0f912.tar.zst PeerTube-00d6b0dda4b1964ab11127851c0fc7106cc0f912.zip |
Add migration (for db, folders...) mechanism
Diffstat (limited to 'server/initializers')
-rw-r--r-- | server/initializers/constants.js | 16 | ||||
-rw-r--r-- | server/initializers/database.js | 1 | ||||
-rw-r--r-- | server/initializers/migrations/0005-create-application.js | 17 | ||||
-rw-r--r-- | server/initializers/migrations/0010-users-password.js | 23 | ||||
-rw-r--r-- | server/initializers/migrator.js | 56 |
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 | ||
57 | const MONGO_MIGRATION_SCRIPTS = [ | ||
58 | { | ||
59 | script: '0005-create-application', | ||
60 | version: 5 | ||
61 | }, | ||
62 | { | ||
63 | script: '0010-users-password', | ||
64 | version: 10 | ||
65 | } | ||
66 | ] | ||
67 | const 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) |
58 | let REQUESTS_INTERVAL = 600000 | 70 | let 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') | |||
6 | const logger = require('../helpers/logger') | 6 | const logger = require('../helpers/logger') |
7 | 7 | ||
8 | // Bootstrap models | 8 | // Bootstrap models |
9 | require('../models/application') | ||
9 | require('../models/user') | 10 | require('../models/user') |
10 | require('../models/oauth-client') | 11 | require('../models/oauth-client') |
11 | require('../models/oauth-token') | 12 | require('../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 | |||
6 | const mongoose = require('mongoose') | ||
7 | |||
8 | const Application = mongoose.model('Application') | ||
9 | |||
10 | exports.up = function (callback) { | ||
11 | const application = new Application() | ||
12 | application.save(callback) | ||
13 | } | ||
14 | |||
15 | exports.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 | |||
5 | const mongoose = require('mongoose') | ||
6 | |||
7 | const User = mongoose.model('User') | ||
8 | |||
9 | exports.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 | |||
21 | exports.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 | |||
3 | const eachSeries = require('async/eachSeries') | ||
4 | const mongoose = require('mongoose') | ||
5 | const path = require('path') | ||
6 | |||
7 | const constants = require('./constants') | ||
8 | const logger = require('../helpers/logger') | ||
9 | |||
10 | const Application = mongoose.model('Application') | ||
11 | |||
12 | const migrator = { | ||
13 | migrate: migrate | ||
14 | } | ||
15 | |||
16 | function 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 | |||
55 | module.exports = migrator | ||
56 | |||