@@ -231,6 +231,7 @@ pub async fn templates_from_config(
231
231
232
232
fn database_connect_options_from_config (
233
233
config : & DatabaseConfig ,
234
+ opts : & DatabaseConnectOptions ,
234
235
) -> Result < PgConnectOptions , anyhow:: Error > {
235
236
let options = if let Some ( uri) = config. uri . as_deref ( ) {
236
237
uri. parse ( )
@@ -315,17 +316,19 @@ fn database_connect_options_from_config(
315
316
None => options,
316
317
} ;
317
318
318
- let options = options
319
- . log_statements ( LevelFilter :: Debug )
320
- . log_slow_statements ( LevelFilter :: Warn , Duration :: from_millis ( 100 ) ) ;
319
+ let mut options = options. log_statements ( LevelFilter :: Debug ) ;
320
+
321
+ if opts. log_slow_statements {
322
+ options = options. log_slow_statements ( LevelFilter :: Warn , Duration :: from_millis ( 100 ) ) ;
323
+ }
321
324
322
325
Ok ( options)
323
326
}
324
327
325
328
/// Create a database connection pool from the configuration
326
329
#[ tracing:: instrument( name = "db.connect" , skip_all, err( Debug ) ) ]
327
330
pub async fn database_pool_from_config ( config : & DatabaseConfig ) -> Result < PgPool , anyhow:: Error > {
328
- let options = database_connect_options_from_config ( config) ?;
331
+ let options = database_connect_options_from_config ( config, & DatabaseConnectOptions :: default ( ) ) ?;
329
332
PgPoolOptions :: new ( )
330
333
. max_connections ( config. max_connections . into ( ) )
331
334
. min_connections ( config. min_connections )
@@ -337,12 +340,37 @@ pub async fn database_pool_from_config(config: &DatabaseConfig) -> Result<PgPool
337
340
. context ( "could not connect to the database" )
338
341
}
339
342
343
+ pub struct DatabaseConnectOptions {
344
+ pub log_slow_statements : bool ,
345
+ }
346
+
347
+ impl Default for DatabaseConnectOptions {
348
+ fn default ( ) -> Self {
349
+ Self {
350
+ log_slow_statements : true ,
351
+ }
352
+ }
353
+ }
354
+
340
355
/// Create a single database connection from the configuration
341
356
#[ tracing:: instrument( name = "db.connect" , skip_all, err( Debug ) ) ]
342
357
pub async fn database_connection_from_config (
343
358
config : & DatabaseConfig ,
344
359
) -> Result < PgConnection , anyhow:: Error > {
345
- database_connect_options_from_config ( config) ?
360
+ database_connect_options_from_config ( config, & DatabaseConnectOptions :: default ( ) ) ?
361
+ . connect ( )
362
+ . await
363
+ . context ( "could not connect to the database" )
364
+ }
365
+
366
+ /// Create a single database connection from the configuration,
367
+ /// with specific options.
368
+ #[ tracing:: instrument( name = "db.connect" , skip_all, err( Debug ) ) ]
369
+ pub async fn database_connection_from_config_with_options (
370
+ config : & DatabaseConfig ,
371
+ options : & DatabaseConnectOptions ,
372
+ ) -> Result < PgConnection , anyhow:: Error > {
373
+ database_connect_options_from_config ( config, options) ?
346
374
. connect ( )
347
375
. await
348
376
. context ( "could not connect to the database" )
0 commit comments