Skip to content

Commit

Permalink
update to sqlx 8.1 (waiting for sqlx PR)
Browse files Browse the repository at this point in the history
  • Loading branch information
pxp9 committed Nov 23, 2024
1 parent 31a5ac6 commit 009b61c
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 43 deletions.
90 changes: 73 additions & 17 deletions fang/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[package]
name = "fang"
version = "0.11.0-rc1"
authors = ["Ayrat Badykov <ayratin555@gmail.com>" , "Pepe Márquez <pepe.marquezromero@gmail.com>"]
authors = [
"Ayrat Badykov <ayratin555@gmail.com>",
"Pepe Márquez <pepe.marquezromero@gmail.com>",
]
description = "Background job processing library for Rust"
repository = "https://github.com/ayrat555/fang"
edition = "2021"
Expand All @@ -15,27 +18,72 @@ rust-version = "1.77"
doctest = false

[features]
default = ["blocking", "asynk-sqlx", "derive-error", "blocking-postgres", "blocking-mysql" , "blocking-sqlite", "migrations-postgres", "migrations-sqlite", "migrations-mysql"]
asynk-postgres = ["asynk-sqlx" , "sqlx?/postgres"]
asynk-sqlite = ["asynk-sqlx" , "sqlx?/sqlite"]
asynk-mysql = ["asynk-sqlx" , "sqlx?/mysql"]
asynk-sqlx = ["asynk" , "dep:sqlx"]
asynk = ["dep:tokio", "dep:async-trait", "dep:async-recursion" ]
default = [
"blocking",
"asynk-sqlx",
"derive-error",
"blocking-postgres",
"blocking-mysql",
"blocking-sqlite",
"migrations-postgres",
"migrations-sqlite",
"migrations-mysql",
]
asynk-postgres = ["asynk-sqlx", "sqlx?/postgres"]
asynk-sqlite = ["asynk-sqlx", "sqlx?/sqlite"]
asynk-mysql = ["asynk-sqlx", "sqlx?/mysql"]
asynk-sqlx = ["asynk", "dep:sqlx"]
asynk = ["dep:tokio", "dep:async-trait", "dep:async-recursion"]
derive-error = ["dep:fang-derive-error"]
blocking = ["dep:diesel", "dep:diesel-derive-enum", "dep:dotenvy", "diesel?/chrono" , "diesel?/serde_json" , "diesel?/uuid", "diesel?/r2d2"]
blocking-postgres = [ "blocking", "diesel?/postgres"]
blocking-sqlite = ["blocking", "diesel?/sqlite" ]
blocking-mysql = [ "blocking", "diesel?/mysql"]
blocking = [
"dep:diesel",
"dep:diesel-derive-enum",
"dep:dotenvy",
"diesel?/chrono",
"diesel?/serde_json",
"diesel?/uuid",
"diesel?/r2d2",
]
blocking-postgres = ["blocking", "diesel?/postgres"]
blocking-sqlite = ["blocking", "diesel?/sqlite"]
blocking-mysql = ["blocking", "diesel?/mysql"]
migrations-postgres = ["migrations", "diesel?/postgres"]
migrations-sqlite = ["migrations", "diesel?/sqlite"]
migrations-mysql = ["migrations", "diesel?/mysql"]
migrations = ["dep:diesel_migrations", "dep:diesel"]


[dev-dependencies]
fang-derive-error = { version = "0.1.0"}
diesel_migrations = { version = "2.1" , features = ["postgres", "sqlite" , "mysql"]}
sqlx = {version = "0.6.3", features = ["any" , "macros" , "chrono", "uuid", "json","runtime-tokio-rustls", "postgres", "sqlite", "mysql"]}
fang-derive-error = { version = "0.1.0" }
diesel_migrations = { version = "2.1", features = [
"postgres",
"sqlite",
"mysql",
] }
#sqlx = { version = "0.8.1", features = [
# "any",
# "macros",
# "chrono",
# "uuid",
# "json",
# "runtime-tokio-rustls",
# "postgres",
# "sqlite",
# "mysql",
#] }

sqlx = { git = "https://github.com/pxp9/sqlx.git", branch = "main", features = [
"any",
"macros",
"chrono",
"uuid",
"json",
"runtime-tokio-rustls",
"postgres",
"sqlite",
"mysql",
] }

#console-subscriber = "0.2.0" # for tokio tracing debug

[dependencies]
Expand All @@ -51,8 +99,16 @@ thiserror = "1.0"
typed-builder = "0.14"
typetag = "0.2"
uuid = { version = "1.1", features = ["v4"] }
fang-derive-error = { version = "0.1.0" , optional = true}
sqlx = {version = "0.6.3", features = ["any" , "macros" , "chrono", "uuid", "json", "runtime-tokio-rustls"], optional = true}
fang-derive-error = { version = "0.1.0", optional = true }
sqlx = { git = "https://github.com/pxp9/sqlx.git", branch = "main", features = [
"any",
"macros",
"chrono",
"uuid",
"json",
"runtime-tokio-rustls",
], optional = true }


[dependencies.diesel]
version = "2.1"
Expand All @@ -70,7 +126,7 @@ optional = true

[dependencies.tokio]
version = "1.25"
features = ["rt", "time", "macros"]#, "tracing"]
features = ["rt", "time", "macros"] #, "tracing"]
optional = true

