Update CLI generate entity schema argument behavior

This commit is contained in:
Billy Chan 2021-08-10 23:36:27 +08:00 committed by Chris Tsang
parent ff8d921343
commit 10d670e472
3 changed files with 11 additions and 3 deletions

View File

@ -9,5 +9,9 @@ cargo run -- -h
Running Entity Generator: Running Entity Generator:
```sh ```sh
cargo run -- generate entity -u mysql://sea:sea@localhost/bakery -s bakery -o out # MySQL (`--database-schema` option is ignored)
cargo run -- generate entity -u mysql://sea:sea@localhost/bakery -o out
# PostgreSQL
cargo run -- generate entity -u postgres://sea:sea@localhost/bakery -s public -o out
``` ```

View File

@ -21,8 +21,10 @@ pub fn build_cli() -> App<'static, 'static> {
.long("database-schema") .long("database-schema")
.short("s") .short("s")
.help("Database schema") .help("Database schema")
.long_help("Database schema\n \
- For MySQL, this argument is ignored.\n \
- For PostgreSQL, this argument is optional with default value 'public'.")
.takes_value(true) .takes_value(true)
.required(true)
.env("DATABASE_SCHEMA"), .env("DATABASE_SCHEMA"),
) )
.arg( .arg(

View File

@ -23,13 +23,14 @@ async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box<dyn Er
match matches.subcommand() { match matches.subcommand() {
("entity", Some(args)) => { ("entity", Some(args)) => {
let url = args.value_of("DATABASE_URL").unwrap(); let url = args.value_of("DATABASE_URL").unwrap();
let schema = args.value_of("DATABASE_SCHEMA").unwrap();
let output_dir = args.value_of("OUTPUT_DIR").unwrap(); let output_dir = args.value_of("OUTPUT_DIR").unwrap();
let table_stmts = if url.starts_with("mysql://") { let table_stmts = if url.starts_with("mysql://") {
use sea_schema::mysql::discovery::SchemaDiscovery; use sea_schema::mysql::discovery::SchemaDiscovery;
use sqlx::MySqlPool; use sqlx::MySqlPool;
let url_parts: Vec<&str> = url.split("/").collect();
let schema = url_parts.last().unwrap();
let connection = MySqlPool::connect(url).await?; let connection = MySqlPool::connect(url).await?;
let schema_discovery = SchemaDiscovery::new(connection, schema); let schema_discovery = SchemaDiscovery::new(connection, schema);
let schema = schema_discovery.discover().await; let schema = schema_discovery.discover().await;
@ -42,6 +43,7 @@ async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box<dyn Er
use sea_schema::postgres::discovery::SchemaDiscovery; use sea_schema::postgres::discovery::SchemaDiscovery;
use sqlx::PgPool; use sqlx::PgPool;
let schema = args.value_of("DATABASE_SCHEMA").unwrap_or("public");
let connection = PgPool::connect(url).await?; let connection = PgPool::connect(url).await?;
let schema_discovery = SchemaDiscovery::new(connection, schema); let schema_discovery = SchemaDiscovery::new(connection, schema);
let schema = schema_discovery.discover().await; let schema = schema_discovery.discover().await;