mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 19:42:24 +02:00
First version with PostgreSQL
This commit is contained in:
parent
108626609e
commit
feb4bdfd9b
68 changed files with 1171 additions and 730 deletions
|
@ -1,10 +1,8 @@
|
|||
'use strict'
|
||||
|
||||
const config = require('config')
|
||||
const mongoose = require('mongoose')
|
||||
|
||||
const Client = mongoose.model('OAuthClient')
|
||||
const User = mongoose.model('User')
|
||||
const db = require('./database')
|
||||
|
||||
const checker = {
|
||||
checkConfig,
|
||||
|
@ -44,7 +42,7 @@ function checkMissedConfig () {
|
|||
}
|
||||
|
||||
function clientsExist (callback) {
|
||||
Client.list(function (err, clients) {
|
||||
db.OAuthClient.list(function (err, clients) {
|
||||
if (err) return callback(err)
|
||||
|
||||
return callback(null, clients.length !== 0)
|
||||
|
@ -52,7 +50,7 @@ function clientsExist (callback) {
|
|||
}
|
||||
|
||||
function usersExist (callback) {
|
||||
User.countTotal(function (err, totalUsers) {
|
||||
db.User.countTotal(function (err, totalUsers) {
|
||||
if (err) return callback(err)
|
||||
|
||||
return callback(null, totalUsers !== 0)
|
||||
|
|
|
@ -14,13 +14,13 @@ const PAGINATION_COUNT_DEFAULT = 15
|
|||
|
||||
// Sortable columns per schema
|
||||
const SEARCHABLE_COLUMNS = {
|
||||
VIDEOS: [ 'name', 'magnetUri', 'podHost', 'author', 'tags' ]
|
||||
VIDEOS: [ 'name', 'magnetUri', 'host', 'author', 'tags' ]
|
||||
}
|
||||
|
||||
// Sortable columns per schema
|
||||
const SORTABLE_COLUMNS = {
|
||||
USERS: [ 'username', '-username', 'createdDate', '-createdDate' ],
|
||||
VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdDate', '-createdDate' ]
|
||||
USERS: [ 'username', '-username', 'createdAt', '-createdAt' ],
|
||||
VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdAt', '-createdAt' ]
|
||||
}
|
||||
|
||||
const OAUTH_LIFETIME = {
|
||||
|
@ -67,9 +67,8 @@ const CONSTRAINTS_FIELDS = {
|
|||
VIDEOS: {
|
||||
NAME: { min: 3, max: 50 }, // Length
|
||||
DESCRIPTION: { min: 3, max: 250 }, // Length
|
||||
MAGNET: {
|
||||
INFO_HASH: { min: 10, max: 50 } // Length
|
||||
},
|
||||
EXTNAME: [ '.mp4', '.ogv', '.webm' ],
|
||||
INFO_HASH: { min: 10, max: 50 }, // Length
|
||||
DURATION: { min: 1, max: 7200 }, // Number
|
||||
TAGS: { min: 1, max: 3 }, // Number of total tags
|
||||
TAG: { min: 2, max: 10 }, // Length
|
||||
|
@ -88,7 +87,7 @@ const FRIEND_SCORE = {
|
|||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const MONGO_MIGRATION_SCRIPTS = [
|
||||
const MIGRATION_SCRIPTS = [
|
||||
{
|
||||
script: '0005-create-application',
|
||||
version: 5
|
||||
|
@ -122,7 +121,7 @@ const MONGO_MIGRATION_SCRIPTS = [
|
|||
version: 40
|
||||
}
|
||||
]
|
||||
const LAST_MONGO_SCHEMA_VERSION = (maxBy(MONGO_MIGRATION_SCRIPTS, 'version'))['version']
|
||||
const LAST_SQL_SCHEMA_VERSION = (maxBy(MIGRATION_SCRIPTS, 'version'))['version']
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
|
@ -198,8 +197,8 @@ module.exports = {
|
|||
CONFIG,
|
||||
CONSTRAINTS_FIELDS,
|
||||
FRIEND_SCORE,
|
||||
LAST_MONGO_SCHEMA_VERSION,
|
||||
MONGO_MIGRATION_SCRIPTS,
|
||||
LAST_SQL_SCHEMA_VERSION,
|
||||
MIGRATION_SCRIPTS,
|
||||
OAUTH_LIFETIME,
|
||||
PAGINATION_COUNT_DEFAULT,
|
||||
PODS_SCORE,
|
||||
|
|
|
@ -1,36 +1,46 @@
|
|||
'use strict'
|
||||
|
||||
const mongoose = require('mongoose')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const Sequelize = require('sequelize')
|
||||
|
||||
const constants = require('../initializers/constants')
|
||||
const logger = require('../helpers/logger')
|
||||
|
||||
// Bootstrap models
|
||||
require('../models/application')
|
||||
require('../models/oauth-token')
|
||||
require('../models/user')
|
||||
require('../models/oauth-client')
|
||||
require('../models/video')
|
||||
// Request model needs Video model
|
||||
require('../models/pods')
|
||||
// Request model needs Pod model
|
||||
require('../models/request')
|
||||
const database = {}
|
||||
|
||||
const database = {
|
||||
connect: connect
|
||||
}
|
||||
const sequelize = new Sequelize(constants.CONFIG.DATABASE.DBNAME, 'peertube', 'peertube', {
|
||||
dialect: 'postgres',
|
||||
host: constants.CONFIG.DATABASE.HOSTNAME,
|
||||
port: constants.CONFIG.DATABASE.PORT
|
||||
})
|
||||
|
||||
function connect () {
|
||||
mongoose.Promise = global.Promise
|
||||
mongoose.connect('mongodb://' + constants.CONFIG.DATABASE.HOSTNAME + ':' + constants.CONFIG.DATABASE.PORT + '/' + constants.CONFIG.DATABASE.DBNAME)
|
||||
mongoose.connection.on('error', function () {
|
||||
throw new Error('Mongodb connection error.')
|
||||
const modelDirectory = path.join(__dirname, '..', 'models')
|
||||
fs.readdir(modelDirectory, function (err, files) {
|
||||
if (err) throw err
|
||||
|
||||
files.filter(function (file) {
|
||||
if (file === 'utils.js') return false
|
||||
|
||||
return true
|
||||
})
|
||||
.forEach(function (file) {
|
||||
const model = sequelize.import(path.join(modelDirectory, file))
|
||||
|
||||
database[model.name] = model
|
||||
})
|
||||
|
||||
mongoose.connection.on('open', function () {
|
||||
logger.info('Connected to mongodb.')
|
||||
Object.keys(database).forEach(function (modelName) {
|
||||
if ('associate' in database[modelName]) {
|
||||
database[modelName].associate(database)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
logger.info('Database is ready.')
|
||||
})
|
||||
|
||||
database.sequelize = sequelize
|
||||
database.Sequelize = Sequelize
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
|
|
|
@ -3,26 +3,27 @@
|
|||
const config = require('config')
|
||||
const each = require('async/each')
|
||||
const mkdirp = require('mkdirp')
|
||||
const mongoose = require('mongoose')
|
||||
const passwordGenerator = require('password-generator')
|
||||
const path = require('path')
|
||||
const series = require('async/series')
|
||||
|
||||
const checker = require('./checker')
|
||||
const constants = require('./constants')
|
||||
const db = require('./database')
|
||||
const logger = require('../helpers/logger')
|
||||
const peertubeCrypto = require('../helpers/peertube-crypto')
|
||||
|
||||
const Application = mongoose.model('Application')
|
||||
const Client = mongoose.model('OAuthClient')
|
||||
const User = mongoose.model('User')
|
||||
|
||||
const installer = {
|
||||
installApplication
|
||||
}
|
||||
|
||||
function installApplication (callback) {
|
||||
series([
|
||||
function createDatabase (callbackAsync) {
|
||||
db.sequelize.sync().asCallback(callbackAsync)
|
||||
// db.sequelize.sync({ force: true }).asCallback(callbackAsync)
|
||||
},
|
||||
|
||||
function createDirectories (callbackAsync) {
|
||||
createDirectoriesIfNotExist(callbackAsync)
|
||||
},
|
||||
|
@ -65,16 +66,18 @@ function createOAuthClientIfNotExist (callback) {
|
|||
|
||||
logger.info('Creating a default OAuth Client.')
|
||||
|
||||
const secret = passwordGenerator(32, false)
|
||||
const client = new Client({
|
||||
const id = passwordGenerator(32, false, /[a-z0-9]/)
|
||||
const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
|
||||
const client = db.OAuthClient.build({
|
||||
clientId: id,
|
||||
clientSecret: secret,
|
||||
grants: [ 'password', 'refresh_token' ]
|
||||
})
|
||||
|
||||
client.save(function (err, createdClient) {
|
||||
client.save().asCallback(function (err, createdClient) {
|
||||
if (err) return callback(err)
|
||||
|
||||
logger.info('Client id: ' + createdClient._id)
|
||||
logger.info('Client id: ' + createdClient.clientId)
|
||||
logger.info('Client secret: ' + createdClient.clientSecret)
|
||||
|
||||
return callback(null)
|
||||
|
@ -106,21 +109,21 @@ function createOAuthAdminIfNotExist (callback) {
|
|||
password = passwordGenerator(8, true)
|
||||
}
|
||||
|
||||
const user = new User({
|
||||
const user = db.User.build({
|
||||
username,
|
||||
password,
|
||||
role
|
||||
})
|
||||
|
||||
user.save(function (err, createdUser) {
|
||||
user.save().asCallback(function (err, createdUser) {
|
||||
if (err) return callback(err)
|
||||
|
||||
logger.info('Username: ' + username)
|
||||
logger.info('User password: ' + password)
|
||||
|
||||
logger.info('Creating Application collection.')
|
||||
const application = new Application({ mongoSchemaVersion: constants.LAST_MONGO_SCHEMA_VERSION })
|
||||
application.save(callback)
|
||||
const application = db.Application.build({ sqlSchemaVersion: constants.LAST_SQL_SCHEMA_VERSION })
|
||||
application.save().asCallback(callback)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,24 +1,22 @@
|
|||
'use strict'
|
||||
|
||||
const eachSeries = require('async/eachSeries')
|
||||
const mongoose = require('mongoose')
|
||||
const path = require('path')
|
||||
|
||||
const constants = require('./constants')
|
||||
const db = require('./database')
|
||||
const logger = require('../helpers/logger')
|
||||
|
||||
const Application = mongoose.model('Application')
|
||||
|
||||
const migrator = {
|
||||
migrate: migrate
|
||||
}
|
||||
|
||||
function migrate (callback) {
|
||||
Application.loadMongoSchemaVersion(function (err, actualVersion) {
|
||||
db.Application.loadSqlSchemaVersion(function (err, actualVersion) {
|
||||
if (err) return callback(err)
|
||||
|
||||
// If there are a new mongo schemas
|
||||
if (!actualVersion || actualVersion < constants.LAST_MONGO_SCHEMA_VERSION) {
|
||||
if (!actualVersion || actualVersion < constants.LAST_SQL_SCHEMA_VERSION) {
|
||||
logger.info('Begin migrations.')
|
||||
|
||||
eachSeries(constants.MONGO_MIGRATION_SCRIPTS, function (entity, callbackEach) {
|
||||
|
@ -36,12 +34,12 @@ function migrate (callback) {
|
|||
if (err) return callbackEach(err)
|
||||
|
||||
// Update the new mongo version schema
|
||||
Application.updateMongoSchemaVersion(versionScript, callbackEach)
|
||||
db.Application.updateSqlSchemaVersion(versionScript, callbackEach)
|
||||
})
|
||||
}, function (err) {
|
||||
if (err) return callback(err)
|
||||
|
||||
logger.info('Migrations finished. New mongo version schema: %s', constants.LAST_MONGO_SCHEMA_VERSION)
|
||||
logger.info('Migrations finished. New SQL version schema: %s', constants.LAST_SQL_SCHEMA_VERSION)
|
||||
return callback(null)
|
||||
})
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue