Skip to content

Commit

Permalink
fix: remove dead references to DB features in sqlx-core
Browse files Browse the repository at this point in the history
This reactivates a couple of tests that had stopped working.
  • Loading branch information
abonander committed Jul 26, 2024
1 parent 5d97b68 commit 417835c
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 182 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,9 @@ required-features = ["postgres", "macros", "migrate"]
name = "postgres-migrate"
path = "tests/postgres/migrate.rs"
required-features = ["postgres", "macros", "migrate"]


[[test]]
name = "postgres-query-builder"
path = "tests/postgres/query_builder.rs"
required-features = ["postgres"]
73 changes: 21 additions & 52 deletions sqlx-core/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,28 @@
//! [`Pool::acquire`] or
//! [`Pool::begin`].
use self::inner::PoolInner;
#[cfg(all(
any(
feature = "postgres",
feature = "mysql",
feature = "mssql",
feature = "sqlite"
),
feature = "any"
))]
use crate::any::{Any, AnyKind};
use crate::connection::Connection;
use crate::database::Database;
use crate::error::Error;
use crate::transaction::Transaction;
use event_listener::EventListener;
use futures_core::FusedFuture;
use futures_util::FutureExt;
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::{Duration, Instant};

use event_listener::EventListener;
use futures_core::FusedFuture;
use futures_util::FutureExt;

use crate::connection::Connection;
use crate::database::Database;
use crate::error::Error;
use crate::transaction::Transaction;

pub use self::connection::PoolConnection;
use self::inner::PoolInner;
#[doc(hidden)]
pub use self::maybe::MaybePoolConnection;
pub use self::options::{PoolConnectionMetadata, PoolOptions};

#[macro_use]
mod executor;

Expand All @@ -89,12 +86,6 @@ mod connection;
mod inner;
mod options;

pub use self::connection::PoolConnection;
pub use self::options::{PoolConnectionMetadata, PoolOptions};

#[doc(hidden)]
pub use self::maybe::MaybePoolConnection;

/// An asynchronous pool of SQLx database connections.
///
/// Create a pool with [Pool::connect] or [Pool::connect_with] and then call [Pool::acquire]
Expand Down Expand Up @@ -434,8 +425,7 @@ impl<DB: Database> Pool<DB> {
///
/// Do something when the pool is closed:
/// ```rust,no_run
/// # #[cfg(feature = "postgres")]
/// # async fn bleh() -> sqlx_core::error::Result<()> {
/// # async fn bleh() -> sqlx::Result<()> {
/// use sqlx::PgPool;
///
/// let pool = PgPool::connect("postgresql://...").await?;
Expand Down Expand Up @@ -463,8 +453,7 @@ impl<DB: Database> Pool<DB> {
///
/// Cancel a long-running operation:
/// ```rust,no_run
/// # #[cfg(feature = "postgres")]
/// # async fn bleh() -> sqlx_core::error::Result<()> {
/// # async fn bleh() -> sqlx::Result<()> {
/// use sqlx::{Executor, PgPool};
///
/// let pool = PgPool::connect("postgresql://...").await?;
Expand All @@ -475,13 +464,15 @@ impl<DB: Database> Pool<DB> {
/// pool2.close_event().do_until(async {
/// // This statement normally won't return for 30 days!
/// // (Assuming the connection doesn't time out first, of course.)
/// pool2.execute("SELECT pg_sleep('30 days')").await;
/// pool2.execute("SELECT pg_sleep('30 days')").await?;
///
/// // If the pool is closed before the statement completes, this won't be printed.
/// // This is because `.do_until()` cancels the future it's given if the
/// // pool is closed first.
/// println!("Waited!");
/// }).await;
///
/// Ok(())
/// }).await
/// });
///
/// // This normally wouldn't return until the above statement completed and the connection
Expand Down Expand Up @@ -534,28 +525,6 @@ impl<DB: Database> Pool<DB> {
}
}

#[cfg(all(
any(
feature = "postgres",
feature = "mysql",
feature = "mssql",
feature = "sqlite"
),
feature = "any"
))]
impl Pool<Any> {
/// Returns the database driver currently in-use by this `Pool`.
///
/// Determined by the connection URL.
pub fn any_kind(&self) -> AnyKind {
self.0
.connect_options
.read()
.expect("write-lock holder panicked")
.kind()
}
}

