Skip to content

Commit 3844fb3

Browse files
committed
Minor cleanups
1 parent f7e6aa3 commit 3844fb3

File tree

2 files changed

+17
-30
lines changed

2 files changed

+17
-30
lines changed

examples/sync-wrapper/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
diesel = { version = "2.1.0", default-features = false }
9+
diesel = { version = "2.1.0", default-features = false, features = ["returning_clauses_for_sqlite_3_35"] }
1010
diesel-async = { version = "0.4.0", path = "../../", features = ["sync-connection-wrapper", "async-connection-wrapper"] }
1111
diesel_migrations = "2.1.0"
1212
futures-util = "0.3.21"

examples/sync-wrapper/src/main.rs

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ use diesel::prelude::*;
22
use diesel::sqlite::{Sqlite, SqliteConnection};
33
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
44
use diesel_async::sync_connection_wrapper::SyncConnectionWrapper;
5-
use diesel_async::{AsyncConnection, RunQueryDsl, SimpleAsyncConnection};
5+
use diesel_async::{AsyncConnection, RunQueryDsl};
66
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
7-
use futures_util::FutureExt;
87

98
// ordinary diesel model setup
109

@@ -59,21 +58,10 @@ async fn transaction(
5958
if old_name.is_empty() {
6059
Ok(Vec::new())
6160
} else {
62-
diesel::sql_query(
63-
r#"
64-
update
65-
users
66-
set
67-
name = ?2
68-
where
69-
name == ?1
70-
returning *
71-
"#,
72-
)
73-
.bind::<diesel::sql_types::Text, _>(old_name)
74-
.bind::<diesel::sql_types::Text, _>(new_name)
75-
.load(c)
76-
.await
61+
diesel::update(users::table.filter(users::name.eq(old_name)))
62+
.set(users::name.eq(new_name))
63+
.load(c)
64+
.await
7765
}
7866
})
7967
})
@@ -90,10 +78,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
9078

9179
let mut sync_wrapper: SyncConnectionWrapper<InnerConnection> = establish(&db_url).await?;
9280

93-
sync_wrapper.batch_execute("DELETE FROM users").await?;
81+
diesel::delete(users::table)
82+
.execute(&mut sync_wrapper)
83+
.await?;
9484

95-
sync_wrapper
96-
.batch_execute("INSERT INTO users(id, name) VALUES (3, 'toto')")
85+
diesel::insert_into(users::table)
86+
.values((users::id.eq(3), users::name.eq("toto")))
87+
.execute(&mut sync_wrapper)
9788
.await?;
9889

9990
let data: Vec<User> = users::table
@@ -119,32 +110,28 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
119110
.await?;
120111
println!("{data:?}");
121112

122-
// let changed = transaction(&mut sync_wrapper, "iLuke", "JustLuke").await?;
123-
// println!("Changed {changed:?}");
124-
125-
// create an async connection for the migrations
113+
// a quick test to check if we correctly handle transactions
126114
let mut conn_a: SyncConnectionWrapper<InnerConnection> = establish(&db_url).await?;
127115
let mut conn_b: SyncConnectionWrapper<InnerConnection> = establish(&db_url).await?;
128116

129-
tokio::spawn(async move {
117+
let handle_1 = tokio::spawn(async move {
130118
loop {
131119
let changed = transaction(&mut conn_a, "iLuke", "JustLuke").await;
132120
println!("Changed {changed:?}");
133121
std::thread::sleep(std::time::Duration::from_secs(1));
134122
}
135123
});
136124

137-
tokio::spawn(async move {
125+
let handle_2 = tokio::spawn(async move {
138126
loop {
139127
let changed = transaction(&mut conn_b, "JustLuke", "iLuke").await;
140128
println!("Changed {changed:?}");
141129
std::thread::sleep(std::time::Duration::from_secs(1));
142130
}
143131
});
144132

145-
loop {
146-
std::thread::sleep(std::time::Duration::from_secs(1));
147-
}
133+
let _ = handle_2.await;
134+
let _ = handle_1.await;
148135

149136
Ok(())
150137
}

0 commit comments

Comments
 (0)