Update registry DB insert to be atomic
Updates the `RegistryInsertNodeOperation::insert_node` operation for the
Diesel-backed registry store to be fully atomic. Previously, the query
to check for the existence of a node was outside the database
transaction that added or updated the node, which made the operation
non-atomic.
@@ -42,6 +42,7 @@ impl<'a> RegistryInsertNodeOperation for RegistryOperations<'a, diesel::pg::PgCo
// Verify that the node's required fields are non-empty
check_node_required_fields_are_not_empty(&node)?;
+ self.conn.transaction::<(), _, _>(|| {
// Verify that the node's endpoints are unique. This requires a raw SQL query because of
// the complicated `WHERE` statement.
let filters = node
@@ -80,7 +81,6 @@ impl<'a> RegistryInsertNodeOperation for RegistryOperations<'a, diesel::pg::PgCo
)
})?;
- self.conn.transaction::<(), _, _>(|| {
if existing_node.is_none() {
// Add new node
insert_into(splinter_nodes::table)
@@ -182,6 +182,7 @@ impl<'a> RegistryInsertNodeOperation for RegistryOperations<'a, diesel::sqlite::
// Verify that the node's required fields are non-empty
check_node_required_fields_are_not_empty(&node)?;
+ self.conn.transaction::<(), _, _>(|| {
// Verify that the node's endpoints are unique. This requires a raw SQL query because of
// the complicated `WHERE` statement.
let filters = node
@@ -220,7 +221,6 @@ impl<'a> RegistryInsertNodeOperation for RegistryOperations<'a, diesel::sqlite::
)
})?;
- self.conn.transaction::<(), _, _>(|| {
if existing_node.is_none() {
// Add new node
insert_into(splinter_nodes::table)
