-
Notifications
You must be signed in to change notification settings - Fork 15
/
upgrade.js
54 lines (47 loc) · 1.46 KB
/
upgrade.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
function createUserDeviceNames(rclient, next) {
// create user device name sets when they are missing
var DeviceProvider = require('./deviceProvider').DeviceProvider;
var deviceProvider = new DeviceProvider(rclient);
rclient.smembers("uids", function(error, uids) {
if (error) { return next(error); }
var count = uids.length;
if (count === 0) {
return next();
}
function doneForUid() {
if (--count === 0) {
return next();
}
}
function saveDeviceNamesList(uid, next) {
console.log("DB UPGRADE: saving device names list for uid: " + uid);
deviceProvider.findByUserId(uid, function(error, devices) {
if (error) { return next(error); }
var dcount = devices.length;
if (dcount === 0) {
rclient.sadd("uid:" + uid + ":deviceNames", '');
return next();
}
devices.forEach(function(device) {
rclient.sadd("uid:" + uid + ":deviceNames", device.name ? device.name : '');
if (--dcount === 0) {
return next();
}
});
});
}
uids.forEach(function(uid) {
rclient.exists("uid:" + uid + ":deviceNames", function(error, exists) {
if (error) { return next(error); }
if (!exists) {
saveDeviceNamesList(uid, doneForUid);
} else {
doneForUid();
}
});
});
});
}
exports.upgrade = function(rclient, next) {
createUserDeviceNames(rclient, next);
};