Skip to content

Commit

Permalink
query-engine-c-abi: Remove openssl dependency (#4840)
Browse files Browse the repository at this point in the history
* query-engine-c-abi: Remove openssl dependency

Native drivers are not properly excluded in c-abi build: `quaint`'s
`native` feature, pulled through `request-handlers` -> `sql-connector`
chain pulls all drivers in. In turn, `tiberius` brings openssl
requirement with it and that requires custom build for Android.

This PR ensures that tiberus is properly excluded from quaint
dependencies when building for native sqlite only. Unfortunately, that
means that `native-*` features now propogate to `sql-query-connector`
and `request-handlers` crates as well.

* Use cfg_aliases in quaint

* Use cfg_aliases in request-handlers
  • Loading branch information
SevInf committed Apr 25, 2024
1 parent 5d2f494 commit 031bd63
Show file tree
Hide file tree
Showing 36 changed files with 226 additions and 202 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codspeed.yml
Expand Up @@ -31,7 +31,7 @@ jobs:
run: cargo codspeed build -p schema --features all_connectors

- name: "Build the benchmark targets: request-handlers"
run: cargo codspeed build -p request-handlers --features native,all
run: cargo codspeed build -p request-handlers --features all

- name: Run the benchmarks
uses: CodSpeedHQ/action@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-quaint.yml
Expand Up @@ -16,7 +16,7 @@ jobs:
fail-fast: false
matrix:
features:
- "--lib --features=all"
- "--lib --features=all-native"
env:
TEST_MYSQL: "mysql://root:prisma@localhost:3306/prisma"
TEST_MYSQL8: "mysql://root:prisma@localhost:3307/prisma"
Expand Down
16 changes: 15 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions libs/user-facing-errors/Cargo.toml
Expand Up @@ -16,3 +16,13 @@ quaint = { path = "../../quaint", default-features = false, optional = true }
[features]
default = []
sql = ["quaint"]
all-native = [
"postgresql-native",
"mssql-native",
"mysql-native",
"sqlite-native",
]
postgresql-native = ["quaint/postgresql-native"]
mssql-native = ["quaint/mssql-native"]
mysql-native = ["quaint/mysql-native"]
sqlite-native = ["quaint/sqlite-native"]
40 changes: 32 additions & 8 deletions libs/user-facing-errors/src/quaint.rs
Expand Up @@ -2,7 +2,12 @@ use crate::{common, query_engine, KnownError};
use indoc::formatdoc;
use quaint::{error::ErrorKind, prelude::ConnectionInfo};

#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(
feature = "mssql-native",
feature = "mysql-native",
feature = "postgresql-native",
feature = "sqlite-native"
))]
use quaint::{connector::NativeConnectionInfo, error::NativeErrorKind};

