From 563562d32536f18559cfa90b51305b9856748131 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 9 Aug 2024 18:32:14 +0800 Subject: [PATCH] Bump SQLx to 0.8 (#2305) * Bump SQLx to 0.8 * Bump bigdecimal to 0.4 * Bump sea-query * Bump sea-schema * Fix clippy warnings * Patch for examples * Fix: enable serde feature on dependent crates * `TryGetable` for MySQL String * Fix pi_tests * Fix uuid_tests * Handle MySQL into_json * examples CI * Fix * Drop Git override * Bump to RC version * revert --- .github/workflows/rust.yml | 1 + Cargo.toml | 10 ++++---- sea-orm-cli/Cargo.toml | 4 ++-- sea-orm-codegen/Cargo.toml | 2 +- sea-orm-migration/Cargo.toml | 2 +- src/executor/query.rs | 46 +++++++++++++++++++++++++++++++++++- src/query/json.rs | 9 ++++++- tests/pi_tests.rs | 44 +++++++++++++++++++++------------- 8 files changed, 91 insertions(+), 27 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 97874619..e7ecfdbe 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -260,6 +260,7 @@ jobs: components: rustfmt - run: find ${{ matrix.path }} -type f -name 'Cargo.toml' -print0 | xargs -t -0 -I {} cargo fmt --manifest-path {} -- --check - uses: dtolnay/rust-toolchain@stable + - run: find ${{ matrix.path }} -type f -name 'Cargo.toml' -print0 | xargs -t -0 -I {} cargo update --manifest-path {} - run: find ${{ matrix.path }} -type f -name 'Cargo.toml' -print0 | xargs -t -0 -I {} cargo build --manifest-path {} - run: find ${{ matrix.path }} -type f -name 'Cargo.toml' -print0 | xargs -t -0 -I {} cargo test --manifest-path {} - run: ${{'! '}}${{ '[ -d "' }}${{ matrix.path }}${{ '/service" ]' }} || find ${{ matrix.path }}/service -type f -name 'Cargo.toml' -print0 | xargs -t -0 -I {} cargo test --manifest-path {} --features mock diff --git a/Cargo.toml b/Cargo.toml index 6e4bb931..a13e0c8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,14 +32,14 @@ futures = { version = "0.3", default-features = false, features = ["std"] } log = { version = "0.4", default-features = false } tracing = { version = "0.1", default-features = false, features = ["attributes", "log"] } rust_decimal = { version = "1", default-features = false, optional = true } -bigdecimal = { version = "0.3", default-features = false, optional = true } +bigdecimal = { version = "0.4", default-features = false, optional = true } sea-orm-macros = { version = "~1.0.0", path = "sea-orm-macros", default-features = false, features = ["strum"] } -sea-query = { version = "0.31.0", default-features = false, features = ["thread-safe", "hashable-value", "backend-mysql", "backend-postgres", "backend-sqlite"] } -sea-query-binder = { version = "0.6.0", default-features = false, optional = true } +sea-query = { version = "0.32.0-rc.1", default-features = false, features = ["thread-safe", "hashable-value", "backend-mysql", "backend-postgres", "backend-sqlite"] } +sea-query-binder = { version = "0.7.0-rc.1", default-features = false, optional = true } strum = { version = "0.26", default-features = false } serde = { version = "1.0", default-features = false } serde_json = { version = "1.0", default-features = false, optional = true } -sqlx = { version = "0.7", default-features = false, optional = true } +sqlx = { version = "0.8", default-features = false, optional = true } uuid = { version = "1", default-features = false, optional = true } ouroboros = { version = "0.17", default-features = false } url = { version = "2.2", default-features = false } @@ -76,7 +76,7 @@ default = [ macros = ["sea-orm-macros/derive"] mock = [] proxy = ["serde_json", "serde/derive"] -with-json = ["serde_json", "sea-query/with-json", "chrono?/serde", "time?/serde", "uuid?/serde", "sea-query-binder?/with-json", "sqlx?/json"] +with-json = ["serde_json", "sea-query/with-json", "chrono?/serde", "rust_decimal?/serde", "bigdecimal?/serde", "uuid?/serde", "time?/serde", "sea-query-binder?/with-json", "sqlx?/json"] with-chrono = ["chrono", "sea-query/with-chrono", "sea-query-binder?/with-chrono", "sqlx?/chrono"] with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal", "sea-query-binder?/with-rust_decimal", "sqlx?/rust_decimal"] with-bigdecimal = ["bigdecimal", "sea-query/with-bigdecimal", "sea-query-binder?/with-bigdecimal", "sqlx?/bigdecimal"] diff --git a/sea-orm-cli/Cargo.toml b/sea-orm-cli/Cargo.toml index 68021fd2..5fdc4dda 100644 --- a/sea-orm-cli/Cargo.toml +++ b/sea-orm-cli/Cargo.toml @@ -38,8 +38,8 @@ clap = { version = "4.3", features = ["env", "derive"], optional = true } dotenvy = { version = "0.15", default-features = false, optional = true } async-std = { version = "1.9", default-features = false, features = ["attributes", "tokio1"], optional = true } sea-orm-codegen = { version = "=1.0.0", path = "../sea-orm-codegen", default-features = false, optional = true } -sea-schema = { version = "0.15.0" } -sqlx = { version = "0.7", default-features = false, features = ["mysql", "postgres"], optional = true } +sea-schema = { version = "0.16.0-rc.1" } +sqlx = { version = "0.8", default-features = false, features = ["mysql", "postgres"], optional = true } tracing-subscriber = { version = "0.3.17", default-features = false, features = ["env-filter", "fmt"] } tracing = { version = "0.1", default-features = false } url = { version = "2.2", default-features = false } diff --git a/sea-orm-codegen/Cargo.toml b/sea-orm-codegen/Cargo.toml index 0bf1857f..d4c54645 100644 --- a/sea-orm-codegen/Cargo.toml +++ b/sea-orm-codegen/Cargo.toml @@ -17,7 +17,7 @@ name = "sea_orm_codegen" path = "src/lib.rs" [dependencies] -sea-query = { version = "0.31.0", default-features = false, features = ["thread-safe"] } +sea-query = { version = "0.32.0-rc.1", default-features = false, features = ["thread-safe"] } syn = { version = "2", default-features = false, features = ["parsing", "proc-macro", "derive", "printing"] } quote = { version = "1", default-features = false } heck = { version = "0.4", default-features = false } diff --git a/sea-orm-migration/Cargo.toml b/sea-orm-migration/Cargo.toml index c14c2ff3..47caee71 100644 --- a/sea-orm-migration/Cargo.toml +++ b/sea-orm-migration/Cargo.toml @@ -25,7 +25,7 @@ clap = { version = "4.3", features = ["env", "derive"], optional = true } dotenvy = { version = "0.15", default-features = false, optional = true } sea-orm = { version = "~1.0.0", path = "../", default-features = false, features = ["macros"] } sea-orm-cli = { version = "~1.0.0", path = "../sea-orm-cli", default-features = false, optional = true } -sea-schema = { version = "0.15.0" } +sea-schema = { version = "0.16.0-rc.1" } tracing = { version = "0.1", default-features = false, features = ["log"] } tracing-subscriber = { version = "0.3.17", default-features = false, features = ["env-filter", "fmt"] } futures = { version = "0.3", default-features = false, features = ["std"] } diff --git a/src/executor/query.rs b/src/executor/query.rs index abc6ca6d..ce3ae4c8 100644 --- a/src/executor/query.rs +++ b/src/executor/query.rs @@ -463,7 +463,6 @@ try_getable_unsigned!(u16); try_getable_mysql!(u64); try_getable_all!(f32); try_getable_all!(f64); -try_getable_all!(String); try_getable_all!(Vec); #[cfg(feature = "with-json")] @@ -705,6 +704,51 @@ impl TryGetable for u32 { } } +impl TryGetable for String { + #[allow(unused_variables)] + fn try_get_by(res: &QueryResult, idx: I) -> Result { + match &res.row { + #[cfg(feature = "sqlx-mysql")] + QueryResultRow::SqlxMySql(row) => row + .try_get::>, _>(idx.as_sqlx_mysql_index()) + .map_err(|e| sqlx_error_to_query_err(e).into()) + .and_then(|opt| opt.ok_or_else(|| err_null_idx_col(idx))) + .map(|bytes| { + String::from_utf8(bytes).map_err(|e| { + DbErr::TryIntoErr { + from: "Vec", + into: "String", + source: Box::new(e), + } + .into() + }) + })?, + #[cfg(feature = "sqlx-postgres")] + QueryResultRow::SqlxPostgres(row) => row + .try_get::, _>(idx.as_sqlx_postgres_index()) + .map_err(|e| sqlx_error_to_query_err(e).into()) + .and_then(|opt| opt.ok_or_else(|| err_null_idx_col(idx))), + #[cfg(feature = "sqlx-sqlite")] + QueryResultRow::SqlxSqlite(row) => row + .try_get::, _>(idx.as_sqlx_sqlite_index()) + .map_err(|e| sqlx_error_to_query_err(e).into()) + .and_then(|opt| opt.ok_or_else(|| err_null_idx_col(idx))), + #[cfg(feature = "mock")] + QueryResultRow::Mock(row) => row.try_get(idx).map_err(|e| { + debug_print!("{:#?}", e.to_string()); + err_null_idx_col(idx) + }), + #[cfg(feature = "proxy")] + QueryResultRow::Proxy(row) => row.try_get(idx).map_err(|e| { + debug_print!("{:#?}", e.to_string()); + err_null_idx_col(idx) + }), + #[allow(unreachable_patterns)] + _ => unreachable!(), + } + } +} + #[allow(dead_code)] fn err_null_idx_col(idx: I) -> TryGetError { TryGetError::Null(format!("{idx:?}")) diff --git a/src/query/json.rs b/src/query/json.rs index da91fb3b..6b5fad36 100644 --- a/src/query/json.rs +++ b/src/query/json.rs @@ -34,6 +34,13 @@ impl FromQueryResult for JsonValue { } }; } + macro_rules! match_mysql_compatible_type { + ( $type: ty ) => { + if <$type as Type>::compatible(col_type) { + try_get_type!($type, col) + } + }; + } match_mysql_type!(bool); match_mysql_type!(i8); match_mysql_type!(i16); @@ -64,9 +71,9 @@ impl FromQueryResult for JsonValue { match_mysql_type!(time::OffsetDateTime); #[cfg(feature = "with-rust_decimal")] match_mysql_type!(rust_decimal::Decimal); + match_mysql_compatible_type!(String); #[cfg(feature = "with-json")] try_get_type!(serde_json::Value, col); - try_get_type!(String, col); #[cfg(feature = "with-uuid")] try_get_type!(uuid::Uuid, col); try_get_type!(Vec, col); diff --git a/tests/pi_tests.rs b/tests/pi_tests.rs index ccf35f3a..2fed3906 100644 --- a/tests/pi_tests.rs +++ b/tests/pi_tests.rs @@ -18,33 +18,45 @@ async fn main() -> Result<(), DbErr> { } pub async fn create_and_update_pi(db: &DatabaseConnection) -> Result<(), DbErr> { - let pi = pi::Model { + fn trunc_dec_scale(mut model: pi::Model) -> pi::Model { + model.decimal = model.decimal.trunc_with_scale(3); + model.big_decimal = model.big_decimal.with_scale(3); + model.decimal_opt = model.decimal_opt.map(|decimal| decimal.trunc_with_scale(3)); + model.big_decimal_opt = model + .big_decimal_opt + .map(|big_decimal| big_decimal.with_scale(3)); + model + } + + let pi = trunc_dec_scale(pi::Model { id: 1, decimal: rust_dec(3.1415926536), big_decimal: BigDecimal::from_str("3.1415926536").unwrap(), decimal_opt: None, big_decimal_opt: None, - }; + }); - let res = pi.clone().into_active_model().insert(db).await?; + let res = trunc_dec_scale(pi.clone().into_active_model().insert(db).await?); - let model = Pi::find().one(db).await?; - assert_eq!(model, Some(res)); - assert_eq!(model, Some(pi.clone())); + let model = trunc_dec_scale(Pi::find().one(db).await?.unwrap()); + assert_eq!(model, res); + assert_eq!(model, pi.clone()); - let res = pi::ActiveModel { - decimal_opt: Set(Some(rust_dec(3.1415926536))), - big_decimal_opt: Set(Some(BigDecimal::from_str("3.1415926536").unwrap())), - ..pi.clone().into_active_model() - } - .update(db) - .await?; + let res = trunc_dec_scale( + pi::ActiveModel { + decimal_opt: Set(Some(rust_dec(3.1415926536))), + big_decimal_opt: Set(Some(BigDecimal::from_str("3.1415926536").unwrap())), + ..pi.clone().into_active_model() + } + .update(db) + .await?, + ); - let model = Pi::find().one(db).await?; - assert_eq!(model, Some(res)); + let model = trunc_dec_scale(Pi::find().one(db).await?.unwrap()); + assert_eq!(model, res); assert_eq!( model, - Some(pi::Model { + trunc_dec_scale(pi::Model { id: 1, decimal: rust_dec(3.1415926536), big_decimal: BigDecimal::from_str("3.1415926536").unwrap(),