Repository: diesel-rs/diesel (language: rust)

Issue:
Print a diff if `diesel print-schema --locked` fails due to a mismatch
We want to improve the user experience of diesel-cli. The `diesel print-schema` command provides a `--locked` schema flag that allows users to check if there `schema.rs` file is up to data. It's designed to be used in CI setups. It currently only provides the feedback that the schema file would change, but there is no output what exactly would change. To improve the user experience there we would like to print a diff between the current version of the schema stored in the `schema.rs` file and what `schema.rs` file would be constructed from the database.

The relevant comparison code is here: https://github.com/diesel-rs/diesel/blob/09d5e1050a40d698b10af98bd10495c98ccfaa7f/diesel_cli/src/main.rs#L284-L288

Implementing this feature should be as simple as:

1. Add a dependency on [similar-assert](https://docs.rs/similar-asserts/latest/) to the `Cargo.toml` file of `diesel-cli`
2. Construct a instance of [`similar_assert::SimpleDiff`](https://docs.rs/similar-asserts/latest/similar_asserts/struct.SimpleDiff.html) from the old and new version of the schema
3. Print the diff via `println!`


Discussion / hints:
id like to have a go at this

Fix patch:
diff --git a/diesel_cli/Cargo.toml b/diesel_cli/Cargo.toml
index 842db31c4892..1452b299de9a 100644
--- a/diesel_cli/Cargo.toml
+++ b/diesel_cli/Cargo.toml
@@ -56,6 +56,7 @@ syn = { version = "2", features = ["visit"] }
 tracing = "0.1"
 tracing-subscriber = { version = "0.3.10", features = ["env-filter"] }
 thiserror = "1.0.10"
+similar-asserts = "1.5.0"
 
 [dependencies.diesel]
 version = "~2.2.0"
diff --git a/diesel_cli/src/main.rs b/diesel_cli/src/main.rs
index 140dfffdfe08..fc571ea9b65b 100644
--- a/diesel_cli/src/main.rs
+++ b/diesel_cli/src/main.rs
@@ -30,6 +30,7 @@ use database::InferConnection;
 use diesel::backend::Backend;
 use diesel::Connection;
 use diesel_migrations::{FileBasedMigrations, HarnessWithOutput, MigrationHarness};
+use similar_asserts::SimpleDiff;
 use std::error::Error;
 use std::io::stdout;
 use std::path::{Path, PathBuf};
@@ -282,6 +283,14 @@ fn regenerate_schema_if_file_specified(matches: &ArgMatches) -> Result<(), crate
                     .map_err(|e| crate::errors::Error::IoError(e, Some(path.to_owned())))?;
 
                 if schema.lines().ne(old_buf.lines()) {
+                    // it's fine to leak here, we will
+                    // exit the application anyway soon
+                    let label = path.file_name().expect("We have a file name here");
+                    let label = label.to_string_lossy().into_owned().leak();
+                    println!(
+                        "{}",
+                        SimpleDiff::from_str(&old_buf, &schema, label, "new schema")
+                    );
                     return Err(crate::errors::Error::SchemaWouldChange(
                         path.display().to_string(),
                     ));


Test patch: