forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgrade.js
179 lines (155 loc) · 4.7 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
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
"use strict";
var db = require('./database'),
async = require('async'),
winston = require('winston'),
Upgrade = {},
minSchemaDate = Date.UTC(2015, 7, 18), // This value gets updated every new MINOR version
schemaDate, thisSchemaDate,
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
latestSchema = Date.UTC(2015, 10, 6);
Upgrade.check = function(callback) {
db.get('schemaDate', function(err, value) {
if (err) {
return callback(err);
}
if (!value) {
db.set('schemaDate', latestSchema, function(err) {
if (err) {
return callback(err);
}
callback(null);
});
return;
}
var schema_ok = parseInt(value, 10) >= latestSchema;
callback(!schema_ok ? new Error('schema-out-of-date') : null);
});
};
Upgrade.update = function(schemaDate, callback) {
db.set('schemaDate', schemaDate, callback);
};
Upgrade.upgrade = function(callback) {
var updatesMade = false;
winston.info('Beginning database schema update');
async.series([
function(next) {
// Prepare for upgrade & check to make sure the upgrade is possible
db.get('schemaDate', function(err, value) {
if(!value) {
db.set('schemaDate', latestSchema, function() {
next();
});
schemaDate = latestSchema;
} else {
schemaDate = parseInt(value, 10);
}
if (schemaDate >= minSchemaDate) {
next();
} else {
next(new Error('upgrade-not-possible'));
}
});
},
function(next) {
thisSchemaDate = Date.UTC(2015, 8, 30);
if (schemaDate < thisSchemaDate) {
updatesMade = true;
winston.info('[2015/09/30] Converting default Gravatar image to default User Avatar');
async.waterfall([
async.apply(db.isObjectField, 'config', 'customGravatarDefaultImage'),
function(keyExists, _next) {
if (keyExists) {
_next();
} else {
winston.info('[2015/09/30] Converting default Gravatar image to default User Avatar skipped');
Upgrade.update(thisSchemaDate, next);
next();
}
},
async.apply(db.getObjectField, 'config', 'customGravatarDefaultImage'),
async.apply(db.setObjectField, 'config', 'defaultAvatar'),
async.apply(db.deleteObjectField, 'config', 'customGravatarDefaultImage')
], function(err) {
if (err) {
return next(err);
}
winston.info('[2015/09/30] Converting default Gravatar image to default User Avatar done');
Upgrade.update(thisSchemaDate, next);
});
} else {
winston.info('[2015/09/30] Converting default Gravatar image to default User Avatar skipped');
next();
}
},
function(next) {
thisSchemaDate = Date.UTC(2015, 10, 6);
if (schemaDate < thisSchemaDate) {
updatesMade = true;
winston.info('[2015/11/06] Removing gravatar');
db.getSortedSetRange('users:joindate', 0, -1, function(err, uids) {
if (err) {
return next(err);
}
async.eachLimit(uids, 500, function(uid, next) {
db.getObjectFields('user:' + uid, ['picture', 'gravatarpicture'], function(err, userData) {
if (err) {
return next(err);
}
if (!userData.picture || !userData.gravatarpicture) {
return next();
}
if (userData.gravatarpicture === userData.picture) {
async.series([
function (next) {
db.setObjectField('user:' + uid, 'picture', '', next);
},
function (next) {
db.deleteObjectField('user:' + uid, 'gravatarpicture', next);
}
], next);
} else {
db.deleteObjectField('user:' + uid, 'gravatarpicture', next);
}
});
}, function(err) {
if (err) {
return next(err);
}
winston.info('[2015/11/06] Gravatar pictures removed!');
Upgrade.update(thisSchemaDate, next);
});
});
} else {
winston.info('[2015/11/06] Gravatar removal skipped');
next();
}
}
// Add new schema updates here
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 24!!!
], function(err) {
if (!err) {
if(updatesMade) {
winston.info('[upgrade] Schema update complete!');
} else {
winston.info('[upgrade] Schema already up to date!');
}
} else {
switch(err.message) {
case 'upgrade-not-possible':
winston.error('[upgrade] NodeBB upgrade could not complete, as your database schema is too far out of date.');
winston.error('[upgrade] Please ensure that you did not skip any minor version upgrades.');
winston.error('[upgrade] (e.g. v0.1.x directly to v0.3.x)');
break;
default:
winston.error('[upgrade] Errors were encountered while updating the NodeBB schema: ' + err.message);
break;
}
}
if (typeof callback === 'function') {
callback(err);
} else {
process.exit();
}
});
};
module.exports = Upgrade;