From 3e84cd28ee35f3de3265ed9a4b7b6a7971ec9478 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Sun, 25 Feb 2024 05:48:49 +0000 Subject: [PATCH] replace not null --- sql/create_tables.sql | 2 +- store/insert.ts | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/sql/create_tables.sql b/sql/create_tables.sql index d81568cde..f954091e4 100644 --- a/sql/create_tables.sql +++ b/sql/create_tables.sql @@ -51,7 +51,7 @@ CREATE TABLE IF NOT EXISTS player_matches ( match_id bigint REFERENCES matches(match_id) ON DELETE CASCADE, account_id bigint, player_slot integer, - hero_id integer NOT NULL, + hero_id integer, item_0 integer, item_1 integer, item_2 integer, diff --git a/store/insert.ts b/store/insert.ts index fc8692da0..bf0915574 100644 --- a/store/insert.ts +++ b/store/insert.ts @@ -157,6 +157,17 @@ export async function insertMatch( console.log('[UPSERTMATCHPOSTGRES]: skipping due to check'); return; } + // If parsed data, we want to make sure the match exists in DB + // Otherwise we could end up with parsed data only rows for matches we skipped above + // We might want to switch the upsert to UPDATE instead for parsed case + // That requires writing a new SQL query though + // If we do that then we can put the NOT NULL constraint on hero_id + if (options.type === 'parsed') { + const { rows } = await db.raw('select match_id from matches where match_id = ?', [match.match_id]); + if (!rows.length) { + return; + } + } if (!isProLeague) { // Skip if not in a pro league (premium or professional tier) console.log('[UPSERTMATCHPOSTGRES]: skipping due to league'); @@ -193,7 +204,7 @@ export async function insertMatch( pm.lane_role = laneData.lane_role ?? null; pm.is_roaming = laneData.is_roaming ?? null; } - console.log('[UPSERTMATCHPOSTGRES]: player_match', pm.player_slot, pm.hero_id); + console.log('[UPSERTMATCHPOSTGRES]: player_match', pm.match_id, pm.player_slot, pm.hero_id); return upsert(trx, 'player_matches', pm, { match_id: pm.match_id, player_slot: pm.player_slot,