Add channels.last_loaded column

This commit is contained in:
Calvin Montgomery 2017-03-13 20:55:06 -07:00
parent d62d5e0cab
commit 8f266ccd44
4 changed files with 40 additions and 5 deletions

View File

@ -2,7 +2,7 @@
"author": "Calvin Montgomery", "author": "Calvin Montgomery",
"name": "CyTube", "name": "CyTube",
"description": "Online media synchronizer and chat", "description": "Online media synchronizer and chat",
"version": "3.32.1", "version": "3.33.0",
"repository": { "repository": {
"url": "http://github.com/calzoneman/sync" "url": "http://github.com/calzoneman/sync"
}, },

View File

@ -93,10 +93,10 @@ function Channel(name) {
if (err && err !== "Channel is not registered") { if (err && err !== "Channel is not registered") {
self.emit("loadFail", "Failed to load channel data from the database. Please try again later."); self.emit("loadFail", "Failed to load channel data from the database. Please try again later.");
self.setFlag(Flags.C_ERROR); self.setFlag(Flags.C_ERROR);
return;
} else { } else {
self.initModules(); self.initModules();
self.loadState(); self.loadState();
db.channels.updateLastLoaded(self.id);
} }
}); });
} }

View File

@ -147,8 +147,8 @@ module.exports = {
} }
db.query("INSERT INTO `channels` " + db.query("INSERT INTO `channels` " +
"(`name`, `owner`, `time`) VALUES (?, ?, ?)", "(`name`, `owner`, `time`, `last_loaded`) VALUES (?, ?, ?, ?)",
[name, owner, Date.now()], [name, owner, Date.now(), new Date()],
function (err, res) { function (err, res) {
if (err) { if (err) {
callback(err, null); callback(err, null);
@ -641,5 +641,20 @@ module.exports = {
} }
db.query("DELETE FROM `channel_bans` WHERE channel=?", [chan], callback); db.query("DELETE FROM `channel_bans` WHERE channel=?", [chan], callback);
},
/**
* Updates the `last_loaded` column to be the current timestamp
*/
updateLastLoaded: function updateLastLoaded(channelId) {
if (channelId <= 0) {
return;
}
db.query("UPDATE channels SET last_loaded = ? WHERE id = ?", [new Date(), channelId], error => {
if (error) {
Logger.errlog.log(`Failed to update last_loaded column for channel ID ${channelId}: ${error}`);
}
});
} }
}; };

View File

@ -3,7 +3,7 @@ var Logger = require("../logger");
var Q = require("q"); var Q = require("q");
import Promise from 'bluebird'; import Promise from 'bluebird';
const DB_VERSION = 9; const DB_VERSION = 10;
var hasUpdates = []; var hasUpdates = [];
module.exports.checkVersion = function () { module.exports.checkVersion = function () {
@ -63,6 +63,8 @@ function update(version, cb) {
addUsernameDedupeColumn(cb); addUsernameDedupeColumn(cb);
} else if (version < 9) { } else if (version < 9) {
populateUsernameDedupeColumn(cb); populateUsernameDedupeColumn(cb);
} else if (version < 10) {
addChannelLastLoadedColumn(cb);
} }
} }
@ -388,3 +390,21 @@ function populateUsernameDedupeColumn(cb) {
}) })
}); });
} }
function addChannelLastLoadedColumn(cb) {
db.query("ALTER TABLE channels ADD COLUMN last_loaded TIMESTAMP NOT NULL DEFAULT 0", error => {
if (error) {
Logger.errlog.log(`Failed to add last_loaded column: ${error}`);
return;
}
db.query("ALTER TABLE channels ADD INDEX i_last_loaded (last_loaded)", error => {
if (error) {
Logger.errlog.log(`Failed to add index on last_loaded column: ${error}`);
return;
}
cb();
});
});
}