Skip to content

Commit

Permalink
Escape postgres identifiers when using upper case
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Aug 20, 2023
1 parent 3863375 commit 9c95b0f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion IHP/IDE/SchemaDesigner/Compiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 16 additions & 1 deletion Test/IDE/SchemaDesigner/CompilerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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

0 comments on commit 9c95b0f

Please sign in to comment.