Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to sqlx 8 (waiting for sqlx release) #159

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 89 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 <[email protected]>" , "Pepe Márquez <[email protected]>"]
authors = [
"Ayrat Badykov <[email protected]>",
"Pepe Márquez <[email protected]>",
]
description = "Background job processing library for Rust"
repository = "https://github.com/ayrat555/fang"
edition = "2021"
Expand All @@ -15,27 +18,75 @@ 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", features = [
# "any",
# "macros",
# "chrono",
# "uuid",
# "json",
# "runtime-tokio-rustls",
# "postgres",
# "sqlite",
# "mysql",
#] }

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

url = { version = "2.2.2" }


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

[dependencies]
Expand All @@ -51,8 +102,29 @@ 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 = { version = "0.8", features = [
# "any",
# "macros",
# "chrono",
# "uuid",
# "json",
# "runtime-tokio-rustls",
# "postgres",
# "sqlite",
# "mysql",
#] }
sqlx = { git = "https://github.com/launchbadge/sqlx.git", branch = "main", features = [
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it won't be possible to release a new version if the crate will reference a github repo

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know , I am officially waiting for the sqlx 8.3 release.

"any",
"macros",
"chrono",
"uuid",
"json",
"runtime-tokio-rustls",
], optional = true }

url = { version = "2.2.2" }


[dependencies.diesel]
version = "2.1"
Expand All @@ -70,7 +142,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,18 +13,18 @@ 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;

#[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 std::str::FromStr;
use thiserror::Error;
use typed_builder::TypedBuilder;
use url::Url;
use uuid::Uuid;

#[cfg(feature = "asynk-postgres")]
Expand Down Expand Up @@ -54,6 +54,8 @@ pub enum AsyncQueueError {
#[error(transparent)]
SerdeError(#[from] serde_json::Error),
#[error(transparent)]
UrlError(#[from] url::ParseError),
#[error(transparent)]
CronError(#[from] CronError),
#[error("returned invalid result (expected {expected:?}, found {found:?})")]
ResultError { expected: u64, found: u64 },
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 => {
"postgres" => {
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 kind = Url::parse(&self.uri)?;

let kind: AnyKind = self.uri.parse::<AnyConnectOptions>()?.kind();
let kind = kind.scheme();

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

Expand Down
9 changes: 4 additions & 5 deletions fang/src/asynk/async_runnable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,12 @@ pub trait AsyncRunnable: Send + Sync {
///
/**
```rust
fn cron(&self) -> Option<Scheduled> {
let expression = "0/20 * * * Aug-Sep * 2022/1";
Some(Scheduled::CronPattern(expression.to_string()))
}
fn cron(&self) -> Option<Scheduled> {
let expression = "0/20 * * * Aug-Sep * 2022/1";
Some(Scheduled::CronPattern(expression.to_string()))
}
```
*/

/// In order to schedule a task once, use the `Scheduled::ScheduleOnce` enum variant.
fn cron(&self) -> Option<Scheduled> {
None
Expand Down
7 changes: 3 additions & 4 deletions fang/src/asynk/backend_sqlx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ 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;
Expand Down Expand Up @@ -102,7 +102,6 @@ impl BackendSqlX {
}
}

// I think it is useful to have this method, although it is not used
pub(crate) fn _name(&self) -> &str {
match *self {
#[cfg(feature = "asynk-postgres")]
Expand Down Expand Up @@ -155,7 +154,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
1 change: 0 additions & 1 deletion fang/src/blocking/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ mod worker_tests {

#[test]
#[ignore]

fn no_schedule_until_run() {
let task = TaskScheduled {};

Expand Down
6 changes: 6 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[toolchain]
versioned = "1.83"
channel = "stable-x86_64-unknown-linux-gnu"
components = ["rustfmt", "rustc-dev", "clippy"]
targets = ["x86_64-unknown-linux-gnu"]
profile = "minimal"
Loading