Skip to content

Commit

Permalink
fix SchemaCompiler filterPrimaryKey (#1915)
Browse files Browse the repository at this point in the history
* fix SchemaCompiler filterPrimaryKey

* Fix getInstanceDecl if instance is last

getInstanceDecl errored if the instance that is searched for is at the end of file and doesn't have any newlines trailing it

* Add tests for FilterPrimaryKey instance
  • Loading branch information
MonaMayrhofer authored Feb 28, 2024
1 parent d075ea3 commit 6971e39
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion IHP/SchemaCompiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ instance QueryBuilder.FilterPrimaryKey "#{name}" where
where
primaryKeyPattern = case primaryKeyColumns table of
[] -> error $ "Impossible happened in compilePrimaryKeyInstance. No primary keys found for table " <> cs name <> ". At least one primary key is required."
[c] -> c.name
[c] -> columnNameToFieldName c.name
cs -> "(Id (" <> intercalate ", " (map (columnNameToFieldName . (.name)) cs) <> "))"

primaryKeyFilters :: [Text]
Expand Down
52 changes: 50 additions & 2 deletions Test/SchemaCompilerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,55 @@ tests = do
builder |> QueryBuilder.filterWhere (#id, id)
{-# INLINE filterWhereId #-}
|]

describe "compileFilterPrimaryKeyInstance" do
it "should compile FilterPrimaryKey instance when primary key is called id" do
let statement = StatementCreateTable $ CreateTable {
name = "things",
columns = [ Column "id" PUUID Nothing False False Nothing ],
primaryKeyConstraint = PrimaryKeyConstraint ["id"],
constraints = [],
unlogged = False
}
let compileOutput = compileStatementPreview [statement] statement |> Text.strip

getInstanceDecl "QueryBuilder.FilterPrimaryKey" compileOutput `shouldBe` [trimming|
instance QueryBuilder.FilterPrimaryKey "things" where
filterWhereId id builder =
builder |> QueryBuilder.filterWhere (#id, id)
{-# INLINE filterWhereId #-}
|]
it "should compile FilterPrimaryKey instance when primary key is called thing_id" do
let statement = StatementCreateTable $ CreateTable {
name = "things",
columns = [ Column "thing_id" PUUID Nothing False False Nothing ],
primaryKeyConstraint = PrimaryKeyConstraint ["thing_id"],
constraints = [],
unlogged = False
}
let compileOutput = compileStatementPreview [statement] statement |> Text.strip

getInstanceDecl "QueryBuilder.FilterPrimaryKey" compileOutput `shouldBe` [trimming|
instance QueryBuilder.FilterPrimaryKey "things" where
filterWhereId thingId builder =
builder |> QueryBuilder.filterWhere (#thingId, thingId)
{-# INLINE filterWhereId #-}
|]
it "should compile FilterPrimaryKey instance when primary key is composite of thing_id other_id" do
let statement = StatementCreateTable $ CreateTable {
name = "thing_other_rels",
columns = [ Column "thing_id" PUUID Nothing False False Nothing, Column "other_id" PUUID Nothing False False Nothing],
primaryKeyConstraint = PrimaryKeyConstraint ["thing_id", "other_id"],
constraints = [],
unlogged = False
}
let compileOutput = compileStatementPreview [statement] statement |> Text.strip

getInstanceDecl "QueryBuilder.FilterPrimaryKey" compileOutput `shouldBe` [trimming|
instance QueryBuilder.FilterPrimaryKey "thing_other_rels" where
filterWhereId (Id (thingId, otherId)) builder =
builder |> QueryBuilder.filterWhere (#thingId, thingId) |> QueryBuilder.filterWhere (#otherId, otherId)
{-# INLINE filterWhereId #-}
|]

getInstanceDecl :: Text -> Text -> Text
getInstanceDecl instanceName full =
Expand All @@ -460,4 +508,4 @@ getInstanceDecl instanceName full =
takeInstanceDecl (line:rest)
| isEmpty line = []
| otherwise = line : takeInstanceDecl rest
takeInstanceDecl [] = error "never encountered newline?"
takeInstanceDecl [] = [] -- EOF reached

0 comments on commit 6971e39

Please sign in to comment.