Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose PostgreSQL Application Name Option Config #2143

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
use std::borrow::Cow;
use std::time::Duration;

mod connection;
mod db_connection;
#[cfg(feature = "mock")]
#[cfg_attr(docsrs, doc(cfg(feature = "mock")))]
mod mock;
#[cfg(feature = "proxy")]
#[cfg_attr(docsrs, doc(cfg(feature = "proxy")))]
mod proxy;
mod statement;
mod stream;
mod transaction;
use tracing::instrument;

pub use connection::*;
pub use db_connection::*;
Expand All @@ -21,13 +12,23 @@ pub use mock::*;
#[cfg_attr(docsrs, doc(cfg(feature = "proxy")))]
pub use proxy::*;
pub use statement::*;
use std::borrow::Cow;
pub use stream::*;
use tracing::instrument;
pub use transaction::*;

use crate::error::*;

mod connection;
mod db_connection;
#[cfg(feature = "mock")]
#[cfg_attr(docsrs, doc(cfg(feature = "mock")))]
mod mock;
#[cfg(feature = "proxy")]
#[cfg_attr(docsrs, doc(cfg(feature = "proxy")))]
mod proxy;
mod statement;
mod stream;
mod transaction;

/// Defines a database
#[derive(Debug, Default)]
pub struct Database;
Expand Down Expand Up @@ -63,6 +64,7 @@ pub struct ConnectOptions {
/// Schema search path (PostgreSQL only)
pub(crate) schema_search_path: Option<String>,
pub(crate) test_before_acquire: bool,
pub(crate) application_name: Option<&'static str>,
}

impl Database {
Expand Down Expand Up @@ -157,6 +159,7 @@ impl ConnectOptions {
sqlcipher_key: None,
schema_search_path: None,
test_before_acquire: true,
application_name: None,
}
}

Expand Down Expand Up @@ -297,4 +300,15 @@ impl ConnectOptions {
self.test_before_acquire = value;
self
}

/// Set the application name for the connection (PostgreSQL only)
pub fn application_name(&mut self, value: &'static str) -> &mut Self {
self.application_name = Some(value);
self
}

/// Get the application name for the connection (PostgreSQL only)
pub fn get_application_name(&self) -> Option<&'static str> {
self.application_name
}
}
8 changes: 8 additions & 0 deletions src/driver/sqlx_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ impl SqlxPostgresConnector {
.url
.parse::<PgConnectOptions>()
.map_err(sqlx_error_to_conn_err)?;

use sqlx::ConnectOptions;

if !options.sqlx_logging {
opt = opt.disable_statement_logging();
} else {
Expand All @@ -61,10 +63,16 @@ impl SqlxPostgresConnector {
);
}
}

if let Some(application_name) = options.application_name {
opt = opt.application_name(application_name);
}

let set_search_path_sql = options
.schema_search_path
.as_ref()
.map(|schema| format!("SET search_path = '{schema}'"));

let mut pool_options = options.sqlx_pool_options();
if let Some(sql) = set_search_path_sql {
pool_options = pool_options.after_connect(move |conn, _| {
Expand Down
Loading