/// Returns a new [Pool] tied to the same shared connection pool.
impl<DB: Database> Clone for Pool<DB> {
fn clone(&self) -> Self {
Expand Down
115 changes: 0 additions & 115 deletions sqlx-core/src/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,118 +593,3 @@ where
self
}
}

#[cfg(all(test, feature = "postgres"))]
mod test {
use crate::postgres::Postgres;

use super::*;

#[test]
fn test_new() {
let qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("SELECT * FROM users");
assert_eq!(qb.query, "SELECT * FROM users");
}

#[test]
fn test_push() {
let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("SELECT * FROM users");
let second_line = " WHERE last_name LIKE '[A-N]%;";
qb.push(second_line);

assert_eq!(
qb.query,
"SELECT * FROM users WHERE last_name LIKE '[A-N]%;".to_string(),
);
}

#[test]
#[should_panic]
fn test_push_panics_when_no_arguments() {
let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("SELECT * FROM users;");
qb.arguments = None;

qb.push("SELECT * FROM users;");
}

#[test]
fn test_push_bind() {
let mut qb: QueryBuilder<'_, Postgres> =
QueryBuilder::new("SELECT * FROM users WHERE id = ");

qb.push_bind(42i32)
.push(" OR membership_level = ")
.push_bind(3i32);

assert_eq!(
qb.query,
"SELECT * FROM users WHERE id = $1 OR membership_level = $2"
);
}

#[test]
fn test_build() {
let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("SELECT * FROM users");

qb.push(" WHERE id = ").push_bind(42i32);
let query = qb.build();

assert_eq!(
query.statement.unwrap_left(),
"SELECT * FROM users WHERE id = $1"
);
assert_eq!(query.persistent, true);
}

#[test]
fn test_reset() {
let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("");

let _query = qb
.push("SELECT * FROM users WHERE id = ")
.push_bind(42i32)
.build();

qb.reset();

assert_eq!(qb.query, "");
}

#[test]
fn test_query_builder_reuse() {
let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("");

let _query = qb
.push("SELECT * FROM users WHERE id = ")
.push_bind(42i32)
.build();

qb.reset();

let query = qb.push("SELECT * FROM users WHERE id = 99").build();

assert_eq!(
query.statement.unwrap_left(),
"SELECT * FROM users WHERE id = 99"
);
}

#[test]
fn test_query_builder_with_args() {
let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("");

let query = qb
.push("SELECT * FROM users WHERE id = ")
.push_bind(42i32)
.build();

let mut qb: QueryBuilder<'_, Postgres> =
QueryBuilder::new_with(query.sql(), query.take_arguments());
let query = qb.push("OR membership_level =").push_bind(3i32).build();

assert_eq!(
query.sql(),
"SELECT * FROM users WHERE id = $1 OR membership_level = $2"
);
}
}
7 changes: 4 additions & 3 deletions sqlx-core/src/testing/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,14 @@ fn foreign_key_depth(
}

#[test]
#[cfg(feature = "postgres")]
#[cfg(feature = "any")]
fn test_additive_fixture() -> Result<()> {
use crate::postgres::Postgres;
// Just need something that implements `Database`
use crate::any::Any;

let mut snapshot = FixtureSnapshot {
tables: BTreeMap::new(),
db: PhantomData::<Postgres>,
db: PhantomData::<Any>,
};

snapshot.tables.insert(
Expand Down
12 changes: 0 additions & 12 deletions tests/any/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,6 @@ async fn test_pool_callbacks() -> anyhow::Result<()> {

let conn_options: AnyConnectOptions = std::env::var("DATABASE_URL")?.parse()?;

#[cfg(feature = "mssql")]
if conn_options.kind() == sqlx::any::AnyKind::Mssql {
// MSSQL doesn't support `CREATE TEMPORARY TABLE`,
// because why follow conventions when you can subvert them?
// Instead, you prepend `#` to the table name for a session-local temporary table
// which you also have to do when referencing it.

// Since that affects basically every query here,
// it's just easier to have a separate MSSQL-specific test case.
return Ok(());
}

let current_id = AtomicI32::new(0);

let pool = AnyPoolOptions::new()
Expand Down
Loading

0 comments on commit 417835c

Please sign in to comment.