You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.1 KiB

  1. const _ = require('lodash')
  2. const cfgHelper = require('../helpers/config')
  3. const Promise = require('bluebird')
  4. const fs = require('fs-extra')
  5. const path = require('path')
  6. /* global WIKI */
  7. module.exports = {
  8. updates: {
  9. channel: 'BETA',
  10. version: WIKI.version,
  11. releaseDate: WIKI.releaseDate,
  12. minimumVersionRequired: '2.0.0-beta.0',
  13. minimumNodeRequired: '10.12.0'
  14. },
  15. init() {
  16. // Clear content cache
  17. fs.emptyDir(path.join(WIKI.ROOTPATH, 'data/cache'))
  18. return this
  19. },
  20. /**
  21. * Upgrade from WIKI.js 1.x - MongoDB database
  22. *
  23. * @param {Object} opts Options object
  24. */
  25. async upgradeFromMongo (opts) {
  26. WIKI.logger.info('Upgrading from MongoDB...')
  27. let mongo = require('mongodb').MongoClient
  28. let parsedMongoConStr = cfgHelper.parseConfigValue(opts.mongoCnStr)
  29. return new Promise((resolve, reject) => {
  30. // Connect to MongoDB
  31. mongo.connect(parsedMongoConStr, {
  32. autoReconnect: false,
  33. reconnectTries: 2,
  34. reconnectInterval: 1000,
  35. connectTimeoutMS: 5000,
  36. socketTimeoutMS: 5000
  37. }, async (err, db) => {
  38. try {
  39. if (err !== null) { throw err }
  40. let users = db.collection('users')
  41. // Check if users table is populated
  42. let userCount = await users.count()
  43. if (userCount < 2) {
  44. throw new Error('MongoDB Upgrade: Users table is empty!')
  45. }
  46. // Import all users
  47. let userData = await users.find({
  48. email: {
  49. $not: 'guest'
  50. }
  51. }).toArray()
  52. await WIKI.models.User.bulkCreate(_.map(userData, usr => {
  53. return {
  54. email: usr.email,
  55. name: usr.name || 'Imported User',
  56. password: usr.password || '',
  57. provider: usr.provider || 'local',
  58. providerId: usr.providerId || '',
  59. role: 'user',
  60. createdAt: usr.createdAt
  61. }
  62. }))
  63. resolve(true)
  64. } catch (err) {
  65. reject(err)
  66. }
  67. db.close()
  68. })
  69. })
  70. }
  71. }