Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sea-orm-codegen/src/entity/active_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ mod tests {
&TokenStream::new(),
&TokenStream::new(),
EntityFormat::Compact,
Default::default(),
)
.to_string(),
quote!(
Expand Down Expand Up @@ -235,6 +236,7 @@ mod tests {
&TokenStream::new(),
&TokenStream::new(),
EntityFormat::Compact,
Default::default(),
)
.to_string(),
quote!(
Expand Down Expand Up @@ -287,6 +289,7 @@ mod tests {
&bonus_derive(["specta::Type", "ts_rs::TS"]),
&TokenStream::new(),
EntityFormat::Compact,
Default::default(),
)
.to_string(),
build_generated_enum(),
Expand Down Expand Up @@ -324,6 +327,7 @@ mod tests {
&TokenStream::new(),
&bonus_attributes([r#"serde(rename_all = "camelCase")"#]),
EntityFormat::Compact,
Default::default(),
)
.to_string(),
quote!(
Expand Down Expand Up @@ -357,6 +361,7 @@ mod tests {
&TokenStream::new(),
&bonus_attributes([r#"serde(rename_all = "camelCase")"#, "ts(export)"]),
EntityFormat::Compact,
Default::default(),
)
.to_string(),
quote!(
Expand Down Expand Up @@ -408,6 +413,7 @@ mod tests {
&TokenStream::new(),
&TokenStream::new(),
EntityFormat::Compact,
Default::default(),
)
.to_string(),
quote!(
Expand Down
83 changes: 83 additions & 0 deletions sea-orm-codegen/src/entity/writer/oxide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Column>) -> 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());
}
}