From 6ab65ef87ea09ef29a754e05102a60e3fa91d1b6 Mon Sep 17 00:00:00 2001 From: Syco21 Date: Fri, 22 May 2026 21:13:19 +0200 Subject: [PATCH] Remove stale cards during import that no longer exist in YGOPRODeck --- src/controllers/importController.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/controllers/importController.js b/src/controllers/importController.js index 427085b..7474715 100644 --- a/src/controllers/importController.js +++ b/src/controllers/importController.js @@ -173,9 +173,23 @@ async function importCardsInternal() { } } + // Remove cards that no longer exist in the API + const apiCardIds = new Set(cards.map(c => c.id)); + const staleIds = existingCards.map(r => r.id).filter(id => !apiCardIds.has(id)); + if (staleIds.length) { + for (let i = 0; i < staleIds.length; i += 500) { + const chunk = staleIds.slice(i, i + 500); + const placeholders = chunk.map(() => '?').join(','); + await db.execute(`DELETE FROM card_images WHERE card_id IN (${placeholders})`, chunk); + await db.execute(`DELETE FROM card_sets_rarity WHERE card_id IN (${placeholders})`, chunk); + await db.execute(`DELETE FROM cards WHERE id IN (${placeholders})`, chunk); + } + } + return { total_cards: cards.length, cards_added: addedCards, + cards_removed: staleIds.length, images_added: addedImages, set_rarities_added: addedSetRarities, duration_seconds: parseFloat(((Date.now() - startTime) / 1000).toFixed(2)),