use super::common::{BEHAVIOR, FILTERING, INPUT_OUTPUT, LIMITS, MODE, OUTPUT_FORMAT};
use crate::differ::{format_diff, DiffConfig, DiffOutputFormat, Differ};
use clap::{Args, ValueHint};
use std::collections::HashMap;
use std::io::Write;
use std::path::PathBuf;
use std::time::Instant;
#[derive(Args)]
pub struct DiffArgs {
#[arg(value_hint = ValueHint::FilePath, help_heading = INPUT_OUTPUT)]
old_file: PathBuf,
#[arg(value_hint = ValueHint::FilePath, help_heading = INPUT_OUTPUT)]
new_file: PathBuf,
#[arg(short, long, value_hint = ValueHint::FilePath, help_heading = INPUT_OUTPUT)]
output: Option<PathBuf>,
#[arg(short, long, help_heading = INPUT_OUTPUT)]
dialect: Option<String>,
#[arg(short, long, help_heading = FILTERING)]
tables: Option<String>,
#[arg(short, long, help_heading = FILTERING)]
exclude: Option<String>,
#[arg(long, help_heading = FILTERING)]
ignore_columns: Option<String>,
#[arg(long, conflicts_with = "data_only", help_heading = MODE)]
schema_only: bool,
#[arg(long, conflicts_with = "schema_only", help_heading = MODE)]
data_only: bool,
#[arg(long, help_heading = MODE)]
primary_key: Option<String>,
#[arg(long, help_heading = BEHAVIOR)]
allow_no_pk: bool,
#[arg(long, help_heading = BEHAVIOR)]
ignore_order: bool,
#[arg(long, default_value = "10000000", help_heading = LIMITS)]
max_pk_entries: usize,
#[arg(short, long, default_value = "text", help_heading = OUTPUT_FORMAT)]
format: Option<String>,
#[arg(short, long, help_heading = OUTPUT_FORMAT)]
verbose: bool,
#[arg(short, long, help_heading = OUTPUT_FORMAT)]
progress: bool,
}
fn parse_pk_overrides(s: &str) -> HashMap<String, Vec<String>> {
s.split(',')
.filter_map(|pair| {
let (table, cols) = pair.split_once(':')?;
let columns: Vec<String> = cols.split('+').map(|c| c.trim().to_string()).collect();
Some((table.trim().to_lowercase(), columns))
})
.collect()
}
pub fn run(args: DiffArgs) -> anyhow::Result<()> {
let DiffArgs {
old_file,
new_file,
output,
dialect,
tables,
exclude,
ignore_columns,
schema_only,
data_only,
primary_key,
allow_no_pk,
ignore_order,
max_pk_entries,
format,
verbose,
progress,
} = args;
if !old_file.exists() {
anyhow::bail!("Old file does not exist: {}", old_file.display());
}
if !new_file.exists() {
anyhow::bail!("New file does not exist: {}", new_file.display());
}
let output_format: DiffOutputFormat = format
.as_deref()
.map(|f| f.parse().map_err(|e: String| anyhow::anyhow!(e)))
.transpose()?
.unwrap_or(DiffOutputFormat::Text);
let is_json = matches!(output_format, DiffOutputFormat::Json);
let resolved_dialect = super::common::resolve_dialect(&old_file, dialect.as_deref(), false)?;
let tables_filter: Vec<String> = tables
.map(|t| t.split(',').map(|s| s.trim().to_string()).collect())
.unwrap_or_default();
let exclude_filter: Vec<String> = exclude
.map(|t| t.split(',').map(|s| s.trim().to_string()).collect())
.unwrap_or_default();
let old_size = std::fs::metadata(&old_file)?.len();
let new_size = std::fs::metadata(&new_file)?.len();
let total_bytes = if schema_only || data_only {
old_size + new_size
} else {
(old_size + new_size) * 2
};
if !is_json {
eprintln!("Comparing: {} → {}", old_file.display(), new_file.display());
eprintln!(
" Old: {:.2} MB, New: {:.2} MB",
old_size as f64 / (1024.0 * 1024.0),
new_size as f64 / (1024.0 * 1024.0)
);
eprintln!(" Dialect: {}", resolved_dialect);
eprintln!();
}
let start_time = Instant::now();
let pb = if progress && !is_json {
let pb = super::common::byte_progress_bar(total_bytes);
pb.set_message("Comparing...");
Some(pb)
} else {
None
};
let pk_overrides = primary_key
.as_deref()
.map(parse_pk_overrides)
.unwrap_or_default();
let ignore_columns_vec: Vec<String> = ignore_columns
.map(|s| s.split(',').map(|p| p.trim().to_string()).collect())
.unwrap_or_default();
let config = DiffConfig {
old_path: old_file,
new_path: new_file,
dialect: Some(resolved_dialect),
schema_only,
data_only,
tables: tables_filter,
exclude: exclude_filter,
format: output_format,
verbose,
progress,
max_pk_entries,
allow_no_pk,
ignore_column_order: ignore_order,
pk_overrides,
ignore_columns: ignore_columns_vec.clone(),
};
let mut differ = Differ::new(config.clone());
if let Some(ref pb) = pb {
let pb_clone = pb.clone();
differ = differ.with_progress(move |current, total| {
pb_clone.set_length(total);
pb_clone.set_position(current);
});
}
let result = differ.diff()?;
if let Some(pb) = pb {
pb.finish_with_message("done");
}
let elapsed = start_time.elapsed();
let output_text = format_diff(&result, output_format, resolved_dialect);
match output {
Some(path) => {
let mut file = std::fs::File::create(&path)?;
file.write_all(output_text.as_bytes())?;
if !is_json {
eprintln!("Output written to: {}", path.display());
}
}
None => {
print!("{}", output_text);
}
}
if !is_json {
eprintln!();
eprintln!("Diff completed in {:.3?}", elapsed);
}
Ok(())
}