You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/upgrade.js

291 lines
7.4 KiB
JavaScript

"use strict";
var //db = require('./database'),
11 years ago
// TODO: temp until upgrade is figured out with dbal,
RDB = require('./database/redis').client,
11 years ago
async = require('async'),
winston = require('winston'),
notifications = require('./notifications'),
categories = require('./categories'),
Upgrade = {},
schemaDate, thisSchemaDate;
Upgrade.check = function(callback) {
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
var latestSchema = new Date(2013, 11, 2).getTime();
RDB.get('schemaDate', function(err, value) {
11 years ago
if (parseInt(value, 10) >= latestSchema) {
callback(true);
} else {
callback(false);
}
});
};
11 years ago
Upgrade.upgrade = function(callback) {
winston.info('Beginning Redis database schema update');
async.series([
function(next) {
RDB.get('schemaDate', function(err, value) {
schemaDate = value;
next();
});
11 years ago
},
function(next) {
thisSchemaDate = new Date(2013, 9, 3).getTime();
if (schemaDate < thisSchemaDate) {
async.series([
function(next) {
RDB.keys('uid:*:notifications:flag', function(err, keys) {
if (keys.length > 0) {
winston.info('[2013/10/03] Removing deprecated Notification Flags');
async.each(keys, function(key, next) {
RDB.del(key, next);
}, next);
} else {
winston.info('[2013/10/03] No Notification Flags found. Good.');
next();
}
});
},
function(next) {
winston.info('[2013/10/03] Updating Notifications');
RDB.keys('uid:*:notifications:*', function(err, keys) {
async.each(keys, function(key, next) {
RDB.zrange(key, 0, -1, function(err, nids) {
async.each(nids, function(nid, next) {
notifications.get(nid, null, function(notif_data) {
if (notif_data) {
RDB.zadd(key, notif_data.datetime, nid, next);
11 years ago
} else {
next();
}
});
}, next);
});
}, next);
});
},
function(next) {
RDB.keys('notifications:*', function(err, keys) {
if (keys.length > 0) {
winston.info('[2013/10/03] Removing Notification Scores');
async.each(keys, function(key, next) {
if (key === 'notifications:next_nid') {
return next();
}
11 years ago
RDB.hdel(key, 'score', next);
}, next);
} else {
winston.info('[2013/10/03] No Notification Scores found. Good.');
next();
}
11 years ago
});
}
], next);
} else {
winston.info('[2013/10/03] Updates to Notifications skipped.');
next();
}
},
function(next) {
thisSchemaDate = new Date(2013, 9, 23).getTime();
if (schemaDate < thisSchemaDate) {
RDB.keys('notifications:*', function(err, keys) {
11 years ago
keys = keys.filter(function(key) {
if (key === 'notifications:next_nid') {
return false;
} else {
return true;
}
}).map(function(key) {
return key.slice(14);
11 years ago
});
winston.info('[2013/10/23] Adding existing notifications to set');
if(keys && Array.isArray(keys)) {
async.each(keys, function(key, cb) {
RDB.sadd('notifications', key, cb);
}, next);
} else next();
});
} else {
winston.info('[2013/10/23] Updates to Notifications skipped.');
next();
}
},
function(next) {
thisSchemaDate = new Date(2013, 10, 11).getTime();
if (schemaDate < thisSchemaDate) {
RDB.hset('config', 'postDelay', 10, function(err, success) {
winston.info('[2013/11/11] Updated postDelay to 10 seconds.');
next();
});
} else {
winston.info('[2013/11/11] Update to postDelay skipped.');
next();
}
11 years ago
},
function(next) {
thisSchemaDate = new Date(2013, 10, 22).getTime();
if (schemaDate < thisSchemaDate) {
RDB.keys('category:*', function(err, categories) {
async.each(categories, function(categoryStr, next) {
var hex;
RDB.hgetall(categoryStr, function(err, categoryObj) {
switch(categoryObj.blockclass) {
case 'category-purple':
hex = '#ab1290';
break;
case 'category-darkblue':
hex = '#004c66';
break;
case 'category-blue':
hex = '#0059b2';
break;
case 'category-darkgreen':
hex = '#004000';
break;
case 'category-orange':
hex = '#ff7a4d';
break;
default:
hex = '#0059b2';
break;
}
RDB.hset(categoryStr, 'bgColor', hex, next);
RDB.hdel(categoryStr, 'blockclass');
});
}, function() {
winston.info('[2013/11/22] Updated Category colours.');
next();
});
});
} else {
winston.info('[2013/11/22] Update to Category colours skipped.');
next();
}
},
function(next) {
thisSchemaDate = new Date(2013, 10, 26).getTime();
if (schemaDate < thisSchemaDate) {
categories.getAllCategories(0, function(err, categories) {
function updateIcon(category, next) {
var icon = '';
if(category.icon === 'icon-lightbulb') {
icon = 'fa-lightbulb-o';
} else if(category.icon === 'icon-plus-sign') {
icon = 'fa-plus';
} else if(category.icon === 'icon-screenshot') {
icon = 'fa-crosshairs';
} else {
icon = category.icon.replace('icon-', 'fa-');
}
RDB.hset('category:' + category.cid, 'icon', icon, next);
}
async.each(categories.categories, updateIcon, function(err) {
if(err) {
return next(err);
}
winston.info('[2013/11/26] Updated Category icons.');
next();
});
});
} else {
winston.info('[2013/11/26] Update to Category icons skipped.');
next();
}
},
function(next) {
function updateKeyToHash(key, next) {
RDB.get(key, function(err, value) {
RDB.hset('global', newKeys[key], value, next);
});
}
thisSchemaDate = new Date(2013, 11, 2).getTime();
if (schemaDate < thisSchemaDate) {
var keys = [
'global:next_user_id',
'next_topic_id',
'next_gid',
'notifications:next_nid',
'global:next_category_id',
'global:next_message_id',
'global:next_post_id',
'usercount',
'totaltopiccount',
'totalpostcount'
];
var newKeys = {
'global:next_user_id':'nextUid',
'next_topic_id':'nextTid',
'next_gid':'nextGid',
'notifications:next_nid':'nextNid',
'global:next_category_id':'nextCid',
'global:next_message_id':'nextMid',
'global:next_post_id':'nextPid',
'usercount':'userCount',
'totaltopiccount':'topicCount',
'totalpostcount':'postCount'
};
async.each(keys, updateKeyToHash, function(err) {
if(err) {
return next(err);
}
winston.info('[2013/12/2] Updated global keys to hash.');
next();
});
} else {
winston.info('[2013/12/2] Update to global keys skipped');
next();
}
},
// Add new schema updates here
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 12!!!
], function(err) {
if (!err) {
RDB.set('schemaDate', thisSchemaDate, function(err) {
if (!err) {
winston.info('[upgrade] Redis schema update complete!');
11 years ago
if (callback) {
callback(err);
} else {
process.exit();
}
} else {
winston.error('[upgrade] Could not update NodeBB schema date!');
11 years ago
process.exit();
}
});
} else {
winston.error('[upgrade] Errors were encountered while updating the NodeBB schema: ' + err.message);
11 years ago
process.exit();
}
});
};
module.exports = Upgrade;