[dependencies.async-trait]
Expand Down
34 changes: 19 additions & 15 deletions fang/src/asynk/async_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ use async_trait::async_trait;
use chrono::DateTime;
use chrono::Utc;
use cron::Schedule;
use sqlx::any::AnyConnectOptions;
use sqlx::any::AnyKind;
use sqlx::any::install_default_drivers;
use sqlx::any::Any;
use sqlx::any::AnyTypeInfo;
#[cfg(any(
feature = "asynk-postgres",
feature = "asynk-mysql",
feature = "asynk-sqlite"
))]
use sqlx::pool::PoolOptions;
//use sqlx::any::install_default_drivers; // this is supported in sqlx 0.7
use sqlx::Encode;
use sqlx::TypeInfo;
use std::str::FromStr;
use thiserror::Error;
use typed_builder::TypedBuilder;
Expand Down Expand Up @@ -238,37 +240,37 @@ use std::env;
use super::backend_sqlx::BackendSqlX;

async fn get_pool(
kind: AnyKind,
kind: &str,
_uri: &str,
_max_connections: u32,
) -> Result<InternalPool, AsyncQueueError> {
match kind {
#[cfg(feature = "asynk-postgres")]
AnyKind::Postgres => {
"PostgreSQL" => {
let pool = PoolOptions::<Postgres>::new()
.max_connections(_max_connections)
.connect(_uri)
.await?;

Ok(InternalPool::Pg(pool))
}
#[cfg(feature = "asynk-mysql")]
AnyKind::MySql => {
let pool = PoolOptions::<MySql>::new()
#[cfg(feature = "asynk-sqlite")]
"SQLite" => {
let pool = PoolOptions::<Sqlite>::new()
.max_connections(_max_connections)
.connect(_uri)
.await?;

Ok(InternalPool::MySql(pool))
Ok(InternalPool::Sqlite(pool))
}
#[cfg(feature = "asynk-sqlite")]
AnyKind::Sqlite => {
let pool = PoolOptions::<Sqlite>::new()
#[cfg(feature = "asynk-mysql")]
"MySql" => {
let pool = PoolOptions::<MySql>::new()
.max_connections(_max_connections)
.connect(_uri)
.await?;

Ok(InternalPool::Sqlite(pool))
Ok(InternalPool::MySql(pool))
}
#[allow(unreachable_patterns)]
_ => Err(AsyncQueueError::ConnectionError),
Expand All @@ -287,9 +289,11 @@ impl AsyncQueue {

/// Connect to the db if not connected
pub async fn connect(&mut self) -> Result<(), AsyncQueueError> {
//install_default_drivers();
install_default_drivers();

let any_type_info: AnyTypeInfo = sqlx::Encode::<Any>::produces(&self.uri).unwrap();

let kind: AnyKind = self.uri.parse::<AnyConnectOptions>()?.kind();
let kind: &str = any_type_info.name();

let pool = get_pool(kind, &self.uri, self.max_pool_size).await?;

Expand Down
16 changes: 8 additions & 8 deletions fang/src/asynk/backend_sqlx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use chrono::{DateTime, Utc};
use sha2::Digest;
use sha2::Sha256;
use {
chrono::Duration, sqlx::any::AnyQueryResult, sqlx::database::HasArguments, sqlx::Database,
sqlx::Encode, sqlx::Executor, sqlx::FromRow, sqlx::IntoArguments, sqlx::Pool, sqlx::Type,
chrono::Duration, sqlx::any::AnyQueryResult, sqlx::Database, sqlx::Encode, sqlx::Executor,
sqlx::FromRow, sqlx::IntoArguments, sqlx::Pool, sqlx::Type,
};

use std::fmt::Debug;
use std::str::FromStr;
use typed_builder::TypedBuilder;
use uuid::Uuid;

Expand Down Expand Up @@ -102,15 +103,14 @@ impl BackendSqlX {
}
}

// I think it is useful to have this method, although it is not used
pub(crate) fn _name(&self) -> &str {
pub(crate) fn name(&self) -> &str {
match *self {
#[cfg(feature = "asynk-postgres")]
BackendSqlX::Pg => BackendSqlXPg::_name(),
BackendSqlX::Pg => BackendSqlXPg::name(),
#[cfg(feature = "asynk-sqlite")]
BackendSqlX::Sqlite => BackendSqlXSQLite::_name(),
BackendSqlX::Sqlite => BackendSqlXSQLite::name(),
#[cfg(feature = "asynk-mysql")]
BackendSqlX::MySql => BackendSqlXMySQL::_name(),
BackendSqlX::MySql => BackendSqlXMySQL::name(),
}
}
}
Expand Down Expand Up @@ -155,7 +155,7 @@ where
for<'r> &'r Uuid: Encode<'r, DB> + Type<DB>,
for<'r> &'r serde_json::Value: Encode<'r, DB> + Type<DB>,
for<'r> &'r Pool<DB>: Executor<'r, Database = DB>,
for<'r> <DB as HasArguments<'r>>::Arguments: IntoArguments<'r, DB>,
for<'r> <DB as Database>::Arguments<'r>: IntoArguments<'r, DB>,
<DB as Database>::QueryResult: Into<AnyQueryResult>,
{
async fn fetch_task_type(
Expand Down
2 changes: 1 addition & 1 deletion fang/src/asynk/backend_sqlx/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ impl BackendSqlXMySQL {
}
}

pub(super) fn _name() -> &'static str {
pub(super) fn name() -> &'static str {
"MySQL"
}
}
2 changes: 1 addition & 1 deletion fang/src/asynk/backend_sqlx/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl BackendSqlXPg {
}
}

pub(super) fn _name() -> &'static str {
pub(super) fn name() -> &'static str {
"PostgreSQL"
}
}
2 changes: 1 addition & 1 deletion fang/src/asynk/backend_sqlx/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl BackendSqlXSQLite {
}
}

pub(super) fn _name() -> &'static str {
pub(super) fn name() -> &'static str {
"SQLite"
}
}

0 comments on commit 009b61c

Please sign in to comment.