From e1883af5fc719e244379173b2cf9891535d2ca00 Mon Sep 17 00:00:00 2001 From: Tsiry Sandratraina Date: Mon, 17 Oct 2022 15:59:23 +0300 Subject: [PATCH 1/6] Update COMMUNITY.md (#1127) --- COMMUNITY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/COMMUNITY.md b/COMMUNITY.md index e12fe1a1..5fad7db5 100644 --- a/COMMUNITY.md +++ b/COMMUNITY.md @@ -57,6 +57,7 @@ If you have built an app using SeaORM and want to showcase it, feel free to open - [url_shortener](https://github.com/michidk/url_shortener) | A simple self-hosted URL shortener written in Rust | DB: MySQL, Postgres, SQLite - [RGB Lib](https://github.com/RGB-Tools/rgb-lib) | A library to manage wallets for RGB assets | DB: MySQL, Postgres, SQLite - [RCloud](https://github.com/p0rtL6/RCloud) | A self-hosted lightweight cloud drive alternative +- [Music Player](https://github.com/tsirysndr/music-player) | An extensible music server written in Rust 🚀🎵✨ | DB: SQLite ## Learning Resources From bfab8720b44169055f3ea4aa9220d92e0af3bd91 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Mon, 17 Oct 2022 21:10:30 +0800 Subject: [PATCH 2/6] Improve error messages #1125 --- src/executor/paginator.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/executor/paginator.rs b/src/executor/paginator.rs index 08ceffbd..562dfbdc 100644 --- a/src/executor/paginator.rs +++ b/src/executor/paginator.rs @@ -234,6 +234,7 @@ where type Selector = S; fn paginate(self, db: &'db C, page_size: u64) -> Paginator<'db, C, S> { + assert!(page_size != 0, "page_size should not be zero"); Paginator { query: self.query, page: 0, @@ -251,6 +252,7 @@ where { type Selector = S; fn paginate(self, db: &'db C, page_size: u64) -> Paginator<'db, C, S> { + assert!(page_size != 0, "page_size should not be zero"); let sql = &self.stmt.sql.trim()[6..]; let mut query = SelectStatement::new(); query.expr(if let Some(values) = self.stmt.values { @@ -768,4 +770,12 @@ mod tests { assert_eq!(db.into_transaction_log(), Transaction::wrap(stmts)); Ok(()) } + + #[smol_potat::test] + #[should_panic] + async fn error() { + let (db, _pages) = setup(); + + fruit::Entity::find().paginate(&db, 0); + } } From a0fd72e6353282434131132518de1f633a1791ee Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 18 Oct 2022 00:04:03 +0800 Subject: [PATCH 3/6] Serialize `time` types as `serde_json::Value` (#1042) * Implement `IntoActiveValue` for `time` types. I tried to implement a [custom active model](https://www.sea-ql.org/SeaORM/docs/advanced-query/custom-active-model/), and one of the columns was `Option`. I got a compiler error: ``` error[E0277]: the trait bound `std::option::Option: IntoActiveValue<_>` is not satisfied ``` Looking into the source code, it seemed a simple oversight that this trait was implemented for the `chrono` types but not the `time` types, and it was easy enough to fix since there's already a macro to implement it for new types. I also noticed that the `time` types are not accounted for in `src/query/json.rs` while the `chrono` types are, which I assume is also an oversight. However, I don't have a need for that at this point and the fix for that seemed less trivial, so I'm just bringing it to your attention. Thanks for SeaORM! * Implement `IntoActiveValue` for `Vec` types * Add tests to double check and prevent it from happening again * Add docs * Fixup * Serialize `time` types as `serde_json::Value` Co-authored-by: Jimmy Cuadra --- Cargo.toml | 2 +- src/query/json.rs | 16 ++++++++++++++++ tests/time_crate_tests.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index bb9013b3..2c407639 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,7 +70,7 @@ default = [ ] macros = ["sea-orm-macros"] mock = [] -with-json = ["serde_json", "sea-query/with-json", "chrono/serde", "sqlx?/json"] +with-json = ["serde_json", "sea-query/with-json", "chrono/serde", "time/serde", "sqlx?/json"] with-chrono = ["chrono", "sea-query/with-chrono", "sqlx?/chrono"] with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal", "sqlx?/decimal"] with-uuid = ["uuid", "sea-query/with-uuid", "sqlx?/uuid"] diff --git a/src/query/json.rs b/src/query/json.rs index 28aa1146..e1c1f0bf 100644 --- a/src/query/json.rs +++ b/src/query/json.rs @@ -54,6 +54,14 @@ impl FromQueryResult for JsonValue { match_mysql_type!(chrono::NaiveDateTime); #[cfg(feature = "with-chrono")] match_mysql_type!(chrono::DateTime); + #[cfg(feature = "with-time")] + match_mysql_type!(time::Date); + #[cfg(feature = "with-time")] + match_mysql_type!(time::Time); + #[cfg(feature = "with-time")] + match_mysql_type!(time::PrimitiveDateTime); + #[cfg(feature = "with-time")] + match_mysql_type!(time::OffsetDateTime); #[cfg(feature = "with-rust_decimal")] match_mysql_type!(rust_decimal::Decimal); #[cfg(feature = "with-json")] @@ -106,6 +114,14 @@ impl FromQueryResult for JsonValue { match_postgres_type!(chrono::NaiveDateTime); #[cfg(feature = "with-chrono")] match_postgres_type!(chrono::DateTime); + #[cfg(feature = "with-time")] + match_postgres_type!(time::Date); + #[cfg(feature = "with-time")] + match_postgres_type!(time::Time); + #[cfg(feature = "with-time")] + match_postgres_type!(time::PrimitiveDateTime); + #[cfg(feature = "with-time")] + match_postgres_type!(time::OffsetDateTime); #[cfg(feature = "with-rust_decimal")] match_postgres_type!(rust_decimal::Decimal); #[cfg(feature = "with-json")] diff --git a/tests/time_crate_tests.rs b/tests/time_crate_tests.rs index f7a4cff0..6419919c 100644 --- a/tests/time_crate_tests.rs +++ b/tests/time_crate_tests.rs @@ -1,6 +1,7 @@ pub mod common; pub use common::{features::*, setup::*, TestContext}; use sea_orm::{entity::prelude::*, DatabaseConnection, IntoActiveModel}; +use serde_json::json; use time::macros::{date, time}; #[sea_orm_macros::test] @@ -42,5 +43,31 @@ pub async fn create_transaction_log(db: &DatabaseConnection) -> Result<(), DbErr Some(transaction_log.clone()) ); + let json = TransactionLog::find().into_json().one(db).await?.unwrap(); + + #[cfg(feature = "sqlx-postgres")] + assert_eq!( + json, + json!({ + "id": 1, + "date": "2022-03-13", + "time": "16:24:00", + "date_time": "2022-03-13T16:24:00", + "date_time_tz": "2022-03-13T16:24:00+00:00", + }) + ); + + #[cfg(feature = "sqlx-mysql")] + assert_eq!( + json, + json!({ + "id": 1, + "date": "2022-03-13", + "time": "16:24:00", + "date_time": "2022-03-13T16:24:00", + "date_time_tz": "2022-03-13T16:24:00Z", + }) + ); + Ok(()) } From 9bbab855d9c69297281f8fbfd9d67388eef9ef35 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Tue, 18 Oct 2022 00:06:04 +0800 Subject: [PATCH 4/6] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69c17f7e..686fe416 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). * Added blanket implementations of `IntoActiveValue` for `Option` values https://github.com/SeaQL/sea-orm/pull/833 * Added `into_model` & `into_json` to `Cursor` https://github.com/SeaQL/sea-orm/pull/1112 * Added `set_schema_search_path` method to `ConnectOptions` for setting schema search path of PostgreSQL connection https://github.com/SeaQL/sea-orm/pull/1056 +* Serialize `time` types as `serde_json::Value` https://github.com/SeaQL/sea-orm/pull/1042 ### Bug fixes From a47fd9156711764c8dd7d98b86399862fcfddff9 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Tue, 18 Oct 2022 00:27:46 +0800 Subject: [PATCH 5/6] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 686fe416..f71e16c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). * [sea-orm-cli] Generate migration in modules https://github.com/SeaQL/sea-orm/pull/933 * [sea-orm-cli] Generate `DeriveRelation` on empty `Relation` enum https://github.com/SeaQL/sea-orm/pull/1019 * [sea-orm-cli] Generate entity derive `Eq` if possible https://github.com/SeaQL/sea-orm/pull/988 -* Run migration on any PostgreSQL schema https://github.com/SeaQL/sea-orm/pull/1056 +* [sea-orm-cli] Run migration on any PostgreSQL schema https://github.com/SeaQL/sea-orm/pull/1056 ### Enhancements From b255d7f11436b2f1d5f7d893b86ef2f563f5c565 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Tue, 18 Oct 2022 00:29:19 +0800 Subject: [PATCH 6/6] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f71e16c3..101b1baa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). * Support `distinct` & `distinct_on` expression https://github.com/SeaQL/sea-orm/pull/902 * `fn column()` also handle enum type https://github.com/SeaQL/sea-orm/pull/973 * Added `acquire_timeout` on `ConnectOptions` https://github.com/SeaQL/sea-orm/pull/897 -* `migrate fresh` command will drop all PostgreSQL types https://github.com/SeaQL/sea-orm/pull/864, https://github.com/SeaQL/sea-orm/pull/991 +* [sea-orm-cli] `migrate fresh` command will drop all PostgreSQL types https://github.com/SeaQL/sea-orm/pull/864, https://github.com/SeaQL/sea-orm/pull/991 * Better compile error for entity without primary key https://github.com/SeaQL/sea-orm/pull/1020 * Added blanket implementations of `IntoActiveValue` for `Option` values https://github.com/SeaQL/sea-orm/pull/833 * Added `into_model` & `into_json` to `Cursor` https://github.com/SeaQL/sea-orm/pull/1112