Repository: diesel-rs/diesel (language: rust)

Issue:
Metadata lookup with no schema fails on non-lowercase types
## Setup

### Versions

- **Rust:** latest stable
- **Diesel:** latest master
- **Database:** postgres
- **Operating System:** Linux

## Problem Description
I'm [currently updating diesel-derive-enum](https://github.com/adwhit/diesel-derive-enum/pull/66) to support the latest diesel master version.
It fails tests, because, probably since https://github.com/diesel-rs/diesel/commit/887be77536ae855ade3a85aa3c0875c597d23db6, the method employed to resolve these types decides to lowercase when looking up.

### What are you trying to accomplish?
https://github.com/diesel-rs/diesel/blob/master/diesel/src/pg/metadata_lookup.rs#L91-L97
```
SELECT "pg_type"."oid", "pg_type"."typarray" FROM "pg_type" WHERE ("pg_type"."oid" = $1::regtype::oid) -- binds: ["Stylized_External_Type"]
```

### What is the expected output?
*resolves to the existing type*

### What is the actual output?
```
    DatabaseError(
        Unknown,
        "type \"stylized_external_type\" does not exist",
    ),
```

### Steps to reproduce

I've created #2993 so that the tests make the issue obvious.
We need to add the fix
 - but I'm not sure what it should be
 EDIT: Found a fix.

## Possibly linked

#2948


Discussion / hints:

Fix patch:
diff --git a/diesel/src/pg/metadata_lookup.rs b/diesel/src/pg/metadata_lookup.rs
index 8b72e1b9e0a5..8768ad765330 100644
--- a/diesel/src/pg/metadata_lookup.rs
+++ b/diesel/src/pg/metadata_lookup.rs
@@ -90,9 +90,9 @@ fn lookup_type<T: Connection<Backend = Pg>>(
         // and let postgres figure out the name resolution on it's own.
         metadata_query
             .filter(
-                pg_type::oid.eq(crate::dsl::sql("")
+                pg_type::oid.eq(crate::dsl::sql("quote_ident(")
                     .bind::<crate::sql_types::Text, _>(&cache_key.type_name)
-                    .sql("::regtype::oid")),
+                    .sql(")::regtype::oid")),
             )
             .first(conn)?
     };


Test patch:
diff --git a/diesel_tests/tests/custom_types.rs b/diesel_tests/tests/custom_types.rs
index a94ea8b1b528..314ce8551b73 100644
--- a/diesel_tests/tests/custom_types.rs
+++ b/diesel_tests/tests/custom_types.rs
@@ -16,7 +16,7 @@ table! {
 }
 
 #[derive(SqlType)]
-#[diesel(postgres_type(name = "my_type"))]
+#[diesel(postgres_type(name = "My_Type"))]
 pub struct MyType;
 
 #[derive(Debug, PartialEq, FromSqlRow, AsExpression)]
@@ -69,10 +69,10 @@ fn custom_types_round_trip() {
     connection
         .batch_execute(
             r#"
-        CREATE TYPE my_type AS ENUM ('foo', 'bar');
+        CREATE TYPE "My_Type" AS ENUM ('foo', 'bar');
         CREATE TABLE custom_types (
             id SERIAL PRIMARY KEY,
-            custom_enum my_type NOT NULL
+            custom_enum "My_Type" NOT NULL
         );
     "#,
         )
@@ -95,7 +95,7 @@ table! {
 }
 
 #[derive(SqlType)]
-#[diesel(postgres_type(name = "my_type", schema = "custom_schema"))]
+#[diesel(postgres_type(name = "My_Type", schema = "custom_schema"))]
 pub struct MyTypeInCustomSchema;
 
 #[derive(Debug, PartialEq, FromSqlRow, AsExpression)]
@@ -149,10 +149,10 @@ fn custom_types_in_custom_schema_round_trip() {
         .batch_execute(
             r#"
         CREATE SCHEMA IF NOT EXISTS custom_schema;
-        CREATE TYPE custom_schema.my_type AS ENUM ('foo', 'bar');
+        CREATE TYPE custom_schema."My_Type" AS ENUM ('foo', 'bar');
         CREATE TABLE custom_schema.custom_types_with_custom_schema (
             id SERIAL PRIMARY KEY,
-            custom_enum custom_schema.my_type NOT NULL
+            custom_enum custom_schema."My_Type" NOT NULL
         );
     "#,
         )