@@ -219,6 +219,7 @@ pub async fn templates_from_config(
219
219
220
220
fn database_connect_options_from_config (
221
221
config : & DatabaseConfig ,
222
+ opts : & DatabaseConnectOptions ,
222
223
) -> Result < PgConnectOptions , anyhow:: Error > {
223
224
let options = if let Some ( uri) = config. uri . as_deref ( ) {
224
225
uri. parse ( )
@@ -301,17 +302,19 @@ fn database_connect_options_from_config(
301
302
None => options,
302
303
} ;
303
304
304
- let options = options
305
- . log_statements ( LevelFilter :: Debug )
306
- . log_slow_statements ( LevelFilter :: Warn , Duration :: from_millis ( 100 ) ) ;
305
+ let mut options = options. log_statements ( LevelFilter :: Debug ) ;
306
+
307
+ if opts. log_slow_statements {
308
+ options = options. log_slow_statements ( LevelFilter :: Warn , Duration :: from_millis ( 100 ) ) ;
309
+ }
307
310
308
311
Ok ( options)
309
312
}
310
313
311
314
/// Create a database connection pool from the configuration
312
315
#[ tracing:: instrument( name = "db.connect" , skip_all, err( Debug ) ) ]
313
316
pub async fn database_pool_from_config ( config : & DatabaseConfig ) -> Result < PgPool , anyhow:: Error > {
314
- let options = database_connect_options_from_config ( config) ?;
317
+ let options = database_connect_options_from_config ( config, & DatabaseConnectOptions :: default ( ) ) ?;
315
318
PgPoolOptions :: new ( )
316
319
. max_connections ( config. max_connections . into ( ) )
317
320
. min_connections ( config. min_connections )
@@ -323,12 +326,37 @@ pub async fn database_pool_from_config(config: &DatabaseConfig) -> Result<PgPool
323
326
. context ( "could not connect to the database" )
324
327
}
325
328
329
+ pub struct DatabaseConnectOptions {
330
+ pub log_slow_statements : bool ,
331
+ }
332
+
333
+ impl Default for DatabaseConnectOptions {
334
+ fn default ( ) -> Self {
335
+ Self {
336
+ log_slow_statements : true ,
337
+ }
338
+ }
339
+ }
340
+
326
341
/// Create a single database connection from the configuration
327
342
#[ tracing:: instrument( name = "db.connect" , skip_all, err( Debug ) ) ]
328
343
pub async fn database_connection_from_config (
329
344
config : & DatabaseConfig ,
330
345
) -> Result < PgConnection , anyhow:: Error > {
331
- database_connect_options_from_config ( config) ?
346
+ database_connect_options_from_config ( config, & DatabaseConnectOptions :: default ( ) ) ?
347
+ . connect ( )
348
+ . await
349
+ . context ( "could not connect to the database" )
350
+ }
351
+
352
+ /// Create a single database connection from the configuration,
353
+ /// with specific options.
354
+ #[ tracing:: instrument( name = "db.connect" , skip_all, err( Debug ) ) ]
355
+ pub async fn database_connection_from_config_with_options (
356
+ config : & DatabaseConfig ,
357
+ options : & DatabaseConnectOptions ,
358
+ ) -> Result < PgConnection , anyhow:: Error > {
359
+ database_connect_options_from_config ( config, options) ?
332
360
. connect ( )
333
361
. await
334
362
. context ( "could not connect to the database" )
0 commit comments