mirror of https://github.com/requarks/wiki
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.
69 lines
1.8 KiB
69 lines
1.8 KiB
7 years ago
|
const _ = require('lodash')
|
||
7 years ago
|
const cfgHelper = require('../helpers/config')
|
||
7 years ago
|
const Promise = require('bluebird')
|
||
|
|
||
|
/* global wiki */
|
||
8 years ago
|
|
||
|
module.exports = {
|
||
8 years ago
|
/**
|
||
7 years ago
|
* Upgrade from Wiki.js 1.x - MongoDB database
|
||
8 years ago
|
*
|
||
7 years ago
|
* @param {Object} opts Options object
|
||
8 years ago
|
*/
|
||
7 years ago
|
async upgradeFromMongo (opts) {
|
||
|
wiki.telemetry.sendEvent('setup', 'upgradeFromMongo')
|
||
8 years ago
|
|
||
7 years ago
|
wiki.logger.info('Upgrading from MongoDB...')
|
||
|
|
||
7 years ago
|
let mongo = require('mongodb').MongoClient
|
||
|
let parsedMongoConStr = cfgHelper.parseConfigValue(opts.mongoCnStr)
|
||
8 years ago
|
|
||
7 years ago
|
return new Promise((resolve, reject) => {
|
||
|
// Connect to MongoDB
|
||
|
|
||
7 years ago
|
mongo.connect(parsedMongoConStr, {
|
||
7 years ago
|
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()
|
||
7 years ago
|
if (userCount < 2) {
|
||
|
throw new Error('MongoDB Upgrade: Users table is empty!')
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
// Import all users
|
||
|
let userData = await users.find({
|
||
|
email: {
|
||
|
$not: 'guest'
|
||
|
}
|
||
|
}).toArray()
|
||
|
await wiki.db.User.bulkCreate(_.map(userData, usr => {
|
||
|
return {
|
||
|
email: usr.email,
|
||
|
name: usr.name || 'Imported User',
|
||
|
password: usr.password || '',
|
||
|
provider: usr.provider || 'local',
|
||
|
providerId: usr.providerId || '',
|
||
|
role: 'user',
|
||
|
createdAt: usr.createdAt
|
||
|
}
|
||
|
}))
|
||
8 years ago
|
|
||
7 years ago
|
resolve(true)
|
||
|
} catch (err) {
|
||
|
reject(err)
|
||
|
}
|
||
7 years ago
|
db.close()
|
||
7 years ago
|
})
|
||
8 years ago
|
})
|
||
|
}
|
||
|
}
|