Postgres: Quote custom scalar types in SQL

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/10914
GitOrigin-RevId: 1ea4690b801913b4fc3fa8631090e3caa76b1cdc
This commit is contained in:
Philip Lykke Carlsen 2024-07-05 14:21:14 +02:00 committed by hasura-bot
parent 90dbbccce5
commit 69219958d6
2 changed files with 58 additions and 11 deletions

View File

@ -105,7 +105,9 @@ cockroachFixture =
}
setup :: Text
setup = "create type \"_made_up_type\" as enum ('a', 'b', 'c');"
setup =
"create type \"_made_up_type\" as enum ('a', 'b', 'c');\n"
<> "create type \"TypeWithInterestingCapitalization\" as enum ('a', 'b', 'c');"
--------------------------------------------------------------------------------
-- Schema
@ -146,6 +148,15 @@ enumArrayType =
Schema.bstCockroach = Just "_made_up_type[]"
}
interestingArrayType :: Schema.ScalarType
interestingArrayType =
Schema.TCustomType
$ Schema.defaultBackendScalarType
{ Schema.bstPostgres = Just "\"TypeWithInterestingCapitalization\"[]",
Schema.bstCitus = Just "\"TypeWithInterestingCapitalization\"[]",
Schema.bstCockroach = Just "\"TypeWithInterestingCapitalization\"[]"
}
schema :: [Schema.Table]
schema =
[ (Schema.table "author")
@ -155,7 +166,10 @@ schema =
Schema.column "emails" textArrayType,
Schema.column "grid" nestedIntArrayType,
Schema.column "jsons" jsonArrayType,
Schema.column "made_up" enumArrayType
Schema.column "made_up" enumArrayType,
(Schema.column "interesting" interestingArrayType)
{ Schema.columnNullable = True
}
],
Schema.tablePrimaryKey = ["id"]
}
@ -192,7 +206,8 @@ singleArrayTests = do
emails: "{ash@ash.com, ash123@ash.com}",
grid: "{}",
jsons: "{}",
made_up: "{}"
made_up: "{}",
interesting: "{}"
}
]
) {
@ -610,6 +625,22 @@ introspectionTests = do
}
}
},
{
"name": "interesting",
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "TypeWithInterestingCapitalization",
"ofType": null
}
}
}
},
{
"name": "jsons",
"type": {
@ -736,9 +767,8 @@ introspectionWithDisabledFeatureTests = do
}
}
},
{
"name": "id",
"type": {
{"name": "id",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
@ -748,6 +778,13 @@ introspectionWithDisabledFeatureTests = do
}
}
},
{"name": "interesting",
"type" : {
"kind": "SCALAR",
"name": "_TypeWithInterestingCapitalization",
"ofType": null
}
},
{
"name": "jsons",
"type": {

View File

@ -468,6 +468,8 @@ pgScalarTypeToText = \case
PGLtree -> "ltree"
PGLquery -> "lquery"
PGLtxtquery -> "ltxtquery"
-- It's not really clear that this case is actually used for anything, since
-- arrays seem to always require special handling for callers of this function.
PGArray t -> pgScalarTypeToText t <> "[]"
PGUnknown t -> t
PGCompositeScalar t -> t
@ -554,11 +556,19 @@ instance HasCodec PGScalarType where
instance ToSQL PGScalarType where
toSQL =
TB.text . \case
-- Format enum type names as identifiers to preserve case sensitivity
-- https://github.com/hasura/graphql-engine/issues/4014
PGEnumScalar t -> pgFmtIdentifier t
scalarType -> pgScalarTypeToText scalarType
TB.text . quoteType
where
-- Custom types may use non-lower casing and characters that do not belong to
-- identifiers lexicographically and thus need to be quoted.
quoteType :: PGScalarType -> Text
quoteType =
\case
-- Quoting an array type is e.g. '"MyType"[]', not '"MyType[]"', which necessitates this somewhat awkward recursion.
PGArray t -> quoteType t <> "[]"
PGUnknown t -> dquote t
PGCompositeScalar t -> dquote t
PGEnumScalar t -> dquote t
t -> pgScalarTypeToText t
instance ToJSON PGScalarType where
toJSON = String . pgScalarTypeToText