]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/initializers/installer.js
Update migrations code
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.js
index c12187871653bc124a1ae534be86c34f467b8895..d5382364ed77d4ccc47c8624232a0ae53af56ebd 100644 (file)
@@ -3,25 +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 Client = mongoose.model('OAuthClient')
-const User = mongoose.model('User')
-
 const installer = {
-  installApplication: installApplication
+  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)
     },
@@ -64,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)
@@ -105,19 +109,20 @@ function createOAuthAdminIfNotExist (callback) {
       password = passwordGenerator(8, true)
     }
 
-    const user = new User({
-      username: username,
-      password: password,
-      role: role
+    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: ' + createdUser.username)
-      logger.info('User password: ' + createdUser.password)
+      logger.info('Username: ' + username)
+      logger.info('User password: ' + password)
 
-      return callback(null)
+      logger.info('Creating Application table.')
+      db.Application.create({ migrationVersion: constants.LAST_MIGRATION_VERSION }).asCallback(callback)
     })
   })
 }