11import pLimit from 'p-limit' ;
2- import { closeDatabase , rootPgPool , sequelize } from '../../config/database' ;
2+ import { rootPgPool , sequelize } from '../../config/database' ;
33import { getGuardsFromBalances } from './payload' ;
44import Guard from '../../models/guard' ;
55
6- const CONCURRENCY_LIMIT = 4 ; // Number of concurrent fetches allowed
6+ const CONCURRENCY_LIMIT = 50 ; // Number of concurrent fetches allowed
77const limitFetch = pLimit ( CONCURRENCY_LIMIT ) ;
88
99export async function backfillGuards ( ) {
@@ -17,13 +17,19 @@ export async function backfillGuards() {
1717 await rootPgPool . query ( deleteGuardsQuery ) ;
1818
1919 const limit = 1000 ; // Number of rows to process in one batch
20- let offset = 0 ;
20+ let currentId = 0 ;
2121
2222 while ( true ) {
23- console . log ( `Fetching rows from offset : ${ offset } , limit: ${ limit } ` ) ;
23+ console . log ( `Fetching rows starting from : ${ currentId } ` ) ;
2424 const res = await rootPgPool . query (
25- `SELECT b.id, b.account, b."chainId", b.module FROM "Balances" b ORDER BY b.id LIMIT $1 OFFSET $2` ,
26- [ limit , offset ] ,
25+ `
26+ SELECT b.id, b.account, b."chainId", b.module
27+ FROM "Balances" b
28+ WHERE b.id > $2
29+ ORDER BY b.id
30+ LIMIT $1
31+ ` ,
32+ [ limit , currentId ] ,
2733 ) ;
2834
2935 const rows = res . rows ;
@@ -53,13 +59,13 @@ export async function backfillGuards() {
5359 } ) ;
5460
5561 await tx . commit ( ) ;
56- console . log ( `Batch at offset ${ offset } processed successfully.` ) ;
57- offset += limit ;
62+ console . log ( `Row at ${ currentId } id processed successfully.` ) ;
63+ currentId = rows [ rows . length - 1 ] . id ;
5864 } catch ( batchError ) {
59- console . error ( `Error processing batch at offset ${ offset } :` , batchError ) ;
65+ console . error ( `Error processing at id ${ currentId } :` , batchError ) ;
6066 try {
6167 await tx . rollback ( ) ;
62- console . log ( `Transaction for batch at offset ${ offset } rolled back.` ) ;
68+ console . log ( `Transaction for id at ${ currentId } rolled back.` ) ;
6369 } catch ( rollbackError ) {
6470 console . error ( 'Error during rollback:' , rollbackError ) ;
6571 }
0 commit comments