@@ -3,6 +3,7 @@ use rusqlite::config::DbConfig;
3
3
use rusqlite:: { Connection , Transaction , TransactionBehavior } ;
4
4
use std:: ops:: Deref ;
5
5
use std:: path:: { Path , PathBuf } ;
6
+ use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
6
7
use std:: sync:: { Arc , Mutex } ;
7
8
use std:: time:: Duration ;
8
9
@@ -79,7 +80,12 @@ pub struct InnerConnections {
79
80
db_file : PathBuf ,
80
81
}
81
82
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
+
82
87
impl Connections {
88
+ /// Create a new db connection pool using the given db file
83
89
pub fn new ( db_file : impl AsRef < Path > ) -> Self {
84
90
Self {
85
91
inner : Arc :: new ( InnerConnections {
@@ -89,6 +95,18 @@ impl Connections {
89
95
}
90
96
}
91
97
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
+
92
110
/// Start a new write (immediate) transaction. If doing writes, it is important to use this
93
111
/// instead of `.read()` because here the busy timeout / busy handler actually works as it is
94
112
/// applied before the transaction starts.
0 commit comments