From d9fc17e0f380c118461b5051b330e7ab95b05363 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Thu, 27 Jun 2013 16:41:59 -0400 Subject: [PATCH] Add owner field to channel table Make sure you run the update script to update existing databases: `node update.js` --- channel.js | 2 +- database.js | 7 ++++--- update.js | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/channel.js b/channel.js index 471c73b7..2fee75d7 100644 --- a/channel.js +++ b/channel.js @@ -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; diff --git a/database.js b/database.js index 6855b1e9..30a42530 100644 --- a/database.js +++ b/database.js @@ -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); diff --git a/update.js b/update.js index b1a8435b..1c9e93ce 100644 --- a/update.js +++ b/update.js @@ -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; + } + }); +}