diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-12-11 21:50:51 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-12-19 21:22:28 +0100 |
commit | feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c (patch) | |
tree | 2abc9fbc9569760e218fd52835850b757344b420 /server/initializers | |
parent | 108626609eda75e4ecc0a83a650a4d53c46220e0 (diff) | |
download | PeerTube-feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c.tar.gz PeerTube-feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c.tar.zst PeerTube-feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c.zip |
First version with PostgreSQL
Diffstat (limited to 'server/initializers')
-rw-r--r-- | server/initializers/checker.js | 8 | ||||
-rw-r--r-- | server/initializers/constants.js | 19 | ||||
-rw-r--r-- | server/initializers/database.js | 58 | ||||
-rw-r--r-- | server/initializers/installer.js | 29 | ||||
-rw-r--r-- | server/initializers/migrator.js | 12 |
5 files changed, 67 insertions, 59 deletions
diff --git a/server/initializers/checker.js b/server/initializers/checker.js index aea013fa9..7b402de82 100644 --- a/server/initializers/checker.js +++ b/server/initializers/checker.js | |||
@@ -1,10 +1,8 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | 3 | const config = require('config') |
4 | const mongoose = require('mongoose') | ||
5 | 4 | ||
6 | const Client = mongoose.model('OAuthClient') | 5 | const db = require('./database') |
7 | const User = mongoose.model('User') | ||
8 | 6 | ||
9 | const checker = { | 7 | const checker = { |
10 | checkConfig, | 8 | checkConfig, |
@@ -44,7 +42,7 @@ function checkMissedConfig () { | |||
44 | } | 42 | } |
45 | 43 | ||
46 | function clientsExist (callback) { | 44 | function clientsExist (callback) { |
47 | Client.list(function (err, clients) { | 45 | db.OAuthClient.list(function (err, clients) { |
48 | if (err) return callback(err) | 46 | if (err) return callback(err) |
49 | 47 | ||
50 | return callback(null, clients.length !== 0) | 48 | return callback(null, clients.length !== 0) |
@@ -52,7 +50,7 @@ function clientsExist (callback) { | |||
52 | } | 50 | } |
53 | 51 | ||
54 | function usersExist (callback) { | 52 | function usersExist (callback) { |
55 | User.countTotal(function (err, totalUsers) { | 53 | db.User.countTotal(function (err, totalUsers) { |
56 | if (err) return callback(err) | 54 | if (err) return callback(err) |
57 | 55 | ||
58 | return callback(null, totalUsers !== 0) | 56 | return callback(null, totalUsers !== 0) |
diff --git a/server/initializers/constants.js b/server/initializers/constants.js index 3ddf87454..1ad0c82a0 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js | |||
@@ -14,13 +14,13 @@ const PAGINATION_COUNT_DEFAULT = 15 | |||
14 | 14 | ||
15 | // Sortable columns per schema | 15 | // Sortable columns per schema |
16 | const SEARCHABLE_COLUMNS = { | 16 | const SEARCHABLE_COLUMNS = { |
17 | VIDEOS: [ 'name', 'magnetUri', 'podHost', 'author', 'tags' ] | 17 | VIDEOS: [ 'name', 'magnetUri', 'host', 'author', 'tags' ] |
18 | } | 18 | } |
19 | 19 | ||
20 | // Sortable columns per schema | 20 | // Sortable columns per schema |
21 | const SORTABLE_COLUMNS = { | 21 | const SORTABLE_COLUMNS = { |
22 | USERS: [ 'username', '-username', 'createdDate', '-createdDate' ], | 22 | USERS: [ 'username', '-username', 'createdAt', '-createdAt' ], |
23 | VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdDate', '-createdDate' ] | 23 | VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdAt', '-createdAt' ] |
24 | } | 24 | } |
25 | 25 | ||
26 | const OAUTH_LIFETIME = { | 26 | const OAUTH_LIFETIME = { |
@@ -67,9 +67,8 @@ const CONSTRAINTS_FIELDS = { | |||
67 | VIDEOS: { | 67 | VIDEOS: { |
68 | NAME: { min: 3, max: 50 }, // Length | 68 | NAME: { min: 3, max: 50 }, // Length |
69 | DESCRIPTION: { min: 3, max: 250 }, // Length | 69 | DESCRIPTION: { min: 3, max: 250 }, // Length |
70 | MAGNET: { | 70 | EXTNAME: [ '.mp4', '.ogv', '.webm' ], |
71 | INFO_HASH: { min: 10, max: 50 } // Length | 71 | INFO_HASH: { min: 10, max: 50 }, // Length |
72 | }, | ||
73 | DURATION: { min: 1, max: 7200 }, // Number | 72 | DURATION: { min: 1, max: 7200 }, // Number |
74 | TAGS: { min: 1, max: 3 }, // Number of total tags | 73 | TAGS: { min: 1, max: 3 }, // Number of total tags |
75 | TAG: { min: 2, max: 10 }, // Length | 74 | TAG: { min: 2, max: 10 }, // Length |
@@ -88,7 +87,7 @@ const FRIEND_SCORE = { | |||
88 | 87 | ||
89 | // --------------------------------------------------------------------------- | 88 | // --------------------------------------------------------------------------- |
90 | 89 | ||
91 | const MONGO_MIGRATION_SCRIPTS = [ | 90 | const MIGRATION_SCRIPTS = [ |
92 | { | 91 | { |
93 | script: '0005-create-application', | 92 | script: '0005-create-application', |
94 | version: 5 | 93 | version: 5 |
@@ -122,7 +121,7 @@ const MONGO_MIGRATION_SCRIPTS = [ | |||
122 | version: 40 | 121 | version: 40 |
123 | } | 122 | } |
124 | ] | 123 | ] |
125 | const LAST_MONGO_SCHEMA_VERSION = (maxBy(MONGO_MIGRATION_SCRIPTS, 'version'))['version'] | 124 | const LAST_SQL_SCHEMA_VERSION = (maxBy(MIGRATION_SCRIPTS, 'version'))['version'] |
126 | 125 | ||
127 | // --------------------------------------------------------------------------- | 126 | // --------------------------------------------------------------------------- |
128 | 127 | ||
@@ -198,8 +197,8 @@ module.exports = { | |||
198 | CONFIG, | 197 | CONFIG, |
199 | CONSTRAINTS_FIELDS, | 198 | CONSTRAINTS_FIELDS, |
200 | FRIEND_SCORE, | 199 | FRIEND_SCORE, |
201 | LAST_MONGO_SCHEMA_VERSION, | 200 | LAST_SQL_SCHEMA_VERSION, |
202 | MONGO_MIGRATION_SCRIPTS, | 201 | MIGRATION_SCRIPTS, |
203 | OAUTH_LIFETIME, | 202 | OAUTH_LIFETIME, |
204 | PAGINATION_COUNT_DEFAULT, | 203 | PAGINATION_COUNT_DEFAULT, |
205 | PODS_SCORE, | 204 | PODS_SCORE, |
diff --git a/server/initializers/database.js b/server/initializers/database.js index 0564e4e77..cc6f59b63 100644 --- a/server/initializers/database.js +++ b/server/initializers/database.js | |||
@@ -1,36 +1,46 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const mongoose = require('mongoose') | 3 | const fs = require('fs') |
4 | const path = require('path') | ||
5 | const Sequelize = require('sequelize') | ||
4 | 6 | ||
5 | const constants = require('../initializers/constants') | 7 | const constants = require('../initializers/constants') |
6 | const logger = require('../helpers/logger') | 8 | const logger = require('../helpers/logger') |
7 | 9 | ||
8 | // Bootstrap models | 10 | const database = {} |
9 | require('../models/application') | 11 | |
10 | require('../models/oauth-token') | 12 | const sequelize = new Sequelize(constants.CONFIG.DATABASE.DBNAME, 'peertube', 'peertube', { |
11 | require('../models/user') | 13 | dialect: 'postgres', |
12 | require('../models/oauth-client') | 14 | host: constants.CONFIG.DATABASE.HOSTNAME, |
13 | require('../models/video') | 15 | port: constants.CONFIG.DATABASE.PORT |
14 | // Request model needs Video model | 16 | }) |
15 | require('../models/pods') | 17 | |
16 | // Request model needs Pod model | 18 | const modelDirectory = path.join(__dirname, '..', 'models') |
17 | require('../models/request') | 19 | fs.readdir(modelDirectory, function (err, files) { |
18 | 20 | if (err) throw err | |
19 | const database = { | 21 | |
20 | connect: connect | 22 | files.filter(function (file) { |
21 | } | 23 | if (file === 'utils.js') return false |
22 | 24 | ||
23 | function connect () { | 25 | return true |
24 | mongoose.Promise = global.Promise | ||
25 | mongoose.connect('mongodb://' + constants.CONFIG.DATABASE.HOSTNAME + ':' + constants.CONFIG.DATABASE.PORT + '/' + constants.CONFIG.DATABASE.DBNAME) | ||
26 | mongoose.connection.on('error', function () { | ||
27 | throw new Error('Mongodb connection error.') | ||
28 | }) | 26 | }) |
27 | .forEach(function (file) { | ||
28 | const model = sequelize.import(path.join(modelDirectory, file)) | ||
29 | 29 | ||
30 | mongoose.connection.on('open', function () { | 30 | database[model.name] = model |
31 | logger.info('Connected to mongodb.') | ||
32 | }) | 31 | }) |
33 | } | 32 | |
33 | Object.keys(database).forEach(function (modelName) { | ||
34 | if ('associate' in database[modelName]) { | ||
35 | database[modelName].associate(database) | ||
36 | } | ||
37 | }) | ||
38 | |||
39 | logger.info('Database is ready.') | ||
40 | }) | ||
41 | |||
42 | database.sequelize = sequelize | ||
43 | database.Sequelize = Sequelize | ||
34 | 44 | ||
35 | // --------------------------------------------------------------------------- | 45 | // --------------------------------------------------------------------------- |
36 | 46 | ||
diff --git a/server/initializers/installer.js b/server/initializers/installer.js index 1df300ba8..4823bc8c8 100644 --- a/server/initializers/installer.js +++ b/server/initializers/installer.js | |||
@@ -3,26 +3,27 @@ | |||
3 | const config = require('config') | 3 | const config = require('config') |
4 | const each = require('async/each') | 4 | const each = require('async/each') |
5 | const mkdirp = require('mkdirp') | 5 | const mkdirp = require('mkdirp') |
6 | const mongoose = require('mongoose') | ||
7 | const passwordGenerator = require('password-generator') | 6 | const passwordGenerator = require('password-generator') |
8 | const path = require('path') | 7 | const path = require('path') |
9 | const series = require('async/series') | 8 | const series = require('async/series') |
10 | 9 | ||
11 | const checker = require('./checker') | 10 | const checker = require('./checker') |
12 | const constants = require('./constants') | 11 | const constants = require('./constants') |
12 | const db = require('./database') | ||
13 | const logger = require('../helpers/logger') | 13 | const logger = require('../helpers/logger') |
14 | const peertubeCrypto = require('../helpers/peertube-crypto') | 14 | const peertubeCrypto = require('../helpers/peertube-crypto') |
15 | 15 | ||
16 | const Application = mongoose.model('Application') | ||
17 | const Client = mongoose.model('OAuthClient') | ||
18 | const User = mongoose.model('User') | ||
19 | |||
20 | const installer = { | 16 | const installer = { |
21 | installApplication | 17 | installApplication |
22 | } | 18 | } |
23 | 19 | ||
24 | function installApplication (callback) { | 20 | function installApplication (callback) { |
25 | series([ | 21 | series([ |
22 | function createDatabase (callbackAsync) { | ||
23 | db.sequelize.sync().asCallback(callbackAsync) | ||
24 | // db.sequelize.sync({ force: true }).asCallback(callbackAsync) | ||
25 | }, | ||
26 | |||
26 | function createDirectories (callbackAsync) { | 27 | function createDirectories (callbackAsync) { |
27 | createDirectoriesIfNotExist(callbackAsync) | 28 | createDirectoriesIfNotExist(callbackAsync) |
28 | }, | 29 | }, |
@@ -65,16 +66,18 @@ function createOAuthClientIfNotExist (callback) { | |||
65 | 66 | ||
66 | logger.info('Creating a default OAuth Client.') | 67 | logger.info('Creating a default OAuth Client.') |
67 | 68 | ||
68 | const secret = passwordGenerator(32, false) | 69 | const id = passwordGenerator(32, false, /[a-z0-9]/) |
69 | const client = new Client({ | 70 | const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/) |
71 | const client = db.OAuthClient.build({ | ||
72 | clientId: id, | ||
70 | clientSecret: secret, | 73 | clientSecret: secret, |
71 | grants: [ 'password', 'refresh_token' ] | 74 | grants: [ 'password', 'refresh_token' ] |
72 | }) | 75 | }) |
73 | 76 | ||
74 | client.save(function (err, createdClient) { | 77 | client.save().asCallback(function (err, createdClient) { |
75 | if (err) return callback(err) | 78 | if (err) return callback(err) |
76 | 79 | ||
77 | logger.info('Client id: ' + createdClient._id) | 80 | logger.info('Client id: ' + createdClient.clientId) |
78 | logger.info('Client secret: ' + createdClient.clientSecret) | 81 | logger.info('Client secret: ' + createdClient.clientSecret) |
79 | 82 | ||
80 | return callback(null) | 83 | return callback(null) |
@@ -106,21 +109,21 @@ function createOAuthAdminIfNotExist (callback) { | |||
106 | password = passwordGenerator(8, true) | 109 | password = passwordGenerator(8, true) |
107 | } | 110 | } |
108 | 111 | ||
109 | const user = new User({ | 112 | const user = db.User.build({ |
110 | username, | 113 | username, |
111 | password, | 114 | password, |
112 | role | 115 | role |
113 | }) | 116 | }) |
114 | 117 | ||
115 | user.save(function (err, createdUser) { | 118 | user.save().asCallback(function (err, createdUser) { |
116 | if (err) return callback(err) | 119 | if (err) return callback(err) |
117 | 120 | ||
118 | logger.info('Username: ' + username) | 121 | logger.info('Username: ' + username) |
119 | logger.info('User password: ' + password) | 122 | logger.info('User password: ' + password) |
120 | 123 | ||
121 | logger.info('Creating Application collection.') | 124 | logger.info('Creating Application collection.') |
122 | const application = new Application({ mongoSchemaVersion: constants.LAST_MONGO_SCHEMA_VERSION }) | 125 | const application = db.Application.build({ sqlSchemaVersion: constants.LAST_SQL_SCHEMA_VERSION }) |
123 | application.save(callback) | 126 | application.save().asCallback(callback) |
124 | }) | 127 | }) |
125 | }) | 128 | }) |
126 | } | 129 | } |
diff --git a/server/initializers/migrator.js b/server/initializers/migrator.js index 6b31d994f..9e5350e60 100644 --- a/server/initializers/migrator.js +++ b/server/initializers/migrator.js | |||
@@ -1,24 +1,22 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const eachSeries = require('async/eachSeries') | 3 | const eachSeries = require('async/eachSeries') |
4 | const mongoose = require('mongoose') | ||
5 | const path = require('path') | 4 | const path = require('path') |
6 | 5 | ||
7 | const constants = require('./constants') | 6 | const constants = require('./constants') |
7 | const db = require('./database') | ||
8 | const logger = require('../helpers/logger') | 8 | const logger = require('../helpers/logger') |
9 | 9 | ||
10 | const Application = mongoose.model('Application') | ||
11 | |||
12 | const migrator = { | 10 | const migrator = { |
13 | migrate: migrate | 11 | migrate: migrate |
14 | } | 12 | } |
15 | 13 | ||
16 | function migrate (callback) { | 14 | function migrate (callback) { |
17 | Application.loadMongoSchemaVersion(function (err, actualVersion) { | 15 | db.Application.loadSqlSchemaVersion(function (err, actualVersion) { |
18 | if (err) return callback(err) | 16 | if (err) return callback(err) |
19 | 17 | ||
20 | // If there are a new mongo schemas | 18 | // If there are a new mongo schemas |
21 | if (!actualVersion || actualVersion < constants.LAST_MONGO_SCHEMA_VERSION) { | 19 | if (!actualVersion || actualVersion < constants.LAST_SQL_SCHEMA_VERSION) { |
22 | logger.info('Begin migrations.') | 20 | logger.info('Begin migrations.') |
23 | 21 | ||
24 | eachSeries(constants.MONGO_MIGRATION_SCRIPTS, function (entity, callbackEach) { | 22 | eachSeries(constants.MONGO_MIGRATION_SCRIPTS, function (entity, callbackEach) { |
@@ -36,12 +34,12 @@ function migrate (callback) { | |||
36 | if (err) return callbackEach(err) | 34 | if (err) return callbackEach(err) |
37 | 35 | ||
38 | // Update the new mongo version schema | 36 | // Update the new mongo version schema |
39 | Application.updateMongoSchemaVersion(versionScript, callbackEach) | 37 | db.Application.updateSqlSchemaVersion(versionScript, callbackEach) |
40 | }) | 38 | }) |
41 | }, function (err) { | 39 | }, function (err) { |
42 | if (err) return callback(err) | 40 | if (err) return callback(err) |
43 | 41 | ||
44 | logger.info('Migrations finished. New mongo version schema: %s', constants.LAST_MONGO_SCHEMA_VERSION) | 42 | logger.info('Migrations finished. New SQL version schema: %s', constants.LAST_SQL_SCHEMA_VERSION) |
45 | return callback(null) | 43 | return callback(null) |
46 | }) | 44 | }) |
47 | } else { | 45 | } else { |