sql-gen 0.1.3

A CLI tool for generating models based on a SQL Database using SQLx
use clap::{App, Arg, SubCommand};

mod db_queries;
mod generate;
mod migrate;
mod models;
mod query_generate;
mod utils;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    dotenv::dotenv().ok();
    let matches = App::new("SQL Gen")
        .subcommand(
            SubCommand::with_name("generate")
                .about("Generate structs and queries for tables")
                .arg(
                    Arg::with_name("output")
                        .short('o')
                        .long("output")
                        .value_name("SQLGEN_MODEL_OUTPUT_FOLDER")
                        .help("Sets the output folder for generated structs")
                        .takes_value(true)
                        .required(true),
                )
                .arg(
                    Arg::with_name("database")
                        .short('d')
                        .long("database")
                        .value_name("DATABASE_URL")
                        .help("Sets the database connection URL")
                        .takes_value(true)
                        .required(true),
                )
                .arg(
                    Arg::with_name("context")
                        .short('c')
                        .long("context")
                        .value_name("SQLGEN_CONTEXT_NAME")
                        .help("The name of the context for calling functions. Defaults to DB name")
                        .takes_value(true),
                )
                .arg(
                    Arg::with_name("schema")
                        .short('s')
                        .long("schema")
                        .takes_value(true)
                        .multiple(true)
                        .use_delimiter(true)
                        .help("Specify the schema name(s)"),
                )
                .arg(
                    Arg::with_name("table")
                        .short('t')
                        .long("table")
                        .takes_value(true)
                        .value_name("SQLGEN_TABLE")
                        .multiple(true)
                        .use_delimiter(true)
                        .help("Specify the table name(s)"),
                )
                .arg(
                    Arg::new("force")
                        .short('f')
                        .long("force")
                        .value_name("SQLGEN_OVERWRITE")
                        .takes_value(false)
                        .required(false)
                        .help("Overwrites existing files sharing names in that folder"),
                ),
        )
        .subcommand(
            SubCommand::with_name("migrate")
                .about("Generate SQL migrations based on struct differences")
                .arg(
                    Arg::with_name("include")
                        .short('i')
                        .long("include")
                        .value_name("SQLGEN_MODEL_FOLDER")
                        .help("Sets the folder containing existing struct files")
                        .takes_value(true)
                        .required(true),
                )
                .arg(
                    Arg::with_name("table")
                        .short('t')
                        .long("table")
                        .value_name("SQLGEN_TABLE")
                        .takes_value(true)
                        .multiple(true)
                        .help("Specify the table name(s)"),
                )
                .arg(
                    Arg::with_name("schema")
                        .short('s')
                        .long("schema")
                        .takes_value(true)
                        .use_delimiter(true)
                        .multiple(true)
                        .help("Specify the schema name(s)"),
                )
                .arg(
                    Arg::with_name("output")
                        .short('o')
                        .long("output")
                        .value_name("SQLGEN_MIGRATION_OUTPUT")
                        .help("Sets the output folder for migrations")
                        .takes_value(true)
                        .required(true),
                )
                .arg(
                    Arg::with_name("database")
                        .short('d')
                        .long("database")
                        .value_name("DATABASE_URL")
                        .help("Sets the database connection URL")
                        .takes_value(true)
                        .required(true),
                ),
        )
        .get_matches();

    if let Some(matches) = matches.subcommand_matches("generate") {
        let output_folder = matches.value_of("output").unwrap();
        let context = matches.value_of("context");
        let database_url = matches.value_of("database").unwrap();
        // let tables: Option<Vec<&str>> = matches.values_of("table").map(|tables| tables.collect());
        let schemas: Option<Vec<&str>> =
            matches.values_of("schema").map(|schemas| schemas.collect());
        let force = matches.is_present("force");
        generate::generate(output_folder, database_url, context, force, None, schemas).await?;
    } else if let Some(matches) = matches.subcommand_matches("migrate") {
        let include_folder = matches.value_of("include").unwrap();
        let output_folder = matches.value_of("output").unwrap();
        let database_url = matches.value_of("database").unwrap();
        // let tables: Option<Vec<&str>> = matches.values_of("table").map(|tables| tables.collect());
        let schemas: Option<Vec<&str>> =
            matches.values_of("schema").map(|schemas| schemas.collect());
        migrate::migrate(include_folder, output_folder, database_url, None, None).await?;
    }
    Ok(())
}