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] 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" } }