Add owner field to channel table

Make sure you run the update script to update existing databases:
`node update.js`
This commit is contained in:
calzoneman 2013-06-27 16:41:59 -04:00
parent e030a2bfa6
commit d9fc17e0f3
3 changed files with 49 additions and 7 deletions

View file

@ -293,7 +293,7 @@ Channel.prototype.tryRegister = function(user) {
});
}
else {
if(Database.registerChannel(this.name)) {
if(Database.registerChannel(this.name, user.name)) {
ActionLog.record(user.ip, user.name, "channel-register-success", [this.name]);
this.registered = true;
this.initialized = true;

View file

@ -105,6 +105,7 @@ function init() {
var query = ["CREATE TABLE IF NOT EXISTS `channels` (",
"`id` INT NOT NULL AUTO_INCREMENT,",
"`name` VARCHAR(255) NOT NULL,",
"`owner` VARCHAR(20) NOT NULL,",
"PRIMARY KEY(`id`))",
"ENGINE = MyISAM;"].join("");
var results = db.querySync(query);
@ -249,7 +250,7 @@ function globalUnbanIP(ip) {
/* REGION Channel Registration/Loading */
function registerChannel(name) {
function registerChannel(name, owner) {
if(!name.match(/^[a-zA-Z0-9-_]+$/)) {
return false;
}
@ -305,8 +306,8 @@ function registerChannel(name) {
// Insert into channel table
query = createQuery(
"INSERT INTO `channels` VALUES (NULL, ?)",
[name]
"INSERT INTO `channels` VALUES (NULL, ?, ?)",
[name, owner]
);
results = db.querySync(query);

View file

@ -1,21 +1,62 @@
var Config = require("./config.js");
var Database = require("./database.js");
Config.DEBUG = true;
//Config.DEBUG = true;
Database.setup(Config);
Database.init();
var query;
var db = Database.getConnection();
// Check for already existing
query = "SELECT email FROM registrations WHERE 1";
query = "SELECT owner FROM channels WHERE 1";
if(!db.querySync(query)) {
query = "ALTER TABLE registrations ADD email VARCHAR(255) NOT NULL";
query = "ALTER TABLE channels ADD owner VARCHAR(20) NOT NULL";
var res = db.querySync(query);
if(!res) {
console.log(db);
console.log("Update failed!");
}
else {
populateChannelOwners();
}
}
db.closeSync();
process.exit(0);
function populateChannelOwners() {
query = "SELECT * FROM channels WHERE 1";
var res = db.querySync(query);
if(!res) {
console.log(db);
console.log("Update failed!");
return;
}
var channels = res.fetchAllSync();
channels.forEach(function(chan) {
chan = chan.name;
console.log("Fixing " + chan);
query = "SELECT name FROM `chan_"+chan+"_ranks` WHERE rank>=10 ORDER BY rank";
res = db.querySync(query);
if(!res) {
console.log(db);
console.log("Update failed!");
return;
}
var results = res.fetchAllSync();
if(results.length == 0) {
console.log("bad channel: " + chan);
return;
}
var owner = results[0].name;
query = "UPDATE channels SET owner='"+owner+"' WHERE name='"+chan+"'";
console.log("setting owner=" + owner + " for /r/" + chan);
res = db.querySync(query);
if(!res) {
console.log(db);
console.log("Update failed!");
return;
}
});
}