From da0daa3fee45b5664cf84c717fa2e5f7efafbf1d Mon Sep 17 00:00:00 2001 From: Forest Anderson Date: Thu, 29 Dec 2022 21:28:28 -0500 Subject: [PATCH 1/4] Add JsonBinary attribute to column --- sea-orm-codegen/src/entity/column.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/sea-orm-codegen/src/entity/column.rs b/sea-orm-codegen/src/entity/column.rs index bcc1932b3..fecb71fa4 100644 --- a/sea-orm-codegen/src/entity/column.rs +++ b/sea-orm-codegen/src/entity/column.rs @@ -94,6 +94,7 @@ impl Column { ColumnType::Decimal(Some((p, s))) => Some(format!("Decimal(Some(({}, {})))", p, s)), ColumnType::Money(Some((p, s))) => Some(format!("Money(Some({}, {}))", p, s)), ColumnType::Text => Some("Text".to_owned()), + ColumnType::JsonBinary => Some("JsonBinary".to_owned()), ColumnType::Custom(iden) => { Some(format!("Custom(\"{}\".to_owned())", iden.to_string())) } From e45d5474d73c9a2e941dbb303c70c84cc2bba9ad Mon Sep 17 00:00:00 2001 From: Forest Anderson Date: Fri, 30 Dec 2022 11:11:45 -0500 Subject: [PATCH 2/4] Add Postgres test section, test binary json --- sea-orm-codegen/src/entity/writer.rs | 90 +++++++++++++++++++ sea-orm-codegen/tests/postgres/binary_json.rs | 21 +++++ 2 files changed, 111 insertions(+) create mode 100644 sea-orm-codegen/tests/postgres/binary_json.rs diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 21568380b..a76dfd346 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -1957,4 +1957,94 @@ mod tests { Ok(expected.to_string()) } + + #[test] + fn test_gen_postgres() -> io::Result<()> { + let entities = vec![ + // This tests that the JsonBinary column type is annotated + // correctly in compact entity form. More information can be found + // in this issue: + // + // https://github.com/SeaQL/sea-orm/issues/1344 + Entity { + table_name: "task".to_owned(), + columns: vec![ + Column { + name: "id".to_owned(), + col_type: ColumnType::Integer(Some(11)), + auto_increment: true, + not_null: true, + unique: false, + }, + Column { + name: "payload".to_owned(), + col_type: ColumnType::Json, + auto_increment: false, + not_null: true, + unique: false, + }, + Column { + name: "payload_binary".to_owned(), + col_type: ColumnType::JsonBinary, + auto_increment: false, + not_null: true, + unique: false, + }, + ], + relations: vec![], + conjunct_relations: vec![], + primary_keys: vec![PrimaryKey { + name: "id".to_owned(), + }], + }, + ]; + const ENTITY_FILES: [&str; 1] = [include_str!("../../tests/postgres/binary_json.rs")]; + + assert_eq!(entities.len(), ENTITY_FILES.len()); + + for (i, entity) in entities.iter().enumerate() { + assert_eq!( + parse_from_file(ENTITY_FILES[i].as_bytes())?.to_string(), + EntityWriter::gen_compact_code_blocks( + entity, + &crate::WithSerde::None, + &crate::DateTimeCrate::Chrono, + &None, + false, + false, + &TokenStream::new(), + &TokenStream::new(), + ) + .into_iter() + .skip(1) + .fold(TokenStream::new(), |mut acc, tok| { + acc.extend(tok); + acc + }) + .to_string() + ); + assert_eq!( + parse_from_file(ENTITY_FILES[i].as_bytes())?.to_string(), + EntityWriter::gen_compact_code_blocks( + entity, + &crate::WithSerde::None, + &crate::DateTimeCrate::Chrono, + &Some("public".to_owned()), + false, + false, + &TokenStream::new(), + &TokenStream::new(), + ) + .into_iter() + .skip(1) + .fold(TokenStream::new(), |mut acc, tok| { + acc.extend(tok); + acc + }) + .to_string() + ); + } + + Ok(()) + } } diff --git a/sea-orm-codegen/tests/postgres/binary_json.rs b/sea-orm-codegen/tests/postgres/binary_json.rs new file mode 100644 index 000000000..3d7cf9507 --- /dev/null +++ b/sea-orm-codegen/tests/postgres/binary_json.rs @@ -0,0 +1,21 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0 +//! +//! This file tests that the JsonBinary column type is annotated correctly is +//! compact entity form. More information can be found in this issue: +//! +//! https://github.com/SeaQL/sea-orm/issues/1344 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "task")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub payload: Json, + #[sea_orm(column_type = "JsonBinary")] + pub payload_binary: Json, +} +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} +impl ActiveModelBehavior for ActiveModel {} From e02e20ce75b71cf42fc83916064bc9e64ee063cc Mon Sep 17 00:00:00 2001 From: Forest Anderson Date: Sun, 15 Jan 2023 22:25:12 -0500 Subject: [PATCH 3/4] Added expanded entity format test --- sea-orm-codegen/src/entity/writer.rs | 23 +++++++ .../tests/postgres/binary_json_expanded.rs | 65 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 sea-orm-codegen/tests/postgres/binary_json_expanded.rs diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index a76dfd346..c22083ff2 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -2000,6 +2000,9 @@ mod tests { ]; const ENTITY_FILES: [&str; 1] = [include_str!("../../tests/postgres/binary_json.rs")]; + const ENTITY_FILES_EXPANDED: [&str; 1] = + [include_str!("../../tests/postgres/binary_json_expanded.rs")]; + assert_eq!(entities.len(), ENTITY_FILES.len()); for (i, entity) in entities.iter().enumerate() { @@ -2043,6 +2046,26 @@ mod tests { }) .to_string() ); + assert_eq!( + parse_from_file(ENTITY_FILES_EXPANDED[i].as_bytes())?.to_string(), + EntityWriter::gen_expanded_code_blocks( + entity, + &crate::WithSerde::None, + &crate::DateTimeCrate::Chrono, + &Some("schema_name".to_owned()), + false, + false, + &TokenStream::new(), + &TokenStream::new(), + ) + .into_iter() + .skip(1) + .fold(TokenStream::new(), |mut acc, tok| { + acc.extend(tok); + acc + }) + .to_string() + ); } Ok(()) diff --git a/sea-orm-codegen/tests/postgres/binary_json_expanded.rs b/sea-orm-codegen/tests/postgres/binary_json_expanded.rs new file mode 100644 index 000000000..543e77f1f --- /dev/null +++ b/sea-orm-codegen/tests/postgres/binary_json_expanded.rs @@ -0,0 +1,65 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0 +//! +//! This file tests that the JsonBinary column type is annotated correctly is +//! expanded entity form. More information can be found in this issue: +//! +//! https://github.com/SeaQL/sea-orm/issues/1344 + +use sea_orm::entity::prelude::*; + +#[derive(Copy, Clone, Default, Debug, DeriveEntity)] +pub struct Entity; +impl EntityName for Entity { + fn schema_name(&self) -> Option< &str > { + Some("schema_name") + } + fn table_name(&self) -> &str { + "task" + } +} + +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq)] +pub struct Model { + pub id: i32, + pub payload: Json, + pub payload_binary: Json, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] +pub enum Column { + Id, + Payload, + PayloadBinary, +} + +#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] +pub enum PrimaryKey { + Id, +} + +impl PrimaryKeyTrait for PrimaryKey { + type ValueType = i32; + fn auto_increment() -> bool { + true + } +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation {} +impl ColumnTrait for Column { + type EntityName = Entity; + fn def(&self) -> ColumnDef { + match self { + Self::Id => ColumnType::Integer.def(), + // This is the part that is being tested. + Self::Payload => ColumnType::Json.def(), + Self::PayloadBinary => ColumnType::JsonBinary.def(), + } + } +} +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + panic!("No RelationDef") + } +} +impl ActiveModelBehavior for ActiveModel {} From 44d65755fdb505a11ae9b77ed1342bb831f40b5d Mon Sep 17 00:00:00 2001 From: Forest Anderson Date: Sun, 15 Jan 2023 22:49:25 -0500 Subject: [PATCH 4/4] Fixed unit test --- sea-orm-codegen/src/entity/writer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index c22083ff2..e14883e15 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -1971,7 +1971,7 @@ mod tests { columns: vec![ Column { name: "id".to_owned(), - col_type: ColumnType::Integer(Some(11)), + col_type: ColumnType::Integer, auto_increment: true, not_null: true, unique: false,