-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
main.rs
106 lines (79 loc) · 2.93 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use sqlx::query;
async fn insert_and_verify(
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
test_id: i64,
) -> Result<(), Box<dyn std::error::Error>> {
query!(
r#"INSERT INTO todos (id, description)
VALUES ( $1, $2 )
"#,
test_id,
"test todo"
)
// In 0.7, `Transaction` can no longer implement `Executor` directly,
// so it must be dereferenced to the internal connection type.
.execute(&mut **transaction)
.await?;
// check that inserted todo can be fetched inside the uncommitted transaction
let _ = query!(r#"SELECT FROM todos WHERE id = $1"#, test_id)
.fetch_one(&mut **transaction)
.await?;
Ok(())
}
async fn explicit_rollback_example(
pool: &sqlx::PgPool,
test_id: i64,
) -> Result<(), Box<dyn std::error::Error>> {
let mut transaction = pool.begin().await?;
insert_and_verify(&mut transaction, test_id).await?;
transaction.rollback().await?;
Ok(())
}
async fn implicit_rollback_example(
pool: &sqlx::PgPool,
test_id: i64,
) -> Result<(), Box<dyn std::error::Error>> {
let mut transaction = pool.begin().await?;
insert_and_verify(&mut transaction, test_id).await?;
// no explicit rollback here but the transaction object is dropped at the end of the scope
Ok(())
}
async fn commit_example(
pool: &sqlx::PgPool,
test_id: i64,
) -> Result<(), Box<dyn std::error::Error>> {
let mut transaction = pool.begin().await?;
insert_and_verify(&mut transaction, test_id).await?;
transaction.commit().await?;
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let conn_str =
std::env::var("DATABASE_URL").expect("Env var DATABASE_URL is required for this example.");
let pool = sqlx::PgPool::connect(&conn_str).await?;
let test_id = 1;
// remove any old values that might be in the table already with this id from a previous run
let _ = query!(r#"DELETE FROM todos WHERE id = $1"#, test_id)
.execute(&pool)
.await?;
explicit_rollback_example(&pool, test_id).await?;
// check that inserted todo is not visible outside the transaction after explicit rollback
let inserted_todo = query!(r#"SELECT FROM todos WHERE id = $1"#, test_id)
.fetch_one(&pool)
.await;
assert!(inserted_todo.is_err());
implicit_rollback_example(&pool, test_id).await?;
// check that inserted todo is not visible outside the transaction after implicit rollback
let inserted_todo = query!(r#"SELECT FROM todos WHERE id = $1"#, test_id)
.fetch_one(&pool)
.await;
assert!(inserted_todo.is_err());
commit_example(&pool, test_id).await?;
// check that inserted todo is visible outside the transaction after commit
let inserted_todo = query!(r#"SELECT FROM todos WHERE id = $1"#, test_id)
.fetch_one(&pool)
.await;
assert!(inserted_todo.is_ok());
Ok(())
}