Skip to content

Commit

Permalink
Add transaction retry to schema upgrade integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smklein committed Oct 5, 2023
1 parent d300fb8 commit 432d07c
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions nexus/tests/integration_tests/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ async fn test_setup<'a>(
builder
}

async fn apply_update_as_transaction(
client: &omicron_test_utils::dev::db::Client,
sql: &str,
) -> Result<(), tokio_postgres::Error> {
client.batch_execute("BEGIN;").await?;
client.batch_execute(&sql).await?;
client.batch_execute("COMMIT;").await?;
Ok(())
}

async fn apply_update(
log: &Logger,
crdb: &CockroachInstance,
Expand All @@ -87,15 +97,21 @@ async fn apply_update(

for _ in 0..times_to_apply {
for sql in sqls.iter() {
client
.batch_execute("BEGIN;")
.await
.expect("Failed to BEGIN update");
client.batch_execute(&sql).await.expect("Failed to execute update");
client
.batch_execute("COMMIT;")
.await
.expect("Failed to COMMIT update");
loop {
let result = apply_update_as_transaction(&client, sql).await;
match result {
Ok(()) => break,
Err(err) => {
if let Some(code) = err.code() {
if code == &tokio_postgres::error::SqlState::T_R_SERIALIZATION_FAILURE {
warn!(log, "Transaction retrying");
continue;
}
}
panic!("Failed to apply update: {err}");
}
}
}
}
}

Expand Down

0 comments on commit 432d07c

Please sign in to comment.