52 lines
1.5 KiB
Rust
52 lines
1.5 KiB
Rust
use sqlx::Executor;
|
|
use std::str::FromStr;
|
|
|
|
use brass_config::{load_config, parse_env, Environment};
|
|
use clap::{Args, Parser, Subcommand};
|
|
use sqlx::{postgres::PgConnectOptions, Connection, PgConnection};
|
|
|
|
#[derive(Parser)]
|
|
#[command(about, long_about = None)]
|
|
struct Cli {
|
|
#[command(subcommand)]
|
|
command: Command,
|
|
#[arg(short, long, global = true, help = "Choose the environment (development, test, production).", value_parser = parse_env, default_value = "development")]
|
|
environment: Environment,
|
|
}
|
|
|
|
#[derive(Subcommand)]
|
|
enum Command {
|
|
Setup,
|
|
Reset,
|
|
}
|
|
|
|
#[derive(Args)]
|
|
struct CommandArgs {}
|
|
|
|
#[async_std::main]
|
|
async fn main() {
|
|
let cli = Cli::parse();
|
|
let config = load_config(&cli.environment).expect("Could not load config!");
|
|
|
|
match cli.command {
|
|
Command::Setup => {}
|
|
Command::Reset => {
|
|
let db_config = PgConnectOptions::from_str(&config.database_url).expect("Invalid DATABASE_URL!");
|
|
let db_name = db_config
|
|
.get_database()
|
|
.expect("Failed to get database name!");
|
|
|
|
let root_db_config = db_config.clone().database("postgres");
|
|
let mut root_connection: PgConnection = Connection::connect_with(&root_db_config).await.unwrap();
|
|
|
|
let query = format!("DROP DATABASE {}", db_name);
|
|
root_connection
|
|
.execute(query.as_str())
|
|
.await
|
|
.expect("Failed to drop database!");
|
|
|
|
//Ok(String::from(db_name))
|
|
}
|
|
}
|
|
}
|