From e3dbd58bf27555dc58627220e3d19c0252061118 Mon Sep 17 00:00:00 2001 From: Austin Bonander Date: Fri, 5 Jun 2020 19:58:01 -0700 Subject: [PATCH] PgConnection: use more obvious/safer query for `ping()` --- sqlx-core/src/postgres/connection/mod.rs | 3 ++- tests/postgres/postgres.rs | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/sqlx-core/src/postgres/connection/mod.rs b/sqlx-core/src/postgres/connection/mod.rs index 343de9df..a54bd7ab 100644 --- a/sqlx-core/src/postgres/connection/mod.rs +++ b/sqlx-core/src/postgres/connection/mod.rs @@ -117,7 +117,8 @@ impl Connection for PgConnection { } fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>> { - self.execute("SELECT 1").map_ok(|_| ()).boxed() + // By sending a comment we avoid an error if the connection was in the middle of a rowset + self.execute("/* SQLx ping */").map_ok(|_| ()).boxed() } #[doc(hidden)] diff --git a/tests/postgres/postgres.rs b/tests/postgres/postgres.rs index ec319697..4fe44686 100644 --- a/tests/postgres/postgres.rs +++ b/tests/postgres/postgres.rs @@ -1,6 +1,6 @@ use futures::TryStreamExt; use sqlx::postgres::PgRow; -use sqlx::{postgres::Postgres, Executor, Row}; +use sqlx::{postgres::Postgres, Connection, Executor, Row}; use sqlx_core::postgres::{PgDatabaseError, PgErrorPosition, PgSeverity}; use sqlx_test::new; @@ -18,6 +18,15 @@ async fn it_connects() -> anyhow::Result<()> { Ok(()) } +#[sqlx_macros::test] +async fn it_pings() -> anyhow::Result<()> { + let mut conn = new::().await?; + + conn.ping().await?; + + Ok(()) +} + #[sqlx_macros::test] async fn it_maths() -> anyhow::Result<()> { let mut conn = new::().await?;