Skip to content

Commit

Permalink
fix: crdb enum error on stmt caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Weakky committed Nov 15, 2023
1 parent 174e7d3 commit be722f1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
31 changes: 13 additions & 18 deletions quaint/src/visitor/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,28 @@ impl<'a> Visitor<'a> for Postgres<'a> {
variants: Vec<EnumVariant<'a>>,
name: Option<EnumName<'a>>,
) -> visitor::Result {
let len = variants.len();

// Since enums are user-defined custom types, tokio-postgres fires an additional query
// when parameterizing values of type enum to know which custom type the value refers to.
// Casting the enum value to `TEXT` avoid this roundtrip since `TEXT` is a builtin type.
if let Some(enum_name) = name.clone() {
self.surround_with("ARRAY[", "]", |s| {
for (i, variant) in variants.into_iter().enumerate() {
s.add_parameter(variant.into_text());
s.parameter_substitution()?;
s.write("::text")?;

if i < (len - 1) {
s.write(", ")?;
}
self.add_parameter(Value::array(variants.into_iter().map(|v| v.into_text())));

self.surround_with("CAST(", ")", |s| {
s.parameter_substitution()?;
s.write("::text[]")?;
s.write(" AS ")?;

if let Some(schema_name) = enum_name.schema_name {
s.surround_with_backticks(schema_name.deref())?;
s.write(".")?
}

s.surround_with_backticks(enum_name.name.deref())?;
s.write("[]")?;

Ok(())
})?;

self.write("::")?;
if let Some(schema_name) = enum_name.schema_name {
self.surround_with_backticks(schema_name.deref())?;
self.write(".")?
}
self.surround_with_backticks(enum_name.name.deref())?;
self.write("[]")?;
} else {
self.visit_parameterized(Value::array(
variants.into_iter().map(|variant| variant.into_enum(name.clone())),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod prisma_17103;
mod prisma_18517;
mod prisma_20799;
mod prisma_21369;
mod prisma_21901;
mod prisma_5952;
mod prisma_6173;
mod prisma_7010;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use indoc::indoc;
use query_engine_tests::*;

#[test_suite(schema(schema))]
mod prisma_21901 {
fn schema() -> String {
let schema = indoc! {
r#"model Test {
#id(id, Int, @id)
colors Color[]
color Color?
}
enum Color {
red
blue
green
}
"#
};

schema.to_owned()
}

// fixes https://github.com/prisma/prisma/issues/21901
#[connector_test]
async fn test(runner: Runner) -> TestResult<()> {
insta::assert_snapshot!(
run_query!(
runner,
r#"mutation { createOneTest(data: { id: 1, colors: ["red"] }) { colors } }"#
),
@r###"{"data":{"createOneTest":{"colors":["red"]}}}"###
);

insta::assert_snapshot!(
run_query!(runner, fmt_execute_raw(r#"TRUNCATE TABLE "prisma_21901_test"."Test" CASCADE;"#, [])),
@r###"{"data":{"executeRaw":0}}"###
);

insta::assert_snapshot!(
run_query!(
runner,
r#"mutation { createOneTest(data: { id: 2, colors: ["blue"] }) { colors } }"#
),
@r###"{"data":{"createOneTest":{"colors":["blue"]}}}"###
);

Ok(())
}
}

0 comments on commit be722f1

Please sign in to comment.