Skip to content

Commit

Permalink
store: Fix handling of enum arrays
Browse files Browse the repository at this point in the history
Roundtripping arrays of enums would fail because we would read an array of
enums back as a single string "{yellow,red,BLUE}" instead of the array
["yellow", "red", "BLUE"]. Storing an update to such an entity, even if
users make no changes to that field, would fail because Postgres expects an
array and we were sending a scalar value.

This fixes a bug introduced in PR
#5372
  • Loading branch information
lutter committed Jan 9, 2025
1 parent a5d30ce commit fea46ab
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
3 changes: 2 additions & 1 deletion store/postgres/src/relational/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ impl<'a> Table<'a> {
table: &'b Table<'b>,
column: &'b RelColumn,
) {
let name = format!("{}.{}::text", table.alias.as_str(), &column.name);
let cast = if column.is_list() { "text[]" } else { "text" };
let name = format!("{}.{}::{}", table.alias.as_str(), &column.name, cast);

match (column.is_list(), column.is_nullable()) {
(true, true) => select.add_field(sql::<Nullable<Array<Text>>>(&name)),
Expand Down
42 changes: 42 additions & 0 deletions store/test-store/tests/postgres/relational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ const THINGS_GQL: &str = r#"
id: Bytes!,
name: String!
}
# For testing handling of enums and enum arrays
type Spectrum @entity {
id: ID!,
main: Color!
all: [Color!]!
}
"#;

lazy_static! {
Expand Down Expand Up @@ -739,6 +746,41 @@ fn serialize_bigdecimal() {
});
}

#[test]
fn enum_arrays() {
// We had an issue where we would read an array of enums back as a
// single string; for this test, we would get back the string
// "{yellow,red,BLUE}" instead of the array ["yellow", "red", "BLUE"]
run_test(|conn, layout| {
let spectrum = entity! { THINGS_SCHEMA =>
id: "rainbow",
main: "yellow",
all: vec!["yellow", "red", "BLUE"]
};

insert_entity(
conn,
layout,
&THINGS_SCHEMA.entity_type("Spectrum").unwrap(),
vec![spectrum.clone()],
);

let actual = layout
.find(
conn,
&THINGS_SCHEMA
.entity_type("Spectrum")
.unwrap()
.parse_key("rainbow")
.unwrap(),
BLOCK_NUMBER_MAX,
)
.expect("Failed to read Spectrum[rainbow]")
.unwrap();
assert_entity_eq!(spectrum, actual);
});
}

fn count_scalar_entities(conn: &mut PgConnection, layout: &Layout) -> usize {
let filter = EntityFilter::Or(vec![
EntityFilter::Equal("bool".into(), true.into()),
Expand Down

0 comments on commit fea46ab

Please sign in to comment.