From 10192019d89bfbd57a392f3050d9ec0d32723d2b Mon Sep 17 00:00:00 2001 From: Austin Bonander Date: Fri, 31 May 2024 13:25:08 -0700 Subject: [PATCH] fix(postgres): get correctly qualified type name in describe --- sqlx-postgres/src/connection/describe.rs | 11 +++++++++- tests/postgres/derives.rs | 26 ++++++++++++++++++++++++ tests/postgres/setup.sql | 4 ++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/sqlx-postgres/src/connection/describe.rs b/sqlx-postgres/src/connection/describe.rs index ed938977..22fb0a75 100644 --- a/sqlx-postgres/src/connection/describe.rs +++ b/sqlx-postgres/src/connection/describe.rs @@ -187,7 +187,16 @@ impl PgConnection { fn fetch_type_by_oid(&mut self, oid: Oid) -> BoxFuture<'_, Result> { Box::pin(async move { let (name, typ_type, category, relation_id, element, base_type): (String, i8, i8, Oid, Oid, Oid) = query_as( - "SELECT typname, typtype, typcategory, typrelid, typelem, typbasetype FROM pg_catalog.pg_type WHERE oid = $1", + // Converting the OID to `regtype` and then `text` will give us the name that + // the type will need to be found at by search_path. + "SELECT oid::regtype::text, \ + typtype, \ + typcategory, \ + typrelid, \ + typelem, \ + typbasetype \ + FROM pg_catalog.pg_type \ + WHERE oid = $1", ) .bind(oid) .fetch_one(&mut *self) diff --git a/tests/postgres/derives.rs b/tests/postgres/derives.rs index dd05e929..d840589f 100644 --- a/tests/postgres/derives.rs +++ b/tests/postgres/derives.rs @@ -724,3 +724,29 @@ async fn test_skip() -> anyhow::Result<()> { Ok(()) } + +#[cfg(feature = "macros")] +#[sqlx_macros::test] +async fn test_enum_with_schema() -> anyhow::Result<()> { + #[derive(Debug, PartialEq, Eq, sqlx::Type)] + #[sqlx(type_name = "foo.\"Foo\"")] + enum Foo { + Bar, + Baz, + } + + let mut conn = new::().await?; + + let foo: Foo = sqlx::query_scalar("SELECT $1::foo.\"Foo\"") + .bind(Foo::Bar) + .fetch_one(&mut conn).await?; + + assert_eq!(foo, Foo::Bar); + + let foo: Foo = sqlx::query_scalar("SELECT $1::foo.\"Foo\"") + .bind(Foo::Baz) + .fetch_one(&mut conn) + .await?; + + assert_eq!(foo, Foo::Baz); +} \ No newline at end of file diff --git a/tests/postgres/setup.sql b/tests/postgres/setup.sql index 5a415324..425bd4c5 100644 --- a/tests/postgres/setup.sql +++ b/tests/postgres/setup.sql @@ -51,3 +51,7 @@ CREATE OR REPLACE PROCEDURE forty_two(INOUT forty_two INT = NULL) CREATE TABLE test_citext ( foo CITEXT NOT NULL ); + +CREATE SCHEMA IF NOT EXISTS foo; + +CREATE ENUM foo."Foo" ('Bar', 'Baz'); \ No newline at end of file