Skip to content

Commit 4d57478

Browse files
committed
refactor: Simplify query in write_local_chain
Now that the schema has a unique constraint on block height, we can remove the extra select query.
1 parent 2acbe34 commit 4d57478

1 file changed

Lines changed: 3 additions & 12 deletions

File tree

src/async_store.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,11 @@ impl Store {
140140
for (&height, hash) in &local_chain.blocks {
141141
match hash {
142142
Some(hash) => {
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")
143+
sqlx::query("INSERT OR IGNORE INTO block(height, hash) VALUES($1, $2)")
147144
.bind(height)
148-
.fetch_optional(&self.pool)
145+
.bind(hash.to_string())
146+
.execute(&self.pool)
149147
.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-
}
157148
}
158149
None => {
159150
sqlx::query("DELETE FROM block WHERE height = $1")

0 commit comments

Comments
 (0)