Skip to content

Commit 8f11f58

Browse files
committed
fix: Avoid inserting new block rows of existing height
1 parent f2a808d commit 8f11f58

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/async_store.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,20 @@ impl Store {
140140
for (&height, hash) in &local_chain.blocks {
141141
match hash {
142142
Some(hash) => {
143-
sqlx::query("INSERT OR IGNORE INTO block(height, hash) VALUES($1, $2)")
143+
// Avoid inserting new rows of existing height.
144+
// FIXME: The correct way to handle this is to have a unique constraint on `height`
145+
// in the block table schema.
146+
let row_option = sqlx::query("SELECT height FROM block WHERE height = $1")
144147
.bind(height)
145-
.bind(hash.to_string())
146-
.execute(&self.pool)
148+
.fetch_optional(&self.pool)
147149
.await?;
150+
if row_option.is_none() {
151+
sqlx::query("INSERT OR IGNORE INTO block(height, hash) VALUES($1, $2)")
152+
.bind(height)
153+
.bind(hash.to_string())
154+
.execute(&self.pool)
155+
.await?;
156+
}
148157
}
149158
None => {
150159
sqlx::query("DELETE FROM block WHERE height = $1")

0 commit comments

Comments
 (0)