PgConnection: use more obvious/safer query for ping()

This commit is contained in:
Austin Bonander 2020-06-05 19:58:01 -07:00 committed by Austin Bonander
parent a0d1106f90
commit e3dbd58bf2
2 changed files with 12 additions and 2 deletions

View File

@ -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)]

View File

@ -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::<Postgres>().await?;
conn.ping().await?;
Ok(())
}
#[sqlx_macros::test]
async fn it_maths() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;