restructure generator
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
pub mod db;
|
||||
use crate::generator::DatabaseUrl;
|
||||
|
||||
use super::{Module, ModulesContext};
|
||||
@@ -5,28 +6,97 @@ use color_eyre::Result;
|
||||
use serde::Deserialize;
|
||||
use serde_inline_default::serde_inline_default;
|
||||
|
||||
#[serde_inline_default]
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct DiscoveryConfig {
|
||||
#[serde_inline_default(false)]
|
||||
pub enable: bool,
|
||||
#[serde_inline_default(None)]
|
||||
pub database_schema: Option<String>,
|
||||
#[serde_inline_default(10)]
|
||||
pub max_connections: u32,
|
||||
#[serde_inline_default(30)]
|
||||
pub acquire_timeout: u32,
|
||||
pub acquire_timeout: u64,
|
||||
pub filter: DiscoveryFilterConfig,
|
||||
}
|
||||
impl Default for DiscoveryConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
enable: false,
|
||||
database_schema: None,
|
||||
max_connections: 10,
|
||||
acquire_timeout: 30,
|
||||
filter: DiscoveryFilterConfig::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct DiscoveryFilterConfig {
|
||||
pub include_hidden: bool,
|
||||
pub skip_seaql_migrations: bool,
|
||||
#[serde(flatten)]
|
||||
pub table: Option<TableConfig>,
|
||||
}
|
||||
|
||||
impl Default for DiscoveryFilterConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
include_hidden: false,
|
||||
skip_seaql_migrations: true,
|
||||
table: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
#[serde(untagged)]
|
||||
pub enum TableConfig {
|
||||
Specific { only: Vec<String> },
|
||||
Exclude { exclude: Vec<String> },
|
||||
}
|
||||
|
||||
impl DiscoveryFilterConfig {
|
||||
pub fn get_filter(&self) -> Box<dyn Fn(&String) -> bool + Send> {
|
||||
let include_hidden = self.include_hidden;
|
||||
if let Some(table) = &self.table {
|
||||
match table {
|
||||
TableConfig::Specific { only } => {
|
||||
let specific = only.clone();
|
||||
Box::new(move |table: &String| {
|
||||
(include_hidden || !table.starts_with('_')) && specific.contains(table)
|
||||
})
|
||||
}
|
||||
TableConfig::Exclude { exclude } => {
|
||||
let exclude = exclude.clone();
|
||||
Box::new(move |table: &String| {
|
||||
(include_hidden || !table.starts_with('_')) && !exclude.contains(table)
|
||||
})
|
||||
}
|
||||
}
|
||||
} else if self.skip_seaql_migrations {
|
||||
Box::new(move |table: &String| {
|
||||
(include_hidden || !table.starts_with('_'))
|
||||
&& !table.starts_with("seaql_migrations")
|
||||
})
|
||||
} else {
|
||||
Box::new(move |table: &String| (include_hidden || !table.starts_with('_')))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DiscoveredSchema {
|
||||
pub tables: Vec<Table>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DiscoveryModule;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Module for DiscoveryModule {
|
||||
fn init(&self, ctx: &mut ModulesContext) -> Result<()> {
|
||||
fn init(&mut self, ctx: &mut ModulesContext) -> Result<()> {
|
||||
ctx.get_config_auto::<DiscoveryConfig>("modules.discovery")?;
|
||||
Ok(())
|
||||
}
|
||||
async fn validate(&self, ctx: &mut ModulesContext) -> Result<bool> {
|
||||
async fn validate(&mut self, ctx: &mut ModulesContext) -> Result<bool> {
|
||||
let map = ctx.get_anymap();
|
||||
|
||||
if let (Some(config), Some(_)) = (map.get::<DiscoveryConfig>(), map.get::<DatabaseUrl>()) {
|
||||
@@ -36,4 +106,17 @@ impl Module for DiscoveryModule {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
async fn execute(&mut self, ctx: &mut ModulesContext) -> Result<()> {
|
||||
if let (Some(config), Some(url)) = (
|
||||
ctx.get_anymap().get::<DiscoveryConfig>(),
|
||||
ctx.get_anymap().get::<DatabaseUrl>(),
|
||||
) {
|
||||
let url = url.clone();
|
||||
|
||||
let (stmts, db_type) = db::get_tables(url.0, config).await?;
|
||||
tracing::info!(?stmts, ?db_type);
|
||||
// db::generate(ctx).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user