From 9c95b0f96d9dca10ddead555f7547560ee9af774 Mon Sep 17 00:00:00 2001 From: Marc Scholten Date: Sun, 20 Aug 2023 12:40:44 -0700 Subject: [PATCH] Escape postgres identifiers when using upper case --- IHP/IDE/SchemaDesigner/Compiler.hs | 3 ++- Test/IDE/SchemaDesigner/CompilerSpec.hs | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/IHP/IDE/SchemaDesigner/Compiler.hs b/IHP/IDE/SchemaDesigner/Compiler.hs index 41697c800..b05af3c59 100644 --- a/IHP/IDE/SchemaDesigner/Compiler.hs +++ b/IHP/IDE/SchemaDesigner/Compiler.hs @@ -206,9 +206,10 @@ compilePostgresType (PCustomType theType) = theType compileIdentifier :: Text -> Text compileIdentifier identifier = if identifierNeedsQuoting then tshow identifier else identifier where - identifierNeedsQuoting = isKeyword || containsChar ' ' || containsChar '-' + identifierNeedsQuoting = isKeyword || containsChar ' ' || containsChar '-' || isUsingUppercase isKeyword = IHP.Prelude.toUpper identifier `elem` keywords containsChar char = Text.any (char ==) identifier + isUsingUppercase = Text.toLower identifier /= identifier keywords = [ "ABORT" , "ABSOLUTE" diff --git a/Test/IDE/SchemaDesigner/CompilerSpec.hs b/Test/IDE/SchemaDesigner/CompilerSpec.hs index 544e12ef5..22a2f3bc4 100644 --- a/Test/IDE/SchemaDesigner/CompilerSpec.hs +++ b/Test/IDE/SchemaDesigner/CompilerSpec.hs @@ -571,7 +571,7 @@ tests = do compileSql [statement] `shouldBe` sql it "should compile a CREATE TABLE statement with a composite primary key" do - let sql = cs [plain|CREATE TABLE orderTrucks (\n order_id BIGSERIAL NOT NULL,\n truck_id BIGSERIAL NOT NULL,\n PRIMARY KEY(order_id, truck_id)\n);\n|] + let sql = cs [plain|CREATE TABLE "orderTrucks" (\n order_id BIGSERIAL NOT NULL,\n truck_id BIGSERIAL NOT NULL,\n PRIMARY KEY(order_id, truck_id)\n);\n|] let statement = StatementCreateTable CreateTable { name = "orderTrucks" , columns = @@ -1052,4 +1052,19 @@ tests = do , primaryKeyConstraint = PrimaryKeyConstraint [] } ] + compileSql statements `shouldBe` sql + + it "should escape policy names with different casing" do + let sql = [trimming| + CREATE POLICY "Public" ON plans USING (true) WITH CHECK (false); + |] <> "\n" + let statements = [ + CreatePolicy + { name = "Public" + , action = Nothing + , tableName = "plans" + , using = Just (VarExpression "true") + , check = Just (VarExpression "false") + } + ] compileSql statements `shouldBe` sql \ No newline at end of file