File tree Expand file tree Collapse file tree 1 file changed +12
-3
lines changed
Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Original file line number Diff line number Diff 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" )
You can’t perform that action at this time.
0 commit comments