Skip to content

Commit

Permalink
Support ENABLE ROW LEVEL SECURITY statements in Schema.sql
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Sep 30, 2021
1 parent 6545122 commit cb10692
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions IHP/IDE/SchemaDesigner/Compiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ compileStatement AddConstraint { tableName, constraintName, constraint } = "ALTE
compileStatement Comment { content } = "-- " <> content
compileStatement CreateIndex { indexName, unique, tableName, expressions, whereClause } = "CREATE" <> (if unique then " UNIQUE " else " ") <> "INDEX " <> indexName <> " ON " <> tableName <> " (" <> (intercalate ", " (map compileExpression expressions)) <> ")" <> (case whereClause of Just expression -> " WHERE " <> compileExpression expression; Nothing -> "") <> ";"
compileStatement CreateFunction { functionName, functionBody, orReplace } = "CREATE " <> (if orReplace then "OR REPLACE " else "") <> "FUNCTION " <> functionName <> "() RETURNS TRIGGER AS $$" <> functionBody <> "$$ language plpgsql;"
compileStatement EnableRowLevelSecurity { tableName } = "ALTER TABLE " <> tableName <> " ENABLE ROW LEVEL SECURITY;"
compileStatement UnknownStatement { raw } = raw <> ";"

-- | Emit a PRIMARY KEY constraint when there are multiple primary key columns
Expand Down
20 changes: 15 additions & 5 deletions IHP/IDE/SchemaDesigner/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ parseDDL :: Parser [Statement]
parseDDL = manyTill statement eof

statement = do
s <- try createExtension <|> try (StatementCreateTable <$> createTable) <|> try createIndex <|> try createFunction <|> try createTrigger <|> createEnumType <|> addConstraint <|> comment
s <- try createExtension <|> try (StatementCreateTable <$> createTable) <|> try createIndex <|> try createFunction <|> try createTrigger <|> createEnumType <|> alterTable <|> comment
space
pure s

Expand Down Expand Up @@ -115,10 +115,7 @@ createEnumType = do
char ';'
pure CreateEnumType { name, values }

addConstraint = do
lexeme "ALTER"
lexeme "TABLE"
tableName <- identifier
addConstraint tableName = do
lexeme "ADD"
lexeme "CONSTRAINT"
constraintName <- identifier
Expand Down Expand Up @@ -460,6 +457,19 @@ createTrigger = do
raw <- cs <$> someTill (anySingle) (char ';')
pure UnknownStatement { raw = "CREATE TRIGGER " <> raw }

alterTable = do
lexeme "ALTER"
lexeme "TABLE"
tableName <- identifier
addConstraint tableName <|> enableRowLevelSecurity tableName

enableRowLevelSecurity tableName = do
lexeme "ENABLE"
lexeme "ROW"
lexeme "LEVEL"
lexeme "SECURITY"
char ';'
pure EnableRowLevelSecurity { tableName }

-- | Turns sql like '1::double precision' into just '1'
removeTypeCasts :: Expression -> Expression
Expand Down
2 changes: 2 additions & 0 deletions IHP/IDE/SchemaDesigner/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ data Statement
| CreateIndex { indexName :: Text, unique :: Bool, tableName :: Text, expressions :: [Expression], whereClause :: Maybe Expression }
-- | CREATE OR REPLACE FUNCTION functionName() RETURNS TRIGGER AS $$functionBody$$ language plpgsql;
| CreateFunction { functionName :: Text, functionBody :: Text, orReplace :: Bool }
-- | ALTER TABLE tableName ENABLE ROW LEVEL SECURITY;
| EnableRowLevelSecurity { tableName :: Text }
deriving (Eq, Show)

data CreateTable
Expand Down
1 change: 1 addition & 0 deletions IHP/IDE/SchemaDesigner/View/Layout.hs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ renderObjectSelector statements activeObjectName = [hsx|
renderObject CreateIndex {} id = mempty
renderObject CreateFunction {} id = mempty
renderObject UnknownStatement {} id = mempty
renderObject EnableRowLevelSecurity {} id = mempty

shouldRenderObject (StatementCreateTable CreateTable {}) = True
shouldRenderObject CreateEnumType {} = True
Expand Down
7 changes: 6 additions & 1 deletion Test/IDE/SchemaDesigner/CompilerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -481,4 +481,9 @@ tests = do
(IsExpression (VarExpression "source") (NotExpression (VarExpression "NULL")))
(IsExpression (VarExpression "source_id") (NotExpression (VarExpression "NULL"))))
}
compileSql [index] `shouldBe` sql
compileSql [index] `shouldBe` sql

it "should compile 'ENABLE ROW LEVEL SECURITY' statements" do
let sql = "ALTER TABLE tasks ENABLE ROW LEVEL SECURITY;\n"
let statements = [EnableRowLevelSecurity { tableName = "tasks" }]
compileSql statements `shouldBe` sql
2 changes: 2 additions & 0 deletions Test/IDE/SchemaDesigner/ParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ tests = do
(IsExpression (VarExpression "source_id") (NotExpression (VarExpression "NULL"))))
}

it "should parse 'ENABLE ROW LEVEL SECURITY' statements" do
parseSql "ALTER TABLE tasks ENABLE ROW LEVEL SECURITY;" `shouldBe` EnableRowLevelSecurity { tableName = "tasks" }

col :: Column
col = Column
Expand Down

0 comments on commit cb10692

Please sign in to comment.