Skip to content

Commit fea46ab

Browse files
committed
store: Fix handling of enum arrays
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
1 parent a5d30ce commit fea46ab

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

store/postgres/src/relational/dsl.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ impl<'a> Table<'a> {
322322
table: &'b Table<'b>,
323323
column: &'b RelColumn,
324324
) {
325-
let name = format!("{}.{}::text", table.alias.as_str(), &column.name);
325+
let cast = if column.is_list() { "text[]" } else { "text" };
326+
let name = format!("{}.{}::{}", table.alias.as_str(), &column.name, cast);
326327

327328
match (column.is_list(), column.is_nullable()) {
328329
(true, true) => select.add_field(sql::<Nullable<Array<Text>>>(&name)),

store/test-store/tests/postgres/relational.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ const THINGS_GQL: &str = r#"
167167
id: Bytes!,
168168
name: String!
169169
}
170+
171+
# For testing handling of enums and enum arrays
172+
type Spectrum @entity {
173+
id: ID!,
174+
main: Color!
175+
all: [Color!]!
176+
}
170177
"#;
171178

172179
lazy_static! {
@@ -739,6 +746,41 @@ fn serialize_bigdecimal() {
739746
});
740747
}
741748

749+
#[test]
750+
fn enum_arrays() {
751+
// We had an issue where we would read an array of enums back as a
752+
// single string; for this test, we would get back the string
753+
// "{yellow,red,BLUE}" instead of the array ["yellow", "red", "BLUE"]
754+
run_test(|conn, layout| {
755+
let spectrum = entity! { THINGS_SCHEMA =>
756+
id: "rainbow",
757+
main: "yellow",
758+
all: vec!["yellow", "red", "BLUE"]
759+
};
760+
761+
insert_entity(
762+
conn,
763+
layout,
764+
&THINGS_SCHEMA.entity_type("Spectrum").unwrap(),
765+
vec![spectrum.clone()],
766+
);
767+
768+
let actual = layout
769+
.find(
770+
conn,
771+
&THINGS_SCHEMA
772+
.entity_type("Spectrum")
773+
.unwrap()
774+
.parse_key("rainbow")
775+
.unwrap(),
776+
BLOCK_NUMBER_MAX,
777+
)
778+
.expect("Failed to read Spectrum[rainbow]")
779+
.unwrap();
780+
assert_entity_eq!(spectrum, actual);
781+
});
782+
}
783+
742784
fn count_scalar_entities(conn: &mut PgConnection, layout: &Layout) -> usize {
743785
let filter = EntityFilter::Or(vec![
744786
EntityFilter::Equal("bool".into(), true.into()),

0 commit comments

Comments
 (0)