Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with versions of Persistent both before and after 2.5. #150

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
language: c

sudo: false

matrix:
include:
- env: STACK=STACK ARGS=""
addons: {apt: {packages: [libgmp-dev]}}
- env: STACK=STACK ARGS="--resolver lts-1"
addons: {apt: {packages: [libgmp-dev]}}
- env: STACK=STACK ARGS="--resolver lts-2"
addons: {apt: {packages: [libgmp-dev]}}
- env: STACK=STACK ARGS="--resolver lts-3"
addons: {apt: {packages: [libgmp-dev]}}
- env: STACK=STACK ARGS="--resolver lts-4"
addons: {apt: {packages: [libgmp-dev]}}
- env: STACK=STACK ARGS="--resolver lts-5"
addons: {apt: {packages: [libgmp-dev]}}
- env: STACK=STACK ARGS="--resolver lts-6"
addons: {apt: {packages: [libgmp-dev]}}
- env: STACK=STACK ARGS="--resolver lts"
addons: {apt: {packages: [libgmp-dev]}}
- env: STACK=STACK ARGS="--resolver nightly"
addons: {apt: {packages: [libgmp-dev]}}
- env: CABALVER=head GHCVER=head
addons: {apt: {packages: [cabal-install-head,ghc-head], sources: [hvr-ghc]}}

allow_failures:
- env: CABALVER=head GHCVER=head

before_install:
- export PATH=/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$PATH

install:
- |
set -e
if [ -z "$STACK" ]; then
export PATH=/home/travis/.cabal/bin:/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$PATH

echo "$(ghc --version) [$(ghc --print-project-git-commit-id 2> /dev/null || echo '?')]"
cabal update

else
travis_retry curl -o stack.tar.gz -L https://www.stackage.org/stack/linux-x86_64
mkdir ./stack
tar -xf stack.tar.gz -C ./stack --strip-components=1
export PATH=$PATH:./stack/

echo "Stack $(stack --version)"
stack update
fi

script:
- |
set -e
if [ -n "$STACK" ]; then
echo -e "\033[0;32mstack setup.\033[0m"
stack $ARGS --no-terminal setup

echo -e "\033[0;32mstack build.\033[0m"
stack $ARGS --no-terminal build

echo -e "\033[0;32mstack test.\033[0m"
stack $ARGS --no-terminal test

else
cabal --version
ghc --version

echo -e "\033[0;32mcabal install only deps.\033[0m"
cabal install --only-dependencies --enable-tests

echo -e "\033[0;32mcabal config with tests.\033[0m"
cabal configure --enable-tests

echo -e "\033[0;32mcabal build.\033[0m"
cabal build

echo -e "\033[0;32mcabal test.\033[0m"
cabal test

echo -e "\033[0;32mcabal check.\033[0m"
cabal check

echo -e "\033[0;32mcabal sdist.\033[0m"
cabal sdist

