Merge channel libraries into a single table

This commit is contained in:
Calvin Montgomery 2014-06-23 21:09:18 -07:00
parent 523bdfe065
commit 0abaaba690
3 changed files with 64 additions and 42 deletions

View File

@ -32,7 +32,6 @@ function initTables(name, owner, callback) {
if (err) {
dropTable("chan_" + name + "_ranks");
dropTable("chan_" + name + "_ranks");
dropTable("chan_" + name + "_library");
callback(err, null);
return;
}
@ -212,31 +211,23 @@ module.exports = {
err = e2;
}
dropTable("chan_" + name + "_library", function (e3) {
db.query("DELETE FROM `channels` WHERE name=?", [name],
function (e3) {
if (err && e3) {
err += "\n" + e3;
} else if (e3) {
err = e3;
}
db.query("DELETE FROM `channels` WHERE name=?", [name],
function (e4) {
if (err && e4) {
err += "\n" + e4;
} else if (e4) {
err = e4;
fs.unlink(path.join(__dirname, "..", "..", "chandump", name),
function (err) {
if (err && err.code !== "ENOENT") {
Logger.errlog.log("Deleting chandump failed:");
Logger.errlog.log(err);
}
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));
});
});
});
@ -445,9 +436,10 @@ module.exports = {
codec: media.meta.codec
});
db.query("INSERT INTO `chan_" + chan + "_library` (id, title, seconds, type, meta) " +
"VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE id=id",
[media.id, media.title, media.seconds, media.type, meta], callback);
db.query("INSERT INTO `channel_libraries` " +
"(id, title, seconds, type, meta, channel) " +
"VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE id=id",
[media.id, media.title, media.seconds, media.type, meta, chan], callback);
},
/**
@ -463,7 +455,7 @@ module.exports = {
return;
}
db.query("SELECT * FROM `chan_" + chan + "_library` WHERE id=?", [id],
db.query("SELECT * FROM `channel_libraries` WHERE id=? AND channel=?", [id, chan],
function (err, rows) {
if (err) {
callback(err, null);
@ -486,8 +478,8 @@ module.exports = {
return;
}
db.query("SELECT * FROM `chan_" + chan + "_library` WHERE title LIKE ?",
["%" + search + "%"], callback);
db.query("SELECT * FROM `channel_libraries` WHERE title LIKE ? AND channel=?",
["%" + search + "%", chan], callback);
},
/**
@ -503,7 +495,8 @@ module.exports = {
return;
}
db.query("DELETE FROM `chan_" + chan + "_library` WHERE id=?", [id], callback);
db.query("DELETE FROM `channel_libraries` WHERE id=? AND channel=?",
[id, chan], callback);
},
/**

View File

@ -72,6 +72,17 @@ const TBL_META = "" +
"PRIMARY KEY (`key`))" +
"CHARACTER SET utf8";
const TBL_LIBRARIES = "" +
"CREATE TABLE IF NOT EXISTS `channel_libraries` (" +
"`id` VARCHAR(255) NOT NULL," +
"`title` VARCHAR(255) NOT NULL," +
"`seconds` INT NOT NULL," +
"`type` VARCHAR(2) NOT NULL," +
"`meta` TEXT NOT NULL," +
"`channel` VARCHAR(30) NOT NULL," +
"PRIMARY KEY(`id`, `channel`), INDEX(`channel`, `title`)" +
") CHARACTER SET utf8";
module.exports.init = function (queryfn, cb) {
var tables = {
users: TBL_USERS,
@ -81,7 +92,8 @@ module.exports.init = function (queryfn, cb) {
user_playlists: TBL_USER_PLAYLISTS,
aliases: TBL_ALIASES,
stats: TBL_STATS,
meta: TBL_META
meta: TBL_META,
channel_libraries: TBL_LIBRARIES
};
var AsyncQueue = require("../asyncqueue");
@ -111,21 +123,6 @@ module.exports.createChannelTables = function (name, queryfn, cb) {
"`name` VARCHAR(20) NOT NULL," +
"`rank` INT NOT NULL," +
"PRIMARY KEY (`name`)) " +
"CHARACTER SET utf8", createLibraryTable);
};
var createLibraryTable = function (err) {
if (err) {
cb(err);
return;
}
queryfn("CREATE TABLE `chan_" + name + "_library` (" +
"`id` VARCHAR(255) NOT NULL," +
"`title` VARCHAR(255) NOT NULL," +
"`seconds` INT NOT NULL," +
"`type` VARCHAR(2) NOT NULL," +
"`meta` TEXT NOT NULL," +
"PRIMARY KEY (`id`))" +
"CHARACTER SET utf8", createBansTable);
};

View File

@ -2,7 +2,7 @@ var db = require("../database");
var Logger = require("../logger");
var Q = require("q");
const DB_VERSION = 3;
const DB_VERSION = 4;
var hasUpdates = [];
module.exports.checkVersion = function () {
@ -40,6 +40,8 @@ module.exports.checkVersion = function () {
function update(version, cb) {
if (version < 3 && hasUpdates.indexOf(2) < 0) {
addMetaColumnToLibraries(cb);
} else if (version < 4) {
mergeLibraries(cb);
}
}
@ -68,3 +70,33 @@ function addMetaColumnToLibraries(cb) {
Logger.errlog.log("Adding meta column to library tables failed: " + err);
}).done(cb);
}
function mergeLibraries(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(/_library$/);
});
var queue = [];
rows.forEach(function (table) {
var name = table.match(/chan_(.*?)_library$/)[1];
queue.push(Q.nfcall(db.query,
"INSERT INTO `channel_libraries` SELECT id, title, seconds, type, meta, ?" +
" AS channel FROM `" + table + "`", [name])
.then(function () {
Logger.syslog.log("Copied " + table + " to channel_libraries");
}).catch(function (err) {
Logger.errlog.log("Copying " + table + " to channel_libraries failed: " +
err);
})
);
});
return Q.all(queue);
}).catch(function (err) {
Logger.errlog.log("Copying libraries to channel_libraries failed: " + err);
}).done(cb);
}