@@ -3,6 +3,7 @@ use rusqlite::config::DbConfig;
33use rusqlite:: { Connection , Transaction , TransactionBehavior } ;
44use std:: ops:: Deref ;
55use std:: path:: { Path , PathBuf } ;
6+ use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
67use std:: sync:: { Arc , Mutex } ;
78use std:: time:: Duration ;
89
@@ -79,7 +80,12 @@ pub struct InnerConnections {
7980 db_file : PathBuf ,
8081}
8182
83+ /// Increased whenever new_in_memory is called. Makes sure that the test binary can run multiple
84+ /// tests in parallel with distinct in memory db instances (otherwise they would clash)
85+ static MEMORY_COUNTER : AtomicU64 = AtomicU64 :: new ( 0 ) ;
86+
8287impl Connections {
88+ /// Create a new db connection pool using the given db file
8389 pub fn new ( db_file : impl AsRef < Path > ) -> Self {
8490 Self {
8591 inner : Arc :: new ( InnerConnections {
@@ -89,6 +95,18 @@ impl Connections {
8995 }
9096 }
9197
98+ /// Create a new db connection pool using an in memory db
99+ pub fn new_in_memory ( ) -> Self {
100+ let count = MEMORY_COUNTER . fetch_add ( 1 , Ordering :: Relaxed ) ;
101+
102+ Self {
103+ inner : Arc :: new ( InnerConnections {
104+ conns : Mutex :: new ( vec ! [ ] ) ,
105+ db_file : format ! ( "file:memdb{count}?mode=memory&cache=shared" ) . into ( ) ,
106+ } ) ,
107+ }
108+ }
109+
92110 /// Start a new write (immediate) transaction. If doing writes, it is important to use this
93111 /// instead of `.read()` because here the busy timeout / busy handler actually works as it is
94112 /// applied before the transaction starts.
0 commit comments