@@ -76,6 +76,9 @@ pub struct Database {
7676}
7777
7878impl Database {
79+ /// Opens the database at the default path inside [`WORK_DIR`].
80+ // - Checks for legacy database filenames and renames if found.
81+ // - Delegates to `Database::new` for pool setup and migrations.
7982 pub fn open ( ) -> Fallible < Self > {
8083 let path = WORK_DIR . join ( DATABASE_PATH ) ;
8184 if !path. exists ( ) {
@@ -100,6 +103,7 @@ impl Database {
100103 Database :: new ( SqliteConnectionManager { file : path } , None )
101104 }
102105
106+ /// Opens or creates a database at the given filesystem path.
103107 pub fn open_at ( path : & Path ) -> Fallible < Self > {
104108 std:: fs:: create_dir_all ( & * WORK_DIR ) ?;
105109 Database :: new (
@@ -121,6 +125,11 @@ impl Database {
121125 )
122126 }
123127
128+ /// Builds the connection pool, configures WAL mode, and runs pending migrations.
129+ // - Creates an r2d2 pool with up to 20 connections (covers all production threads).
130+ // - Enables WAL journal mode for concurrent reads during writes.
131+ // - Sets synchronous=NORMAL for better performance (safe under WAL).
132+ // - Runs all pending schema migrations.
124133 fn new ( conn : SqliteConnectionManager , tempfile : Option < NamedTempFile > ) -> Fallible < Self > {
125134 let pool = Pool :: builder ( )
126135 // By inspection we have 13 threads in production, so make sure each of them can get a
@@ -161,6 +170,7 @@ impl Database {
161170 } )
162171 }
163172
173+ /// Runs a closure inside a database transaction, committing on success or rolling back on error.
164174 pub fn transaction < T , F : FnOnce ( & TransactionHandle ) -> Fallible < T > > (
165175 & self ,
166176 will_write : bool ,
@@ -194,11 +204,13 @@ pub struct TransactionHandle<'a> {
194204}
195205
196206impl TransactionHandle < ' _ > {
207+ /// Commits the transaction, persisting all changes.
197208 pub fn commit ( self ) -> Fallible < ( ) > {
198209 self . transaction . commit ( ) ?;
199210 Ok ( ( ) )
200211 }
201212
213+ /// Rolls the transaction back, discarding all changes.
202214 pub fn rollback ( self ) -> Fallible < ( ) > {
203215 self . transaction . rollback ( ) ?;
204216 Ok ( ( ) )
@@ -207,8 +219,10 @@ impl TransactionHandle<'_> {
207219
208220/// Convenience methods for executing SQL queries against the database.
209221pub trait QueryUtils {
222+ /// Acquires a connection and passes it to the closure.
210223 fn with_conn < T , F : FnOnce ( & Connection ) -> Fallible < T > > ( & self , f : F ) -> Fallible < T > ;
211224
225+ /// Returns `true` if the query matches at least one row.
212226 fn exists ( & self , sql : & str , params : & [ & dyn ToSql ] ) -> Fallible < bool > {
213227 self . with_conn ( |conn| {
214228 self . trace ( sql, || {
@@ -218,6 +232,7 @@ pub trait QueryUtils {
218232 } )
219233 }
220234
235+ /// Executes a statement and returns the number of rows changed.
221236 fn execute ( & self , sql : & str , params : & [ & dyn ToSql ] ) -> Fallible < usize > {
222237 self . with_conn ( |conn| {
223238 self . trace ( sql, || {
@@ -228,6 +243,7 @@ pub trait QueryUtils {
228243 } )
229244 }
230245
246+ /// Like [`execute`](Self::execute), but uses a prepared-statement cache.
231247 fn execute_cached ( & self , sql : & str , params : & [ & dyn ToSql ] ) -> Fallible < usize > {
232248 self . with_conn ( |conn| {
233249 self . trace ( sql, || {
@@ -238,6 +254,7 @@ pub trait QueryUtils {
238254 } )
239255 }
240256
257+ /// Returns the first row of a query, or `None` if the result set is empty.
241258 fn get_row < T , P > (
242259 & self ,
243260 sql : & str ,
@@ -261,6 +278,7 @@ pub trait QueryUtils {
261278 } )
262279 }
263280
281+ /// Executes a query and collects all rows into a `Vec`.
264282 fn query < T , F : FnMut ( & Row ) -> rusqlite:: Result < T > > (
265283 & self ,
266284 sql : & str ,
@@ -282,6 +300,7 @@ pub trait QueryUtils {
282300 } )
283301 }
284302
303+ /// Returns the first row mapped through a fallible closure, or `None`.
285304 fn query_row < T , F : FnOnce ( & Row ) -> Fallible < T > > (
286305 & self ,
287306 sql : & str ,
@@ -300,6 +319,7 @@ pub trait QueryUtils {
300319 } )
301320 }
302321
322+ /// Runs a closure and logs the SQL statement if it takes longer than 500 ms.
303323 fn trace < T , F : FnOnce ( ) -> T > ( & self , sql : & str , f : F ) -> T {
304324 let start = Instant :: now ( ) ;
305325 let res = f ( ) ;
0 commit comments