Skip to content

Commit

Permalink
sea-orm-migration: use clap derive
Browse files Browse the repository at this point in the history
  • Loading branch information
Thanh Van committed May 13, 2022
1 parent 4feedaf commit 8a55dcf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 57 deletions.
4 changes: 2 additions & 2 deletions sea-orm-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub enum MigrateSubcommands {
default_value = "1",
help = "Number of pending migrations to be rolled back"
)]
num: i64,
num: u32,
},
#[clap(about = "Rollback applied migrations")]
Down {
Expand All @@ -69,7 +69,7 @@ pub enum MigrateSubcommands {
default_value = "1",
help = "Number of pending migrations to be rolled back"
)]
num: i64,
num: u32,
},
}

Expand Down
89 changes: 34 additions & 55 deletions sea-orm-migration/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use clap::{Command, Arg};
use clap::Parser;
use dotenv::dotenv;
use std::{fmt::Display, process::exit};
use tracing_subscriber::{prelude::*, EnvFilter};

use sea_orm::{Database, DbConn};
use sea_orm_cli::migration::get_subcommands;
use sea_orm_cli::MigrateSubcommands;

use super::MigratorTrait;

Expand All @@ -15,28 +15,30 @@ where
dotenv().ok();
let url = std::env::var("DATABASE_URL").expect("Environment variable 'DATABASE_URL' not set");
let db = &Database::connect(&url).await.unwrap();
let app = build_cli();
get_matches(migrator, db, app).await;
let cli = Cli::parse();

run_migrate(migrator, db, cli.command, cli.verbose).await;
}

pub async fn get_matches<M>(_: M, db: &DbConn, app: Command<'static>)
where
pub async fn run_migrate<M>(
_: M,
db: &DbConn,
command: Option<MigrateSubcommands>,
verbose: Option<bool>,
) where
M: MigratorTrait,
{
let matches = app.get_matches();
let mut verbose = false;
let filter = match matches.subcommand() {
Some((_, args)) => match args.is_present("VERBOSE") {
true => {
verbose = true;
"debug"
}
let filter = match verbose {
Some(v) => match v {
true => "debug",
false => "sea_orm_migration=info",
},
None => "sea_schema::migration=info",
};

let filter_layer = EnvFilter::try_new(filter).unwrap();
if verbose {

if let Some(_) = verbose {
let fmt_layer = tracing_subscriber::fmt::layer();
tracing_subscriber::registry()
.with(filter_layer)
Expand All @@ -52,50 +54,27 @@ where
.with(fmt_layer)
.init()
};
match matches.subcommand() {
Some((cmd, args)) => match cmd {
"fresh" => M::fresh(db).await,
"refresh" => M::refresh(db).await,
"reset" => M::reset(db).await,
"status" => M::status(db).await,
"up" => match args.subcommand() {
Some(_) => {
let str = args.value_of("NUM_MIGRATION").unwrap_or_default();
let steps = str.parse().ok();
M::up(db, steps).await
}
None => M::up(db, None).await,
},
"down" => match args.subcommand() {
Some(_) => {
let str = args.value_of("NUM_MIGRATION").unwrap();
let steps = str.parse().ok().unwrap_or(1);
M::down(db, Some(steps)).await
}
None => M::down(db, Some(1)).await,
},
_ => M::up(db, None).await,
},
None => M::up(db, None).await,

match command {
Some(MigrateSubcommands::Fresh) => M::fresh(db).await,
Some(MigrateSubcommands::Refresh) => M::refresh(db).await,
Some(MigrateSubcommands::Reset) => M::reset(db).await,
Some(MigrateSubcommands::Status) => M::status(db).await,
Some(MigrateSubcommands::Up { num }) => M::up(db, Some(num)).await,
Some(MigrateSubcommands::Down { num }) => M::down(db, Some(num)).await,
_ => M::up(db, None).await,
}
.unwrap_or_else(handle_error);
}

pub fn build_cli() -> Command<'static> {
let mut app = Command::new("sea-orm-migration")
.version(env!("CARGO_PKG_VERSION"))
.arg(
Arg::new("VERBOSE")
.long("verbose")
.short('v')
.help("Show debug messages")
.takes_value(false)
.global(true),
);
for subcommand in get_subcommands() {
app = app.subcommand(subcommand);
}
app
#[derive(Parser)]
#[clap(version)]
pub struct Cli {
#[clap(short = 'v', long, global = true, help = "Show debug messages")]
verbose: Option<bool>,

#[clap(subcommand)]
command: Option<MigrateSubcommands>,
}

fn handle_error<E>(error: E)
Expand Down

0 comments on commit 8a55dcf

Please sign in to comment.