Skip to content

Commit

Permalink
fixing updating order/operations for iot session key filter reconcili…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
jeffgrunewald committed Aug 11, 2023
1 parent 55d077f commit f43ad58
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions iot_config/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,16 +713,18 @@ pub async fn update_skfs(
) -> anyhow::Result<()> {
let mut transaction = db.begin().await?;

let added_updates: Vec<(Skf, proto::ActionV1)> = insert_skfs(to_add, &mut transaction)
// Always process removes before adds to ensure updating existing values doesn't result in
// removing a value that was just added
let removed_updates: Vec<(Skf, proto::ActionV1)> = remove_skfs(to_remove, &mut transaction)
.await?
.into_iter()
.map(|added_skf| (added_skf, proto::ActionV1::Add))
.map(|removed_skf| (removed_skf, proto::ActionV1::Remove))
.collect();

let removed_updates: Vec<(Skf, proto::ActionV1)> = remove_skfs(to_remove, &mut transaction)
let added_updates: Vec<(Skf, proto::ActionV1)> = insert_skfs(to_add, &mut transaction)
.await?
.into_iter()
.map(|removed_skf| (removed_skf, proto::ActionV1::Remove))
.map(|added_skf| (added_skf, proto::ActionV1::Add))
.collect();

transaction.commit().await?;
Expand Down Expand Up @@ -767,7 +769,7 @@ async fn insert_skfs(skfs: &[Skf], db: impl sqlx::PgExecutor<'_>) -> anyhow::Res
const SKF_INSERT_VALS: &str =
" insert into route_session_key_filters (route_id, devaddr, session_key, max_copies) ";
const SKF_INSERT_CONFLICT: &str =
" on conflict (route_id, devaddr, session_key) do nothing returning * ";
" on conflict (route_id, devaddr, session_key) update set max_copies = excluded.max_copies returning * ";

let mut query_builder: sqlx::QueryBuilder<sqlx::Postgres> =
sqlx::QueryBuilder::new(SKF_INSERT_VALS);
Expand Down

0 comments on commit f43ad58

Please sign in to comment.