@@ -2,9 +2,8 @@ use diesel::prelude::*;
2
2
use diesel:: sqlite:: { Sqlite , SqliteConnection } ;
3
3
use diesel_async:: async_connection_wrapper:: AsyncConnectionWrapper ;
4
4
use diesel_async:: sync_connection_wrapper:: SyncConnectionWrapper ;
5
- use diesel_async:: { AsyncConnection , RunQueryDsl , SimpleAsyncConnection } ;
5
+ use diesel_async:: { AsyncConnection , RunQueryDsl } ;
6
6
use diesel_migrations:: { embed_migrations, EmbeddedMigrations , MigrationHarness } ;
7
- use futures_util:: FutureExt ;
8
7
9
8
// ordinary diesel model setup
10
9
@@ -59,21 +58,10 @@ async fn transaction(
59
58
if old_name. is_empty ( ) {
60
59
Ok ( Vec :: new ( ) )
61
60
} 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
77
65
}
78
66
} )
79
67
} )
@@ -90,10 +78,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
90
78
91
79
let mut sync_wrapper: SyncConnectionWrapper < InnerConnection > = establish ( & db_url) . await ?;
92
80
93
- sync_wrapper. batch_execute ( "DELETE FROM users" ) . await ?;
81
+ diesel:: delete ( users:: table)
82
+ . execute ( & mut sync_wrapper)
83
+ . await ?;
94
84
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)
97
88
. await ?;
98
89
99
90
let data: Vec < User > = users:: table
@@ -119,32 +110,28 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
119
110
. await ?;
120
111
println ! ( "{data:?}" ) ;
121
112
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
126
114
let mut conn_a: SyncConnectionWrapper < InnerConnection > = establish ( & db_url) . await ?;
127
115
let mut conn_b: SyncConnectionWrapper < InnerConnection > = establish ( & db_url) . await ?;
128
116
129
- tokio:: spawn ( async move {
117
+ let handle_1 = tokio:: spawn ( async move {
130
118
loop {
131
119
let changed = transaction ( & mut conn_a, "iLuke" , "JustLuke" ) . await ;
132
120
println ! ( "Changed {changed:?}" ) ;
133
121
std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) ;
134
122
}
135
123
} ) ;
136
124
137
- tokio:: spawn ( async move {
125
+ let handle_2 = tokio:: spawn ( async move {
138
126
loop {
139
127
let changed = transaction ( & mut conn_b, "JustLuke" , "iLuke" ) . await ;
140
128
println ! ( "Changed {changed:?}" ) ;
141
129
std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) ;
142
130
}
143
131
} ) ;
144
132
145
- loop {
146
- std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) ;
147
- }
133
+ let _ = handle_2. await ;
134
+ let _ = handle_1. await ;
148
135
149
136
Ok ( ( ) )
150
137
}
0 commit comments