forked from sogebot/sogeBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.js
215 lines (191 loc) · 5.63 KB
/
migrate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
'use strict'
var _ = require('lodash')
var Database = require('nedb-promise')
var DB = new Database({
filename: 'sogeBot.db',
autoload: true
})
var migration = {
'1.1': {
description: '1.0 to 1.1',
process: async function () {
console.log('-> Renaming settings')
await settingsRename('volume', 'songs_volume')
await settingsRename('duration', 'songs_duration')
await settingsRename('shuffle', 'songs_shuffle')
}
},
'1.3': {
description: '1.1 to 1.3',
process: async function () {
console.log('-> Removing bet templates')
await removeFromDB('bets_template')
console.log('-> Alias table update')
await aliasDbUpdate_1_3()
console.log('-> Custom Commands update')
await customCmdsDbUpdate_1_3()
console.log('-> Keywords update')
await keywordsDbUpdate_1_3()
console.log('-> Users update')
await usersDbUpdate_1_3()
console.log('-> Prices update')
await pricesDbUpdate_1_3()
console.log('-> Notices update')
await noticesDbUpdate_1_3()
}
}
}
async function main () {
await migrate('1.1')
await migrate('1.3')
console.log('=> EVERY PROCESS IS DONE')
process.exit()
}
async function migrate (aVersion) {
console.log('Migration from %s', migration[aVersion].description)
await migration[aVersion].process()
console.log('=> DONE')
}
main();
async function settingsRename(aOrig, aRenamed) {
let setting = await DB.findOne({
$where: function () {
return this.type === 'settings' && Object.keys(this).indexOf(aOrig) >= 0;
}
})
if (!_.isNull(setting)) {
setting[aRenamed] = setting[aOrig]
await DB.remove({ _id: setting._id})
delete setting[aOrig]
delete setting._id
await DB.insert(setting)
}
}
async function aliasDbUpdate_1_3() {
let aliases = await DB.find({
$where: function () {
return this._id.startsWith('alias_')
}
})
if (aliases.length === 0) return
let aliasUpdate = { alias: []}
_.each(aliases, function (alias) {
DB.remove({_id: alias._id})
aliasUpdate.alias.push({alias: alias.alias, command: alias.command, enabled: true})
})
await DB.update({ _id: 'alias' }, { $set: aliasUpdate }, { upsert: true })
}
async function pricesDbUpdate_1_3() {
let prices = await DB.find({
$where: function () {
return this._id.startsWith('price_')
}
})
if (prices.length === 0) return
let pricesUpdate = { prices: []}
_.each(prices, function (price) {
DB.remove({_id: price._id})
pricesUpdate.prices.push({price: price.price, command: price.command, enabled: true})
})
await DB.update({ _id: 'prices' }, { $set: pricesUpdate }, { upsert: true })
}
async function customCmdsDbUpdate_1_3() {
let commands = await DB.find({
$where: function () {
return this._id.startsWith('customcmds_')
}
})
if (commands.length === 0) return
let commandsUpdate = { commands: []}
_.each(commands, function (command) {
commandsUpdate.commands.push({command: command.command, response: command.response, enabled: true})
})
await DB.remove({
$where: function () {
return this._id.startsWith('customcmds_')
}
}, { multi: true })
await DB.update({ _id: 'commands' }, { $set: commandsUpdate }, { upsert: true })
}
async function removeFromDB(id) {
await DB.remove({ _id: id })
}
async function keywordsDbUpdate_1_3() {
let kwds = await DB.find({
$where: function () {
return this._id.startsWith('kwd_')
}
})
if (kwds.length === 0) return
let kwdsUpdate = { keywords: [] }
_.each(kwds, function (kwd) {
kwdsUpdate.keywords.push({keyword: kwd.keyword, response: kwd.response, enabled: true})
})
await DB.remove({
$where: function () {
return this._id.startsWith('kwd_')
}
}, { multi: true })
await DB.update({ _id: 'keywords' }, { $set: kwdsUpdate }, { upsert: true })
}
async function noticesDbUpdate_1_3() {
let list = await DB.find({
$where: function () {
return this._id.startsWith('notice_')
}
})
if (list.length === 0) return
let listUpdate = { notices: [] }
_.each(list, function (o) {
listUpdate.notices.push({text: o.text, time: o.time, id: o._id.split('_')[1], enabled: true})
})
await DB.remove({
$where: function () {
return this._id.startsWith('notice_')
}
}, { multi: true })
await DB.update({ _id: 'notices' }, { $set: listUpdate }, { upsert: true })
}
async function removeFromDB(id) {
await DB.remove({ _id: id })
}
async function usersDbUpdate_1_3() {
let users = await DB.find({
$where: function () {
return this._id.startsWith('user_')
}
})
if (users.length === 0) return
let usersUpdate = { users: {} }
_.each(users, function (user) {
delete user._id
let time = {
message: (_.isUndefined(user.lastMessageTime)) ? 0 : user.lastMessageTime,
watched: (_.isUndefined(user.watchTime)) ? 0 : user.watchTime,
parted: (_.isUndefined(user.partedTime)) ? 0 : user.partedTime,
points: (_.isUndefined(user.pointsGrantedAt)) ? 0 : user.pointsGrantedAt
}
delete user.lastMessageTime
delete user.watchTime
delete user.partedTime
delete user.pointsGrantedAt
user.time = time
let is = {
online: false,
follower: (_.isUndefined(user.isFollower)) ? false : user.isFollower
}
delete user.isOnline
delete user.isFollower
user.is = is
usersUpdate.users[user.username] = user
})
await DB.remove({
$where: function () {
return this._id.startsWith('user_')
}
}, { multi: true })
await DB.update({ _id: 'users' }, { $set: usersUpdate }, { upsert: true })
}
async function removeFromDB(id) {
await DB.remove({ _id: id })
}