launchbadge / sqlx

🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite.
Apache License 2.0
12.49k stars 1.19k forks source link

[offline] Strip whitespace before hashing keys for sqlx-data.json #1440

Open zopieux opened 2 years ago

zopieux commented 2 years ago

sqlx offline mode hashes the query SQL text in order to build keys for sqlx-data.json. I'm using raw multi-line string literals to help with readability:

sqlx::query!(r#"
  SELECT …
"#, foo);

As a result, even a simple cargo run fmt can change the indent of the closing tag and add trailing whitespace, resulting in a different hash, resulting in a cache miss and having to rebuild the file (prepare).

I would suggest applying a .strip() on the query string before hashing. This would not introduce any non-determinism: no SQL dialect will have a behavioral difference caused by leading or trailing whitespace, aka. SELECT 1 and \n\n SELECT 1\n\t \t\n are identical.


Note: this won't solve the issue of eg. indenting the whole block of SQL, but I don't want to suggest dealing with this, because that'd risk affecting the SQL body. To be safe this would require being able to parse SQL, which is a non-goal of sqlx.

djc commented 1 year ago

FWIW, this is also causing issues for us. Trailing whitespace in queries changes the query cache key, making it harder to work with.