From 10ff69c7b9c82b08fc5948a0f3090cce3130bd8f Mon Sep 17 00:00:00 2001 From: Matt Parsons Date: Mon, 6 May 2019 19:35:27 -0600 Subject: [PATCH] mkEntityDefList (#904) * 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] --- .travis.yml | 4 --- persistent-template/ChangeLog.md | 4 +++ persistent-template/Database/Persist/TH.hs | 31 +++++++++++++++++++ persistent-template/persistent-template.cabal | 2 +- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 84ac0d87f..ae183e21a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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]}} @@ -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 diff --git a/persistent-template/ChangeLog.md b/persistent-template/ChangeLog.md index 1e0787a4b..f5aa7f414 100644 --- a/persistent-template/ChangeLog.md +++ b/persistent-template/ChangeLog.md @@ -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) diff --git a/persistent-template/Database/Persist/TH.hs b/persistent-template/Database/Persist/TH.hs index 5df4c7c31..66a7b2430 100644 --- a/persistent-template/Database/Persist/TH.hs +++ b/persistent-template/Database/Persist/TH.hs @@ -32,6 +32,7 @@ module Database.Persist.TH , mkMigrate , mkSave , mkDeleteCascade + , mkEntityDefList , share , derivePersistField , derivePersistFieldJSON @@ -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 [])] diff --git a/persistent-template/persistent-template.cabal b/persistent-template/persistent-template.cabal index 462915e62..c1f7734aa 100644 --- a/persistent-template/persistent-template.cabal +++ b/persistent-template/persistent-template.cabal @@ -1,5 +1,5 @@ name: persistent-template -version: 2.7.0 +version: 2.7.1 license: MIT license-file: LICENSE author: Michael Snoyman