-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmigrations.js
46 lines (43 loc) · 1.06 KB
/
migrations.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
async function from9to10 (db, cb) {
// move retained messages from hash to keys
const RETAINEDKEY = 'retained'
function retainedKey (topic) {
return `${RETAINEDKEY}:${encodeURIComponent(topic)}`
}
// get all topics
db.hkeys(RETAINEDKEY, function (err, topics) {
if (err) {
return cb(err)
}
Promise.all(topics.map(t => {
return new Promise((resolve, reject) => {
// get packet payload
db.hgetBuffer(RETAINEDKEY, t, function (err, payload) {
if (err) {
return reject(err)
}
// set packet with new format
db.set(retainedKey(t), payload, function (err) {
if (err) {
return reject(err)
}
// remove old packet
db.hdel(RETAINEDKEY, t, function (err) {
if (err) {
return reject(err)
}
resolve()
})
})
})
})
})).then(() => {
cb(null)
}).catch(err => {
cb(err)
})
})
}
module.exports = {
from9to10
}