From 2f7419e65dc2089aebb36b7db0f5de3a2fa1e287 Mon Sep 17 00:00:00 2001 From: Marc Scholten Date: Tue, 16 Aug 2022 09:17:16 +0200 Subject: [PATCH] Fixed migration generator fails with `a IN (..)` expression in a policy --- IHP/IDE/CodeGen/MigrationGenerator.hs | 3 +++ IHP/IDE/SchemaDesigner/Compiler.hs | 1 + IHP/IDE/SchemaDesigner/Parser.hs | 1 + IHP/IDE/SchemaDesigner/Types.hs | 2 ++ Test/IDE/CodeGeneration/MigrationGenerator.hs | 14 ++++++++++++++ 5 files changed, 21 insertions(+) diff --git a/IHP/IDE/CodeGen/MigrationGenerator.hs b/IHP/IDE/CodeGen/MigrationGenerator.hs index 87ca63348..e85c20717 100644 --- a/IHP/IDE/CodeGen/MigrationGenerator.hs +++ b/IHP/IDE/CodeGen/MigrationGenerator.hs @@ -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) @@ -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) @@ -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) diff --git a/IHP/IDE/SchemaDesigner/Compiler.hs b/IHP/IDE/SchemaDesigner/Compiler.hs index a7c7f9fd9..71c242e1d 100644 --- a/IHP/IDE/SchemaDesigner/Compiler.hs +++ b/IHP/IDE/SchemaDesigner/Compiler.hs @@ -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 diff --git a/IHP/IDE/SchemaDesigner/Parser.hs b/IHP/IDE/SchemaDesigner/Parser.hs index 520adfc87..9d518408c 100644 --- a/IHP/IDE/SchemaDesigner/Parser.hs +++ b/IHP/IDE/SchemaDesigner/Parser.hs @@ -443,6 +443,7 @@ table = [ , binary "||" ConcatenationExpression , binary "IS" IsExpression + , binary "IN" InExpression , prefix "NOT" NotExpression , prefix "EXISTS" ExistsExpression , typeCast diff --git a/IHP/IDE/SchemaDesigner/Types.hs b/IHP/IDE/SchemaDesigner/Types.hs index 9721a2bab..225ae2a8e 100644 --- a/IHP/IDE/SchemaDesigner/Types.hs +++ b/IHP/IDE/SchemaDesigner/Types.hs @@ -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 diff --git a/Test/IDE/CodeGeneration/MigrationGenerator.hs b/Test/IDE/CodeGeneration/MigrationGenerator.hs index 89748d40a..512e9c4ee 100644 --- a/Test/IDE/CodeGeneration/MigrationGenerator.hs +++ b/Test/IDE/CodeGeneration/MigrationGenerator.hs @@ -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