deps: remove "q" (#731)

Insert Star Trek joke here.

Also did significant refactoring of the surrounding logic for the things
that depended on Q.
This commit is contained in:
Calvin Montgomery 2018-02-24 19:47:50 -08:00 committed by GitHub
parent d5f5c91b05
commit 79556d9365
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 738 additions and 296 deletions

View file

@ -328,10 +328,10 @@ module.exports = {
var replace = "(" + names.map(function () { return "?"; }).join(",") + ")";
/* Last substitution is the channel to select ranks for */
names.push(chan);
const sub = names.concat([chan]);
db.query("SELECT * FROM `channel_ranks` WHERE name IN " +
replace + " AND channel=?", names,
replace + " AND channel=?", sub,
function (err, rows) {
if (err) {
callback(err, []);

View file

@ -1,5 +1,4 @@
var db = require("../database");
var Q = require("q");
import Promise from 'bluebird';
const LOGGER = require('@calzoneman/jsli')('database/update');
@ -41,15 +40,9 @@ module.exports.checkVersion = function () {
};
function update(version, cb) {
if (version < 4) {
if (version < 7) {
LOGGER.error('Cannot auto-upgrade: db_version 4 is too old!');
process.exit(1);
} else if (version < 5) {
fixUtf8mb4(cb);
} else if (version < 6) {
fixCustomEmbeds(cb);
} else if (version < 7) {
fixCustomEmbedsInUserPlaylists(cb);
} else if (version < 8) {
addUsernameDedupeColumn(cb);
} else if (version < 9) {
@ -61,110 +54,6 @@ function update(version, cb) {
}
}
function fixUtf8mb4(cb) {
var queries = [
"ALTER TABLE `users` MODIFY `profile` TEXT CHARACTER SET utf8mb4 NOT NULL",
"ALTER TABLE `global_bans` MODIFY `reason` VARCHAR(255) CHARACTER SET utf8mb4 NOT NULL",
"ALTER TABLE `channel_libraries` MODIFY `title` VARCHAR(255) CHARACTER SET utf8mb4 NOT NULL",
"ALTER TABLE `channel_bans` MODIFY `reason` VARCHAR(255) CHARACTER SET utf8mb4 NOT NULL"
];
Q.allSettled(queries.map(function (query) {
return Q.nfcall(db.query, query);
})).then(function () {
LOGGER.info("Fixed utf8mb4");
cb();
}).catch(function (e) {
LOGGER.error("Failed to fix utf8mb4: " + e);
});
};
function fixCustomEmbeds(cb) {
var CustomEmbedFilter = require("../customembed").filter;
Q.nfcall(db.query, "SELECT * FROM `channel_libraries` WHERE type='cu'")
.then(function (rows) {
var all = [];
rows.forEach(function (row) {
if (row.id.indexOf("cu:") === 0) return;
all.push(Q.nfcall(db.query, "DELETE FROM `channel_libraries` WHERE `id`=? AND `channel`=?",
[row.id, row.channel]));
try {
var media = CustomEmbedFilter(row.id);
all.push(Q.nfcall(db.channels.addToLibrary, row.channel, media));
} catch(e) {
console.error("WARNING: Unable to convert " + row.id);
}
});
Q.allSettled(all).then(function () {
LOGGER.info("Converted custom embeds.");
cb();
});
});
}
function fixCustomEmbedsInUserPlaylists(cb) {
var CustomEmbedFilter = require("../customembed").filter;
Q.nfcall(db.query, "SELECT * FROM `user_playlists` WHERE `contents` LIKE '%\"type\":\"cu\"%'")
.then(function (rows) {
var all = [];
rows.forEach(function (row) {
var data;
try {
data = JSON.parse(row.contents);
} catch (e) {
return;
}
var updated = [];
var item;
while ((item = data.shift()) !== undefined) {
if (item.type !== "cu") {
updated.push(item);
continue;
}
if (/^cu:/.test(item.id)) {
updated.push(item);
continue;
}
var media;
try {
media = CustomEmbedFilter(item.id);
} catch (e) {
LOGGER.info("WARNING: Unable to convert " + item.id);
continue;
}
updated.push({
id: media.id,
title: item.title,
seconds: media.seconds,
type: media.type,
meta: {
embed: media.meta.embed
}
});
all.push(Q.nfcall(db.query, "UPDATE `user_playlists` SET `contents`=?, `count`=? WHERE `user`=? AND `name`=?",
[JSON.stringify(updated), updated.length, row.user, row.name]));
}
});
Q.allSettled(all).then(function () {
LOGGER.info('Fixed custom embeds in user_playlists');
cb();
});
}).catch(function (err) {
LOGGER.error(err.stack);
});
}
function addUsernameDedupeColumn(cb) {
LOGGER.info("Adding name_dedupe column on the users table");
db.query("ALTER TABLE users ADD COLUMN name_dedupe VARCHAR(20) UNIQUE DEFAULT NULL", (error) => {