diff --git a/sea-orm-codegen/src/entity/active_enum.rs b/sea-orm-codegen/src/entity/active_enum.rs index a6e71948ca..cb5468b3b0 100644 --- a/sea-orm-codegen/src/entity/active_enum.rs +++ b/sea-orm-codegen/src/entity/active_enum.rs @@ -192,6 +192,7 @@ mod tests { &TokenStream::new(), &TokenStream::new(), EntityFormat::Compact, + Default::default(), ) .to_string(), quote!( @@ -235,6 +236,7 @@ mod tests { &TokenStream::new(), &TokenStream::new(), EntityFormat::Compact, + Default::default(), ) .to_string(), quote!( @@ -287,6 +289,7 @@ mod tests { &bonus_derive(["specta::Type", "ts_rs::TS"]), &TokenStream::new(), EntityFormat::Compact, + Default::default(), ) .to_string(), build_generated_enum(), @@ -324,6 +327,7 @@ mod tests { &TokenStream::new(), &bonus_attributes([r#"serde(rename_all = "camelCase")"#]), EntityFormat::Compact, + Default::default(), ) .to_string(), quote!( @@ -357,6 +361,7 @@ mod tests { &TokenStream::new(), &bonus_attributes([r#"serde(rename_all = "camelCase")"#, "ts(export)"]), EntityFormat::Compact, + Default::default(), ) .to_string(), quote!( @@ -408,6 +413,7 @@ mod tests { &TokenStream::new(), &TokenStream::new(), EntityFormat::Compact, + Default::default(), ) .to_string(), quote!( diff --git a/sea-orm-codegen/src/entity/writer/oxide.rs b/sea-orm-codegen/src/entity/writer/oxide.rs index cdc818bc2a..310db0e7f9 100644 --- a/sea-orm-codegen/src/entity/writer/oxide.rs +++ b/sea-orm-codegen/src/entity/writer/oxide.rs @@ -40,10 +40,33 @@ impl EntityWriter { imports.extend(Self::gen_import_serde(with_serde)); imports.extend(Self::gen_import_active_enum(entity)); + imports.extend(Self::gen_import_uuid(entity)); imports } + pub fn gen_import_uuid(entity: &Entity) -> TokenStream { + fn has_uuid(col_type: &sea_query::ColumnType) -> bool { + match col_type { + sea_query::ColumnType::Uuid => true, + sea_query::ColumnType::Array(inner) => has_uuid(inner), + _ => false, + } + } + + if entity + .columns + .iter() + .any(|col| has_uuid(col.get_inner_col_type())) + { + quote! { + use uuid::Uuid; + } + } else { + TokenStream::new() + } + } + #[allow(clippy::too_many_arguments)] pub fn gen_oxide_model_struct( entity: &Entity, @@ -199,3 +222,63 @@ impl EntityWriter { } } } + +#[cfg(test)] +mod tests { + use crate::{Column, Entity, EntityWriter}; + use sea_query::{ColumnType, RcOrArc}; + + fn column(name: &str, col_type: ColumnType) -> Column { + Column { + name: name.to_owned(), + col_type, + auto_increment: false, + not_null: true, + unique: false, + unique_key: None, + } + } + + fn entity(columns: Vec) -> Entity { + Entity { + table_name: "test".to_owned(), + columns, + relations: vec![], + conjunct_relations: vec![], + primary_keys: vec![], + } + } + + #[test] + fn gen_import_uuid_emits_import_for_uuid_column() { + let entity = entity(vec![ + column("id", ColumnType::BigInteger), + column("uuid", ColumnType::Uuid), + ]); + assert_eq!( + EntityWriter::gen_import_uuid(&entity).to_string(), + "use uuid :: Uuid ;" + ); + } + + #[test] + fn gen_import_uuid_emits_import_for_uuid_array_column() { + let entity = entity(vec![column( + "uuids", + ColumnType::Array(RcOrArc::new(ColumnType::Uuid)), + )]); + assert_eq!( + EntityWriter::gen_import_uuid(&entity).to_string(), + "use uuid :: Uuid ;" + ); + } + + #[test] + fn gen_import_uuid_emits_nothing_without_uuid_column() { + let entity = entity(vec![ + column("id", ColumnType::BigInteger), + column("name", ColumnType::Text), + ]); + assert!(EntityWriter::gen_import_uuid(&entity).is_empty()); + } +}