Skip to content

Commit

Permalink
Fixed migration generator fails with a IN (..) expression in a policy
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Aug 16, 2022
1 parent 5f05d27 commit 2f7419e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions IHP/IDE/CodeGen/MigrationGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ normalizeExpression (NotEqExpression a b) = NotEqExpression (normalizeExpression
normalizeExpression (EqExpression a b) = EqExpression (normalizeExpression a) (normalizeExpression b)
normalizeExpression (AndExpression a b) = AndExpression (normalizeExpression a) (normalizeExpression b)
normalizeExpression (IsExpression a b) = IsExpression (normalizeExpression a) (normalizeExpression b)
normalizeExpression (InExpression a b) = InExpression (normalizeExpression a) (normalizeExpression b)
normalizeExpression (NotExpression a) = NotExpression (normalizeExpression a)
normalizeExpression (OrExpression a b) = OrExpression (normalizeExpression a) (normalizeExpression b)
normalizeExpression (LessThanExpression a b) = LessThanExpression (normalizeExpression a) (normalizeExpression b)
Expand Down Expand Up @@ -521,6 +522,7 @@ unqualifyExpression scope expression = unqualifyExpression expression
unqualifyExpression (EqExpression a b) = EqExpression (unqualifyExpression a) (unqualifyExpression b)
unqualifyExpression (AndExpression a b) = AndExpression (unqualifyExpression a) (unqualifyExpression b)
unqualifyExpression (IsExpression a b) = IsExpression (unqualifyExpression a) (unqualifyExpression b)
unqualifyExpression (InExpression a b) = InExpression (unqualifyExpression a) (unqualifyExpression b)
unqualifyExpression (NotExpression a) = NotExpression (unqualifyExpression a)
unqualifyExpression (OrExpression a b) = OrExpression (unqualifyExpression a) (unqualifyExpression b)
unqualifyExpression (LessThanExpression a b) = LessThanExpression (unqualifyExpression a) (unqualifyExpression b)
Expand Down Expand Up @@ -551,6 +553,7 @@ resolveAlias (Just alias) fromExpression expression =
e@(EqExpression a b) -> EqExpression (rec a) (rec b)
e@(AndExpression a b) -> AndExpression (rec a) (rec b)
e@(IsExpression a b) -> IsExpression (rec a) (rec b)
e@(InExpression a b) -> InExpression (rec a) (rec b)
e@(NotExpression a) -> NotExpression (rec a)
e@(OrExpression a b) -> OrExpression (rec a) (rec b)
e@(LessThanExpression a b) -> LessThanExpression (rec a) (rec b)
Expand Down
1 change: 1 addition & 0 deletions IHP/IDE/SchemaDesigner/Compiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ compileExpression (NotEqExpression a b) = compileExpression a <> " <> " <> compi
compileExpression (EqExpression a b) = compileExpressionWithOptionalParenthese a <> " = " <> compileExpressionWithOptionalParenthese b
compileExpression (IsExpression a (NotExpression b)) = compileExpressionWithOptionalParenthese a <> " IS NOT " <> compileExpressionWithOptionalParenthese b -- 'IS (NOT NULL)' => 'IS NOT NULL'
compileExpression (IsExpression a b) = compileExpressionWithOptionalParenthese a <> " IS " <> compileExpressionWithOptionalParenthese b
compileExpression (InExpression a b) = compileExpressionWithOptionalParenthese a <> " IN " <> compileExpressionWithOptionalParenthese b
compileExpression (NotExpression a) = "NOT " <> compileExpressionWithOptionalParenthese a
compileExpression (AndExpression a b) = compileExpressionWithOptionalParenthese a <> " AND " <> compileExpressionWithOptionalParenthese b
compileExpression (OrExpression a b) = compileExpressionWithOptionalParenthese a <> " OR " <> compileExpressionWithOptionalParenthese b
Expand Down
1 change: 1 addition & 0 deletions IHP/IDE/SchemaDesigner/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ table = [
, binary "||" ConcatenationExpression

, binary "IS" IsExpression
, binary "IN" InExpression
, prefix "NOT" NotExpression
, prefix "EXISTS" ExistsExpression
, typeCast
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 @@ -160,6 +160,8 @@ data Expression =
| AndExpression Expression Expression
-- | a IS b
| IsExpression Expression Expression
-- | a IN b
| InExpression Expression Expression
-- | NOT a
| NotExpression Expression
-- | EXISTS a
Expand Down
14 changes: 14 additions & 0 deletions Test/IDE/CodeGeneration/MigrationGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,20 @@ END;$$ language PLPGSQL;|]

diffSchemas targetSchema actualSchema `shouldBe` migration

it "should work with IN expressions" do
let targetSchema = sql $ cs [plain|
CREATE POLICY "Users can read and edit their own record" ON public.users USING ((id IN ( SELECT users_1.id
FROM public.users users_1
WHERE ((users_1.id = public.ihp_user_id()) OR (users_1.user_role = 'admin'::text))))) WITH CHECK ((id = public.ihp_user_id()));
|]
let actualSchema = sql $ cs [plain|
|]
let migration = sql [i|
|]

diffSchemas targetSchema actualSchema `shouldBe` migration



sql :: Text -> [Statement]
sql code = case Megaparsec.runParser Parser.parseDDL "" code of
Expand Down

0 comments on commit 2f7419e

Please sign in to comment.