Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/db/src/token_transfers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ use crate::{DbResult, PgPool};
use indexer_domain::{BlockHeight, LogIndex, TokenStandard, TokenTransfer, Wei};
use sqlx::Row;

/// Insert a single decoded transfer. The `(tx_hash, log_index)` pair is
/// effectively unique for the source log, but the table doesn't pin a unique
/// constraint on it (drizzle didn't either) — the worker dedupes upstream.
/// Insert a single decoded transfer. Retry-safe via `(tx_hash, log_index)`
/// ON CONFLICT DO NOTHING — matches the batch insert path.
Comment on lines +7 to +8
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial | ⚡ Quick win

Add a duplicate-insert regression test.

This change makes the single-row path rely on DB conflict handling for retry safety. Please add coverage that calls insert twice with the same (tx_hash, log_index) and asserts the second call still returns Ok(()) without creating a second row; crates/sync/src/block_writer.rs:41-66 depends on that behavior during retries.

Also applies to: 16-17

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/db/src/token_transfers.rs` around lines 7 - 8, Add a regression test
that verifies the single-row insert is retry-safe by calling the insert function
in crates/db/src/token_transfers.rs twice with the same (tx_hash, log_index) and
asserting the second call returns Ok(()) and that the table still has only one
row for that key; specifically create a test that inserts a decoded transfer via
the same insert function twice and then queries the token_transfers table (or
uses the existing fetch helper) to assert row count is 1, since
crates/sync/src/block_writer.rs:41-66 depends on this conflict-handling
behavior.

pub async fn insert<'e, E>(executor: E, t: &TokenTransfer) -> DbResult<()>
where
E: sqlx::PgExecutor<'e>,
{
sqlx::query(
"INSERT INTO token_transfers (block_height, tx_hash, log_index, contract, standard, \
from_addr, to_addr, token_id, amount) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) \
ON CONFLICT (tx_hash, log_index) DO NOTHING",
)
.bind(t.block_height)
.bind(&t.tx_hash)
Expand Down
Loading