use clap::{Arg, ArgMatches, Command};
use std::path::Path;
use crate::cmd::resolve_db_path;
pub fn command() -> Command {
Command::new("migrate")
.about("Apply any pending database schema migrations")
.long_about(
"Open the local commonmeta database and apply any pending schema \
migrations, then exit. Safe to run on databases built with older \
releases: migrations are idempotent and only alter the schema \
(no existing records are modified or removed).\n\n\
Examples:\n\n\
commonmeta migrate\n\
commonmeta migrate --file /var/lib/commonmeta/commonmeta.sqlite3",
)
.arg(
Arg::new("file")
.long("file")
.help("Path to the SQLite database (overrides COMMONMETA_DB and platform default)"),
)
}
pub fn execute(matches: &ArgMatches) -> Result<(), String> {
let db_path_str = resolve_db_path(matches.get_one::<String>("file"));
let db_path = Path::new(&db_path_str);
if !db_path.exists() {
return Err(format!(
"database not found at '{}'; run 'commonmeta import' first",
db_path_str
));
}
commonmeta::upsert_sqlite(&[], db_path).map_err(|e| e.to_string())?;
println!("migrated {}", db_path_str);
Ok(())
}