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 <ccw.billy.123@gmail.com> * show info per column * refactor: use write! macro * Refactoring Co-authored-by: Billy Chan <ccw.billy.123@gmail.com>
This commit is contained in:
parent
e4f198fbc4
commit
0ce8ee6a31
@ -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<dyn Error>> {
|
||||
@ -21,6 +22,17 @@ pub async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box<dy
|
||||
.with_max_level(tracing::Level::DEBUG)
|
||||
.with_test_writer()
|
||||
.try_init();
|
||||
} else {
|
||||
let filter_layer = EnvFilter::try_new("sea_orm_codegen=info").unwrap();
|
||||
let fmt_layer = tracing_subscriber::fmt::layer()
|
||||
.with_target(false)
|
||||
.with_level(false)
|
||||
.without_time();
|
||||
|
||||
let _ = tracing_subscriber::registry()
|
||||
.with(filter_layer)
|
||||
.with(fmt_layer)
|
||||
.try_init();
|
||||
}
|
||||
|
||||
let max_connections = args
|
||||
|
@ -25,6 +25,7 @@ syn = { version = "^1", default-features = false, features = [
|
||||
quote = "^1"
|
||||
heck = "^0.3"
|
||||
proc-macro2 = "^1"
|
||||
tracing = { version = "0.1", features = ["log"] }
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = { version = "^0.7" }
|
||||
|
@ -3,6 +3,7 @@ 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 {
|
||||
@ -143,6 +144,33 @@ impl Column {
|
||||
}
|
||||
col_def
|
||||
}
|
||||
|
||||
pub fn get_info(&self) -> 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<ColumnDef> 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<String>");
|
||||
|
||||
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();
|
||||
|
@ -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::<Vec<String>>();
|
||||
|
||||
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"),
|
||||
}
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user