aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-09-26 22:36:36 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-09-26 22:39:58 +0200
commit00d6b0dda4b1964ab11127851c0fc7106cc0f912 (patch)
tree3759ff8c8724f85f2554c28edf5bcf58a60085cf /server
parent44c5275e1bdd1d4b4c23f7c423034e4293c927b3 (diff)
downloadPeerTube-00d6b0dda4b1964ab11127851c0fc7106cc0f912.tar.gz
PeerTube-00d6b0dda4b1964ab11127851c0fc7106cc0f912.tar.zst
PeerTube-00d6b0dda4b1964ab11127851c0fc7106cc0f912.zip
Add migration (for db, folders...) mechanism
Diffstat (limited to 'server')
-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
-rw-r--r--server/models/application.js31
-rw-r--r--server/models/user.js5
7 files changed, 148 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
diff --git a/server/models/application.js b/server/models/application.js
new file mode 100644
index 000000000..8185f0915
--- /dev/null
+++ b/server/models/application.js
@@ -0,0 +1,31 @@
1const mongoose = require('mongoose')
2
3// ---------------------------------------------------------------------------
4
5const ApplicationSchema = mongoose.Schema({
6 mongoSchemaVersion: {
7 type: Number,
8 default: 0
9 }
10})
11
12ApplicationSchema.statics = {
13 loadMongoSchemaVersion: loadMongoSchemaVersion,
14 updateMongoSchemaVersion: updateMongoSchemaVersion
15}
16
17mongoose.model('Application', ApplicationSchema)
18
19// ---------------------------------------------------------------------------
20
21function loadMongoSchemaVersion (callback) {
22 return this.findOne({}, { mongoSchemaVersion: 1 }, function (err, data) {
23 const version = data ? data.mongoSchemaVersion : 0
24
25 return callback(err, version)
26 })
27}
28
29function updateMongoSchemaVersion (newVersion, callback) {
30 return this.update({}, { mongoSchemaVersion: newVersion }, callback)
31}
diff --git a/server/models/user.js b/server/models/user.js
index db6f1765b..c2c8807f0 100644
--- a/server/models/user.js
+++ b/server/models/user.js
@@ -28,6 +28,7 @@ UserSchema.methods = {
28UserSchema.statics = { 28UserSchema.statics = {
29 countTotal: countTotal, 29 countTotal: countTotal,
30 getByUsername: getByUsername, 30 getByUsername: getByUsername,
31 list: list,
31 listForApi: listForApi, 32 listForApi: listForApi,
32 loadById: loadById, 33 loadById: loadById,
33 loadByUsername: loadByUsername 34 loadByUsername: loadByUsername
@@ -71,6 +72,10 @@ function getByUsername (username) {
71 return this.findOne({ username: username }) 72 return this.findOne({ username: username })
72} 73}
73 74
75function list (callback) {
76 return this.find(callback)
77}
78
74function listForApi (start, count, sort, callback) { 79function listForApi (start, count, sort, callback) {
75 const query = {} 80 const query = {}
76 return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) 81 return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)