echo -e "\033[0;32mcabal install tarball.\033[0m"
cabal install --force-reinstalls dist/*-*.tar.gz
fi
cache:
directories:
- $HOME/.stack
- $HOME/.cabal

4 changes: 2 additions & 2 deletions esqueleto.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ library
other-modules:
Database.Esqueleto.Internal.PersistentImport
build-depends:
base >= 4.5 && < 4.9
base >= 4.5 && < 4.10
, bytestring
, text >= 0.11 && < 1.3
, persistent >= 2.1.1.7 && < 2.3
, persistent >= 2.1.1.7 && < 2.7
, transformers >= 0.2
, unordered-containers >= 0.2
, tagged >= 0.2
Expand Down
14 changes: 13 additions & 1 deletion src/Database/Esqueleto.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE FlexibleContexts, FlexibleInstances, GADTs #-}
{-# LANGUAGE FlexibleContexts, FlexibleInstances, GADTs, CPP #-}
-- | The @esqueleto@ EDSL (embedded domain specific language).
-- This module replaces @Database.Persist@, so instead of
-- importing that module you should just import this one:
Expand Down Expand Up @@ -405,8 +405,13 @@ import qualified Database.Persist

-- | @valkey i = 'val' . 'toSqlKey'@
-- (<https://github.com/prowdsponsor/esqueleto/issues/9>).
#if MIN_VERSION_persistent(2,5,0)
valkey :: (Esqueleto query expr backend, ToBackendKey SqlBackend entity) =>
Int64 -> expr (Value (Key entity))
#else
valkey :: (Esqueleto query expr backend, ToBackendKey SqlBackend entity, PersistField (Key entity)) =>
Int64 -> expr (Value (Key entity))
#endif
valkey = val . toSqlKey


Expand All @@ -430,8 +435,15 @@ valJ = val . unValue

-- | Synonym for 'Database.Persist.Store.delete' that does not
-- clash with @esqueleto@'s 'delete'.
#if MIN_VERSION_persistent(2,5,0)
deleteKey :: ( PersistStoreWrite b
, MonadIO m
, PersistRecordBackend val b)
=> Key val -> ReaderT b m ()
#else
deleteKey :: ( PersistStore (PersistEntityBackend val)
, MonadIO m
, PersistEntity val )
=> Key val -> ReaderT (PersistEntityBackend val) m ()
#endif
deleteKey = Database.Persist.delete
7 changes: 6 additions & 1 deletion src/Database/Esqueleto/Internal/Language.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
, TypeFamilies
, UndecidableInstances
, GADTs
, CPP
#-}
-- | This is an internal module, anything exported by this module
-- may change without a major version bump. Please use only
Expand Down Expand Up @@ -52,7 +53,6 @@ import Database.Esqueleto.Internal.PersistentImport
import Text.Blaze.Html (Html)

import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL

Expand Down Expand Up @@ -299,8 +299,13 @@ class (Functor query, Applicative query, Monad query) =>
sub_selectDistinct :: PersistField a => query (expr (Value a)) -> expr (Value a)

-- | Project a field of an entity.
#if MIN_VERSION_persistent(2,5,0)
(^.) :: (PersistEntity val) =>
expr (Entity val) -> EntityField val typ -> expr (Value typ)
#else
(^.) :: (PersistEntity val, PersistField typ) =>
expr (Entity val) -> EntityField val typ -> expr (Value typ)
#endif

-- | Project a field of an entity that may be null.
(?.) :: (PersistEntity val, PersistField typ) =>
Expand Down
11 changes: 9 additions & 2 deletions src/Database/Esqueleto/Internal/PersistentImport.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
{-# LANGUAGE CPP#-}
-- | Re-export "Database.Persist.Sql" without any clashes with
-- @esqueleto@.
module Database.Esqueleto.Internal.PersistentImport
( module Database.Persist.Sql
) where

import Database.Persist.Sql hiding
( BackendSpecificFilter, Filter(..), PersistQuery(..), SelectOpt(..)
( BackendSpecificFilter, Filter(..), SelectOpt(..)
, Update(..), delete, deleteWhereCount, updateWhereCount, selectList
, selectKeysList, deleteCascadeWhere, (=.), (+=.), (-=.), (*=.), (/=.)
, (==.), (!=.), (<.), (>.), (<=.), (>=.), (<-.), (/<-.), (||.)
, listToJSON, mapToJSON, getPersistMap, limitOffsetOrder, selectSource
, update )
, update
#if MIN_VERSION_persistent(2,5,0)
, PersistQueryRead(..)
#else
, PersistQuery(..)
#endif
)
18 changes: 18 additions & 0 deletions src/Database/Esqueleto/Internal/Sql.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
, UndecidableInstances
, ScopedTypeVariables
, InstanceSigs
, CPP
#-}
-- | This is an internal module, anything exported by this module
-- may change without a major version bump. Please use only
Expand Down Expand Up @@ -421,8 +422,13 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where
sub_select = sub SELECT
sub_selectDistinct = sub_select . distinct

#if MIN_VERSION_persistent(2,5,0)
(^.) :: forall val typ. (PersistEntity val)
=> SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
#else
(^.) :: forall val typ. (PersistEntity val, PersistField typ)
=> SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
#endif
EEntity ident ^. field
| isComposite = ECompositeKey $ \info -> dot info <$> compositeFields pdef
| otherwise = ERaw Never $ \info -> (dot info $ persistFieldDef field, [])
Expand Down Expand Up @@ -530,15 +536,27 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where
instance ToSomeValues SqlExpr (SqlExpr (Value a)) where
toSomeValues a = [SomeValue a]

#if MIN_VERSION_persistent(2,5,0)
fieldName :: (PersistEntity val)
=> IdentInfo -> EntityField val typ -> TLB.Builder
#else
fieldName :: (PersistEntity val, PersistField typ)
=> IdentInfo -> EntityField val typ -> TLB.Builder
#endif
fieldName info = fromDBName info . fieldDB . persistFieldDef

-- FIXME: Composite/non-id pKS not supported on set
#if MIN_VERSION_persistent(2,5,0)
setAux :: PersistEntity val
=> EntityField val typ
-> (SqlExpr (Entity val) -> SqlExpr (Value typ))
-> SqlExpr (Update val)
#else
setAux :: (PersistEntity val, PersistField typ)
=> EntityField val typ
-> (SqlExpr (Entity val) -> SqlExpr (Value typ))
-> SqlExpr (Update val)
#endif
setAux field mkVal = ESet $ \ent -> unsafeSqlBinOp " = " name (mkVal ent)
where name = ERaw Never $ \info -> (fieldName info field, mempty)

Expand Down
2 changes: 1 addition & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
resolver: lts-5.1
resolver: lts-6.16
7 changes: 7 additions & 0 deletions test/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1395,11 +1395,18 @@ main = do
----------------------------------------------------------------------


#if MIN_VERSION_persistent(2,5,0)
insert' :: ( PersistStoreWrite b
, MonadIO m
, PersistRecordBackend val b)
=> val -> ReaderT b m (Entity val)
#else
insert' :: ( Functor m
, PersistStore (PersistEntityBackend val)
, MonadIO m
, PersistEntity val )
=> val -> ReaderT (PersistEntityBackend val) m (Entity val)
#endif
insert' v = flip Entity v <$> insert v


Expand Down