Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crdb enum casting error when executing the same prepared statement twice #4448

Merged
merged 6 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions quaint/src/visitor/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,27 @@ 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,50 @@
use indoc::indoc;
use query_engine_tests::*;

#[test_suite(schema(schema), capabilities(Enums, ScalarLists), exclude(MongoDb))]
mod prisma_21901 {
fn schema() -> String {
let schema = indoc! {
r#"model Test {
#id(id, Int, @id)
colors 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(())
}
}
Loading