Skip to content

Commit

Permalink
Fixed another bug in migration generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Aug 10, 2023
1 parent 535d4f5 commit 4dc1866
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions IHP/IDE/CodeGen/MigrationGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,8 @@ unqualifyExpression scope expression = doUnqualify expression
doUnqualify (TypeCastExpression a b) = TypeCastExpression (doUnqualify a) b
doUnqualify e@(SelectExpression Select { columns, from, whereClause, alias }) =
let recurse = case from of
VarExpression fromName -> unqualifyExpression fromName
DotExpression (VarExpression "public") fromName -> unqualifyExpression fromName
VarExpression fromName -> doUnqualify . unqualifyExpression fromName
DotExpression (VarExpression "public") fromName -> doUnqualify . unqualifyExpression fromName
_ -> doUnqualify
in
SelectExpression Select { columns = (recurse <$> columns), from = from, whereClause = recurse whereClause, alias }
Expand Down
20 changes: 20 additions & 0 deletions Test/IDE/CodeGeneration/MigrationGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,26 @@ CREATE POLICY "Users can read and edit their own record" ON public.users USING (
let migration = []

diffSchemas targetSchema actualSchema `shouldBe` migration

it "should deal with complex nested SELECT expressions inside a policy" do
-- Tricky part is the `projects.id = ads.project_id` here
-- It needs to be unwrapped to `id = project_id` correctly
let actualSchema = sql $ cs [plain|
CREATE POLICY "Users can manage ads if they can access the project" ON public.ads USING ((EXISTS ( SELECT projects.id
FROM public.projects
WHERE (projects.id = ads.project_id)))) WITH CHECK ((EXISTS ( SELECT projects.id
FROM public.projects
WHERE (projects.id = ads.project_id))));
|]
let targetSchema = sql $ cs [plain|
CREATE POLICY "Users can manage ads if they can access the project" ON ads USING (EXISTS (SELECT id FROM projects WHERE id = project_id)) WITH CHECK (EXISTS (SELECT id FROM projects WHERE id = project_id));
|]
let migration = []

putStrLn $ tshow (normalizeSchema targetSchema)
putStrLn $ tshow (normalizeSchema actualSchema)

diffSchemas targetSchema actualSchema `shouldBe` migration

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

0 comments on commit 4dc1866

Please sign in to comment.