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