Skip to content

Commit

Permalink
fix(datamodel-renderer): sort generator fields deterministically (#5114)
Browse files Browse the repository at this point in the history
  • Loading branch information
FGoessler authored Jan 13, 2025
1 parent c571c6d commit a046f87
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions schema-engine/datamodel-renderer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
once_cell = "1.15.0"
psl.workspace = true
regex.workspace = true
itertools.workspace = true
base64 = "0.13.1"

[dev-dependencies]
Expand Down
31 changes: 28 additions & 3 deletions schema-engine/datamodel-renderer/src/configuration/generator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::value::{Array, Documentation, Env, Text, Value};
use itertools::Itertools;
use psl::PreviewFeature;
use std::{borrow::Cow, fmt};

Expand Down Expand Up @@ -143,7 +144,7 @@ impl fmt::Display for Generator<'_> {
writeln!(f, "binaryTargets = {}", self.binary_targets)?;
}

for (k, v) in self.config.iter() {
for (k, v) in self.config.iter().sorted_by_key(|(k, _)| k) {
writeln!(f, "{k} = {v}")?;
}

Expand Down Expand Up @@ -198,14 +199,38 @@ mod tests {
output = "/dev/null"
previewFeatures = ["multiSchema", "postgresqlExtensions"]
binaryTargets = [env("BINARY TARGET")]
afterGenerate = ["lambda", [], ["print", ["quote", "done!"]]]
customFeatures = ["enums", "models"]
customValue = "meow"
otherValue = "purr"
customFeatures = ["enums", "models"]
afterGenerate = ["lambda", [], ["print", ["quote", "done!"]]]
}
"#]];

let rendered = psl::reformat(&format!("{generator}"), 2).unwrap();
expected.assert_eq(&rendered)
}

#[test]
fn creates_consistent_ordering() {
let mut generator1 = Generator::new("client", Env::value("prisma-client-js"));
generator1.push_config_value("first", "A");
generator1.push_config_value("second", "B");
let rendered1 = psl::reformat(&format!("{generator1}"), 2).unwrap();

let mut generator2 = Generator::new("client", Env::value("prisma-client-js"));
generator2.push_config_value("second", "B");
generator2.push_config_value("first", "A");
let rendered2 = psl::reformat(&format!("{generator2}"), 2).unwrap();

let expected = expect![[r#"
generator client {
provider = "prisma-client-js"
first = "A"
second = "B"
}
"#]];

expected.assert_eq(&rendered1);
expected.assert_eq(&rendered2)
}
}

0 comments on commit a046f87

Please sign in to comment.