From 0ce8ee6a31e5d3bd44f79d82109d5adaa786e3b4 Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Sun, 12 Jun 2022 21:30:49 +0900 Subject: [PATCH] feat(sea-orm-cli): output log about generated file name. (#735) * feat(sea-orm-cli): output lof about generated file name. https://github.com/SeaQL/sea-orm/issues/722 * include column names in info * cargo fmt * Update sea-orm-cli/src/commands.rs Avoid multiple initializations Co-authored-by: Billy Chan * show info per column * refactor: use write! macro * Refactoring Co-authored-by: Billy Chan --- sea-orm-cli/src/commands.rs | 12 ++++++ sea-orm-codegen/Cargo.toml | 1 + sea-orm-codegen/src/entity/column.rs | 64 ++++++++++++++++++++++++++++ sea-orm-codegen/src/entity/writer.rs | 15 ++++++- 4 files changed, 91 insertions(+), 1 deletion(-) diff --git a/sea-orm-cli/src/commands.rs b/sea-orm-cli/src/commands.rs index dadb3ad2..04a3d3b0 100644 --- a/sea-orm-cli/src/commands.rs +++ b/sea-orm-cli/src/commands.rs @@ -3,6 +3,7 @@ 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 tracing_subscriber::{prelude::*, EnvFilter}; use url::Url; pub async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box> { @@ -21,6 +22,17 @@ pub async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box String { + 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 col_info(&self) -> String { + let mut info = String::new(); + if self.auto_increment { + write!(&mut info, ", auto_increment").unwrap(); + } + if self.not_null { + write!(&mut info, ", not_null").unwrap(); + } + if self.unique { + write!(&mut info, ", unique").unwrap(); + } + info + } } impl From for Column { @@ -361,6 +389,42 @@ mod tests { } } + #[test] + fn test_get_info() { + 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().as_str(), "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().as_str(), + "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().as_str(), + "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 88d72ef3..922f4bce 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -4,6 +4,7 @@ use proc_macro2::TokenStream; use quote::{format_ident, quote}; use std::{collections::HashMap, str::FromStr}; use syn::{punctuated::Punctuated, token::Comma}; +use tracing::info; #[derive(Clone, Debug)] pub struct EntityWriter { @@ -94,6 +95,18 @@ impl EntityWriter { self.entities .iter() .map(|entity| { + let entity_file = format!("{}.rs", entity.get_table_name_snake_case()); + let column_info = entity + .columns + .iter() + .map(|column| column.get_info()) + .collect::>(); + + info!("Generating {}", entity_file); + for info in column_info.iter() { + info!(" > {}", info); + } + let mut lines = Vec::new(); Self::write_doc_comment(&mut lines); let code_blocks = if expanded_format { @@ -103,7 +116,7 @@ impl EntityWriter { }; Self::write(&mut lines, code_blocks); OutputFile { - name: format!("{}.rs", entity.get_table_name_snake_case()), + name: entity_file, content: lines.join("\n\n"), } })