oops, stop sending unnecessary inserts/updates

got the types wrong on some stuff, and got pet state sorting wrong!
This commit is contained in:
Emi Matchu 2020-09-19 04:39:08 -07:00
parent 41e70ba8d0
commit 96a126ebba
4 changed files with 183 additions and 85 deletions

View file

@ -1,6 +1,6 @@
const mysql = require("mysql2"); const mysql = require("mysql2");
let globalDb; let globalDbs = new Map();
async function connectToDb({ async function connectToDb({
host = "impress.openneo.net", host = "impress.openneo.net",
@ -8,11 +8,11 @@ async function connectToDb({
password = process.env["IMPRESS_MYSQL_PASSWORD"], password = process.env["IMPRESS_MYSQL_PASSWORD"],
database = "openneo_impress", database = "openneo_impress",
} = {}) { } = {}) {
if (globalDb) { if (globalDbs.has(host)) {
return globalDb; return globalDbs.get(host);
} }
globalDb = mysql const db = mysql
.createConnection({ .createConnection({
host, host,
user, user,
@ -24,7 +24,9 @@ async function connectToDb({
// for compatibility with Honeycomb's automatic tracing. // for compatibility with Honeycomb's automatic tracing.
.promise(); .promise();
return globalDb; globalDbs.set(host, db);
return db;
} }
module.exports = connectToDb; module.exports = connectToDb;

View file

@ -39,6 +39,79 @@ describe("Pet", () => {
expect(res.data).toMatchSnapshot(); expect(res.data).toMatchSnapshot();
expect(getDbCalls()).toMatchInlineSnapshot(` expect(getDbCalls()).toMatchInlineSnapshot(`
Array [ Array [
Array [
"SELECT * FROM pet_types WHERE (species_id = ? AND color_id = ?)",
Array [
"54",
"75",
],
],
Array [
"SELECT * FROM items WHERE id IN (?,?,?,?,?,?,?,?)",
Array [
"37229",
"37375",
"38911",
"38912",
"38913",
"43014",
"43397",
"48313",
],
],
Array [
"SELECT * FROM item_translations WHERE item_id IN (?,?,?,?,?,?,?,?) AND locale = \\"en\\"",
Array [
"37229",
"37375",
"38911",
"38912",
"38913",
"43014",
"43397",
"48313",
],
],
Array [
"SELECT * FROM swf_assets WHERE (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?)",
Array [
"object",
"6829",
"object",
"14855",
"object",
"14856",
"object",
"14857",
"object",
"36414",
"object",
"39646",
"object",
"51959",
"object",
"56478",
"biology",
"7942",
"biology",
"7941",
"biology",
"24008",
"biology",
"21060",
"biology",
"21057",
"biology",
"7946",
],
],
Array [
"SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)",
Array [
"2",
"7941,7942,7946,21057,21060,24008",
],
],
Array [ Array [
"SELECT * FROM species_translations "SELECT * FROM species_translations
WHERE species_id IN (?) AND locale = \\"en\\"", WHERE species_id IN (?) AND locale = \\"en\\"",
@ -127,6 +200,29 @@ describe("Pet", () => {
expect(res2).toHaveNoErrors(); expect(res2).toHaveNoErrors();
expect(res2.data).toMatchSnapshot(); expect(res2.data).toMatchSnapshot();
expect(getDbCalls()).toMatchSnapshot(); expect(getDbCalls()).toMatchSnapshot();
clearDbCalls();
// If we load the pet again, it should only make SELECT queries, not
// INSERT or UPDATE.
await query({
query: gql`
query {
petOnNeopetsDotCom(petName: "roopal27") {
items {
id
}
}
}
`,
});
const dbCalls = getDbCalls();
for (const [query, _] of dbCalls) {
expect(query).toMatch(/SELECT/);
expect(query).not.toMatch(/INSERT/);
expect(query).not.toMatch(/UPDATE/);
}
}); });
it("models updated item data", async () => { it("models updated item data", async () => {

View file

@ -381,103 +381,103 @@ Array [
Array [ Array [
"INSERT INTO swf_assets (body_id, created_at, remote_id, type, url, zone_id, zones_restrict) VALUES (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?);", "INSERT INTO swf_assets (body_id, created_at, remote_id, type, url, zone_id, zones_restrict) VALUES (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?);",
Array [ Array [
180, "180",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"6829", "6829",
"object", "object",
"http://images.neopets.com/cp/items/swf/000/000/006/6829_1707e50385.swf", "http://images.neopets.com/cp/items/swf/000/000/006/6829_1707e50385.swf",
3, "3",
"", "",
180, "180",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"14855", "14855",
"object", "object",
"http://images.neopets.com/cp/items/swf/000/000/014/14855_215f367070.swf", "http://images.neopets.com/cp/items/swf/000/000/014/14855_215f367070.swf",
25, "25",
"", "",
180, "180",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"14856", "14856",
"object", "object",
"http://images.neopets.com/cp/items/swf/000/000/014/14856_46c1b32797.swf", "http://images.neopets.com/cp/items/swf/000/000/014/14856_46c1b32797.swf",
26, "26",
"", "",
180, "180",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"14857", "14857",
"object", "object",
"http://images.neopets.com/cp/items/swf/000/000/014/14857_d43380ef66.swf", "http://images.neopets.com/cp/items/swf/000/000/014/14857_d43380ef66.swf",
40, "40",
"", "",
180, "180",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"36414", "36414",
"object", "object",
"http://images.neopets.com/cp/items/swf/000/000/036/36414_1e2aaab4ad.swf", "http://images.neopets.com/cp/items/swf/000/000/036/36414_1e2aaab4ad.swf",
48, "48",
"", "",
180, "180",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"39646", "39646",
"object", "object",
"http://images.neopets.com/cp/items/swf/000/000/039/39646_e129e22ada.swf", "http://images.neopets.com/cp/items/swf/000/000/039/39646_e129e22ada.swf",
42, "42",
"", "",
180, "180",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"51959", "51959",
"object", "object",
"http://images.neopets.com/cp/items/swf/000/000/051/51959_4439727c48.swf", "http://images.neopets.com/cp/items/swf/000/000/051/51959_4439727c48.swf",
45, "45",
"", "",
180, "180",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"56478", "56478",
"object", "object",
"http://images.neopets.com/cp/items/swf/000/000/056/56478_eabc28e7c7.swf", "http://images.neopets.com/cp/items/swf/000/000/056/56478_eabc28e7c7.swf",
27, "27",
"", "",
0, "0",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"7942", "7942",
"biology", "biology",
"http://images.neopets.com/cp/bio/swf/000/000/007/7942_2eab06fd7b.swf", "http://images.neopets.com/cp/bio/swf/000/000/007/7942_2eab06fd7b.swf",
5, "5",
"0000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000",
0, "0",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"7941", "7941",
"biology", "biology",
"http://images.neopets.com/cp/bio/swf/000/000/007/7941_2c4cc4b846.swf", "http://images.neopets.com/cp/bio/swf/000/000/007/7941_2c4cc4b846.swf",
15, "15",
"0000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000",
0, "0",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"24008", "24008",
"biology", "biology",
"http://images.neopets.com/cp/bio/swf/000/000/024/24008_a05fe9876a.swf", "http://images.neopets.com/cp/bio/swf/000/000/024/24008_a05fe9876a.swf",
30, "30",
"0000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000",
0, "0",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"21060", "21060",
"biology", "biology",
"http://images.neopets.com/cp/bio/swf/000/000/021/21060_d77ba93b7b.swf", "http://images.neopets.com/cp/bio/swf/000/000/021/21060_d77ba93b7b.swf",
33, "33",
"0000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000",
0, "0",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"21057", "21057",
"biology", "biology",
"http://images.neopets.com/cp/bio/swf/000/000/021/21057_4550efbb2f.swf", "http://images.neopets.com/cp/bio/swf/000/000/021/21057_4550efbb2f.swf",
34, "34",
"0000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000",
0, "0",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"7946", "7946",
"biology", "biology",
"http://images.neopets.com/cp/bio/swf/000/000/007/7946_0348dad587.swf", "http://images.neopets.com/cp/bio/swf/000/000/007/7946_0348dad587.swf",
37, "37",
"0000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000",
], ],
], ],
@ -492,25 +492,25 @@ Array [
"SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)", "SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)",
Array [ Array [
"1", "1",
"21057,21060,24008,7941,7942,7946", "7941,7942,7946,21057,21060,24008",
], ],
], ],
Array [ Array [
"INSERT INTO pet_states (female, labeled, mood_id, pet_type_id, swf_asset_ids, unconverted) VALUES (?, ?, ?, ?, ?, ?);", "INSERT INTO pet_states (female, labeled, mood_id, pet_type_id, swf_asset_ids, unconverted) VALUES (?, ?, ?, ?, ?, ?);",
Array [ Array [
false, 0,
true, 1,
2, "2",
"1", "1",
"21057,21060,24008,7941,7942,7946", "7941,7942,7946,21057,21060,24008",
false, 0,
], ],
], ],
Array [ Array [
"SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)", "SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)",
Array [ Array [
"1", "1",
"21057,21060,24008,7941,7942,7946", "7941,7942,7946,21057,21060,24008",
], ],
], ],
Array [ Array [
@ -934,103 +934,103 @@ Array [
Array [ Array [
"INSERT INTO swf_assets (body_id, created_at, remote_id, type, url, zone_id, zones_restrict) VALUES (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?);", "INSERT INTO swf_assets (body_id, created_at, remote_id, type, url, zone_id, zones_restrict) VALUES (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?);",
Array [ Array [
180, "180",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"6829", "6829",
"object", "object",
"http://images.neopets.com/cp/items/swf/000/000/006/6829_1707e50385.swf", "http://images.neopets.com/cp/items/swf/000/000/006/6829_1707e50385.swf",
3, "3",
"", "",
180, "180",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"14855", "14855",
"object", "object",
"http://images.neopets.com/cp/items/swf/000/000/014/14855_215f367070.swf", "http://images.neopets.com/cp/items/swf/000/000/014/14855_215f367070.swf",
25, "25",
"", "",
180, "180",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"14856", "14856",
"object", "object",
"http://images.neopets.com/cp/items/swf/000/000/014/14856_46c1b32797.swf", "http://images.neopets.com/cp/items/swf/000/000/014/14856_46c1b32797.swf",
26, "26",
"", "",
180, "180",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"14857", "14857",
"object", "object",
"http://images.neopets.com/cp/items/swf/000/000/014/14857_d43380ef66.swf", "http://images.neopets.com/cp/items/swf/000/000/014/14857_d43380ef66.swf",
40, "40",
"", "",
180, "180",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"36414", "36414",
"object", "object",
"http://images.neopets.com/cp/items/swf/000/000/036/36414_1e2aaab4ad.swf", "http://images.neopets.com/cp/items/swf/000/000/036/36414_1e2aaab4ad.swf",
48, "48",
"", "",
180, "180",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"39646", "39646",
"object", "object",
"http://images.neopets.com/cp/items/swf/000/000/039/39646_e129e22ada.swf", "http://images.neopets.com/cp/items/swf/000/000/039/39646_e129e22ada.swf",
42, "42",
"", "",
180, "180",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"51959", "51959",
"object", "object",
"http://images.neopets.com/cp/items/swf/000/000/051/51959_4439727c48.swf", "http://images.neopets.com/cp/items/swf/000/000/051/51959_4439727c48.swf",
45, "45",
"", "",
180, "180",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"56478", "56478",
"object", "object",
"http://images.neopets.com/cp/items/swf/000/000/056/56478_eabc28e7c7.swf", "http://images.neopets.com/cp/items/swf/000/000/056/56478_eabc28e7c7.swf",
27, "27",
"", "",
0, "0",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"7942", "7942",
"biology", "biology",
"http://images.neopets.com/cp/bio/swf/000/000/007/7942_2eab06fd7b.swf", "http://images.neopets.com/cp/bio/swf/000/000/007/7942_2eab06fd7b.swf",
5, "5",
"0000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000",
0, "0",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"7941", "7941",
"biology", "biology",
"http://images.neopets.com/cp/bio/swf/000/000/007/7941_2c4cc4b846.swf", "http://images.neopets.com/cp/bio/swf/000/000/007/7941_2c4cc4b846.swf",
15, "15",
"0000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000",
0, "0",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"24008", "24008",
"biology", "biology",
"http://images.neopets.com/cp/bio/swf/000/000/024/24008_a05fe9876a.swf", "http://images.neopets.com/cp/bio/swf/000/000/024/24008_a05fe9876a.swf",
30, "30",
"0000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000",
0, "0",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"21060", "21060",
"biology", "biology",
"http://images.neopets.com/cp/bio/swf/000/000/021/21060_d77ba93b7b.swf", "http://images.neopets.com/cp/bio/swf/000/000/021/21060_d77ba93b7b.swf",
33, "33",
"0000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000",
0, "0",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"21057", "21057",
"biology", "biology",
"http://images.neopets.com/cp/bio/swf/000/000/021/21057_4550efbb2f.swf", "http://images.neopets.com/cp/bio/swf/000/000/021/21057_4550efbb2f.swf",
34, "34",
"0000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000",
0, "0",
2020-01-01T00:00:00.000Z, 2020-01-01T00:00:00.000Z,
"7946", "7946",
"biology", "biology",
"http://images.neopets.com/cp/bio/swf/000/000/007/7946_0348dad587.swf", "http://images.neopets.com/cp/bio/swf/000/000/007/7946_0348dad587.swf",
37, "37",
"0000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000",
], ],
], ],
@ -1064,25 +1064,25 @@ Array [
"SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)", "SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)",
Array [ Array [
"1", "1",
"21057,21060,24008,7941,7942,7946", "7941,7942,7946,21057,21060,24008",
], ],
], ],
Array [ Array [
"INSERT INTO pet_states (female, labeled, mood_id, pet_type_id, swf_asset_ids, unconverted) VALUES (?, ?, ?, ?, ?, ?);", "INSERT INTO pet_states (female, labeled, mood_id, pet_type_id, swf_asset_ids, unconverted) VALUES (?, ?, ?, ?, ?, ?);",
Array [ Array [
false, 0,
true, 1,
2, "2",
"1", "1",
"21057,21060,24008,7941,7942,7946", "7941,7942,7946,21057,21060,24008",
false, 0,
], ],
], ],
Array [ Array [
"SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)", "SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)",
Array [ Array [
"1", "1",
"21057,21060,24008,7941,7942,7946", "7941,7942,7946,21057,21060,24008",
], ],
], ],
Array [ Array [

View file

@ -191,12 +191,12 @@ async function saveModelingData(
type: "object", type: "object",
remoteId: String(objectAsset.asset_id), remoteId: String(objectAsset.asset_id),
url: objectAsset.asset_url, url: objectAsset.asset_url,
zoneId: objectAsset.zone_id, zoneId: String(objectAsset.zone_id),
zonesRestrict: "", zonesRestrict: "",
// TODO: This doesn't actually work... sometimes it needs to be 0, yeah? // TODO: This doesn't actually work... sometimes it needs to be 0, yeah?
// So we actually have to do asset writing after we load the current // So we actually have to do asset writing after we load the current
// row and compare... maybe a cutesy fn syntax here? // row and compare... maybe a cutesy fn syntax here?
bodyId: customPet.body_id, bodyId: String(customPet.body_id),
})); }));
const biologyAssets = Object.values(customPet.biology_by_zone); const biologyAssets = Object.values(customPet.biology_by_zone);
@ -204,9 +204,9 @@ async function saveModelingData(
type: "biology", type: "biology",
remoteId: String(biologyAsset.part_id), remoteId: String(biologyAsset.part_id),
url: biologyAsset.asset_url, url: biologyAsset.asset_url,
zoneId: biologyAsset.zone_id, zoneId: String(biologyAsset.zone_id),
zonesRestrict: biologyAsset.zones_restrict, zonesRestrict: biologyAsset.zones_restrict,
bodyId: 0, bodyId: "0",
})); }));
const incomingSwfAssets = [...incomingItemSwfAssets, ...incomingPetSwfAssets]; const incomingSwfAssets = [...incomingItemSwfAssets, ...incomingPetSwfAssets];
@ -277,13 +277,13 @@ async function saveModelingData(
const incomingPetState = { const incomingPetState = {
petTypeId: petType.id, petTypeId: petType.id,
swfAssetIds: incomingPetSwfAssets swfAssetIds: incomingPetSwfAssets
.map((a) => a.remoteId) .map((row) => row.remoteId)
.sort() .sort((a, b) => Number(a) - Number(b))
.join(","), .join(","),
female: petMetaData.gender === 2, // sorry for this column name :/ female: petMetaData.gender === 2 ? 1 : 0, // sorry for this column name :/
moodId: petMetaData.mood, moodId: String(petMetaData.mood),
unconverted: incomingPetSwfAssets.length === 1, unconverted: incomingPetSwfAssets.length === 1 ? 1 : 0,
labeled: true, labeled: 1,
}; };
await syncToDb(db, [incomingPetState], { await syncToDb(db, [incomingPetState], {