From e5ce17c2ade2a1b36d69a7b049a8a249b5b09c81 Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Fri, 27 May 2022 13:11:32 -0700 Subject: [PATCH] Fix up backfill script (and I ran it) --- functions/src/scripts/backfill-fees.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/functions/src/scripts/backfill-fees.ts b/functions/src/scripts/backfill-fees.ts index 9ca068d2..66bbc0c3 100644 --- a/functions/src/scripts/backfill-fees.ts +++ b/functions/src/scripts/backfill-fees.ts @@ -9,17 +9,15 @@ const firestore = admin.firestore() if (require.main === module) { const contractsRef = firestore.collection('contracts') - contractsRef.get().then((contractsSnaps) => { - let n = 0 + contractsRef.get().then(async (contractsSnaps) => { console.log(`Loaded ${contractsSnaps.size} contracts.`) - contractsSnaps.forEach((ct) => { - const data = ct.data() - if (!('collectedFees' in data)) { - n += 1 - console.log(`Filling in missing fees on contract ${data.id}...`) - ct.ref.update({ collectedFees: noFees }) - } + const needsFilling = contractsSnaps.docs.filter((ct) => { + return !('collectedFees' in ct.data()) }) - console.log(`Updated ${n} contracts.`) + console.log(`Found ${needsFilling.length} contracts to update.`) + await Promise.all( + needsFilling.map((ct) => ct.ref.update({ collectedFees: noFees })) + ) + console.log(`Updated all contracts.`) }) }