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 :: 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 -- Schema
@ -146,6 +148,15 @@ enumArrayType =
Schema.bstCockroach = Just "_made_up_type[]" 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]
schema = schema =
[ (Schema.table "author") [ (Schema.table "author")
@ -155,7 +166,10 @@ schema =
Schema.column "emails" textArrayType, Schema.column "emails" textArrayType,
Schema.column "grid" nestedIntArrayType, Schema.column "grid" nestedIntArrayType,
Schema.column "jsons" jsonArrayType, Schema.column "jsons" jsonArrayType,
Schema.column "made_up" enumArrayType Schema.column "made_up" enumArrayType,
(Schema.column "interesting" interestingArrayType)
{ Schema.columnNullable = True
}
], ],
Schema.tablePrimaryKey = ["id"] Schema.tablePrimaryKey = ["id"]
} }
@ -192,7 +206,8 @@ singleArrayTests = do
emails: "{ash@ash.com, ash123@ash.com}", emails: "{ash@ash.com, ash123@ash.com}",
grid: "{}", grid: "{}",
jsons: "{}", 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", "name": "jsons",
"type": { "type": {
@ -736,9 +767,8 @@ introspectionWithDisabledFeatureTests = do
} }
} }
}, },
{ {"name": "id",
"name": "id", "type": {
"type": {
"kind": "NON_NULL", "kind": "NON_NULL",
"name": null, "name": null,
"ofType": { "ofType": {
@ -748,6 +778,13 @@ introspectionWithDisabledFeatureTests = do
} }
} }
}, },
{"name": "interesting",
"type" : {
"kind": "SCALAR",
"name": "_TypeWithInterestingCapitalization",
"ofType": null
}
},
{ {
"name": "jsons", "name": "jsons",
"type": { "type": {

View File

@ -468,6 +468,8 @@ pgScalarTypeToText = \case
PGLtree -> "ltree" PGLtree -> "ltree"
PGLquery -> "lquery" PGLquery -> "lquery"
PGLtxtquery -> "ltxtquery" 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 <> "[]" PGArray t -> pgScalarTypeToText t <> "[]"
PGUnknown t -> t PGUnknown t -> t
PGCompositeScalar t -> t PGCompositeScalar t -> t
@ -554,11 +556,19 @@ instance HasCodec PGScalarType where
instance ToSQL PGScalarType where instance ToSQL PGScalarType where
toSQL = toSQL =
TB.text . \case TB.text . quoteType
-- Format enum type names as identifiers to preserve case sensitivity where
-- https://github.com/hasura/graphql-engine/issues/4014 -- Custom types may use non-lower casing and characters that do not belong to
PGEnumScalar t -> pgFmtIdentifier t -- identifiers lexicographically and thus need to be quoted.
scalarType -> pgScalarTypeToText scalarType 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 instance ToJSON PGScalarType where
toJSON = String . pgScalarTypeToText toJSON = String . pgScalarTypeToText