Skip to content
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
5 changes: 4 additions & 1 deletion sqlx-postgres/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,10 @@ impl PgConnectOptions {
pub(crate) fn fetch_socket(&self) -> Option<String> {
match self.socket {
Some(ref socket) => {
let full_path = format!("{}/.s.PGSQL.{}", socket.display(), self.port);
let mut full_path = format!("{}/.s.PGSQL.{}", socket.display(), self.port);
if full_path.starts_with("@") {
full_path.replace_range(0..1, "\0")
}
Some(full_path)
}
None if self.host.starts_with('/') => {
Expand Down
14 changes: 12 additions & 2 deletions sqlx-postgres/src/options/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ impl PgConnectOptions {
if let Some(host) = url.host_str() {
let host_decoded = percent_decode_str(host);
options = match host_decoded.clone().next() {
Some(b'/') => options.socket(&*host_decoded.decode_utf8().map_err(Error::config)?),
Some(b'/') | Some(b'@') => {
options.socket(&*host_decoded.decode_utf8().map_err(Error::config)?)
}
_ => options.host(host),
}
}
Expand Down Expand Up @@ -67,7 +69,7 @@ impl PgConnectOptions {
}

"host" => {
if value.starts_with('/') {
if value.starts_with('/') || value.starts_with('@') {
options = options.socket(&*value);
} else {
options = options.host(&value);
Expand Down Expand Up @@ -188,6 +190,14 @@ fn it_parses_socket_correctly_from_parameter() {
assert_eq!(Some("/var/run/postgres/".into()), opts.socket);
}

#[test]
fn it_parses_abstract_socket_correctly_from_parameter() {
let url = "postgres:///?host=@pgdata";
let opts = PgConnectOptions::from_str(url).unwrap();

assert_eq!(Some("@pgdata".into()), opts.socket);
}

#[test]
fn it_parses_host_correctly_from_parameter() {
let url = "postgres:///?host=google.database.com";
Expand Down
17 changes: 17 additions & 0 deletions tests/postgres/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ async fn it_connects() -> anyhow::Result<()> {
Ok(())
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[ignore = "requires a PostgreSQL instance listening on the abstract Unix socket @pg_data"]
#[sqlx_macros::test]
async fn it_connects_via_abstract_socket() -> anyhow::Result<()> {
setup_if_needed();

let opts: PgConnectOptions = env::var("DATABASE_URL")?.parse().unwrap();
let opts = opts.socket("@pg_data");

let mut conn = PgConnection::connect_with(&opts).await?;

let value: i32 = sqlx::query_scalar("SELECT 1").fetch_one(&mut conn).await?;
assert_eq!(1, value);

Ok(())
}

#[sqlx_macros::test]
async fn it_can_select_void() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
Expand Down
Loading