From 96a126ebbaf4ebef80a26d6357648fe4891f74d6 Mon Sep 17 00:00:00 2001 From: Matchu Date: Sat, 19 Sep 2020 04:39:08 -0700 Subject: [PATCH] oops, stop sending unnecessary inserts/updates got the types wrong on some stuff, and got pet state sorting wrong! --- src/server/db.js | 12 +- src/server/query-tests/Pet.test.js | 96 ++++++++++++ .../__snapshots__/Pet.test.js.snap | 140 +++++++++--------- src/server/types/Outfit.js | 20 +-- 4 files changed, 183 insertions(+), 85 deletions(-) diff --git a/src/server/db.js b/src/server/db.js index a981f877..f06b9d92 100644 --- a/src/server/db.js +++ b/src/server/db.js @@ -1,6 +1,6 @@ const mysql = require("mysql2"); -let globalDb; +let globalDbs = new Map(); async function connectToDb({ host = "impress.openneo.net", @@ -8,11 +8,11 @@ async function connectToDb({ password = process.env["IMPRESS_MYSQL_PASSWORD"], database = "openneo_impress", } = {}) { - if (globalDb) { - return globalDb; + if (globalDbs.has(host)) { + return globalDbs.get(host); } - globalDb = mysql + const db = mysql .createConnection({ host, user, @@ -24,7 +24,9 @@ async function connectToDb({ // for compatibility with Honeycomb's automatic tracing. .promise(); - return globalDb; + globalDbs.set(host, db); + + return db; } module.exports = connectToDb; diff --git a/src/server/query-tests/Pet.test.js b/src/server/query-tests/Pet.test.js index 5f9a51d2..3a5f18fc 100644 --- a/src/server/query-tests/Pet.test.js +++ b/src/server/query-tests/Pet.test.js @@ -39,6 +39,79 @@ describe("Pet", () => { expect(res.data).toMatchSnapshot(); expect(getDbCalls()).toMatchInlineSnapshot(` 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 [ "SELECT * FROM species_translations WHERE species_id IN (?) AND locale = \\"en\\"", @@ -127,6 +200,29 @@ describe("Pet", () => { expect(res2).toHaveNoErrors(); expect(res2.data).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 () => { diff --git a/src/server/query-tests/__snapshots__/Pet.test.js.snap b/src/server/query-tests/__snapshots__/Pet.test.js.snap index bd6831b0..ab56c0d9 100644 --- a/src/server/query-tests/__snapshots__/Pet.test.js.snap +++ b/src/server/query-tests/__snapshots__/Pet.test.js.snap @@ -381,103 +381,103 @@ Array [ Array [ "INSERT INTO swf_assets (body_id, created_at, remote_id, type, url, zone_id, zones_restrict) VALUES (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?);", Array [ - 180, + "180", 2020-01-01T00:00:00.000Z, "6829", "object", "http://images.neopets.com/cp/items/swf/000/000/006/6829_1707e50385.swf", - 3, + "3", "", - 180, + "180", 2020-01-01T00:00:00.000Z, "14855", "object", "http://images.neopets.com/cp/items/swf/000/000/014/14855_215f367070.swf", - 25, + "25", "", - 180, + "180", 2020-01-01T00:00:00.000Z, "14856", "object", "http://images.neopets.com/cp/items/swf/000/000/014/14856_46c1b32797.swf", - 26, + "26", "", - 180, + "180", 2020-01-01T00:00:00.000Z, "14857", "object", "http://images.neopets.com/cp/items/swf/000/000/014/14857_d43380ef66.swf", - 40, + "40", "", - 180, + "180", 2020-01-01T00:00:00.000Z, "36414", "object", "http://images.neopets.com/cp/items/swf/000/000/036/36414_1e2aaab4ad.swf", - 48, + "48", "", - 180, + "180", 2020-01-01T00:00:00.000Z, "39646", "object", "http://images.neopets.com/cp/items/swf/000/000/039/39646_e129e22ada.swf", - 42, + "42", "", - 180, + "180", 2020-01-01T00:00:00.000Z, "51959", "object", "http://images.neopets.com/cp/items/swf/000/000/051/51959_4439727c48.swf", - 45, + "45", "", - 180, + "180", 2020-01-01T00:00:00.000Z, "56478", "object", "http://images.neopets.com/cp/items/swf/000/000/056/56478_eabc28e7c7.swf", - 27, + "27", "", - 0, + "0", 2020-01-01T00:00:00.000Z, "7942", "biology", "http://images.neopets.com/cp/bio/swf/000/000/007/7942_2eab06fd7b.swf", - 5, + "5", "0000000000000000000000000000000000000000000000000000", - 0, + "0", 2020-01-01T00:00:00.000Z, "7941", "biology", "http://images.neopets.com/cp/bio/swf/000/000/007/7941_2c4cc4b846.swf", - 15, + "15", "0000000000000000000000000000000000000000000000000000", - 0, + "0", 2020-01-01T00:00:00.000Z, "24008", "biology", "http://images.neopets.com/cp/bio/swf/000/000/024/24008_a05fe9876a.swf", - 30, + "30", "0000000000000000000000000000000000000000000000000000", - 0, + "0", 2020-01-01T00:00:00.000Z, "21060", "biology", "http://images.neopets.com/cp/bio/swf/000/000/021/21060_d77ba93b7b.swf", - 33, + "33", "0000000000000000000000000000000000000000000000000000", - 0, + "0", 2020-01-01T00:00:00.000Z, "21057", "biology", "http://images.neopets.com/cp/bio/swf/000/000/021/21057_4550efbb2f.swf", - 34, + "34", "0000000000000000000000000000000000000000000000000000", - 0, + "0", 2020-01-01T00:00:00.000Z, "7946", "biology", "http://images.neopets.com/cp/bio/swf/000/000/007/7946_0348dad587.swf", - 37, + "37", "0000000000000000000000000000000000000000000000000000", ], ], @@ -492,25 +492,25 @@ Array [ "SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)", Array [ "1", - "21057,21060,24008,7941,7942,7946", + "7941,7942,7946,21057,21060,24008", ], ], Array [ "INSERT INTO pet_states (female, labeled, mood_id, pet_type_id, swf_asset_ids, unconverted) VALUES (?, ?, ?, ?, ?, ?);", Array [ - false, - true, - 2, + 0, + 1, + "2", "1", - "21057,21060,24008,7941,7942,7946", - false, + "7941,7942,7946,21057,21060,24008", + 0, ], ], Array [ "SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)", Array [ "1", - "21057,21060,24008,7941,7942,7946", + "7941,7942,7946,21057,21060,24008", ], ], Array [ @@ -934,103 +934,103 @@ Array [ Array [ "INSERT INTO swf_assets (body_id, created_at, remote_id, type, url, zone_id, zones_restrict) VALUES (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?);", Array [ - 180, + "180", 2020-01-01T00:00:00.000Z, "6829", "object", "http://images.neopets.com/cp/items/swf/000/000/006/6829_1707e50385.swf", - 3, + "3", "", - 180, + "180", 2020-01-01T00:00:00.000Z, "14855", "object", "http://images.neopets.com/cp/items/swf/000/000/014/14855_215f367070.swf", - 25, + "25", "", - 180, + "180", 2020-01-01T00:00:00.000Z, "14856", "object", "http://images.neopets.com/cp/items/swf/000/000/014/14856_46c1b32797.swf", - 26, + "26", "", - 180, + "180", 2020-01-01T00:00:00.000Z, "14857", "object", "http://images.neopets.com/cp/items/swf/000/000/014/14857_d43380ef66.swf", - 40, + "40", "", - 180, + "180", 2020-01-01T00:00:00.000Z, "36414", "object", "http://images.neopets.com/cp/items/swf/000/000/036/36414_1e2aaab4ad.swf", - 48, + "48", "", - 180, + "180", 2020-01-01T00:00:00.000Z, "39646", "object", "http://images.neopets.com/cp/items/swf/000/000/039/39646_e129e22ada.swf", - 42, + "42", "", - 180, + "180", 2020-01-01T00:00:00.000Z, "51959", "object", "http://images.neopets.com/cp/items/swf/000/000/051/51959_4439727c48.swf", - 45, + "45", "", - 180, + "180", 2020-01-01T00:00:00.000Z, "56478", "object", "http://images.neopets.com/cp/items/swf/000/000/056/56478_eabc28e7c7.swf", - 27, + "27", "", - 0, + "0", 2020-01-01T00:00:00.000Z, "7942", "biology", "http://images.neopets.com/cp/bio/swf/000/000/007/7942_2eab06fd7b.swf", - 5, + "5", "0000000000000000000000000000000000000000000000000000", - 0, + "0", 2020-01-01T00:00:00.000Z, "7941", "biology", "http://images.neopets.com/cp/bio/swf/000/000/007/7941_2c4cc4b846.swf", - 15, + "15", "0000000000000000000000000000000000000000000000000000", - 0, + "0", 2020-01-01T00:00:00.000Z, "24008", "biology", "http://images.neopets.com/cp/bio/swf/000/000/024/24008_a05fe9876a.swf", - 30, + "30", "0000000000000000000000000000000000000000000000000000", - 0, + "0", 2020-01-01T00:00:00.000Z, "21060", "biology", "http://images.neopets.com/cp/bio/swf/000/000/021/21060_d77ba93b7b.swf", - 33, + "33", "0000000000000000000000000000000000000000000000000000", - 0, + "0", 2020-01-01T00:00:00.000Z, "21057", "biology", "http://images.neopets.com/cp/bio/swf/000/000/021/21057_4550efbb2f.swf", - 34, + "34", "0000000000000000000000000000000000000000000000000000", - 0, + "0", 2020-01-01T00:00:00.000Z, "7946", "biology", "http://images.neopets.com/cp/bio/swf/000/000/007/7946_0348dad587.swf", - 37, + "37", "0000000000000000000000000000000000000000000000000000", ], ], @@ -1064,25 +1064,25 @@ Array [ "SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)", Array [ "1", - "21057,21060,24008,7941,7942,7946", + "7941,7942,7946,21057,21060,24008", ], ], Array [ "INSERT INTO pet_states (female, labeled, mood_id, pet_type_id, swf_asset_ids, unconverted) VALUES (?, ?, ?, ?, ?, ?);", Array [ - false, - true, - 2, + 0, + 1, + "2", "1", - "21057,21060,24008,7941,7942,7946", - false, + "7941,7942,7946,21057,21060,24008", + 0, ], ], Array [ "SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)", Array [ "1", - "21057,21060,24008,7941,7942,7946", + "7941,7942,7946,21057,21060,24008", ], ], Array [ diff --git a/src/server/types/Outfit.js b/src/server/types/Outfit.js index 4122c61e..0d6f38f0 100644 --- a/src/server/types/Outfit.js +++ b/src/server/types/Outfit.js @@ -191,12 +191,12 @@ async function saveModelingData( type: "object", remoteId: String(objectAsset.asset_id), url: objectAsset.asset_url, - zoneId: objectAsset.zone_id, + zoneId: String(objectAsset.zone_id), zonesRestrict: "", // 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 // 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); @@ -204,9 +204,9 @@ async function saveModelingData( type: "biology", remoteId: String(biologyAsset.part_id), url: biologyAsset.asset_url, - zoneId: biologyAsset.zone_id, + zoneId: String(biologyAsset.zone_id), zonesRestrict: biologyAsset.zones_restrict, - bodyId: 0, + bodyId: "0", })); const incomingSwfAssets = [...incomingItemSwfAssets, ...incomingPetSwfAssets]; @@ -277,13 +277,13 @@ async function saveModelingData( const incomingPetState = { petTypeId: petType.id, swfAssetIds: incomingPetSwfAssets - .map((a) => a.remoteId) - .sort() + .map((row) => row.remoteId) + .sort((a, b) => Number(a) - Number(b)) .join(","), - female: petMetaData.gender === 2, // sorry for this column name :/ - moodId: petMetaData.mood, - unconverted: incomingPetSwfAssets.length === 1, - labeled: true, + female: petMetaData.gender === 2 ? 1 : 0, // sorry for this column name :/ + moodId: String(petMetaData.mood), + unconverted: incomingPetSwfAssets.length === 1 ? 1 : 0, + labeled: 1, }; await syncToDb(db, [incomingPetState], {