use anyhow::{
Context as _,
Result,
};
use clap::{
Arg,
ArgMatches,
Command,
};
use midas::commander::{
Migrator,
MigratorOptions,
};
use midas::config::Config;
use midas::{
migration,
sequel,
};
use crate::commands::Context;
pub fn subcommands() -> Vec<Command> {
vec![
Command::new("create")
.about("Create the configured database")
.arg(
Arg::new("from-migrations")
.long("from-migrations")
.value_name("dir")
.help("Apply migrations from this directory after creating the database")
.num_args(1),
)
.arg(
Arg::new("from-schema")
.long("from-schema")
.value_name("path")
.help("Apply the schema DSL create-from-empty SQL")
.num_args(1),
),
Command::new("drop").about("Drop the configured database").arg(
Arg::new("force")
.long("force")
.help("Skip confirmation checks")
.action(clap::ArgAction::SetTrue),
),
Command::new("reset").about("Drop and recreate the configured database"),
Command::new("exists").about("Check whether the configured database exists"),
]
}
pub fn owns(name: &str) -> bool {
matches!(name, "create" | "drop" | "reset" | "exists")
}
pub fn dispatch(name: &str, matches: &ArgMatches, context: &Context) -> Result<()> {
match name {
"create" => create(
context,
matches.get_one::<String>("from-migrations"),
matches.get_one::<String>("from-schema"),
),
"drop" => drop_database(context),
"reset" => {
drop_database(context)?;
create(context, None, None)
},
"exists" => exists(context),
other => anyhow::bail!("unknown db lifecycle subcommand `{other}`"),
}
}
fn create(context: &Context, from_migrations: Option<&String>, from_schema: Option<&String>) -> Result<()> {
ensure_database(context)?;
if let Some(dir) = from_migrations {
let mut config = context.config.clone();
config.migrations_dir = dir.into();
apply_migrations(&config, context)?;
}
if let Some(path) = from_schema {
let desired = midas_schema::schema::load_ir(std::path::Path::new(path))?;
let sql = midas_schema::schema::create_sql(&desired)?;
if !context.dry_run {
let mut backend = sequel::connect(&context.config)?;
backend.execute_batch(&sql)?;
}
println!("Applied schema DSL from `{path}`.");
}
Ok(())
}
pub(crate) fn ensure_database(context: &Context) -> Result<bool> {
let database_url = context.config.require_database_url()?;
let name = sequel::database_name(database_url)?;
let mut admin = sequel::connect_admin(&context.config)?;
if admin.database_exists(&name)? {
println!("Database `{name}` already exists.");
return Ok(false);
}
if context.dry_run {
println!("Would create database `{name}`.");
return Ok(false);
}
admin
.create_database(&name)
.with_context(|| format!("failed to create database `{name}`"))?;
println!("Created database `{name}`.");
Ok(true)
}
fn drop_database(context: &Context) -> Result<()> {
let database_url = context.config.require_database_url()?;
let name = sequel::database_name(database_url)?;
let mut admin = sequel::connect_admin(&context.config)?;
if !admin.database_exists(&name)? {
println!("Database `{name}` does not exist.");
return Ok(());
}
admin
.force_drop_database(&name)
.with_context(|| format!("failed to drop database `{name}`"))?;
println!("Dropped database `{name}`.");
Ok(())
}
fn exists(context: &Context) -> Result<()> {
let database_url = context.config.require_database_url()?;
let name = sequel::database_name(database_url)?;
let mut admin = sequel::connect_admin(&context.config)?;
let exists = admin.database_exists(&name)?;
if exists {
println!("Database `{name}` exists.");
} else {
println!("Database `{name}` does not exist.");
}
Ok(())
}
fn apply_migrations(config: &Config, context: &Context) -> Result<()> {
let migrations = migration::discover(&config.migrations_dir)?;
let backend = sequel::connect(config)?;
let options = MigratorOptions {
dry_run: context.dry_run,
no_lock: context.no_lock,
transaction: config.transaction,
quiet: false,
ignore_missing: context.ignore_missing,
};
let mut migrator = Migrator::new(backend, migrations).with_options(options);
migrator.up(None)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn owns_lifecycle_commands() {
for name in ["create", "drop", "reset", "exists"] {
assert!(owns(name));
}
assert!(!owns("pull"));
}
}