Skip to content

Commit

Permalink
tools/migrate: Fix migration errors
Browse files Browse the repository at this point in the history
Possibly fixed unique violation and _id function error
  • Loading branch information
sogehige committed Apr 7, 2018
1 parent c89bdd9 commit 81089a5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions libs/databases/database.js
Expand Up @@ -8,10 +8,10 @@ const IMasterController = require('./master')

class Database {
constructor (cluster) {
cluster = _.isNil(cluster) ? true : cluster
this.cluster = _.isNil(cluster) ? true : cluster
this.engine = null

if (require('cluster').isMaster && cluster) this.engine = new IMasterController()
if (require('cluster').isMaster && this.cluster) this.engine = new IMasterController()
else if (config.database.type === 'nedb') this.engine = new INeDB()
else if (config.database.type === 'mongodb') this.engine = new IMongoDB()

Expand Down
32 changes: 17 additions & 15 deletions libs/databases/nedb.js
Expand Up @@ -28,21 +28,23 @@ class INeDB extends Interface {
this.table[table].persistence.setAutocompactionInterval(60000)

// create indexes
switch (table) {
case 'users.bits':
case 'users.tips':
case 'api.stats':
this.table[table].ensureIndex({fieldName: 'timestamp'})
break
case 'users':
this.table[table].ensureIndex({fieldName: 'username', unique: true})
break
case 'cache':
this.table[table].ensureIndex({fieldName: 'key'})
break
case 'stats':
this.table[table].ensureIndex({fieldName: 'whenOnline'})
break
if (this.cluster) {
switch (table) {
case 'users.bits':
case 'users.tips':
case 'api.stats':
this.table[table].ensureIndex({fieldName: 'timestamp'})
break
case 'users':
this.table[table].ensureIndex({fieldName: 'username', unique: true})
break
case 'cache':
this.table[table].ensureIndex({fieldName: 'key'})
break
case 'stats':
this.table[table].ensureIndex({fieldName: 'whenOnline'})
break
}
}
}
return this.table[table]
Expand Down
10 changes: 4 additions & 6 deletions tools/migrate.js
Expand Up @@ -94,13 +94,11 @@ let migration = {
let users = await global.db.engine.find('users')
for (let user of users) {
if (!_.has(user, 'stats.bits') || _.isNil(user.stats.bits)) continue // skip if bits are null/undefined
await Promise.all([
global.db.engine.remove('users', { _id: user._id.toString() }),
global.db.engine.insert('users.bits', { username: user.username, amount: user.stats.bits, message: 'Migrated from 6.x', timestamp: _.now() })
])
await global.db.engine.remove('users', { username: user.username })
await global.db.engine.insert('users.bits', { username: user.username, amount: user.stats.bits, message: 'Migrated from 6.x', timestamp: _.now() })
delete user.stats.bits
delete user._id``
await global.db.engine.insert('users', user)
delete user._id
await global.db.engine.update('users', { username: user.username }, user)
}
}
}]
Expand Down

0 comments on commit 81089a5

Please sign in to comment.