Skip to content

Commit

Permalink
Tag Block/Tx backfills as done instead of removing them
Browse files Browse the repository at this point in the history
  • Loading branch information
Ktl-XV committed Jan 29, 2024
1 parent a552943 commit bf145c3
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 4 deletions.
20 changes: 20 additions & 0 deletions migrations/arbitrum/1706550739000-AddDoneToTxBackfillTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner, getConnection } from 'typeorm';

export class AddDoneToTxBackfillTable1706550739000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`
ALTER TABLE ${schema}.tx_backfill ADD CONSTRAINT tx_backfill_pk PRIMARY KEY (transaction_hash, block_number);
ALTER TABLE ${schema}.tx_backfill ADD done boolean NOT NULL DEFAULT FALSE;
CREATE INDEX tx_backfill_partial ON ${schema}.tx_backfill (transaction_hash, block_number)
WHERE done = FALSE;
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`ALTER TABLE ${schema}.tx_backfill UPDATE DROP COLUMN done;`);
}
}
20 changes: 20 additions & 0 deletions migrations/avalanche/1706550739000-AddDoneToTxBackfillTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner, getConnection } from 'typeorm';

export class AddDoneToTxBackfillTable1706550739000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`
ALTER TABLE ${schema}.tx_backfill ADD CONSTRAINT tx_backfill_pk PRIMARY KEY (transaction_hash, block_number);
ALTER TABLE ${schema}.tx_backfill ADD done boolean NOT NULL DEFAULT FALSE;
CREATE INDEX tx_backfill_partial ON ${schema}.tx_backfill (transaction_hash, block_number)
WHERE done = FALSE;
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`ALTER TABLE ${schema}.tx_backfill UPDATE DROP COLUMN done;`);
}
}
20 changes: 20 additions & 0 deletions migrations/base/1706550739000-AddDoneToTxBackfillTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner, getConnection } from 'typeorm';

export class AddDoneToTxBackfillTable1706550739000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`
ALTER TABLE ${schema}.tx_backfill ADD CONSTRAINT tx_backfill_pk PRIMARY KEY (transaction_hash, block_number);
ALTER TABLE ${schema}.tx_backfill ADD done boolean NOT NULL DEFAULT FALSE;
CREATE INDEX tx_backfill_partial ON ${schema}.tx_backfill (transaction_hash, block_number)
WHERE done = FALSE;
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`ALTER TABLE ${schema}.tx_backfill UPDATE DROP COLUMN done;`);
}
}
20 changes: 20 additions & 0 deletions migrations/bsc/1706550739000-AddDoneToTxBackfillTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner, getConnection } from 'typeorm';

export class AddDoneToTxBackfillTable1706550739000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`
ALTER TABLE ${schema}.tx_backfill ADD CONSTRAINT tx_backfill_pk PRIMARY KEY (transaction_hash, block_number);
ALTER TABLE ${schema}.tx_backfill ADD done boolean NOT NULL DEFAULT FALSE;
CREATE INDEX tx_backfill_partial ON ${schema}.tx_backfill (transaction_hash, block_number)
WHERE done = FALSE;
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`ALTER TABLE ${schema}.tx_backfill UPDATE DROP COLUMN done;`);
}
}
20 changes: 20 additions & 0 deletions migrations/celo/1706550739000-AddDoneToTxBackfillTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner, getConnection } from 'typeorm';

export class AddDoneToTxBackfillTable1706550739000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`
ALTER TABLE ${schema}.tx_backfill ADD CONSTRAINT tx_backfill_pk PRIMARY KEY (transaction_hash, block_number);
ALTER TABLE ${schema}.tx_backfill ADD done boolean NOT NULL DEFAULT FALSE;
CREATE INDEX tx_backfill_partial ON ${schema}.tx_backfill (transaction_hash, block_number)
WHERE done = FALSE;
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`ALTER TABLE ${schema}.tx_backfill UPDATE DROP COLUMN done;`);
}
}
20 changes: 20 additions & 0 deletions migrations/ethereum/1706550739000-AddDoneToTxBackfillTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner, getConnection } from 'typeorm';

export class AddDoneToTxBackfillTable1706550739000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`
ALTER TABLE ${schema}.tx_backfill ADD CONSTRAINT tx_backfill_pk PRIMARY KEY (transaction_hash, block_number);
ALTER TABLE ${schema}.tx_backfill ADD done boolean NOT NULL DEFAULT FALSE;
CREATE INDEX tx_backfill_partial ON ${schema}.tx_backfill (transaction_hash, block_number)
WHERE done = FALSE;
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`ALTER TABLE ${schema}.tx_backfill UPDATE DROP COLUMN done;`);
}
}
20 changes: 20 additions & 0 deletions migrations/fantom/1706550739000-AddDoneToTxBackfillTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner, getConnection } from 'typeorm';

