From 8d7ca3536d9bb286b7fa057efca03dabdac1deac Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Mon, 16 May 2022 14:41:17 +0900 Subject: [PATCH 1/7] feat(sea-orm-cli): output lof about generated file name. https://github.com/SeaQL/sea-orm/issues/722 --- sea-orm-cli/src/commands.rs | 12 ++++++++++++ sea-orm-codegen/Cargo.toml | 1 + sea-orm-codegen/src/entity/writer.rs | 9 ++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/sea-orm-cli/src/commands.rs b/sea-orm-cli/src/commands.rs index dadb3ad2f..fad32ad75 100644 --- a/sea-orm-cli/src/commands.rs +++ b/sea-orm-cli/src/commands.rs @@ -4,6 +4,7 @@ use regex::Regex; use sea_orm_codegen::{EntityTransformer, OutputFile, WithSerde}; use std::{error::Error, fmt::Display, fs, io::Write, path::Path, process::Command, str::FromStr}; use url::Url; +use tracing_subscriber::{prelude::*, EnvFilter}; pub async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box> { match matches.subcommand() { @@ -21,6 +22,17 @@ pub async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box Date: Mon, 16 May 2022 17:28:31 +0900 Subject: [PATCH 2/7] include column names in info --- sea-orm-codegen/src/entity/writer.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 5b51f686c..00d6efe48 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -96,10 +96,12 @@ impl EntityWriter { .iter() .map(|entity| { let entity_file = format!("{}.rs", entity.get_table_name_snake_case()); - info!( - "Generating {}", - entity_file - ); + let column_names = entity + .columns + .iter() + .map(|column| column.name.clone()) + .collect::>(); + info!("Generating {} {:?}", entity_file, column_names); let mut lines = Vec::new(); Self::write_doc_comment(&mut lines); From 49e312375b516f7dab81c49a7aba2938c5899776 Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Mon, 16 May 2022 17:28:41 +0900 Subject: [PATCH 3/7] cargo fmt --- sea-orm-cli/src/commands.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sea-orm-cli/src/commands.rs b/sea-orm-cli/src/commands.rs index fad32ad75..73ccc13bb 100644 --- a/sea-orm-cli/src/commands.rs +++ b/sea-orm-cli/src/commands.rs @@ -3,8 +3,8 @@ use clap::ArgMatches; use regex::Regex; use sea_orm_codegen::{EntityTransformer, OutputFile, WithSerde}; use std::{error::Error, fmt::Display, fs, io::Write, path::Path, process::Command, str::FromStr}; -use url::Url; use tracing_subscriber::{prelude::*, EnvFilter}; +use url::Url; pub async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box> { match matches.subcommand() { From ac586f74288b27a40e1481242d127bb9f787b700 Mon Sep 17 00:00:00 2001 From: kyoto <50972773+kyoto7250@users.noreply.github.com> Date: Tue, 17 May 2022 13:55:56 +0900 Subject: [PATCH 4/7] Update sea-orm-cli/src/commands.rs Avoid multiple initializations Co-authored-by: Billy Chan --- sea-orm-cli/src/commands.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sea-orm-cli/src/commands.rs b/sea-orm-cli/src/commands.rs index 73ccc13bb..04a3d3b0b 100644 --- a/sea-orm-cli/src/commands.rs +++ b/sea-orm-cli/src/commands.rs @@ -29,10 +29,10 @@ pub async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box Date: Tue, 17 May 2022 14:48:17 +0900 Subject: [PATCH 5/7] show info per column --- sea-orm-codegen/src/entity/column.rs | 57 ++++++++++++++++++++++++++++ sea-orm-codegen/src/entity/writer.rs | 10 +++-- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/sea-orm-codegen/src/entity/column.rs b/sea-orm-codegen/src/entity/column.rs index 3ba09446b..3606a5789 100644 --- a/sea-orm-codegen/src/entity/column.rs +++ b/sea-orm-codegen/src/entity/column.rs @@ -143,6 +143,30 @@ impl Column { } col_def } + + pub fn get_info(&self) -> String { + let type_info = self.get_rs_type().to_string().replace(" ", ""); + let key_info = self.key_info(); + if key_info.is_empty() { + return format!("Column `{}`: {}", self.name, type_info); + } else { + return format!("Column `{}`: {}, {}", self.name, type_info, key_info); + } + } + + fn key_info(&self) -> String { + let mut vec: Vec<&str> = vec![]; + if self.auto_increment { + vec.push("auto_increment") + } + if self.not_null { + vec.push("not_null") + } + if self.unique { + vec.push("unique") + } + return vec.join(", "); + } } impl From for Column { @@ -361,6 +385,39 @@ mod tests { } } + #[test] + fn test_get_info() { + let column: Column = ColumnDef::new(Alias::new("id")) + .string() + .to_owned() + .into(); + assert_eq!(column.get_info(), String::from("Column `id`: Option")); + + let column: Column = ColumnDef::new(Alias::new("id")) + .string() + .not_null() + .to_owned() + .into(); + assert_eq!(column.get_info(), String::from("Column `id`: String, not_null")); + + let column: Column = ColumnDef::new(Alias::new("id")) + .string() + .not_null() + .unique_key() + .to_owned() + .into(); + assert_eq!(column.get_info(), String::from("Column `id`: String, not_null, unique")); + + let column: Column = ColumnDef::new(Alias::new("id")) + .string() + .not_null() + .unique_key() + .auto_increment() + .to_owned() + .into(); + assert_eq!(column.get_info(), String::from("Column `id`: String, auto_increment, not_null, unique")); + } + #[test] fn test_from_column_def() { let column: Column = ColumnDef::new(Alias::new("id")).string().to_owned().into(); diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 00d6efe48..922f4bced 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -96,12 +96,16 @@ impl EntityWriter { .iter() .map(|entity| { let entity_file = format!("{}.rs", entity.get_table_name_snake_case()); - let column_names = entity + let column_info = entity .columns .iter() - .map(|column| column.name.clone()) + .map(|column| column.get_info()) .collect::>(); - info!("Generating {} {:?}", entity_file, column_names); + + info!("Generating {}", entity_file); + for info in column_info.iter() { + info!(" > {}", info); + } let mut lines = Vec::new(); Self::write_doc_comment(&mut lines); From b57e17269a1a5416cc13ba04691ac1911a91a555 Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Tue, 17 May 2022 22:22:46 +0900 Subject: [PATCH 6/7] refactor: use write! macro --- sea-orm-codegen/src/entity/column.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/sea-orm-codegen/src/entity/column.rs b/sea-orm-codegen/src/entity/column.rs index 3606a5789..37c0a36e9 100644 --- a/sea-orm-codegen/src/entity/column.rs +++ b/sea-orm-codegen/src/entity/column.rs @@ -1,3 +1,4 @@ +use sea_query::Write; use crate::util::escape_rust_keyword; use heck::{CamelCase, SnakeCase}; use proc_macro2::{Ident, TokenStream}; @@ -150,22 +151,22 @@ impl Column { if key_info.is_empty() { return format!("Column `{}`: {}", self.name, type_info); } else { - return format!("Column `{}`: {}, {}", self.name, type_info, key_info); + return format!("Column `{}`: {}{}", self.name, type_info, key_info); } } fn key_info(&self) -> String { - let mut vec: Vec<&str> = vec![]; + let mut info = String::from(""); if self.auto_increment { - vec.push("auto_increment") + write!(&mut info, ", auto_increment").expect(&format!("Not written `{}`", self.get_name_snake_case())); } if self.not_null { - vec.push("not_null") + write!(&mut info, ", not_null").expect(&format!("Not written `{}`", self.get_name_snake_case())); } if self.unique { - vec.push("unique") + write!(&mut info, ", unique").expect(&format!("Not written `{}`", self.get_name_snake_case())); } - return vec.join(", "); + return info; } } From c68de770664c2e8e4ec9fb16aff49ad3e79a54c4 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Wed, 18 May 2022 11:15:57 +0800 Subject: [PATCH 7/7] Refactoring --- sea-orm-codegen/src/entity/column.rs | 50 ++++++++++++++++------------ 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/sea-orm-codegen/src/entity/column.rs b/sea-orm-codegen/src/entity/column.rs index 37c0a36e9..8f8c95534 100644 --- a/sea-orm-codegen/src/entity/column.rs +++ b/sea-orm-codegen/src/entity/column.rs @@ -1,9 +1,9 @@ -use sea_query::Write; use crate::util::escape_rust_keyword; use heck::{CamelCase, SnakeCase}; use proc_macro2::{Ident, TokenStream}; use quote::{format_ident, quote}; use sea_query::{ColumnDef, ColumnSpec, ColumnType}; +use std::fmt::Write as FmtWrite; #[derive(Clone, Debug)] pub struct Column { @@ -146,27 +146,30 @@ impl Column { } pub fn get_info(&self) -> String { - let type_info = self.get_rs_type().to_string().replace(" ", ""); - let key_info = self.key_info(); - if key_info.is_empty() { - return format!("Column `{}`: {}", self.name, type_info); - } else { - return format!("Column `{}`: {}{}", self.name, type_info, key_info); - } + let mut info = String::new(); + let type_info = self.get_rs_type().to_string().replace(' ', ""); + let col_info = self.col_info(); + write!( + &mut info, + "Column `{}`: {}{}", + self.name, type_info, col_info + ) + .unwrap(); + info } - fn key_info(&self) -> String { - let mut info = String::from(""); + fn col_info(&self) -> String { + let mut info = String::new(); if self.auto_increment { - write!(&mut info, ", auto_increment").expect(&format!("Not written `{}`", self.get_name_snake_case())); + write!(&mut info, ", auto_increment").unwrap(); } if self.not_null { - write!(&mut info, ", not_null").expect(&format!("Not written `{}`", self.get_name_snake_case())); + write!(&mut info, ", not_null").unwrap(); } if self.unique { - write!(&mut info, ", unique").expect(&format!("Not written `{}`", self.get_name_snake_case())); + write!(&mut info, ", unique").unwrap(); } - return info; + info } } @@ -388,18 +391,15 @@ mod tests { #[test] fn test_get_info() { - let column: Column = ColumnDef::new(Alias::new("id")) - .string() - .to_owned() - .into(); - assert_eq!(column.get_info(), String::from("Column `id`: Option")); + let column: Column = ColumnDef::new(Alias::new("id")).string().to_owned().into(); + assert_eq!(column.get_info().as_str(), "Column `id`: Option"); let column: Column = ColumnDef::new(Alias::new("id")) .string() .not_null() .to_owned() .into(); - assert_eq!(column.get_info(), String::from("Column `id`: String, not_null")); + assert_eq!(column.get_info().as_str(), "Column `id`: String, not_null"); let column: Column = ColumnDef::new(Alias::new("id")) .string() @@ -407,7 +407,10 @@ mod tests { .unique_key() .to_owned() .into(); - assert_eq!(column.get_info(), String::from("Column `id`: String, not_null, unique")); + assert_eq!( + column.get_info().as_str(), + "Column `id`: String, not_null, unique" + ); let column: Column = ColumnDef::new(Alias::new("id")) .string() @@ -416,7 +419,10 @@ mod tests { .auto_increment() .to_owned() .into(); - assert_eq!(column.get_info(), String::from("Column `id`: String, auto_increment, not_null, unique")); + assert_eq!( + column.get_info().as_str(), + "Column `id`: String, auto_increment, not_null, unique" + ); } #[test]