@@ -83,6 +83,7 @@ macro_rules! async_test {
83
83
async_test ! ( test_journal_mode) ;
84
84
async_test ! ( test_concurrency) ;
85
85
async_test ! ( test_pool) ;
86
+ async_test ! ( test_pool_conn_for_each) ;
86
87
87
88
async fn test_journal_mode ( ) {
88
89
let tmp_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
@@ -166,3 +167,63 @@ async fn test_pool() {
166
167
. collect :: < Result < ( ) , Error > > ( )
167
168
. expect ( "collecting query results" ) ;
168
169
}
170
+
171
+ async fn test_pool_conn_for_each ( ) {
172
+ // make dummy db
173
+ let tmp_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
174
+ {
175
+ let client = ClientBuilder :: new ( )
176
+ . journal_mode ( JournalMode :: Wal )
177
+ . path ( tmp_dir. path ( ) . join ( "sqlite.db" ) )
178
+ . open_blocking ( )
179
+ . expect ( "client unable to be opened" ) ;
180
+
181
+ client
182
+ . conn_blocking ( |conn| {
183
+ conn. execute (
184
+ "CREATE TABLE testing (id INTEGER PRIMARY KEY, val TEXT NOT NULL)" ,
185
+ ( ) ,
186
+ ) ?;
187
+ conn. execute ( "INSERT INTO testing VALUES (1, ?)" , [ "value1" ] )
188
+ } )
189
+ . expect ( "writing schema and seed data" ) ;
190
+ }
191
+
192
+ let pool = PoolBuilder :: new ( )
193
+ . path ( tmp_dir. path ( ) . join ( "another-sqlite.db" ) )
194
+ . num_conns ( 2 )
195
+ . open ( )
196
+ . await
197
+ . expect ( "pool unable to be opened" ) ;
198
+
199
+ let dummy_db_path = tmp_dir. path ( ) . join ( "sqlite.db" ) ;
200
+ let attach_fn = move |conn : & rusqlite:: Connection | {
201
+ conn. execute (
202
+ "ATTACH DATABASE ? AS dummy" ,
203
+ [ dummy_db_path. to_str ( ) . unwrap ( ) ] ,
204
+ )
205
+ } ;
206
+ // attach to the dummy db via conn_for_each
207
+ pool. conn_for_each ( attach_fn) . await ;
208
+
209
+ // check that the dummy db is attached
210
+ fn check_fn ( conn : & rusqlite:: Connection ) -> Result < Vec < String > , rusqlite:: Error > {
211
+ let mut stmt = conn
212
+ . prepare_cached ( "SELECT name FROM dummy.sqlite_master WHERE type='table'" )
213
+ . unwrap ( ) ;
214
+ let names = stmt
215
+ . query_map ( [ ] , |row| row. get ( 0 ) )
216
+ . unwrap ( )
217
+ . map ( |r| r. unwrap ( ) )
218
+ . collect :: < Vec < String > > ( ) ;
219
+
220
+ Ok ( names)
221
+ }
222
+ let res = pool. conn_for_each ( check_fn) . await ;
223
+ for r in res {
224
+ assert_eq ! ( r. unwrap( ) , vec![ "testing" ] ) ;
225
+ }
226
+
227
+ // cleanup
228
+ pool. close ( ) . await . expect ( "closing client conn" ) ;
229
+ }
0 commit comments