feat: allow to read a configuration file

This commit is contained in:
Alexander Navarro 2025-01-27 16:20:35 -03:00
parent 6ac6c2c178
commit fe5ae0de75
6 changed files with 86 additions and 62 deletions

View file

@ -4,4 +4,4 @@ dev:
cargo run --bin cli -- query "SELECT * FROM sources;" cargo run --bin cli -- query "SELECT * FROM sources;"
build-schema: build-schema:
cargo run --bin cli -- discover -o examples/schema.toml cargo run --bin cli -- discover -c examples/schema.toml -o examples/schema.toml

View file

@ -19,6 +19,56 @@ type = "Integer"
not_null = true not_null = true
reference = { table = "categories", identity = "id", label = "Category Id" } reference = { table = "categories", identity = "id", label = "Category Id" }
[public.entries]
display_name = "Entries"
[[public.entries.columns]]
name = "id"
type = "Integer"
default = "nextval('entries_id_seq'::regclass)"
not_null = true
[[public.entries.columns]]
name = "date"
type = "Date"
default = "CURRENT_TIMESTAMP"
not_null = true
[[public.entries.columns]]
name = "source_id"
type = "Integer"
not_null = true
reference = { table = "sources", identity = "id", label = "Source Id" }
[[public.entries.columns]]
name = "uid"
not_null = true
type = { Varchar = {} }
[[public.entries.columns]]
name = "text"
type = "Text"
not_null = false
[public.categories]
display_name = "Categories"
[[public.categories.columns]]
name = "id"
type = "Integer"
default = "nextval('category_id_seq'::regclass)"
not_null = true
[[public.categories.columns]]
name = "name"
not_null = true
type = { Varchar = { length = 20 } }
[[public.categories.columns]]
name = "uid"
not_null = false
type = { Varchar = { length = 8 } }
[public.sources] [public.sources]
display_name = "Sources" display_name = "Sources"
@ -58,53 +108,3 @@ type = { TimestampWithTimeZone = { precision = 6 } }
name = "deleted_at" name = "deleted_at"
not_null = false not_null = false
type = { TimestampWithTimeZone = { precision = 6 } } type = { TimestampWithTimeZone = { precision = 6 } }
[public.categories]
display_name = "Categories"
[[public.categories.columns]]
name = "id"
type = "Integer"
default = "nextval('category_id_seq'::regclass)"
not_null = true
[[public.categories.columns]]
name = "name"
not_null = true
type = { Varchar = { length = 20 } }
[[public.categories.columns]]
name = "uid"
not_null = false
type = { Varchar = { length = 8 } }
[public.entries]
display_name = "Entries"
[[public.entries.columns]]
name = "id"
type = "Integer"
default = "nextval('entries_id_seq'::regclass)"
not_null = true
[[public.entries.columns]]
name = "date"
type = "Date"
default = "CURRENT_TIMESTAMP"
not_null = true
[[public.entries.columns]]
name = "source_id"
type = "Integer"
not_null = true
reference = { table = "sources", identity = "id", label = "Source Id" }
[[public.entries.columns]]
name = "uid"
not_null = true
type = { Varchar = {} }
[[public.entries.columns]]
name = "text"
type = "Text"
not_null = false

View file

@ -1,6 +1,8 @@
#![allow(unused)] #![allow(unused)]
#![allow(dead_code)] #![allow(dead_code)]
use std::path::PathBuf;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use simple_crud::error::Error; use simple_crud::error::Error;
use simple_crud::error::Result; use simple_crud::error::Result;
@ -17,6 +19,9 @@ struct Cli {
#[arg(short = 'u', long, global = true, env = "CLI_DB_URL")] #[arg(short = 'u', long, global = true, env = "CLI_DB_URL")]
db_url: Option<String>, db_url: Option<String>,
#[arg(short, long, global = true, env = "CLI_CONFIG")]
config_file: Option<PathBuf>,
} }
#[derive(Subcommand)] #[derive(Subcommand)]
@ -52,7 +57,7 @@ async fn main() -> Result<()> {
let result = match &cli.command { let result = match &cli.command {
Commands::Query { sql } => sql::handle_query(url, sql).await, Commands::Query { sql } => sql::handle_query(url, sql).await,
Commands::Discover { schema, output } => { Commands::Discover { schema, output } => {
sql::discover_scheme(url, schema.to_owned(), output.to_owned()).await sql::discover_scheme(url, cli.config_file, schema.to_owned(), output.to_owned()).await
} }
}; };

View file

@ -17,6 +17,9 @@ pub enum Error {
#[error(transparent)] #[error(transparent)]
TomlEncoding(#[from] toml::ser::Error), TomlEncoding(#[from] toml::ser::Error),
#[error(transparent)]
TomlDecodign(#[from] toml::de::Error),
#[error(transparent)] #[error(transparent)]
ParseError(#[from] url::ParseError), ParseError(#[from] url::ParseError),

View file

@ -1,6 +1,6 @@
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::path::Path; use std::path::{Path, PathBuf};
use clap::ValueEnum; use clap::ValueEnum;
use sea_schema::postgres::discovery::SchemaDiscovery; use sea_schema::postgres::discovery::SchemaDiscovery;
@ -49,6 +49,7 @@ pub async fn handle_query(url: String, query: &String) -> Result<()> {
pub async fn discover_scheme( pub async fn discover_scheme(
url: String, url: String,
config_file: Option<PathBuf>,
schema: Option<String>, schema: Option<String>,
output: Option<String>, output: Option<String>,
) -> Result<()> { ) -> Result<()> {
@ -68,12 +69,13 @@ pub async fn discover_scheme(
} }
}; };
let mut data_model = DataModel::new(config_file)?;
println!("{:?}", data_model);
let schema = schema_discovery.discover().await?; let schema = schema_discovery.discover().await?;
let mut data_model = DataModel::new(); data_model.insert_schema(schema.schema.to_owned(), Schema::from(schema));
data_model
.schemas
.insert(schema.schema.to_owned(), Schema::from(schema));
let mut buffer: Box<dyn io::Write> = match output { let mut buffer: Box<dyn io::Write> = match output {
Some(path) => { Some(path) => {

View file

@ -2,12 +2,14 @@ use heck::ToTitleCase;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
use std::fs::read_to_string;
use std::io::Write; use std::io::Write;
use std::path::PathBuf;
use toml_edit::visit_mut::VisitMut; use toml_edit::visit_mut::VisitMut;
use sea_schema::postgres::def as sea_schema_def; use sea_schema::postgres::def as sea_schema_def;
use crate::error; use crate::error::{self, Result};
struct InlineTableFix; struct InlineTableFix;
@ -36,14 +38,26 @@ impl VisitMut for InlineTableFix {
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct DataModel { pub struct DataModel {
#[serde(flatten)] #[serde(flatten)]
pub schemas: HashMap<String, Schema>, schemas: HashMap<String, Schema>,
} }
impl DataModel { impl DataModel {
pub fn new() -> Self { pub fn new(path: Option<PathBuf>) -> Result<Self> {
Self { match path {
schemas: HashMap::new(), Some(path) => {
let content = read_to_string(&path)?;
let data_model: Self = toml::from_str(&content)?;
Ok(data_model)
} }
None => Ok(Self {
schemas: HashMap::new(),
}),
}
}
pub fn insert_schema(&mut self, name: String, schema: Schema) {
self.schemas.insert(name, schema);
} }
} }