system.js 2.09 KB
Newer Older
1
const _ = require('lodash')
2
const cfgHelper = require('../helpers/config')
3
const Promise = require('bluebird')
Nicolas Giard's avatar
Nicolas Giard committed
4 5
const fs = require('fs-extra')
const path = require('path')
6

7
/* global WIKI */
8 9

module.exports = {
10 11 12 13 14 15 16 17
  updates: {
    channel: 'BETA',
    version: WIKI.version,
    releaseDate: WIKI.releaseDate,
    minimumVersionRequired: '2.0.0-beta.0',
    minimumNodeRequired: '10.12.0'
  },
  init() {
Nicolas Giard's avatar
Nicolas Giard committed
18
    // Clear content cache
19
    fs.emptyDir(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, 'cache'))
Nicolas Giard's avatar
Nicolas Giard committed
20

21 22
    return this
  },
NGPixel's avatar
NGPixel committed
23
  /**
24
   * Upgrade from WIKI.js 1.x - MongoDB database
NGPixel's avatar
NGPixel committed
25
   *
26
   * @param {Object} opts Options object
NGPixel's avatar
NGPixel committed
27
   */
28
  async upgradeFromMongo (opts) {
29
    WIKI.logger.info('Upgrading from MongoDB...')
NGPixel's avatar
NGPixel committed
30

31 32
    let mongo = require('mongodb').MongoClient
    let parsedMongoConStr = cfgHelper.parseConfigValue(opts.mongoCnStr)
33

34 35 36
    return new Promise((resolve, reject) => {
      // Connect to MongoDB

37
      mongo.connect(parsedMongoConStr, {
38 39 40 41 42 43 44 45 46 47 48 49 50
        autoReconnect: false,
        reconnectTries: 2,
        reconnectInterval: 1000,
        connectTimeoutMS: 5000,
        socketTimeoutMS: 5000
      }, async (err, db) => {
        try {
          if (err !== null) { throw err }

          let users = db.collection('users')

          // Check if users table is populated
          let userCount = await users.count()
51 52
          if (userCount < 2) {
            throw new Error('MongoDB Upgrade: Users table is empty!')
53 54
          }

55 56 57 58 59 60
          // Import all users
          let userData = await users.find({
            email: {
              $not: 'guest'
            }
          }).toArray()
61
          await WIKI.models.User.bulkCreate(_.map(userData, usr => {
62 63 64 65 66 67 68 69 70 71
            return {
              email: usr.email,
              name: usr.name || 'Imported User',
              password: usr.password || '',
              provider: usr.provider || 'local',
              providerId: usr.providerId || '',
              role: 'user',
              createdAt: usr.createdAt
            }
          }))
NGPixel's avatar
NGPixel committed
72

73
          resolve(true)
Nick's avatar
Nick committed
74 75
        } catch (errc) {
          reject(errc)
76
        }
77
        db.close()
78
      })
79 80 81
    })
  }
}