Skip to content

Commit 587dcf5

Browse files
reivilibresandhose
authored andcommitted
syn2mas: disable logging of slow statements
1 parent 6ff9305 commit 587dcf5

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.
@@ -113,7 +113,13 @@ impl Options {
113113

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

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

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

crates/cli/src/util.rs

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

230230
fn database_connect_options_from_config(
231231
config: &DatabaseConfig,
232+
opts: &DatabaseConnectOptions,
232233
) -> Result<PgConnectOptions, anyhow::Error> {
233234
let options = if let Some(uri) = config.uri.as_deref() {
234235
uri.parse()
@@ -313,17 +314,19 @@ fn database_connect_options_from_config(
313314
None => options,
314315
};
315316

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

320323
Ok(options)
321324
}
322325

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

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

0 commit comments

Comments
 (0)