export class AddDoneToTxBackfillTable1706550739000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`
ALTER TABLE ${schema}.tx_backfill ADD CONSTRAINT tx_backfill_pk PRIMARY KEY (transaction_hash, block_number);
ALTER TABLE ${schema}.tx_backfill ADD done boolean NOT NULL DEFAULT FALSE;
CREATE INDEX tx_backfill_partial ON ${schema}.tx_backfill (transaction_hash, block_number)
WHERE done = FALSE;
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`ALTER TABLE ${schema}.tx_backfill UPDATE DROP COLUMN done;`);
}
}
20 changes: 20 additions & 0 deletions migrations/optimism/1706550739000-AddDoneToTxBackfillTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner, getConnection } from 'typeorm';

export class AddDoneToTxBackfillTable1706550739000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`
ALTER TABLE ${schema}.tx_backfill ADD CONSTRAINT tx_backfill_pk PRIMARY KEY (transaction_hash, block_number);
ALTER TABLE ${schema}.tx_backfill ADD done boolean NOT NULL DEFAULT FALSE;
CREATE INDEX tx_backfill_partial ON ${schema}.tx_backfill (transaction_hash, block_number)
WHERE done = FALSE;
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`ALTER TABLE ${schema}.tx_backfill UPDATE DROP COLUMN done;`);
}
}
20 changes: 20 additions & 0 deletions migrations/polygon/1706550739000-AddDoneToTxBackfillTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner, getConnection } from 'typeorm';

export class AddDoneToTxBackfillTable1706550739000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`
ALTER TABLE ${schema}.tx_backfill ADD CONSTRAINT tx_backfill_pk PRIMARY KEY (transaction_hash, block_number);
ALTER TABLE ${schema}.tx_backfill ADD done boolean NOT NULL DEFAULT FALSE;
CREATE INDEX tx_backfill_partial ON ${schema}.tx_backfill (transaction_hash, block_number)
WHERE done = FALSE;
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
const connection = getConnection();
const { schema } = connection.options as any;
await queryRunner.query(`ALTER TABLE ${schema}.tx_backfill UPDATE DROP COLUMN done;`);
}
}
11 changes: 9 additions & 2 deletions src/scripts/pull_and_save_backfill_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export class BackfillTxScraper {
logger.info(`pulling tx backlog`);

const queryResult = await connection.query(
`SELECT transaction_hash FROM ${SCHEMA}.tx_backfill LIMIT ${MAX_TX_TO_PULL}`,
`SELECT transaction_hash
FROM ${SCHEMA}.tx_backfill
WHERE done = FALSE
ORDER BY block_number
LIMIT ${MAX_TX_TO_PULL}`,
);

const txList = queryResult.map((e: { transaction_hash: string }) => e.transaction_hash);
Expand All @@ -34,7 +38,10 @@ export class BackfillTxScraper {
const txDeleteQuery = `DELETE FROM ${SCHEMA}.transactions WHERE transaction_hash IN (${txHashList})`;
const txReceiptDeleteQuery = `DELETE FROM ${SCHEMA}.transaction_receipts WHERE transaction_hash IN (${txHashList});`;
// const txLogsDeleteQuery = `DELETE FROM ${SCHEMA}.transaction_logs WHERE transaction_hash IN (${txHashList});`;
const txBacklogQuery = `DELETE FROM ${SCHEMA}.tx_backfill WHERE transaction_hash IN (${txHashList});`;
const txBacklogQuery = `
UPDATE ${SCHEMA}.tx_backfill
SET done = true
WHERE block_number IN (${txHashList})`;

const queryRunner = connection.createQueryRunner();
await queryRunner.connect();
Expand Down
7 changes: 5 additions & 2 deletions src/scripts/pull_and_save_block_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ export class BlockEventsScraper {
const oldestBlocksToBackfill = await connection.query(
`SELECT DISTINCT block_number
FROM ${SCHEMA}.tx_backfill
WHERE done = false
ORDER BY block_number
LIMIT ${MAX_BLOCKS_TO_PULL}`,
);
Expand All @@ -361,11 +362,13 @@ export class BlockEventsScraper {
const newBlocks = await web3Source.getBatchBlockInfoAsync(blockNumbers, true);
const success = await getParseSaveBlocksTransactionsEvents(connection, producer, newBlocks, false);
if (success) {
const newBlockNumbers = newBlocks.map((block) => block.number);
const queryRunner = connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.manager.query(
`DELETE FROM ${SCHEMA}.tx_backfill
WHERE block_number IN (${blockNumbers.join(',')})`,
`UPDATE ${SCHEMA}.tx_backfill
SET done = true
WHERE block_number IN (${newBlockNumbers.join(',')})`,
);
queryRunner.release();

Expand Down

0 comments on commit bf145c3

Please sign in to comment.