fix(postgres): get correctly qualified type name in describe

This commit is contained in:
Austin Bonander 2024-05-31 13:25:08 -07:00
parent 05a10de4d8
commit 10192019d8
3 changed files with 40 additions and 1 deletions

View file

@ -187,7 +187,16 @@ impl PgConnection {
fn fetch_type_by_oid(&mut self, oid: Oid) -> BoxFuture<'_, Result<PgTypeInfo, Error>> {
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)

View file

@ -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::<Postgres>().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);
}

View file

@ -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');