Merge channel ranks tables

This commit is contained in:
Calvin Montgomery 2014-06-23 21:40:40 -07:00
parent 0abaaba690
commit 4afd69b2fb
4 changed files with 89 additions and 64 deletions

View File

@ -310,7 +310,7 @@ KickBanModule.prototype.banIP = function (actor, ip, name, reason, cb) {
return error("You do not have ban permissions on this channel"); return error("You do not have ban permissions on this channel");
} }
Q.nfcall(Account.rankForIP, ip).then(function (rank) { Q.nfcall(Account.rankForIP, ip, { channel: chan.name }).then(function (rank) {
if (rank >= actor.account.effectiveRank) { if (rank >= actor.account.effectiveRank) {
throw "You don't have permission to ban IP " + masked; throw "You don't have permission to ban IP " + masked;
} }

View File

@ -30,8 +30,6 @@ function initTables(name, owner, callback) {
module.exports.setRank(name, owner, rank, function (err) { module.exports.setRank(name, owner, rank, function (err) {
if (err) { if (err) {
dropTable("chan_" + name + "_ranks");
dropTable("chan_" + name + "_ranks");
callback(err, null); callback(err, null);
return; return;
} }
@ -203,32 +201,24 @@ module.exports = {
return; return;
} }
dropTable("chan_" + name + "_ranks", function (err) { dropTable("chan_" + name + "_bans", function (err) {
dropTable("chan_" + name + "_bans", function (e2) { db.query("DELETE FROM `channels` WHERE name=?", [name],
if (err && e2) { function (e3) {
err += "\n" + e2; if (err && e3) {
} else if (e2) { err += "\n" + e3;
err = e2; } else if (e3) {
err = e3;
} }
db.query("DELETE FROM `channels` WHERE name=?", [name], fs.unlink(path.join(__dirname, "..", "..", "chandump", name),
function (e3) { function (err) {
if (err && e3) { if (err && err.code !== "ENOENT") {
err += "\n" + e3; Logger.errlog.log("Deleting chandump failed:");
} else if (e3) { Logger.errlog.log(err);
err = e3;
} }
fs.unlink(path.join(__dirname, "..", "..", "chandump", name),
function (err) {
if (err && err.code !== "ENOENT") {
Logger.errlog.log("Deleting chandump failed:");
Logger.errlog.log(err);
}
});
callback(err, !Boolean(err));
}); });
callback(err, !Boolean(err));
}); });
}); });
}, },
@ -304,8 +294,8 @@ module.exports = {
return; return;
} }
db.query("SELECT name,rank FROM `chan_" + chan + "_ranks` WHERE name=?", db.query("SELECT * FROM `channel_ranks` WHERE name=? AND channel=?",
[name], [name, chan],
function (err, rows) { function (err, rows) {
if (err) { if (err) {
callback(err, -1); callback(err, -1);
@ -335,8 +325,12 @@ module.exports = {
} }
var replace = "(" + names.map(function () { return "?"; }).join(",") + ")"; var replace = "(" + names.map(function () { return "?"; }).join(",") + ")";
db.query("SELECT name,rank FROM `chan_" + chan + "_ranks` WHERE name IN " +
replace, names, /* Last substitution is the channel to select ranks for */
names.push(chan);
db.query("SELECT * FROM `channel_ranks` WHERE name IN " +
replace + " AND channel=?", names,
function (err, rows) { function (err, rows) {
if (err) { if (err) {
callback(err, []); callback(err, []);
@ -360,7 +354,7 @@ module.exports = {
return; return;
} }
db.query("SELECT name,rank FROM `chan_" + chan + "_ranks` WHERE 1", callback); db.query("SELECT * FROM `channel_ranks` WHERE channel=?", [chan], callback);
}, },
/** /**
@ -381,25 +375,9 @@ module.exports = {
return; return;
} }
db.query("INSERT INTO `chan_" + chan + "_ranks` (name, rank) VALUES (?, ?) " + db.query("INSERT INTO `channel_ranks` VALUES (?, ?, ?) " +
"ON DUPLICATE KEY UPDATE rank=?", [name, rank, rank], callback); "ON DUPLICATE KEY UPDATE rank=?",
}, [name, rank, chan, rank, chan], callback);
/**
* Inserts a new user rank entry without clobbering an existing one
*/
newRank: function (chan, name, rank, callback) {
if (typeof callback !== "function") {
callback = blackHole;
}
if (!valid(chan)) {
callback("Invalid channel name", null);
return;
}
db.query("INSERT INTO `chan_" + chan + "_ranks` (name, rank) VALUES (?, ?) " +
"ON DUPLICATE KEY UPDATE rank=rank", [name, rank], callback);
}, },
/** /**
@ -415,7 +393,8 @@ module.exports = {
return; return;
} }
db.query("DELETE FROM `chan_" + chan + "_ranks` WHERE name=?", [name], callback); db.query("DELETE FROM `channel_ranks` WHERE name=? AND channel=?", [name, chan],
callback);
}, },
/** /**

View File

@ -83,17 +83,26 @@ const TBL_LIBRARIES = "" +
"PRIMARY KEY(`id`, `channel`), INDEX(`channel`, `title`)" + "PRIMARY KEY(`id`, `channel`), INDEX(`channel`, `title`)" +
") CHARACTER SET utf8"; ") CHARACTER SET utf8";
const TBL_RANKS = "" +
"CREATE TABLE IF NOT EXISTS `channel_ranks` (" +
"`name` VARCHAR(20) NOT NULL," +
"`rank` INT NOT NULL," +
"`channel` VARCHAR(30) NOT NULL," +
"PRIMARY KEY(`name`, `channel`)" +
") CHARACTER SET utf8";
module.exports.init = function (queryfn, cb) { module.exports.init = function (queryfn, cb) {
var tables = { var tables = {
users: TBL_USERS, users: TBL_USERS,
channels: TBL_CHANNELS, channels: TBL_CHANNELS,
channel_libraries: TBL_LIBRARIES,
channel_ranks: TBL_RANKS,
global_bans: TBL_GLOBAL_BANS, global_bans: TBL_GLOBAL_BANS,
password_reset: TBL_PASSWORD_RESET, password_reset: TBL_PASSWORD_RESET,
user_playlists: TBL_USER_PLAYLISTS, user_playlists: TBL_USER_PLAYLISTS,
aliases: TBL_ALIASES, aliases: TBL_ALIASES,
stats: TBL_STATS, stats: TBL_STATS,
meta: TBL_META, meta: TBL_META
channel_libraries: TBL_LIBRARIES
}; };
var AsyncQueue = require("../asyncqueue"); var AsyncQueue = require("../asyncqueue");
@ -118,14 +127,6 @@ module.exports.init = function (queryfn, cb) {
}; };
module.exports.createChannelTables = function (name, queryfn, cb) { module.exports.createChannelTables = function (name, queryfn, cb) {
var createRanksTable = function () {
queryfn("CREATE TABLE `chan_" + name + "_ranks` (" +
"`name` VARCHAR(20) NOT NULL," +
"`rank` INT NOT NULL," +
"PRIMARY KEY (`name`)) " +
"CHARACTER SET utf8", createBansTable);
};
var createBansTable = function (err) { var createBansTable = function (err) {
if (err) { if (err) {
cb(err); cb(err);
@ -141,5 +142,5 @@ module.exports.createChannelTables = function (name, queryfn, cb) {
"CHARACTER SET utf8", cb); "CHARACTER SET utf8", cb);
}; };
createRanksTable(); createBansTable();
}; };

View File

@ -41,7 +41,10 @@ function update(version, cb) {
if (version < 3 && hasUpdates.indexOf(2) < 0) { if (version < 3 && hasUpdates.indexOf(2) < 0) {
addMetaColumnToLibraries(cb); addMetaColumnToLibraries(cb);
} else if (version < 4) { } else if (version < 4) {
mergeLibraries(cb); Q.all([
Q.fcall(mergeChannelLibraries),
Q.fcall(mergeChannelRanks)
]).done(cb)
} }
} }
@ -71,13 +74,13 @@ function addMetaColumnToLibraries(cb) {
}).done(cb); }).done(cb);
} }
function mergeLibraries(cb) { function mergeChannelLibraries(cb) {
Q.nfcall(db.query, "SHOW TABLES") Q.nfcall(db.query, "SHOW TABLES")
.then(function (rows) { .then(function (rows) {
rows = rows.map(function (r) { rows = rows.map(function (r) {
return r[Object.keys(r)[0]]; return r[Object.keys(r)[0]];
}).filter(function (r) { }).filter(function (r) {
return r.match(/_library$/); return r.match(/chan_(.*)?_library$/);
}); });
var queue = []; var queue = [];
@ -91,6 +94,9 @@ function mergeLibraries(cb) {
}).catch(function (err) { }).catch(function (err) {
Logger.errlog.log("Copying " + table + " to channel_libraries failed: " + Logger.errlog.log("Copying " + table + " to channel_libraries failed: " +
err); err);
if (err.stack) {
Logger.errlog.log(err.stack);
}
}) })
); );
}); });
@ -98,5 +104,44 @@ function mergeLibraries(cb) {
return Q.all(queue); return Q.all(queue);
}).catch(function (err) { }).catch(function (err) {
Logger.errlog.log("Copying libraries to channel_libraries failed: " + err); Logger.errlog.log("Copying libraries to channel_libraries failed: " + err);
if (err.stack) {
Logger.errlog.log(err.stack);
}
}).done(cb);
}
function mergeChannelRanks(cb) {
Q.nfcall(db.query, "SHOW TABLES")
.then(function (rows) {
rows = rows.map(function (r) {
return r[Object.keys(r)[0]];
}).filter(function (r) {
return r.match(/chan_(.*?)_ranks$/);
});
var queue = [];
rows.forEach(function (table) {
var name = table.match(/chan_(.*?)_ranks$/)[1];
queue.push(Q.nfcall(db.query,
"INSERT INTO `channel_ranks` SELECT name, rank, ?" +
" AS channel FROM `" + table + "`", [name])
.then(function () {
Logger.syslog.log("Copied " + table + " to channel_ranks");
}).catch(function (err) {
Logger.errlog.log("Copying " + table + " to channel_ranks failed: " +
err);
if (err.stack) {
Logger.errlog.log(err.stack);
}
})
);
});
return Q.all(queue);
}).catch(function (err) {
Logger.errlog.log("Copying ranks to channel_ranks failed: " + err);
if (err.stack) {
Logger.errlog.log(err.stack);
}
}).done(cb); }).done(cb);
} }