Skip to content

Commit f5b17d9

Browse files
committed
syn2mas: disable logging of slow statements
1 parent 2ae1523 commit f5b17d9

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

crates/cli/src/commands/syn2mas.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use sqlx::{Connection, Either, PgConnection, postgres::PgConnectOptions, types::
1515
use syn2mas::{LockedMasDatabase, MasWriter, SynapseReader, synapse_config};
1616
use tracing::{Instrument, error, info_span, warn};
1717

18-
use crate::util::database_connection_from_config;
18+
use crate::util::{DatabaseConnectOptions, database_connection_from_config_with_options};
1919

2020
/// The exit code used by `syn2mas check` and `syn2mas migrate` when there are
2121
/// errors preventing migration.
@@ -114,7 +114,13 @@ impl Options {
114114

115115
let config = DatabaseConfig::extract_or_default(figment)?;
116116

117-
let mut mas_connection = database_connection_from_config(&config).await?;
117+
let mut mas_connection = database_connection_from_config_with_options(
118+
&config,
119+
&DatabaseConnectOptions {
120+
log_slow_statements: false,
121+
},
122+
)
123+
.await?;
118124

119125
MIGRATOR
120126
.run(&mut mas_connection)
@@ -225,7 +231,15 @@ impl Options {
225231
let reader = SynapseReader::new(&mut syn_conn, true).await?;
226232
let mut writer_mas_connections = Vec::with_capacity(NUM_WRITER_CONNECTIONS);
227233
for _ in 0..NUM_WRITER_CONNECTIONS {
228-
writer_mas_connections.push(database_connection_from_config(&config).await?);
234+
writer_mas_connections.push(
235+
database_connection_from_config_with_options(
236+
&config,
237+
&DatabaseConnectOptions {
238+
log_slow_statements: false,
239+
},
240+
)
241+
.await?,
242+
);
229243
}
230244
let writer = MasWriter::new(mas_connection, writer_mas_connections).await?;
231245

crates/cli/src/util.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ pub async fn templates_from_config(
231231

232232
fn database_connect_options_from_config(
233233
config: &DatabaseConfig,
234+
opts: &DatabaseConnectOptions,
234235
) -> Result<PgConnectOptions, anyhow::Error> {
235236
let options = if let Some(uri) = config.uri.as_deref() {
236237
uri.parse()
@@ -315,17 +316,19 @@ fn database_connect_options_from_config(
315316
None => options,
316317
};
317318

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+
}
321324

322325
Ok(options)
323326
}
324327

325328
/// Create a database connection pool from the configuration
326329
#[tracing::instrument(name = "db.connect", skip_all, err(Debug))]
327330
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())?;
329332
PgPoolOptions::new()
330333
.max_connections(config.max_connections.into())
331334
.min_connections(config.min_connections)
@@ -337,12 +340,37 @@ pub async fn database_pool_from_config(config: &DatabaseConfig) -> Result<PgPool
337340
.context("could not connect to the database")
338341
}
339342

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+
340355
/// Create a single database connection from the configuration
341356
#[tracing::instrument(name = "db.connect", skip_all, err(Debug))]
342357
pub async fn database_connection_from_config(
343358
config: &DatabaseConfig,
344359
) -> 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)?
346374
.connect()
347375
.await
348376
.context("could not connect to the database")

0 commit comments

Comments
 (0)