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.

87 lines
2.2 KiB

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