From 009b61c5a43cdc9120adbf4dad26b190b5ca93fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pepe=20M=C3=A1rquez=20Romero?= Date: Sat, 23 Nov 2024 12:53:58 +0100 Subject: [PATCH 1/6] update to sqlx 8.1 (waiting for sqlx PR) --- fang/Cargo.toml | 90 ++++++++++++++++++++----- fang/src/asynk/async_queue.rs | 34 +++++----- fang/src/asynk/backend_sqlx.rs | 16 ++--- fang/src/asynk/backend_sqlx/mysql.rs | 2 +- fang/src/asynk/backend_sqlx/postgres.rs | 2 +- fang/src/asynk/backend_sqlx/sqlite.rs | 2 +- 6 files changed, 103 insertions(+), 43 deletions(-) diff --git a/fang/Cargo.toml b/fang/Cargo.toml index 97be165..1802e3f 100644 --- a/fang/Cargo.toml +++ b/fang/Cargo.toml @@ -1,7 +1,10 @@ [package] name = "fang" version = "0.11.0-rc1" -authors = ["Ayrat Badykov " , "Pepe Márquez "] +authors = [ + "Ayrat Badykov ", + "Pepe Márquez ", +] description = "Background job processing library for Rust" repository = "https://github.com/ayrat555/fang" edition = "2021" @@ -15,17 +18,35 @@ 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"] @@ -33,9 +54,36 @@ 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] @@ -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" @@ -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] diff --git a/fang/src/asynk/async_queue.rs b/fang/src/asynk/async_queue.rs index c44abb0..81a2690 100644 --- a/fang/src/asynk/async_queue.rs +++ b/fang/src/asynk/async_queue.rs @@ -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; @@ -238,13 +240,13 @@ use std::env; use super::backend_sqlx::BackendSqlX; async fn get_pool( - kind: AnyKind, + kind: &str, _uri: &str, _max_connections: u32, ) -> Result { match kind { #[cfg(feature = "asynk-postgres")] - AnyKind::Postgres => { + "PostgreSQL" => { let pool = PoolOptions::::new() .max_connections(_max_connections) .connect(_uri) @@ -252,23 +254,23 @@ async fn get_pool( Ok(InternalPool::Pg(pool)) } - #[cfg(feature = "asynk-mysql")] - AnyKind::MySql => { - let pool = PoolOptions::::new() + #[cfg(feature = "asynk-sqlite")] + "SQLite" => { + let pool = PoolOptions::::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::::new() + #[cfg(feature = "asynk-mysql")] + "MySql" => { + let pool = PoolOptions::::new() .max_connections(_max_connections) .connect(_uri) .await?; - Ok(InternalPool::Sqlite(pool)) + Ok(InternalPool::MySql(pool)) } #[allow(unreachable_patterns)] _ => Err(AsyncQueueError::ConnectionError), @@ -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::::produces(&self.uri).unwrap(); - let kind: AnyKind = self.uri.parse::()?.kind(); + let kind: &str = any_type_info.name(); let pool = get_pool(kind, &self.uri, self.max_pool_size).await?; diff --git a/fang/src/asynk/backend_sqlx.rs b/fang/src/asynk/backend_sqlx.rs index cfe57bb..1587ad6 100644 --- a/fang/src/asynk/backend_sqlx.rs +++ b/fang/src/asynk/backend_sqlx.rs @@ -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; @@ -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(), } } } @@ -155,7 +155,7 @@ where for<'r> &'r Uuid: Encode<'r, DB> + Type, for<'r> &'r serde_json::Value: Encode<'r, DB> + Type, for<'r> &'r Pool: Executor<'r, Database = DB>, - for<'r> >::Arguments: IntoArguments<'r, DB>, + for<'r> ::Arguments<'r>: IntoArguments<'r, DB>, ::QueryResult: Into, { async fn fetch_task_type( diff --git a/fang/src/asynk/backend_sqlx/mysql.rs b/fang/src/asynk/backend_sqlx/mysql.rs index c0d5140..f819256 100644 --- a/fang/src/asynk/backend_sqlx/mysql.rs +++ b/fang/src/asynk/backend_sqlx/mysql.rs @@ -448,7 +448,7 @@ impl BackendSqlXMySQL { } } - pub(super) fn _name() -> &'static str { + pub(super) fn name() -> &'static str { "MySQL" } } diff --git a/fang/src/asynk/backend_sqlx/postgres.rs b/fang/src/asynk/backend_sqlx/postgres.rs index 74d56a1..daf5533 100644 --- a/fang/src/asynk/backend_sqlx/postgres.rs +++ b/fang/src/asynk/backend_sqlx/postgres.rs @@ -214,7 +214,7 @@ impl BackendSqlXPg { } } - pub(super) fn _name() -> &'static str { + pub(super) fn name() -> &'static str { "PostgreSQL" } } diff --git a/fang/src/asynk/backend_sqlx/sqlite.rs b/fang/src/asynk/backend_sqlx/sqlite.rs index 1d000e2..5a1773a 100644 --- a/fang/src/asynk/backend_sqlx/sqlite.rs +++ b/fang/src/asynk/backend_sqlx/sqlite.rs @@ -207,7 +207,7 @@ impl BackendSqlXSQLite { } } - pub(super) fn _name() -> &'static str { + pub(super) fn name() -> &'static str { "SQLite" } } From 309e1448267251f40ab699e91b400cc86ae89ae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pepe=20M=C3=A1rquez=20Romero?= Date: Sat, 14 Dec 2024 18:46:58 +0100 Subject: [PATCH 2/6] make it work, with new without any warnings --- fang/Cargo.toml | 9 +++++++-- fang/src/asynk/async_queue.rs | 18 +++++++++--------- fang/src/asynk/backend_sqlx.rs | 9 ++++----- fang/src/asynk/backend_sqlx/mysql.rs | 2 +- fang/src/asynk/backend_sqlx/postgres.rs | 2 +- fang/src/asynk/backend_sqlx/sqlite.rs | 2 +- 6 files changed, 23 insertions(+), 19 deletions(-) diff --git a/fang/Cargo.toml b/fang/Cargo.toml index 1802e3f..a505cfe 100644 --- a/fang/Cargo.toml +++ b/fang/Cargo.toml @@ -72,7 +72,7 @@ diesel_migrations = { version = "2.1", features = [ # "mysql", #] } -sqlx = { git = "https://github.com/pxp9/sqlx.git", branch = "main", features = [ +sqlx = { git = "https://github.com/launchbadge/sqlx.git", branch = "main", features = [ "any", "macros", "chrono", @@ -84,6 +84,9 @@ sqlx = { git = "https://github.com/pxp9/sqlx.git", branch = "main", features = [ "mysql", ] } +url = { version = "2.2.2" } + + #console-subscriber = "0.2.0" # for tokio tracing debug [dependencies] @@ -100,7 +103,7 @@ typed-builder = "0.14" typetag = "0.2" uuid = { version = "1.1", features = ["v4"] } fang-derive-error = { version = "0.1.0", optional = true } -sqlx = { git = "https://github.com/pxp9/sqlx.git", branch = "main", features = [ +sqlx = { git = "https://github.com/launchbadge/sqlx.git", branch = "main", features = [ "any", "macros", "chrono", @@ -109,6 +112,8 @@ sqlx = { git = "https://github.com/pxp9/sqlx.git", branch = "main", features = [ "runtime-tokio-rustls", ], optional = true } +url = { version = "2.2.2" } + [dependencies.diesel] version = "2.1" diff --git a/fang/src/asynk/async_queue.rs b/fang/src/asynk/async_queue.rs index 81a2690..62fa34a 100644 --- a/fang/src/asynk/async_queue.rs +++ b/fang/src/asynk/async_queue.rs @@ -14,19 +14,17 @@ use chrono::DateTime; use chrono::Utc; use cron::Schedule; 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::Encode; -use sqlx::TypeInfo; use std::str::FromStr; use thiserror::Error; use typed_builder::TypedBuilder; +use url::Url; use uuid::Uuid; #[cfg(feature = "asynk-postgres")] @@ -56,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 }, @@ -246,7 +246,7 @@ async fn get_pool( ) -> Result { match kind { #[cfg(feature = "asynk-postgres")] - "PostgreSQL" => { + "postgres" => { let pool = PoolOptions::::new() .max_connections(_max_connections) .connect(_uri) @@ -255,7 +255,7 @@ async fn get_pool( Ok(InternalPool::Pg(pool)) } #[cfg(feature = "asynk-sqlite")] - "SQLite" => { + "sqlite" => { let pool = PoolOptions::::new() .max_connections(_max_connections) .connect(_uri) @@ -264,7 +264,7 @@ async fn get_pool( Ok(InternalPool::Sqlite(pool)) } #[cfg(feature = "asynk-mysql")] - "MySql" => { + "mysql" => { let pool = PoolOptions::::new() .max_connections(_max_connections) .connect(_uri) @@ -291,9 +291,9 @@ impl AsyncQueue { pub async fn connect(&mut self) -> Result<(), AsyncQueueError> { install_default_drivers(); - let any_type_info: AnyTypeInfo = sqlx::Encode::::produces(&self.uri).unwrap(); + let kind = Url::parse(&self.uri)?; - let kind: &str = any_type_info.name(); + let kind = kind.scheme(); let pool = get_pool(kind, &self.uri, self.max_pool_size).await?; diff --git a/fang/src/asynk/backend_sqlx.rs b/fang/src/asynk/backend_sqlx.rs index 1587ad6..ef138fc 100644 --- a/fang/src/asynk/backend_sqlx.rs +++ b/fang/src/asynk/backend_sqlx.rs @@ -7,7 +7,6 @@ use { }; use std::fmt::Debug; -use std::str::FromStr; use typed_builder::TypedBuilder; use uuid::Uuid; @@ -103,14 +102,14 @@ impl BackendSqlX { } } - 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(), } } } diff --git a/fang/src/asynk/backend_sqlx/mysql.rs b/fang/src/asynk/backend_sqlx/mysql.rs index f819256..c0d5140 100644 --- a/fang/src/asynk/backend_sqlx/mysql.rs +++ b/fang/src/asynk/backend_sqlx/mysql.rs @@ -448,7 +448,7 @@ impl BackendSqlXMySQL { } } - pub(super) fn name() -> &'static str { + pub(super) fn _name() -> &'static str { "MySQL" } } diff --git a/fang/src/asynk/backend_sqlx/postgres.rs b/fang/src/asynk/backend_sqlx/postgres.rs index daf5533..74d56a1 100644 --- a/fang/src/asynk/backend_sqlx/postgres.rs +++ b/fang/src/asynk/backend_sqlx/postgres.rs @@ -214,7 +214,7 @@ impl BackendSqlXPg { } } - pub(super) fn name() -> &'static str { + pub(super) fn _name() -> &'static str { "PostgreSQL" } } diff --git a/fang/src/asynk/backend_sqlx/sqlite.rs b/fang/src/asynk/backend_sqlx/sqlite.rs index 5a1773a..1d000e2 100644 --- a/fang/src/asynk/backend_sqlx/sqlite.rs +++ b/fang/src/asynk/backend_sqlx/sqlite.rs @@ -207,7 +207,7 @@ impl BackendSqlXSQLite { } } - pub(super) fn name() -> &'static str { + pub(super) fn _name() -> &'static str { "SQLite" } } From b1d186296c3fe11a248d695c469906ca41c3a397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pepe=20M=C3=A1rquez=20Romero?= Date: Sat, 14 Dec 2024 18:51:09 +0100 Subject: [PATCH 3/6] lastest 0.8 version (once sqlx release 8.3, we are ready :) ) --- fang/Cargo.toml | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/fang/Cargo.toml b/fang/Cargo.toml index a505cfe..f1b8daf 100644 --- a/fang/Cargo.toml +++ b/fang/Cargo.toml @@ -60,7 +60,7 @@ diesel_migrations = { version = "2.1", features = [ "sqlite", "mysql", ] } -#sqlx = { version = "0.8.1", features = [ +#sqlx = { version = "0.8", features = [ # "any", # "macros", # "chrono", @@ -103,6 +103,17 @@ 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.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", From 1945e664dad3309775f42aa8625345b19d9d6a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pepe=20M=C3=A1rquez=20Romero?= Date: Sat, 14 Dec 2024 19:17:41 +0100 Subject: [PATCH 4/6] add toolchain file --- rust-toolchain.toml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 rust-toolchain.toml diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..626bb26 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,6 @@ +[toolchain] +versioned = "1.82" +channel = "stable-x86_64-unknown-linux-gnu" +components = ["rustfmt", "rustc-dev", "clippy"] +targets = ["x86_64-unknown-linux-gnu"] +profile = "minimal" From 97aa430762c3a4d38fbd280b4453267190572f31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pepe=20M=C3=A1rquez=20Romero?= Date: Sat, 21 Dec 2024 14:34:50 +0100 Subject: [PATCH 5/6] fix clippy --- fang/src/blocking/worker.rs | 1 - rust-toolchain.toml | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/fang/src/blocking/worker.rs b/fang/src/blocking/worker.rs index f2dc33a..937874c 100644 --- a/fang/src/blocking/worker.rs +++ b/fang/src/blocking/worker.rs @@ -435,7 +435,6 @@ mod worker_tests { #[test] #[ignore] - fn no_schedule_until_run() { let task = TaskScheduled {}; diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 626bb26..9a58c88 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,5 +1,5 @@ [toolchain] -versioned = "1.82" +versioned = "1.83" channel = "stable-x86_64-unknown-linux-gnu" components = ["rustfmt", "rustc-dev", "clippy"] targets = ["x86_64-unknown-linux-gnu"] From 7d1917c957b4186b6f0697f176a38a6f25e76334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pepe=20M=C3=A1rquez=20Romero?= Date: Sat, 21 Dec 2024 14:44:11 +0100 Subject: [PATCH 6/6] fix clippy ? --- fang/src/asynk/async_runnable.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/fang/src/asynk/async_runnable.rs b/fang/src/asynk/async_runnable.rs index bc0852b..d63dd56 100644 --- a/fang/src/asynk/async_runnable.rs +++ b/fang/src/asynk/async_runnable.rs @@ -58,13 +58,12 @@ pub trait AsyncRunnable: Send + Sync { /// /** ```rust - fn cron(&self) -> Option { - let expression = "0/20 * * * Aug-Sep * 2022/1"; - Some(Scheduled::CronPattern(expression.to_string())) - } + fn cron(&self) -> Option { + 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 { None