Skip to content

Commit 5302a6d

Browse files
reivilibresandhose
authored andcommitted
syn2mas: disable logging of slow statements
1 parent 95916cf commit 5302a6d

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::{postgres::PgConnectOptions, types::Uuid, Connection, Either, PgConnec
1515
use syn2mas::{synapse_config, LockedMasDatabase, MasWriter, SynapseReader};
1616
use tracing::{error, info_span, warn, Instrument};
1717

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

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

112112
let config = DatabaseConfig::extract_or_default(figment)?;
113113

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

116122
MIGRATOR
117123
.run(&mut mas_connection)
@@ -220,7 +226,15 @@ impl Options {
220226
let reader = SynapseReader::new(&mut syn_conn, true).await?;
221227
let mut writer_mas_connections = Vec::with_capacity(NUM_WRITER_CONNECTIONS);
222228
for _ in 0..NUM_WRITER_CONNECTIONS {
223-
writer_mas_connections.push(database_connection_from_config(&config).await?);
229+
writer_mas_connections.push(
230+
database_connection_from_config_with_options(
231+
&config,
232+
&DatabaseConnectOptions {
233+
log_slow_statements: false,
234+
},
235+
)
236+
.await?,
237+
);
224238
}
225239
let writer = MasWriter::new(mas_connection, writer_mas_connections).await?;
226240

crates/cli/src/util.rs

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

220220
fn database_connect_options_from_config(
221221
config: &DatabaseConfig,
222+
opts: &DatabaseConnectOptions,
222223
) -> Result<PgConnectOptions, anyhow::Error> {
223224
let options = if let Some(uri) = config.uri.as_deref() {
224225
uri.parse()
@@ -301,17 +302,19 @@ fn database_connect_options_from_config(
301302
None => options,
302303
};
303304

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

308311
Ok(options)
309312
}
310313

311314
/// Create a database connection pool from the configuration
312315
#[tracing::instrument(name = "db.connect", skip_all, err(Debug))]
313316
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())?;
315318
PgPoolOptions::new()
316319
.max_connections(config.max_connections.into())
317320
.min_connections(config.min_connections)
@@ -323,12 +326,37 @@ pub async fn database_pool_from_config(config: &DatabaseConfig) -> Result<PgPool
323326
.context("could not connect to the database")
324327
}
325328

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+
326341
/// Create a single database connection from the configuration
327342
#[tracing::instrument(name = "db.connect", skip_all, err(Debug))]
328343
pub async fn database_connection_from_config(
329344
config: &DatabaseConfig,
330345
) -> 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)?
332360
.connect()
333361
.await
334362
.context("could not connect to the database")

0 commit comments

Comments
 (0)