Skip to content

Commit d8d9d04

Browse files
committed
fix(postgres): get correctly qualified type name in describe
1 parent 2df770a commit d8d9d04

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

sqlx-postgres/src/connection/describe.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,16 @@ impl PgConnection {
187187
fn fetch_type_by_oid(&mut self, oid: Oid) -> BoxFuture<'_, Result<PgTypeInfo, Error>> {
188188
Box::pin(async move {
189189
let (name, typ_type, category, relation_id, element, base_type): (String, i8, i8, Oid, Oid, Oid) = query_as(
190-
"SELECT typname, typtype, typcategory, typrelid, typelem, typbasetype FROM pg_catalog.pg_type WHERE oid = $1",
190+
// Converting the OID to `regtype` and then `text` will give us the name that
191+
// the type will need to be found at by search_path.
192+
"SELECT oid::regtype::text, \
193+
typtype, \
194+
typcategory, \
195+
typrelid, \
196+
typelem, \
197+
typbasetype \
198+
FROM pg_catalog.pg_type \
199+
WHERE oid = $1",
191200
)
192201
.bind(oid)
193202
.fetch_one(&mut *self)

tests/postgres/derives.rs

+26
Original file line numberDiff line numberDiff line change
@@ -724,3 +724,29 @@ async fn test_skip() -> anyhow::Result<()> {
724724

725725
Ok(())
726726
}
727+
728+
#[cfg(feature = "macros")]
729+
#[sqlx_macros::test]
730+
async fn test_enum_with_schema() -> anyhow::Result<()> {
731+
#[derive(Debug, PartialEq, Eq, sqlx::Type)]
732+
#[sqlx(type_name = "foo.\"Foo\"")]
733+
enum Foo {
734+
Bar,
735+
Baz,
736+
}
737+
738+
let mut conn = new::<Postgres>().await?;
739+
740+
let foo: Foo = sqlx::query_scalar("SELECT $1::foo.\"Foo\"")
741+
.bind(Foo::Bar)
742+
.fetch_one(&mut conn).await?;
743+
744+
assert_eq!(foo, Foo::Bar);
745+
746+
let foo: Foo = sqlx::query_scalar("SELECT $1::foo.\"Foo\"")
747+
.bind(Foo::Baz)
748+
.fetch_one(&mut conn)
749+
.await?;
750+
751+
assert_eq!(foo, Foo::Baz);
752+
}

tests/postgres/setup.sql

+4
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,7 @@ CREATE OR REPLACE PROCEDURE forty_two(INOUT forty_two INT = NULL)
5151
CREATE TABLE test_citext (
5252
foo CITEXT NOT NULL
5353
);
54+
55+
CREATE SCHEMA IF NOT EXISTS foo;
56+
57+
CREATE ENUM foo."Foo" ('Bar', 'Baz');

0 commit comments

Comments
 (0)