Skip to content

Commit

Permalink
mkEntityDefList (#904)
Browse files Browse the repository at this point in the history
* Fix version bound; revision on Hackage

* mkEntityDefList

* Remove errant comment line [ci skip]

* Switch to formatting

* remove 8.4.1 ci [ci skip]

* remove nightly thing [ci skip]
  • Loading branch information
parsonsmatt authored May 7, 2019
1 parent 1e356ad commit 10ff69c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ matrix:
- env: BUILD=cabal GHCVER=8.2.2 CABALVER=2.0 HAPPYVER=1.19.5 ALEXVER=3.1.7
compiler: ": #GHC 8.2.2"
addons: {postgresql: "9.6", apt: {packages: [cabal-install-2.0,ghc-8.2.2,happy-1.19.5,alex-3.1.7], sources: [hvr-ghc]}}
- env: BUILD=cabal GHCVER=8.4.1 CABALVER=2.0 HAPPYVER=1.19.5 ALEXVER=3.1.7
compiler: ": #GHC 8.4.1"
addons: {postgresql: "9.6", apt: {packages: [cabal-install-2.0,ghc-8.4.1,happy-1.19.5,alex-3.1.7], sources: [hvr-ghc]}}
- env: BUILD=cabal GHCVER=8.4.4 CABALVER=2.0 HAPPYVER=1.19.5 ALEXVER=3.1.7
compiler: ": #GHC 8.4.4"
addons: {postgresql: "9.6", apt: {packages: [cabal-install-2.0,ghc-8.4.4,happy-1.19.5,alex-3.1.7], sources: [hvr-ghc]}}
Expand All @@ -40,7 +37,6 @@ matrix:
# Nightly builds are allowed to fail
- env: BUILD=stack ARGS="--resolver nightly"
compiler: ": #stack nightly"
addons: {apt: {packages: [libgmp-dev]}}

allow_failures:
- env: BUILD=cabal GHCVER=head CABALVER=head HAPPYVER=1.19.5 ALEXVER=3.1.7
Expand Down
4 changes: 4 additions & 0 deletions persistent-template/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.7.1

* Add the `mkEntityDefList` function to work around [#902](https://github.com/yesodweb/persistent/issues/902). [#904](https://github.com/yesodweb/persistent/pull/904)

## 2.7.0

* Depends on `persistent-2.10.0` which provides the `OnlyOneUniqueKey` and `AtLeastOneUniqueKey` classes. Automatically generates instances for these classes based on how many unique keys the entity definition gets. This changes requires `UndecidableInstances` to be enabled on each module that generates entity definitions. [#885](https://github.com/yesodweb/persistent/pull/885)
Expand Down
31 changes: 31 additions & 0 deletions persistent-template/Database/Persist/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module Database.Persist.TH
, mkMigrate
, mkSave
, mkDeleteCascade
, mkEntityDefList
, share
, derivePersistField
, derivePersistFieldJSON
Expand Down Expand Up @@ -1361,6 +1362,36 @@ mkDeleteCascade mps defs = do
[normalClause [VarP key] (DoE stmts)]
]

-- | Creates a declaration for the @['EntityDef']@ from the @persistent@
-- schema. This is necessary because the Persistent QuasiQuoter is unable
-- to know the correct type of ID fields, and assumes that they are all
-- Int64.
--
-- Provide this in the list you give to 'share', much like @'mkMigrate'@.
--
-- @
-- 'share' ['mkMigrate' "migrateAll", 'mkEntityDefList' "entityDefs"] [...]
-- @
--
-- @since 2.7.1
mkEntityDefList
:: String
-- ^ The name that will be given to the 'EntityDef' list.
-> [EntityDef]
-> Q [Dec]
mkEntityDefList entityList entityDefs = do
let entityListName = mkName entityList
edefs <- fmap ListE
. forM entityDefs
$ \(EntityDef { entityHaskell = HaskellName haskellName }) ->
let entityType = conT (mkName (T.unpack haskellName))
in [|entityDef (Proxy :: Proxy $(entityType))|]
typ <- [t|[EntityDef]|]
pure
[ SigD entityListName typ
, ValD (VarP entityListName) (NormalB edefs) []
]

mkUniqueKeys :: EntityDef -> Q Dec
mkUniqueKeys def | entitySum def =
return $ FunD 'persistUniqueKeys [normalClause [WildP] (ListE [])]
Expand Down
2 changes: 1 addition & 1 deletion persistent-template/persistent-template.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: persistent-template
version: 2.7.0
version: 2.7.1
license: MIT
license-file: LICENSE
author: Michael Snoyman <[email protected]>
Expand Down

0 comments on commit 10ff69c

Please sign in to comment.