start reworking config
This commit is contained in:
@@ -6,7 +6,8 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.5.32", features = ["derive", "env"] }
|
clap = { version = "4.5.32", features = ["derive", "env"] }
|
||||||
color-eyre = "0.6.3"
|
color-eyre = "0.6.3"
|
||||||
figment = { version = "0.10.19", features = ["toml"] }
|
confique = { version = "0.3.0", features = ["yaml", "toml"] }
|
||||||
|
inquire = "0.7.5"
|
||||||
sea-orm-codegen = "1.1.8"
|
sea-orm-codegen = "1.1.8"
|
||||||
sea-schema = { version = "0.16.1", features = ["sqlx-all"] }
|
sea-schema = { version = "0.16.1", features = ["sqlx-all"] }
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use url::Url;
|
|||||||
use crate::Config;
|
use crate::Config;
|
||||||
pub async fn get_tables(
|
pub async fn get_tables(
|
||||||
database_url: String,
|
database_url: String,
|
||||||
config: Config,
|
config: &Config,
|
||||||
) -> Result<(Option<String>, Vec<TableCreateStatement>)> {
|
) -> Result<(Option<String>, Vec<TableCreateStatement>)> {
|
||||||
let url = Url::parse(&database_url)?;
|
let url = Url::parse(&database_url)?;
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ pub async fn get_tables(
|
|||||||
use sea_schema::mysql::discovery::SchemaDiscovery;
|
use sea_schema::mysql::discovery::SchemaDiscovery;
|
||||||
use sqlx::MySql;
|
use sqlx::MySql;
|
||||||
|
|
||||||
tracing::info!("Connecting to MySQL ...");
|
tracing::info!("Connecting to MySQL");
|
||||||
let connection = sqlx_connect::<MySql>(
|
let connection = sqlx_connect::<MySql>(
|
||||||
config.sea_orm.max_connections,
|
config.sea_orm.max_connections,
|
||||||
config.sea_orm.acquire_timeout,
|
config.sea_orm.acquire_timeout,
|
||||||
@@ -60,7 +60,7 @@ pub async fn get_tables(
|
|||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
tracing::info!("Discovering schema ...");
|
tracing::info!("Discovering schema");
|
||||||
let schema_discovery = SchemaDiscovery::new(connection, database_name);
|
let schema_discovery = SchemaDiscovery::new(connection, database_name);
|
||||||
let schema = schema_discovery.discover().await?;
|
let schema = schema_discovery.discover().await?;
|
||||||
let table_stmts = schema
|
let table_stmts = schema
|
||||||
@@ -77,7 +77,7 @@ pub async fn get_tables(
|
|||||||
use sea_schema::sqlite::discovery::SchemaDiscovery;
|
use sea_schema::sqlite::discovery::SchemaDiscovery;
|
||||||
use sqlx::Sqlite;
|
use sqlx::Sqlite;
|
||||||
|
|
||||||
tracing::info!("Connecting to SQLite ...");
|
tracing::info!("Connecting to SQLite");
|
||||||
let connection = sqlx_connect::<Sqlite>(
|
let connection = sqlx_connect::<Sqlite>(
|
||||||
config.sea_orm.max_connections,
|
config.sea_orm.max_connections,
|
||||||
config.sea_orm.acquire_timeout,
|
config.sea_orm.acquire_timeout,
|
||||||
@@ -86,7 +86,7 @@ pub async fn get_tables(
|
|||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
tracing::info!("Discovering schema ...");
|
tracing::info!("Discovering schema");
|
||||||
let schema_discovery = SchemaDiscovery::new(connection);
|
let schema_discovery = SchemaDiscovery::new(connection);
|
||||||
let schema = schema_discovery
|
let schema = schema_discovery
|
||||||
.discover()
|
.discover()
|
||||||
@@ -106,7 +106,7 @@ pub async fn get_tables(
|
|||||||
use sea_schema::postgres::discovery::SchemaDiscovery;
|
use sea_schema::postgres::discovery::SchemaDiscovery;
|
||||||
use sqlx::Postgres;
|
use sqlx::Postgres;
|
||||||
|
|
||||||
tracing::info!("Connecting to Postgres ...");
|
tracing::info!("Connecting to Postgres");
|
||||||
let schema = config
|
let schema = config
|
||||||
.sea_orm
|
.sea_orm
|
||||||
.database_schema
|
.database_schema
|
||||||
@@ -119,7 +119,7 @@ pub async fn get_tables(
|
|||||||
Some(schema),
|
Some(schema),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
tracing::info!("Discovering schema ...");
|
tracing::info!("Discovering schema");
|
||||||
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?;
|
||||||
let table_stmts = schema
|
let table_stmts = schema
|
||||||
@@ -130,7 +130,7 @@ pub async fn get_tables(
|
|||||||
.filter(|schema| filter_skip_tables(&schema.info.name))
|
.filter(|schema| filter_skip_tables(&schema.info.name))
|
||||||
.map(|schema| schema.write())
|
.map(|schema| schema.write())
|
||||||
.collect();
|
.collect();
|
||||||
(config.sea_orm.database_schema, table_stmts)
|
(config.sea_orm.database_schema.clone(), table_stmts)
|
||||||
}
|
}
|
||||||
_ => unimplemented!("{} is not supported", url.scheme()),
|
_ => unimplemented!("{} is not supported", url.scheme()),
|
||||||
};
|
};
|
||||||
|
|||||||
68
src/main.rs
68
src/main.rs
@@ -1,32 +1,53 @@
|
|||||||
mod generate;
|
mod generate;
|
||||||
use std::{fs, path::PathBuf};
|
use std::{fs, path::PathBuf, str::FromStr};
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use color_eyre::Result;
|
use color_eyre::{Report, Result};
|
||||||
use figment::{
|
use figment::{
|
||||||
providers::{Format, Serialized, Toml},
|
providers::{Format, Serialized, Toml},
|
||||||
Figment,
|
Figment,
|
||||||
};
|
};
|
||||||
|
use sea_orm_codegen::{
|
||||||
|
DateTimeCrate as CodegenDateTimeCrate, EntityTransformer, EntityWriterContext, WithPrelude,
|
||||||
|
WithSerde,
|
||||||
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||||
struct Config {
|
struct Config {
|
||||||
sea_orm: SeaOrmConfig,
|
sea_orm: SeaOrmConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||||
|
enum DateTimeCrate {
|
||||||
|
Time,
|
||||||
|
Chrono,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<DateTimeCrate> for CodegenDateTimeCrate {
|
||||||
|
fn from(date_time_crate: DateTimeCrate) -> CodegenDateTimeCrate {
|
||||||
|
match date_time_crate {
|
||||||
|
DateTimeCrate::Chrono => CodegenDateTimeCrate::Chrono,
|
||||||
|
DateTimeCrate::Time => CodegenDateTimeCrate::Time,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||||
struct SeaOrmConfig {
|
struct SeaOrmConfig {
|
||||||
expanded_format: bool,
|
expanded_format: bool,
|
||||||
table: SeaOrmTableConfig,
|
table: SeaOrmTableConfig,
|
||||||
include_hidden_tables: bool,
|
with_serde: String,
|
||||||
with_serde: bool,
|
with_prelude: String,
|
||||||
|
with_copy_enums: bool,
|
||||||
serde_skip_deserializing_primary_key: bool,
|
serde_skip_deserializing_primary_key: bool,
|
||||||
serde_skip_hidden_column: bool,
|
serde_skip_hidden_column: bool,
|
||||||
max_connections: u32,
|
max_connections: u32,
|
||||||
acquire_timeout: u64,
|
acquire_timeout: u64,
|
||||||
database_schema: Option<String>,
|
database_schema: Option<String>,
|
||||||
|
date_format: DateTimeCrate,
|
||||||
}
|
}
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||||
struct SeaOrmTableConfig {
|
struct SeaOrmTableConfig {
|
||||||
include_hidden: bool,
|
include_hidden: bool,
|
||||||
only: Vec<String>,
|
only: Vec<String>,
|
||||||
@@ -46,15 +67,40 @@ impl Default for Config {
|
|||||||
max_connections: 10,
|
max_connections: 10,
|
||||||
acquire_timeout: 30,
|
acquire_timeout: 30,
|
||||||
expanded_format: false,
|
expanded_format: false,
|
||||||
include_hidden_tables: false,
|
with_copy_enums: false,
|
||||||
with_serde: false,
|
with_serde: String::from("none"),
|
||||||
|
with_prelude: String::from("none"),
|
||||||
serde_skip_hidden_column: false,
|
serde_skip_hidden_column: false,
|
||||||
serde_skip_deserializing_primary_key: false,
|
serde_skip_deserializing_primary_key: false,
|
||||||
|
date_format: DateTimeCrate::Time,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TryInto<EntityWriterContext> for Config {
|
||||||
|
type Error = Report;
|
||||||
|
fn try_into(self) -> Result<EntityWriterContext> {
|
||||||
|
Ok(EntityWriterContext::new(
|
||||||
|
self.sea_orm.expanded_format,
|
||||||
|
WithPrelude::from_str(&self.sea_orm.with_prelude)?,
|
||||||
|
WithSerde::from_str(&self.sea_orm.with_serde)?,
|
||||||
|
self.sea_orm.with_copy_enums,
|
||||||
|
self.sea_orm.date_format.into(),
|
||||||
|
self.sea_orm.database_schema,
|
||||||
|
false,
|
||||||
|
self.sea_orm.serde_skip_deserializing_primary_key,
|
||||||
|
self.sea_orm.serde_skip_hidden_column,
|
||||||
|
Vec::new(),
|
||||||
|
Vec::new(),
|
||||||
|
Vec::new(),
|
||||||
|
Vec::new(),
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
struct Args {
|
struct Args {
|
||||||
#[clap(short, long, default_value = "generator.toml")]
|
#[clap(short, long, default_value = "generator.toml")]
|
||||||
@@ -73,6 +119,8 @@ async fn main() -> Result<()> {
|
|||||||
.extract()?;
|
.extract()?;
|
||||||
tracing::info!(?config);
|
tracing::info!(?config);
|
||||||
tracing::info!(?args);
|
tracing::info!(?args);
|
||||||
generate::get_tables(args.database_url, config).await?;
|
let (_, table_stmts) = generate::get_tables(args.database_url, &config).await?;
|
||||||
|
let writer_context = config.clone().try_into()?;
|
||||||
|
let output = EntityTransformer::transform(table_stmts)?.generate(&writer_context);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user