Skip to content

Commit 5acdbf4

Browse files
committed
update postgres transaction to display using references in transaction callbacks
1 parent eeb0cf4 commit 5acdbf4

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

examples/postgres/transaction/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ workspace = "../../../"
77
[dependencies]
88
sqlx = { path = "../../../", features = [ "postgres", "runtime-tokio-native-tls" ] }
99
futures = "0.3.1"
10-
tokio = { version = "1.20.0", features = ["macros"]}
10+
tokio = { version = "1.20.0", features = ["macros", "rt-multi-thread"]}

examples/postgres/transaction/src/main.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
use sqlx::query;
2+
use sqlx::Acquire;
3+
use sqlx::Connection;
24

35
async fn insert_and_verify(
46
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
57
test_id: i64,
68
) -> Result<(), Box<dyn std::error::Error>> {
9+
let connection = transaction.acquire().await?;
710
query!(
811
r#"INSERT INTO todos (id, description)
912
VALUES ( $1, $2 )
1013
"#,
1114
test_id,
1215
"test todo"
1316
)
14-
.execute(&mut *transaction)
17+
.execute(&mut *connection)
1518
.await?;
1619

1720
// check that inserted todo can be fetched inside the uncommitted transaction
1821
let _ = query!(r#"SELECT FROM todos WHERE id = $1"#, test_id)
19-
.fetch_one(transaction)
22+
.fetch_one(&mut *connection)
2023
.await?;
2124

2225
Ok(())
@@ -60,6 +63,33 @@ async fn commit_example(
6063
Ok(())
6164
}
6265

66+
async fn insert_and_update_description(
67+
pool: &sqlx::PgPool,
68+
test_id: i64,
69+
new_description: &str,
70+
) -> Result<(), Box<dyn std::error::Error>> {
71+
let mut connection = pool.acquire().await?;
72+
73+
connection
74+
.transaction(|tx| {
75+
Box::pin(async {
76+
insert_and_verify(tx, test_id).await.unwrap();
77+
let conn = tx.acquire().await?;
78+
query!(
79+
r#"UPDATE todos set description = $1 where id = $2"#,
80+
new_description,
81+
test_id,
82+
)
83+
.execute(conn)
84+
.await?;
85+
Ok::<_, sqlx::Error>(())
86+
})
87+
})
88+
.await?;
89+
90+
Ok(())
91+
}
92+
6393
#[tokio::main]
6494
async fn main() -> Result<(), Box<dyn std::error::Error>> {
6595
let conn_str =
@@ -100,5 +130,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
100130

101131
assert!(inserted_todo.is_ok());
102132

133+
let test_id = 2;
134+
135+
let description = String::from("hello world");
136+
insert_and_update_description(&pool, test_id, &description).await?;
137+
138+
// check that inserted todo is visible outside the transaction after commit
139+
let inserted_todo: Result<_, _> = query!(r#"SELECT * FROM todos WHERE id = $1"#, test_id)
140+
.fetch_one(&pool)
141+
.await;
142+
143+
assert!(inserted_todo.is_ok());
144+
assert_eq!(inserted_todo.unwrap().description, description);
145+
103146
Ok(())
104147
}

0 commit comments

Comments
 (0)