Fix UTF8 issues - run 'node update.js 2013-08-20-utf8fix' to correct your database

This commit is contained in:
calzoneman 2013-08-20 19:50:49 -05:00
parent e830ba2d41
commit ffddc434f2
2 changed files with 129 additions and 41 deletions

View file

@ -20,7 +20,8 @@ var Database = function (cfg) {
Logger.errlog.log("! DB connection failed");
return;
}
conn.end();
});
this.global_ipbans = {};
@ -62,9 +63,7 @@ Database.prototype.query = function (query, sub, callback) {
}
}
});
}
function blackHole() {
} function blackHole() {
}
@ -77,7 +76,8 @@ Database.prototype.init = function () {
"`name` VARCHAR(255) NOT NULL,",
"`owner` VARCHAR(20) NOT NULL,",
"PRIMARY KEY(`id`))",
"ENGINE = MyISAM;"].join("");
"ENGINE = MyISAM ",
"CHARACTER SET utf8;"].join("");
self.query(query, function (err, res) {
if(err) {
@ -97,7 +97,8 @@ Database.prototype.init = function () {
"`profile_text` TEXT NOT NULL,",
"`email` VARCHAR(255) NOT NULL,",
"PRIMARY KEY (`id`))",
"ENGINE = MyISAM;"].join("");
"ENGINE = MyISAM ",
"CHARACTER SET utf8;"].join("");
self.query(query, function (err, res) {
if(err) {
@ -110,7 +111,8 @@ Database.prototype.init = function () {
"`ip` VARCHAR(15) NOT NULL,",
"`note` VARCHAR(255) NOT NULL,",
"PRIMARY KEY (`ip`))",
"ENGINE = MyISAM;"].join("");
"ENGINE = MyISAM ",
"CHARACTER SET utf8;"].join("");
self.query(query, function (err, res) {
if(err) {
@ -126,7 +128,8 @@ Database.prototype.init = function () {
"`email` VARCHAR(255) NOT NULL,",
"`expire` BIGINT NOT NULL,",
"PRIMARY KEY (`name`))",
"ENGINE = MyISAM;"].join("");
"ENGINE = MyISAM ",
"CHARACTER SET utf8;"].join("");
self.query(query, function (err, res) {
if(err) {
@ -142,7 +145,8 @@ Database.prototype.init = function () {
"`count` INT NOT NULL,",
"`time` INT NOT NULL,",
"PRIMARY KEY (`user`, `name`))",
"ENGINE = MyISAM;"].join("");
"ENGINE = MyISAM ",
"CHARACTER SET utf8;"].join("");
self.query(query, function (err, res) {
if(err) {
@ -157,7 +161,8 @@ Database.prototype.init = function () {
"`name` VARCHAR(20) NOT NULL,",
"`time` BIGINT NOT NULL,",
"PRIMARY KEY (`visit_id`), INDEX (`ip`))",
"ENGINE = MyISAM;"].join("");
"ENGINE = MyISAM ",
"CHARACTER SET utf8;"].join("");
self.query(query, function (err, res) {
if(err) {
@ -173,7 +178,8 @@ Database.prototype.init = function () {
"`args` TEXT NOT NULL,",
"`time` BIGINT NOT NULL,",
"PRIMARY KEY (`ip`, `time`), INDEX (`action`))",
"ENGINE = MyISAM;"].join("");
"ENGINE = MyISAM ",
"CHARACTER SET utf8;"].join("");
self.query(query, function (err, res) {
if(err) {
@ -188,7 +194,8 @@ Database.prototype.init = function () {
"`chancount` INT NOT NULL,",
"`mem` INT NOT NULL,",
"PRIMARY KEY (`time`))",
"ENGINE = MyISAM;"].join("");
"ENGINE = MyISAM ",
"CHARACTER SET utf8;"].join("");
self.query(query, function (err, res) {
if(err) {
@ -330,7 +337,8 @@ Database.prototype.registerChannel = function (name, owner, callback) {
"`seconds` INT NOT NULL,",
"`type` VARCHAR(2) NOT NULL,",
"PRIMARY KEY (`id`))",
"ENGINE = MyISAM;"].join("");
"ENGINE = MyISAM ",
"CHARACTER SET utf8;"].join("");
self.query(query, function (err, res) {
if(err) {
callback(err, null);
@ -342,7 +350,8 @@ Database.prototype.registerChannel = function (name, owner, callback) {
"`name` VARCHAR(32) NOT NULL,",
"`rank` INT NOT NULL,",
"UNIQUE (`name`))",
"ENGINE = MyISAM;"].join("");
"ENGINE = MyISAM ",
"CHARACTER SET utf8;"].join("");
self.query(query, function (err, res) {
if(err) {
@ -356,7 +365,8 @@ Database.prototype.registerChannel = function (name, owner, callback) {
"`name` VARCHAR(32) NOT NULL,",
"`banner` VARCHAR(32) NOT NULL,",
"PRIMARY KEY (`ip`))",
"ENGINE = MyISAM;"].join("");
"ENGINE = MyISAM ",
"CHARACTER SET utf8;"].join("");
self.query(query, function (err, res) {
if(err) {
@ -558,7 +568,8 @@ Database.prototype.addToLibrary = function (channame, media, callback) {
return;
}
var query = "INSERT INTO `chan_" + channame + "_library` " +
// use INSERT IGNORE to prevent errors from adding duplicates
var query = "INSERT IGNORE INTO `chan_" + channame + "_library` " +
"(id, title, seconds, type) " +
"VALUES (?, ?, ?, ?)";
var params = [

129
update.js
View file

@ -1,35 +1,112 @@
var Config = require("./config.js");
var Database = require("./database.js");
var updates = {
"2013-08-20-utf8fix": fixDBUnicode
};
var x = {};
Config.load(x, "cfg.json", function () {
Database.setup(x.cfg);
Database.init();
var query;
var db = Database.getConnection();
var db = new Database(x.cfg);
query = "SELECT * FROM channels WHERE 1";
var res = db.querySync(query);
if(!res) {
console.log(db);
console.log("Update failed!");
return;
var u = process.argv[2];
if(!(u in updates)) {
console.log("Usage: node update.js <update>");
console.log("Available updates: ");
for(var k in updates) {
console.log(" " + k);
}
process.exit(0);
}
var channels = res.fetchAllSync();
channels.forEach(function(chan) {
chan = chan.name;
query = "UPDATE `chan_" + chan + "_library` SET title=CONCAT(" +
"SUBSTRING(title FROM 0 FOR 97), '...') WHERE " +
"LENGTH(title) > 100";
console.log(query);
res = db.querySync(query);
if(!res) {
console.log(db);
console.log("failed to fix "+chan);
return;
}
});
db.closeSync();
var fn = updates[u];
fn(db);
});
function fixDBUnicode(db) {
db.query("SHOW TABLES", function (err, res) {
if(err) {
console.log(err);
return;
}
var libs = [];
var waiting = res.length;
res.forEach(function (r) {
var k = Object.keys(r)[0];
if(r[k].match(/^chan_[\w-_]{1,30}_library$/)) {
libs.push(r[k]);
waiting--;
return;
} else if(r[k] == "user_playlists") {
waiting--;
return;
}
db.query("ALTER TABLE `" + r[k] + "` CONVERT TO CHARACTER SET utf8", function (err, _) {
waiting--;
if(err)
console.log("FAIL: " + r[k]);
else
console.log("Fixed " + r[k]);
});
});
var s1int = setInterval(function () {
if(waiting == 0) {
clearInterval(s1int);
waiting = libs.length + 1;
libs.forEach(function (lib) {
db.query("ALTER TABLE `"+lib+"` MODIFY title BLOB", function (err, _) {
if(err) {
console.log(err);
waiting--;
return;
}
db.query("ALTER TABLE `"+lib+"` CHARACTER SET utf8", function (err, _) {
if(err) {
console.log(err);
waiting--;
return;
}
db.query("ALTER TABLE `"+lib+"` MODIFY title VARCHAR(255) CHARACTER SET utf8", function (err, _) {
waiting--;
if(err) {
console.log(err);
return;
}
console.log("Fixed user_playlists");
});
});
});
});
db.query("ALTER TABLE user_playlists MODIFY contents MEDIUMBLOB", function (err, _) {
if(err) {
console.log(err);
waiting--;
return;
}
db.query("ALTER TABLE user_playlists CHARACTER SET utf8", function (err, _) {
if(err) {
console.log(err);
waiting--;
return;
}
db.query("ALTER TABLE user_playlists MODIFY contents MEDIUMTEXT CHARACTER SET utf8", function (err, _) {
waiting--;
if(err) {
console.log(err);
return;
}
console.log("Fixed " + lib);
});
});
});
setInterval(function () {
if(waiting == 0) {
console.log("Done");
process.exit(0);
}
}, 1000);
}
}, 1000);
});
}