Skip to content

Commit

Permalink
Generate entity files with PostgreSQL's schema name (#422)
Browse files Browse the repository at this point in the history
* feat(codegen): generate entity files with PostgreSQL's schema name

* Ignore schema name `public`

* Restore changes

* Fix test cases

* cargo fmt

* [cli] fixup
  • Loading branch information
billy1624 authored Jul 11, 2022
1 parent 1a8d22d commit c5aa63e
Show file tree
Hide file tree
Showing 18 changed files with 1,029 additions and 73 deletions.
21 changes: 13 additions & 8 deletions sea-orm-cli/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use chrono::Local;
use regex::Regex;
use sea_orm_codegen::{
EntityTransformer, EntityWriterContext, OutputFile, WithSerde, DateTimeCrate as CodegenDateTimeCrate,
DateTimeCrate as CodegenDateTimeCrate, EntityTransformer, EntityWriterContext, OutputFile,
WithSerde,
};
use std::{error::Error, fmt::Display, fs, io::Write, path::Path, process::Command, str::FromStr};
use tracing_subscriber::{prelude::*, EnvFilter};
Expand Down Expand Up @@ -109,22 +110,23 @@ pub async fn run_generate_command(
Default::default()
};

let table_stmts = match url.scheme() {
let (schema_name, table_stmts) = match url.scheme() {
"mysql" => {
use sea_schema::mysql::discovery::SchemaDiscovery;
use sqlx::MySql;

let connection = connect::<MySql>(max_connections, url.as_str()).await?;
let schema_discovery = SchemaDiscovery::new(connection, database_name);
let schema = schema_discovery.discover().await;
schema
let table_stmts = schema
.tables
.into_iter()
.filter(|schema| filter_tables(&schema.info.name))
.filter(|schema| filter_hidden_tables(&schema.info.name))
.filter(|schema| filter_skip_tables(&schema.info.name))
.map(|schema| schema.write())
.collect()
.collect();
(None, table_stmts)
}
"sqlite" => {
use sea_schema::sqlite::discovery::SchemaDiscovery;
Expand All @@ -133,14 +135,15 @@ pub async fn run_generate_command(
let connection = connect::<Sqlite>(max_connections, url.as_str()).await?;
let schema_discovery = SchemaDiscovery::new(connection);
let schema = schema_discovery.discover().await?;
schema
let table_stmts = schema
.tables
.into_iter()
.filter(|schema| filter_tables(&schema.name))
.filter(|schema| filter_hidden_tables(&schema.name))
.filter(|schema| filter_skip_tables(&schema.name))
.map(|schema| schema.write())
.collect()
.collect();
(None, table_stmts)
}
"postgres" | "postgresql" => {
use sea_schema::postgres::discovery::SchemaDiscovery;
Expand All @@ -150,14 +153,15 @@ pub async fn run_generate_command(
let connection = connect::<Postgres>(max_connections, url.as_str()).await?;
let schema_discovery = SchemaDiscovery::new(connection, schema);
let schema = schema_discovery.discover().await;
schema
let table_stmts = schema
.tables
.into_iter()
.filter(|schema| filter_tables(&schema.info.name))
.filter(|schema| filter_hidden_tables(&schema.info.name))
.filter(|schema| filter_skip_tables(&schema.info.name))
.map(|schema| schema.write())
.collect()
.collect();
(Some(schema.schema), table_stmts)
}
_ => unimplemented!("{} is not supported", url.scheme()),
};
Expand All @@ -166,6 +170,7 @@ pub async fn run_generate_command(
expanded_format,
WithSerde::from_str(&with_serde).unwrap(),
date_time_crate.into(),
schema_name,
);
let output = EntityTransformer::transform(table_stmts)?.generate(&writer_context);

Expand Down
Loading

0 comments on commit c5aa63e

Please sign in to comment.