impl From<&quaint::error::DatabaseConstraint> for query_engine::DatabaseConstraint {
Expand Down Expand Up @@ -43,22 +48,26 @@ pub fn render_quaint_error(kind: &ErrorKind, connection_info: &ConnectionInfo) -

match (kind, connection_info) {
(ErrorKind::DatabaseDoesNotExist { .. }, ConnectionInfo::External(_)) => default_value,
#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(feature = "mssql-native", feature = "mysql-native", feature = "postgresql-native"))]
#[allow(unused_variables)]
(ErrorKind::DatabaseDoesNotExist { db_name }, _) => match connection_info {
#[cfg(feature = "postgresql-native")]
ConnectionInfo::Native(NativeConnectionInfo::Postgres(url)) => {
Some(KnownError::new(common::DatabaseDoesNotExist::Postgres {
database_name: db_name.to_string(),
database_host: url.host().to_owned(),
database_port: url.port(),
}))
}
#[cfg(feature = "mysql-native")]
ConnectionInfo::Native(NativeConnectionInfo::Mysql(url)) => {
Some(KnownError::new(common::DatabaseDoesNotExist::Mysql {
database_name: url.dbname().to_owned(),
database_host: url.host().to_owned(),
database_port: url.port(),
}))
}
#[cfg(feature = "mssql-native")]
ConnectionInfo::Native(NativeConnectionInfo::Mssql(url)) => {
Some(KnownError::new(common::DatabaseDoesNotExist::Mssql {
database_name: url.dbname().to_owned(),
Expand All @@ -70,7 +79,7 @@ pub fn render_quaint_error(kind: &ErrorKind, connection_info: &ConnectionInfo) -
},

(ErrorKind::DatabaseAccessDenied { .. }, ConnectionInfo::External(_)) => default_value,
#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(feature = "mysql-native", feature = "postgresql-native"))]
(ErrorKind::DatabaseAccessDenied { .. }, _) => match connection_info {
ConnectionInfo::Native(NativeConnectionInfo::Postgres(url)) => {
Some(KnownError::new(common::DatabaseAccessDenied {
Expand All @@ -88,15 +97,17 @@ pub fn render_quaint_error(kind: &ErrorKind, connection_info: &ConnectionInfo) -
},

(ErrorKind::DatabaseAlreadyExists { .. }, ConnectionInfo::External(_)) => default_value,
#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(feature = "mysql-native", feature = "postgresql-native"))]
(ErrorKind::DatabaseAlreadyExists { db_name }, _) => match connection_info {
#[cfg(feature = "postgresql-native")]
ConnectionInfo::Native(NativeConnectionInfo::Postgres(url)) => {
Some(KnownError::new(common::DatabaseAlreadyExists {
database_name: format!("{db_name}"),
database_host: url.host().to_owned(),
database_port: url.port(),
}))
}
#[cfg(feature = "mysql-native")]
ConnectionInfo::Native(NativeConnectionInfo::Mysql(url)) => {
Some(KnownError::new(common::DatabaseAlreadyExists {
database_name: format!("{db_name}"),
Expand All @@ -108,7 +119,7 @@ pub fn render_quaint_error(kind: &ErrorKind, connection_info: &ConnectionInfo) -
},

(ErrorKind::AuthenticationFailed { .. }, ConnectionInfo::External(_)) => default_value,
#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(feature = "mysql-native", feature = "postgresql-native"))]
(ErrorKind::AuthenticationFailed { user }, _) => match connection_info {
ConnectionInfo::Native(NativeConnectionInfo::Postgres(url)) => {
Some(KnownError::new(common::IncorrectDatabaseCredentials {
Expand All @@ -126,7 +137,7 @@ pub fn render_quaint_error(kind: &ErrorKind, connection_info: &ConnectionInfo) -
},

(ErrorKind::SocketTimeout { .. }, ConnectionInfo::External(_)) => default_value,
#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(feature = "mssql-native", feature = "mysql-native", feature = "postgresql-native"))]
(ErrorKind::SocketTimeout, _) => match connection_info {
ConnectionInfo::Native(NativeConnectionInfo::Postgres(url)) => {
let time = match url.socket_timeout() {
Expand Down Expand Up @@ -168,12 +179,14 @@ pub fn render_quaint_error(kind: &ErrorKind, connection_info: &ConnectionInfo) -
},

(ErrorKind::TableDoesNotExist { .. }, ConnectionInfo::External(_)) => default_value,
#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(feature = "mssql-native", feature = "mysql-native", feature = "postgresql-native"))]
(ErrorKind::TableDoesNotExist { table: model }, _) => match connection_info {
#[cfg(feature = "postgresql-native")]
ConnectionInfo::Native(NativeConnectionInfo::Postgres(_)) => Some(KnownError::new(common::InvalidModel {
model: format!("{model}"),
kind: common::ModelKind::Table,
})),
#[cfg(feature = "postgresql-native")]
ConnectionInfo::Native(NativeConnectionInfo::Mysql(_)) => Some(KnownError::new(common::InvalidModel {
model: format!("{model}"),
kind: common::ModelKind::Table,
Expand Down Expand Up @@ -215,20 +228,28 @@ pub fn render_quaint_error(kind: &ErrorKind, connection_info: &ConnectionInfo) -
}))
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(
feature = "mssql-native",
feature = "mysql-native",
feature = "postgresql-native",
feature = "sqlite-native"
))]
(ErrorKind::Native(native_error_kind), _) => match (native_error_kind, connection_info) {
#[cfg(feature = "postgresql-native")]
(NativeErrorKind::ConnectionError(_), ConnectionInfo::Native(NativeConnectionInfo::Postgres(url))) => {
Some(KnownError::new(common::DatabaseNotReachable {
database_port: url.port(),
database_host: url.host().to_owned(),
}))
}
#[cfg(feature = "mysql-native")]
(NativeErrorKind::ConnectionError(_), ConnectionInfo::Native(NativeConnectionInfo::Mysql(url))) => {
Some(KnownError::new(common::DatabaseNotReachable {
database_port: url.port(),
database_host: url.host().to_owned(),
}))
}
#[cfg(feature = "mssql-native")]
(NativeErrorKind::ConnectionError(_), ConnectionInfo::Native(NativeConnectionInfo::Mssql(url))) => {
Some(KnownError::new(common::DatabaseNotReachable {
database_port: url.port(),
Expand All @@ -238,18 +259,21 @@ pub fn render_quaint_error(kind: &ErrorKind, connection_info: &ConnectionInfo) -
(NativeErrorKind::TlsError { message }, _) => Some(KnownError::new(common::TlsConnectionError {
message: message.into(),
})),
#[cfg(feature = "postgresql-native")]
(NativeErrorKind::ConnectTimeout, ConnectionInfo::Native(NativeConnectionInfo::Postgres(url))) => {
Some(KnownError::new(common::DatabaseNotReachable {
database_host: url.host().to_owned(),
database_port: url.port(),
}))
}
#[cfg(feature = "mysql-native")]
(NativeErrorKind::ConnectTimeout, ConnectionInfo::Native(NativeConnectionInfo::Mysql(url))) => {
Some(KnownError::new(common::DatabaseNotReachable {
database_host: url.host().to_owned(),
database_port: url.port(),
}))
}
#[cfg(feature = "mssql-native")]
(NativeErrorKind::ConnectTimeout, ConnectionInfo::Native(NativeConnectionInfo::Mssql(url))) => {
Some(KnownError::new(common::DatabaseNotReachable {
database_host: url.host().to_owned(),
Expand Down
12 changes: 9 additions & 3 deletions quaint/Cargo.toml
Expand Up @@ -28,9 +28,12 @@ docs = []
# way to access database-specific methods when you need extra control.
expose-drivers = []

native = ["postgresql-native", "mysql-native", "mssql-native", "sqlite-native"]

all = ["native", "pooled"]
all-native = [
"postgresql-native",
"mysql-native",
"mssql-native",
"sqlite-native",
]

vendored-openssl = [
"postgres-native-tls/vendored-openssl",
Expand Down Expand Up @@ -177,3 +180,6 @@ optional = true
version = "0.6"
features = ["compat"]
optional = true

[build-dependencies]
cfg_aliases = "0.1.0"
14 changes: 14 additions & 0 deletions quaint/build.rs
@@ -0,0 +1,14 @@
use cfg_aliases::cfg_aliases;

fn main() {
cfg_aliases! {
native: {
any(
feature = "mssql-native",
feature = "mysql-native",
feature = "postgresql-native",
feature = "sqlite-native"
)
}
}
}
2 changes: 1 addition & 1 deletion quaint/quaint-test-setup/Cargo.toml
Expand Up @@ -10,4 +10,4 @@ bitflags = "1.2.1"
async-trait.workspace = true
names = "0.11"
tokio = { version = "1.0", features = ["rt-multi-thread"] }
quaint = { path = "..", features = ["all"] }
quaint = { path = "..", features = ["all-native", "pooled"] }
7 changes: 4 additions & 3 deletions quaint/src/connector.rs
Expand Up @@ -13,7 +13,7 @@ mod connection_info;

pub mod external;
pub mod metrics;
#[cfg(feature = "native")]
#[cfg(native)]
pub mod native;
mod queryable;
mod result_set;
Expand All @@ -25,13 +25,14 @@ mod type_identifier;
pub use self::result_set::*;
pub use connection_info::*;

#[cfg(feature = "native")]
#[cfg(native)]
pub use native::*;

pub use external::*;
pub use queryable::*;
pub use transaction::*;
#[cfg(any(feature = "mssql-native", feature = "postgresql-native", feature = "mysql-native"))]

#[cfg(native)]
#[allow(unused_imports)]
pub(crate) use type_identifier::*;

Expand Down
11 changes: 4 additions & 7 deletions quaint/src/connector/connection_info.rs
Expand Up @@ -18,7 +18,7 @@ use std::convert::TryFrom;

use super::ExternalConnectionInfo;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(native)]
use super::NativeConnectionInfo;

/// General information about a SQL connection.
Expand Down Expand Up @@ -267,12 +267,9 @@ impl ConnectionInfo {

pub fn version(&self) -> Option<&str> {
match self {
#[cfg(not(target_arch = "wasm32"))]
ConnectionInfo::Native(nt) => match nt {
NativeConnectionInfo::Mysql(m) => m.version(),
_ => None,
},
ConnectionInfo::External(_) => None,
#[cfg(feature = "mysql-native")]
ConnectionInfo::Native(NativeConnectionInfo::Mysql(m)) => m.version(),
_ => None,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions quaint/src/connector/native.rs
Expand Up @@ -31,6 +31,7 @@ pub enum NativeConnectionInfo {
}

impl NativeConnectionInfo {
#[allow(unused)]
pub fn set_version(&mut self, version: Option<String>) {
#[cfg(feature = "mysql")]
if let NativeConnectionInfo::Mysql(c) = self {
Expand Down
2 changes: 1 addition & 1 deletion quaint/src/connector/timeout.rs
Expand Up @@ -2,7 +2,7 @@ use crate::error::{Error, ErrorKind};
use futures::Future;
use std::time::Duration;

#[cfg(feature = "native")]
#[cfg(native)]
pub async fn connect<T, F, E>(duration: Option<Duration>, f: F) -> crate::Result<T>
where
F: Future<Output = std::result::Result<T, E>>,
Expand Down
10 changes: 6 additions & 4 deletions quaint/src/pooled.rs
Expand Up @@ -152,11 +152,11 @@ mod manager;

pub use manager::*;

#[cfg(feature = "native")]
use crate::{connector::NativeConnectionInfo, error::NativeErrorKind};
#[cfg(native)]
use crate::error::NativeErrorKind;

use crate::{
connector::{ConnectionInfo, PostgresFlavour},
connector::ConnectionInfo,
error::{Error, ErrorKind},
};
use mobc::Pool;
Expand Down Expand Up @@ -305,7 +305,9 @@ impl Builder {
/// - Unknown: Always add a network roundtrip by setting the search path through a database query.
///
/// - Defaults to `PostgresFlavour::Unknown`.
pub fn set_postgres_flavour(&mut self, flavour: PostgresFlavour) {
#[cfg(feature = "postgresql-native")]
pub fn set_postgres_flavour(&mut self, flavour: crate::connector::PostgresFlavour) {
use crate::connector::NativeConnectionInfo;
if let ConnectionInfo::Native(NativeConnectionInfo::Postgres(ref mut url)) = self.connection_info {
url.set_flavour(flavour);
}
Expand Down
2 changes: 1 addition & 1 deletion quaint/src/prelude.rs
Expand Up @@ -6,5 +6,5 @@ pub use crate::connector::{
};
pub use crate::{col, val, values};

#[cfg(feature = "native")]
#[cfg(native)]
pub use crate::connector::NativeConnectionInfo;

0 comments on commit 031bd63

Please sign in to comment.