@@ -10,12 +10,13 @@ use sqlx::{ConnectOptions, Pool, Sqlite};
1010use std:: path:: { Path , PathBuf } ;
1111use std:: sync:: Arc ;
1212use std:: sync:: atomic:: { AtomicBool , Ordering } ;
13+ use tracing:: error;
1314
1415/// SQLite database with connection pooling for concurrent reads and optional exclusive writes.
1516///
16- /// The database is opened in read-write mode but can be used for read-only operations
17- /// by calling `read_pool()`. Write operations are available by calling `acquire_writer()`
18- /// which lazily initializes WAL mode on first use.
17+ /// Once the database is opened it can be used for read-only operations by calling `read_pool()`.
18+ /// Write operations are available by calling `acquire_writer()` which lazily initializes WAL mode
19+ /// on first use.
1920///
2021/// # Example
2122///
@@ -66,7 +67,7 @@ impl SqliteDatabase {
6667 /// If the database is already connected, returns the existing connection.
6768 /// Multiple calls with the same path will return the same database instance.
6869 ///
69- /// The database is created if it doesn't exist. WAL mode is optionally enabled when
70+ /// The database is created if it doesn't exist. WAL mode is enabled when
7071 /// `acquire_writer()` is first called.
7172 ///
7273 /// # Arguments
@@ -131,7 +132,8 @@ impl SqliteDatabase {
131132 // Why do we need to manually create the database file? We could just let the connection
132133 // create it if it doesn't exist, using `create_if_missing(true)`, right? Not if we called
133134 // connect and then our very first query was a read-only query, like `PRAGMA user_version;`,
134- // for example. That would fail because the read pool cannot create the file
135+ // for example. That would fail because the read pool connections are read-only and cannot
136+ // create the file
135137 if !db_exists && !is_memory_database ( & path) {
136138 let create_options = SqliteConnectOptions :: new ( )
137139 . filename ( & path)
@@ -174,7 +176,7 @@ impl SqliteDatabase {
174176 . await
175177 }
176178
177- /// Get a reference to the connection pool for executing SELECT queries
179+ /// Get a reference to the connection pool for executing read queries
178180 ///
179181 /// Use this for concurrent read operations. Multiple readers can access
180182 /// the pool simultaneously.
@@ -240,6 +242,11 @@ impl SqliteDatabase {
240242 . execute ( & mut * conn)
241243 . await ?;
242244
245+ // https://www.sqlite.org/wal.html#performance_considerations
246+ sqlx:: query ( "PRAGMA synchronous = NORMAL" )
247+ . execute ( & mut * conn)
248+ . await ?;
249+
243250 self . wal_initialized . store ( true , Ordering :: SeqCst ) ;
244251 }
245252
@@ -273,11 +280,7 @@ impl SqliteDatabase {
273280
274281 // Remove from registry
275282 if let Err ( e) = uncache_database ( & self . path ) . await {
276- // TODO: Investigate use of "tracing" crate to log this error
277- #[ cfg( debug_assertions) ]
278- eprintln ! ( "Failed to remove database from cache: {}" , e) ;
279- #[ cfg( not( debug_assertions) ) ]
280- let _ = e; // Suppress unused variable warning
283+ error ! ( "Failed to remove database from cache: {}" , e) ;
281284 }
282285
283286 // This will await all readers to be returned
@@ -370,6 +373,7 @@ mod tests {
370373 . fetch_one ( db. read_pool ( ) . unwrap ( ) )
371374 . await
372375 . unwrap ( ) ;
376+
373377 assert_eq ! ( count, 12 ) ;
374378 } ) ) ;
375379 }
@@ -564,6 +568,17 @@ mod tests {
564568 "Journal mode should be WAL after first acquire_writer"
565569 ) ;
566570
571+ // Check sync setting
572+ let ( sync, ) : ( i32 , ) = sqlx:: query_as ( "PRAGMA synchronous" )
573+ . fetch_one ( & mut * writer)
574+ . await
575+ . unwrap ( ) ;
576+
577+ assert_eq ! (
578+ sync, 1 ,
579+ "Sync mode should be NORMAL after first acquire_writer"
580+ ) ;
581+
567582 drop ( writer) ;
568583
569584 db. remove ( ) . await . unwrap ( ) ;
@@ -676,6 +691,7 @@ mod tests {
676691 . fetch_all ( db_clone. read_pool ( ) . unwrap ( ) )
677692 . await
678693 . unwrap ( ) ;
694+
679695 assert ! ( rows. len( ) > 0 ) ;
680696 } ) ) ;
681697 }
@@ -703,6 +719,7 @@ mod tests {
703719 . fetch_one ( db. read_pool ( ) . unwrap ( ) )
704720 . await
705721 . unwrap ( ) ;
722+
706723 assert_eq ! ( count. 0 , 2 ) ;
707724
708725 db. remove ( ) . await . unwrap ( ) ;
0 commit comments