Skip to content

Commit

Permalink
Don't fail on CREATE TRIGGER statements
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Jun 3, 2021
1 parent 013e90e commit a52f3ce
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion IHP/IDE/SchemaDesigner/Compiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ compileStatement AddConstraint { tableName, constraintName, constraint } = "ALTE
compileStatement Comment { content } = "-- " <> content
compileStatement CreateIndex { indexName, tableName, columnNames } = "CREATE INDEX " <> indexName <> " ON " <> tableName <> " (" <> (intercalate ", " columnNames) <> ");"
compileStatement CreateFunction { functionName, functionBody, orReplace } = "CREATE " <> (if orReplace then "OR REPLACE " else "") <> "FUNCTION " <> functionName <> "() RETURNS TRIGGER AS $$" <> functionBody <> "$$ language plpgsql;"
compileStatement UnknownStatement { raw } = raw
compileStatement UnknownStatement { raw } = raw <> ";"

-- | Emit a PRIMARY KEY constraint when there are multiple primary key columns
compilePrimaryKeyConstraint :: PrimaryKeyConstraint -> Maybe Text
Expand Down
10 changes: 9 additions & 1 deletion IHP/IDE/SchemaDesigner/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ parseDDL :: Parser [Statement]
parseDDL = manyTill statement eof

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

Expand Down Expand Up @@ -415,3 +415,11 @@ createFunction = do
lexeme "plpgsql"
char ';'
pure CreateFunction { functionName, functionBody, orReplace }

-- | Triggers are not currently used by IHP, therefore they're implemented using UnknownStatement
-- This avoid errors when having custom triggers in Schema.sql
createTrigger = do
lexeme "CREATE"
lexeme "TRIGGER"
raw <- cs <$> someTill (anySingle) (char ';')
pure UnknownStatement { raw = "CREATE TRIGGER " <> raw }
6 changes: 6 additions & 0 deletions Test/IDE/SchemaDesigner/CompilerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,10 @@ tests = do
, orReplace = False
}

compileSql [statement] `shouldBe` sql


it "should compile a CREATE TRIGGER .." do
let sql = cs [plain|CREATE TRIGGER t AFTER INSERT ON x FOR EACH ROW EXECUTE PROCEDURE y();\n|]
let statement = UnknownStatement { raw = "CREATE TRIGGER t AFTER INSERT ON x FOR EACH ROW EXECUTE PROCEDURE y()" }
compileSql [statement] `shouldBe` sql
10 changes: 10 additions & 0 deletions Test/IDE/SchemaDesigner/ParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,16 @@ tests = do
, orReplace = False
}

it "should parse unsupported SQL as a unknown statement" do
let sql = "CREATE TABLE a(); CREATE TRIGGER t AFTER INSERT ON x FOR EACH ROW EXECUTE PROCEDURE y(); CREATE TABLE b();"
let statements =
[ StatementCreateTable CreateTable { name = "a", columns = [], primaryKeyConstraint = PrimaryKeyConstraint [], constraints = [] }
, UnknownStatement { raw = "CREATE TRIGGER t AFTER INSERT ON x FOR EACH ROW EXECUTE PROCEDURE y()" }
, StatementCreateTable CreateTable { name = "b", columns = [], primaryKeyConstraint = PrimaryKeyConstraint [], constraints = [] }
]
parseSqlStatements sql `shouldBe` statements


col :: Column
col = Column
{ name = ""
Expand Down

0 comments on commit a52f3ce

Please sign in to comment.