tweaking upgrade script so it works with a real db

v1.18.x
Julian Lam 11 years ago
parent 4701eb1cee
commit 96d58288c9

@ -350,38 +350,58 @@ Upgrade.upgrade = function(callback) {
var names = Object.keys(mapping), var names = Object.keys(mapping),
reverseMapping = {}, reverseMapping = {},
isGroupList = /^cid:[0-9]+:privileges:g\+[rw]$/; isGroupList = /^cid:[0-9]+:privileges:g\+[rw]$/,
gid;
for(var groupName in mapping) { for(var groupName in mapping) {
if (mapping.hasOwnProperty(groupName)) { gid = mapping[groupName];
reverseMapping[parseInt(mapping[groupName], 10)] = groupName; if (mapping.hasOwnProperty(groupName) && !reverseMapping.hasOwnProperty(gid)) {
reverseMapping[parseInt(gid, 10)] = groupName;
} }
} }
async.each(names, function(name, next) { async.eachSeries(names, function(name, next) {
async.series([ async.series([
function(next) { function(next) {
// Remove the gid from the hash // Remove the gid from the hash
db.exists('gid:' + mapping[name], function(err, exists) {
if (exists) {
db.deleteObjectField('gid:' + mapping[name], 'gid', next); db.deleteObjectField('gid:' + mapping[name], 'gid', next);
} else {
next();
}
});
}, },
function(next) { function(next) {
// Rename gid hash to groupName hash // Rename gid hash to groupName hash
db.exists('gid:' + mapping[name], function(err, exists) {
if (exists) {
db.rename('gid:' + mapping[name], 'group:' + name, next); db.rename('gid:' + mapping[name], 'group:' + name, next);
} else {
next();
}
});
}, },
function(next) { function(next) {
// Move member lists over // Move member lists over
db.exists('gid:' + mapping[name] + ':members', function(err, exists) { db.exists('gid:' + mapping[name], function(err, dstExists) {
if (err) { if (err) {
return next(err); return next(err);
} }
if (exists) { db.exists('gid:' + mapping[name] + ':members', function(err, srcExists) {
if (err) {
return next(err);
}
if (srcExists && !dstExists) {
db.rename('gid:' + mapping[name] + ':members', 'group:' + name + ':members', next); db.rename('gid:' + mapping[name] + ':members', 'group:' + name + ':members', next);
} else { } else {
// No members, do nothing, they'll be removed later // No members or group memberlist collision: do nothing, they'll be removed later
next(); next();
} }
}); });
});
}, },
function(next) { function(next) {
// Add group to the directory (set) // Add group to the directory (set)
@ -389,19 +409,59 @@ Upgrade.upgrade = function(callback) {
}, },
function(next) { function(next) {
// If this group contained gids, map the gids to group names // If this group contained gids, map the gids to group names
if (isGroupList.test(name)) { // Also check if the mapping and reverseMapping still work, if not, delete this group
if (isGroupList.test(name) && name === reverseMapping[mapping[name]]) {
db.getSetMembers('group:' + name + ':members', function(err, gids) { db.getSetMembers('group:' + name + ':members', function(err, gids) {
async.each(gids, function(gid, next) { async.each(gids, function(gid, next) {
db.setRemove('group:' + name + ':members', gid); db.setRemove('group:' + name + ':members', gid);
db.setAdd('group:' + name + ':members', reverseMapping[gid], next); db.setAdd('group:' + name + ':members', reverseMapping[gid], next);
}, next); }, next);
}); });
} else if (name !== reverseMapping[mapping[name]]) {
async.parallel([
function(next) {
db.delete('group:' + name, next);
},
function(next) {
db.delete('group:' + name + ':members', next);
},
function(next) {
db.setRemove('groups', name, next);
}
], next);
} else {
next();
}
},
function(next) {
// Fix its' name, if it is wrong for whatever reason
db.getObjectField('group:' + name, 'name', function(err, groupName) {
if (name && groupName && name !== groupName) {
console.log('I am renaming', groupName, 'to', name);
async.series([
function(cb) {
db.setObjectField('group:' + name, 'name', name, cb);
},
function(cb) {
db.setRemove('groups', groupName, cb);
},
function(cb) {
db.setAdd('groups', name, cb);
}
], next);
} else { } else {
next(); next();
} }
});
} }
], next); ], next);
}, function(err) { }, function(err) {
if (err) {
winston.error('[2014/3/21] Problem removing gids and pruning groups.');
winston.error(err.message);
return next();
}
// Clean-up // Clean-up
var isValidHiddenGroup = /^cid:[0-9]+:privileges:(g)?\+[rw]$/; var isValidHiddenGroup = /^cid:[0-9]+:privileges:(g)?\+[rw]$/;
async.series([ async.series([
@ -433,11 +493,9 @@ Upgrade.upgrade = function(callback) {
function(next) { function(next) {
Groups.list({ showAllGroups: true }, function(err, groups) { Groups.list({ showAllGroups: true }, function(err, groups) {
async.each(groups, function(group, next) { async.each(groups, function(group, next) {
if (group.memberCount === 0) { // If deleted, empty, or invalidly named hidden group, delete
// If empty, delete group if (group.deleted || group.memberCount === 0 || (group.hidden && !isValidHiddenGroup.test(group.name))) {
Groups.destroy(group.name, next); // console.log('destroying', group.name);
} else if (group.hidden && !isValidHiddenGroup.test(group.name)) {
// If invalidly named hidden group, delete
Groups.destroy(group.name, next); Groups.destroy(group.name, next);
} else { } else {
next(); next();
@ -447,17 +505,17 @@ Upgrade.upgrade = function(callback) {
} }
], function(err) { ], function(err) {
if (err) { if (err) {
winston.error('[2014/3/19] Problem removing gids and pruning groups.'); winston.error('[2014/3/21] Problem removing gids and pruning groups.');
next(); next();
} else { } else {
winston.info('[2014/3/19] Removing gids and pruning groups'); winston.info('[2014/3/21] Removing gids and pruning groups');
Upgrade.update(thisSchemaDate, next); Upgrade.update(thisSchemaDate, next);
} }
}); });
}); });
}); });
} else { } else {
winston.info('[2014/3/19] Removing gids and pruning groups - skipped'); winston.info('[2014/3/21] Removing gids and pruning groups - skipped');
next(); next();
} }
} }

Loading…
Cancel
Save