use clap::{Args, Parser, ValueEnum};
use std::path::PathBuf;
#[derive(Debug, Parser)]
pub struct Tern {
#[clap(subcommand)]
pub commands: TernCommands,
}
#[derive(Debug, Parser)]
pub enum TernCommands {
Migrate(Migrate),
History(History),
}
#[derive(Debug, Parser)]
pub struct Migrate {
#[clap(subcommand)]
pub commands: MigrateCommands,
}
#[derive(Debug, Parser)]
pub struct History {
#[clap(subcommand)]
pub commands: HistoryCommands,
}
#[derive(Debug, Parser)]
pub enum MigrateCommands {
Apply {
#[arg(short, long)]
dryrun: bool,
#[arg(long)]
target_version: Option<i64>,
#[clap(flatten)]
connect_opts: ConnectOpts,
},
ApplyAll {
#[arg(short, long)]
dryrun: bool,
#[clap(flatten)]
connect_opts: ConnectOpts,
},
SoftApply {
#[arg(short, long)]
dryrun: bool,
#[arg(long)]
target_version: Option<i64>,
#[clap(flatten)]
connect_opts: ConnectOpts,
},
ListApplied {
#[clap(flatten)]
connect_opts: ConnectOpts,
},
New {
description: String,
no_tx: bool,
#[arg(long = "type", value_enum)]
migration_type: MigrationType,
#[clap(flatten)]
source: Source,
},
}
#[derive(Debug, Parser)]
pub enum HistoryCommands {
Init {
#[clap(flatten)]
connect_opts: ConnectOpts,
},
Drop {
#[clap(flatten)]
connect_opts: ConnectOpts,
},
SoftApply {
#[arg(long)]
from_version: Option<i64>,
#[arg(long)]
to_version: Option<i64>,
#[clap(flatten)]
connect_opts: ConnectOpts,
},
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum MigrationType {
Sql,
Rust,
}
#[derive(Debug, Args)]
pub struct Source {
#[clap(long)]
pub path: PathBuf,
}
#[derive(Debug, Args)]
pub struct ConnectOpts {
#[clap(long, short = 'D', env)]
pub database_url: Option<String>,
}
impl ConnectOpts {
pub fn required_db_url(&self) -> anyhow::Result<&str> {
self.database_url.as_deref().ok_or_else(
|| anyhow::anyhow!(
"the `--database-url/-D` option or the `DATABASE_URL` environment variable must be provided"
)
)
}
}