From 4d6881fa65f440a4c30c6881c4d8ae2671879007 Mon Sep 17 00:00:00 2001
From: Julian Lam <julian.lam@gmail.com>
Date: Thu, 3 Oct 2013 22:36:00 -0400
Subject: [PATCH] reset update for 0.0.7, and added new schema update for
 notifications

---
 src/upgrade.js | 213 +++++++------------------------------------------
 1 file changed, 31 insertions(+), 182 deletions(-)

diff --git a/src/upgrade.js b/src/upgrade.js
index 36dfb076c4..b060c532c5 100644
--- a/src/upgrade.js
+++ b/src/upgrade.js
@@ -1,193 +1,42 @@
 var RDB = require('./redis.js'),
 	async = require('async'),
 	winston = require('winston'),
-	user = require('./user'),
-	Groups = require('./groups');
-
-
-function upgradeCategory(cid, callback) {
-	RDB.type('categories:' + cid + ':tid', function(err, type) {
-		if (type === 'set') {
-			RDB.smembers('categories:' + cid + ':tid', function(err, tids) {
-
-				function moveTopic(tid, callback) {
-					RDB.hget('topic:' + tid, 'timestamp', function(err, timestamp) {
-						if (err)
-							return callback(err);
-
-						RDB.zadd('temp_categories:' + cid + ':tid', timestamp, tid);
-						callback(null);
+	notifications = require('./notifications')
+	Upgrade = {};
+
+Upgrade.upgrade = function() {
+	winston.info('Beginning Redis database schema update');
+
+	async.series([
+		function(next) {
+			RDB.hget('notifications:1', 'score', function(err, score) {
+				if (score) {
+					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, function(notif_data) {
+										RDB.zadd(key, notif_data.datetime, nid, next);
+									});
+								}, next);
+							});
+						}, next);
 					});
+				} else {
+					winston.info('[2013/10/03] Updates to Notifications skipped.');
 				}
-
-				async.each(tids, moveTopic, function(err) {
-					if (!err) {
-						RDB.rename('temp_categories:' + cid + ':tid', 'categories:' + cid + ':tid');
-						callback(null);
-					} else
-						callback(err);
-				});
-
 			});
-		} else {
-			winston.info('category already upgraded ' + cid);
-			callback(null);
 		}
-	});
-}
-
-function upgradeUser(uid, callback) {
-	user.getUserFields(uid, ['joindate', 'postcount', 'reputation'], function(err, userData) {
-		if (err)
-			return callback(err);
-
-		async.parallel([
-			function(next) {
-				if (userData.joindate)
-					RDB.zadd('users:joindate', userData.joindate, uid, next);
-				else
-					next(null);
-			},
-			function(next) {
-				if (userData.postcount)
-					RDB.zadd('users:postcount', userData.postcount, uid, next);
-				else
-					next(null);
-			},
-			function(next) {
-				if (userData.reputation)
-					RDB.zadd('users:reputation', userData.reputation, uid, next);
-				else
-					next(null);
-			}
-		], function(err, result) {
-			callback(err);
-		});
-	});
-}
-
-function upgradeUserHash(uid, callback) {
-	user.getUserFields(uid, ['username', 'userslug', 'email'], function(err, userData) {
-		if (err)
-			return callback(err);
-
-		async.parallel([
-			function(next) {
-				if (userData.username)
-					RDB.hset('username:uid', userData.username, uid, next);
-				else
-					next(null);
-			},
-			function(next) {
-				if (userData.userslug)
-					RDB.hset('userslug:uid', userData.userslug, uid, next);
-				else
-					next(null);
-			},
-			function(next) {
-				if (userData.email)
-					RDB.hset('email:uid', userData.email, uid, next);
-				else
-					next(null);
-			}
-
-		], function(err, result) {
-			callback(err);
-		});
-	});
-}
-
-function upgradeAdmins(callback) {
-	Groups.getGidFromName('Administrators', function(err, gid) {
-		if (!err && !gid) {
-			winston.info('Upgrading Administrators');
-
-			async.parallel([
-				function(next) {
-					RDB.smembers("administrators", next);
-				},
-				function(next) {
-					Groups.create('Administrators', 'Forum Administrators', next);
-				}
-			], function(err, results) {
-				var gid = results[1].gid;
-
-				async.each(results[0], function(uid, next) {
-					Groups.join(gid, uid, next);
-				}, callback);
-			});
+		// Add new schema updates here
+	], function(err) {
+		if (!err) {
+			winston.info('Redis schema update complete!');
+			process.exit();
 		} else {
-			winston.info('Administrators group OK')
-			callback();
+			winston.error('Errors were encountered while updating the NodeBB schema: ' + err.message);
 		}
 	});
-}
-
-exports.upgrade = function() {
-
-	winston.info('upgrading nodebb now');
-
-	var schema = [
-		function upgradeCategories(next) {
-			winston.info('upgrading categories');
-
-			RDB.lrange('categories:cid', 0, -1, function(err, cids) {
-
-				async.each(cids, upgradeCategory, function(err) {
-					if (!err) {
-						winston.info('upgraded categories');
-						next(null, null);
-					} else {
-						next(err, null);
-					}
-				});
-			});
-		},
-
-		function upgradeUsers(next) {
-			winston.info('upgrading users');
-
-			RDB.lrange('userlist', 0, -1, function(err, uids) {
-
-				async.each(uids, upgradeUser, function(err) {
-					if (!err) {
-						winston.info('upgraded users');
-						next(null, null);
-					} else {
-						next(err, null);
-					}
-				});
+};
 
-			});
-		},
-
-		function upgradeUserHashes(next) {
-			winston.info('upgrading user hashes');
-			RDB.zrange('users:joindate', 0, -1, function(err, uids) {
-				if (err)
-					return next(err);
-
-				async.each(uids, upgradeUserHash, function(err) {
-					if (!err) {
-						winston.info('upgraded user hashes');
-						next(null, null);
-					} else {
-						next(err, null);
-					}
-				});
-			});
-		},
-
-		upgradeAdmins
-	];
-
-	async.series(schema, function(err, results) {
-		if (!err)
-			winston.info('upgrade complete');
-		else
-			winston.err(err);
-
-		process.exit();
-
-	});
-}
\ No newline at end of file
+module.exports = Upgrade;
\ No newline at end of file