pub async fn run(cfg: &AppConfig) -> Result<Changeset>Expand description
2-way diff only (no conflict detection).
Returns the raw Changeset (source vs. current target).
Use run_with_conflicts if you need the 3-way merge.
Use run_with_timing if you also want a performance report.
Examples found in repository?
examples/diff_as_lib.rs (line 40)
36async fn from_config_file(path: &str) -> Result<()> {
37 println!("=== Pattern 1: from config file ({path}) ===\n");
38
39 let cfg = AppConfig::load(path)?;
40 let changeset = diffly::run(&cfg).await?;
41
42 // Write all three output formats (JSON / SQL / HTML)
43 for writer in all_writers() {
44 write_to_file(&*writer, &changeset, &cfg.output.dir)?;
45 println!(
46 "Written: {}/{}.{}",
47 cfg.output.dir,
48 changeset.changeset_id,
49 writer.extension()
50 );
51 }
52
53 print_summary(&changeset);
54 Ok(())
55}
56
57// ─────────────────────────────────────────────────────────────────────────────
58// Pattern 2 — build AppConfig entirely in code, no TOML file required.
59// Useful when config comes from env vars, a CLI flag, a database row, etc.
60// ─────────────────────────────────────────────────────────────────────────────
61async fn programmatic_config() -> Result<()> {
62 println!("=== Pattern 2: programmatic config ===\n");
63
64 let db = |schema: &str| DbConfig {
65 driver: "postgres".into(),
66 host: std::env::var("DB_HOST").unwrap_or_else(|_| "localhost".into()),
67 port: 5432,
68 dbname: "diffly".into(),
69 user: "diffly".into(),
70 password: "diffly".into(),
71 schema: schema.into(),
72 };
73
74 let cfg = AppConfig {
75 source: db("source"),
76 target: db("target"),
77 diff: DiffConfig {
78 tables: vec![
79 TableConfig {
80 name: "pricing_rules".into(),
81 primary_key: vec!["id".into()],
82 excluded_columns: ExcludedColumns(vec![
83 "created_at".into(),
84 "updated_at".into(),
85 ]),
86 },
87 TableConfig {
88 name: "discount_tiers".into(),
89 primary_key: vec!["id".into()],
90 excluded_columns: ExcludedColumns::default(),
91 },
92 TableConfig {
93 name: "tax_rules".into(),
94 primary_key: vec!["region_code".into(), "product_category".into()],
95 excluded_columns: ExcludedColumns::default(),
96 },
97 ],
98 },
99 output: OutputConfig {
100 dir: "./output".into(),
101 },
102 };
103
104 let changeset = diffly::run(&cfg).await?;
105
106 // Write only the SQL migration file
107 let sql_writer = writer_for("sql").expect("sql writer always available");
108 write_to_file(&*sql_writer, &changeset, &cfg.output.dir)?;
109 println!(
110 "SQL written: {}/{}.sql\n",
111 cfg.output.dir, changeset.changeset_id
112 );
113
114 // Hand off to pattern 3
115 inspect_changeset(&changeset);
116 Ok(())
117}