Skip to content

Commit 5dbd756

Browse files
authored
persistent-mongoDB-2.13.0.0 (#1286)
* Fix upsert tests * Fix mongo insert documents * Fix shadowing * Add notes about mongo PersistMap * Re enable some mongodb tests * Re enable building mongo * Stylish haskell * Update changelog * Bump mongodb version * Update mongodb maintaner
1 parent 2aa65e2 commit 5dbd756

File tree

8 files changed

+56
-38
lines changed

8 files changed

+56
-38
lines changed

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ packages:
22
persistent
33
persistent-sqlite
44
persistent-test
5-
-- persistent-mongoDB
5+
persistent-mongoDB
66
persistent-mysql
77
persistent-postgresql
88
persistent-redis

persistent-mongoDB/ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog for persistent-mongoDB
22

3+
## 2.13.0.0
4+
5+
* Fix persistent 2.13 changes [#1286](https://github.com/yesodweb/persistent/pull/1286)
6+
37
## 2.12.0.0
48

59
* Decomposed `HaskellName` into `ConstraintNameHS`, `EntityNameHS`, `FieldNameHS`. Decomposed `DBName` into `ConstraintNameDB`, `EntityNameDB`, `FieldNameDB` respectively. [#1174](https://github.com/yesodweb/persistent/pull/1174)

persistent-mongoDB/Database/Persist/MongoDB.hs

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -112,31 +112,40 @@ module Database.Persist.MongoDB
112112
, module Database.Persist
113113
) where
114114

115-
import qualified Data.List.NonEmpty as NEL
116115
import Control.Exception (throw, throwIO)
117-
import Control.Monad (liftM, (>=>), forM_, unless)
116+
import Control.Monad (forM_, liftM, unless, (>=>))
118117
import Control.Monad.IO.Class (liftIO)
119118
import qualified Control.Monad.IO.Class as Trans
120119
import Control.Monad.IO.Unlift (MonadUnliftIO, withRunInIO)
121120
import Control.Monad.Trans.Reader (ask, runReaderT)
121+
import qualified Data.List.NonEmpty as NEL
122122

123123
import Data.Acquire (mkAcquire)
124-
import Data.Aeson (Value (Number), (.:), (.:?), (.!=), FromJSON(..), ToJSON(..), withText, withObject)
124+
import Data.Aeson
125+
( FromJSON(..)
126+
, ToJSON(..)
127+
, Value(Number)
128+
, withObject
129+
, withText
130+
, (.!=)
131+
, (.:)
132+
, (.:?)
133+
)
125134
import Data.Aeson.Types (modifyFailure)
126135
import Data.Bits (shiftR)
127136
import Data.Bson (ObjectId(..))
128137
import qualified Data.ByteString as BS
129138
import Data.Conduit
130-
import Data.Maybe (mapMaybe, fromJust)
139+
import Data.Maybe (fromJust, mapMaybe)
131140
import Data.Monoid (mappend)
141+
import qualified Data.Pool as Pool
132142
import qualified Data.Serialize as Serialize
133143
import Data.Text (Text)
134144
import qualified Data.Text as T
135145
import qualified Data.Text.Encoding as E
136-
import qualified Data.Traversable as Traversable
137-
import qualified Data.Pool as Pool
138146
import Data.Time (NominalDiffTime)
139147
import Data.Time.Calendar (Day(..))
148+
import qualified Data.Traversable as Traversable
140149
#ifdef HIGH_PRECISION_DATE
141150
import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
142151
#endif
@@ -145,8 +154,14 @@ import Network.Socket (HostName)
145154
import Numeric (readHex)
146155
import System.Environment (lookupEnv)
147156
import Unsafe.Coerce (unsafeCoerce)
157+
import Web.HttpApiData
158+
( FromHttpApiData(..)
159+
, ToHttpApiData(..)
160+
, parseUrlPieceMaybe
161+
, parseUrlPieceWithPrefix
162+
, readTextData
163+
)
148164
import Web.PathPieces (PathPiece(..))
149-
import Web.HttpApiData (ToHttpApiData(..), FromHttpApiData(..), parseUrlPieceMaybe, parseUrlPieceWithPrefix, readTextData)
150165

151166
#ifdef DEBUG
152167
import FileLocation (debug)
@@ -156,8 +171,8 @@ import qualified Database.MongoDB as DB
156171
import Database.MongoDB.Query (Database)
157172

158173
import Database.Persist
159-
import qualified Database.Persist.Sql as Sql
160174
import Database.Persist.EntityDef.Internal (toEmbedEntityDef)
175+
import qualified Database.Persist.Sql as Sql
161176

162177
instance HasPersistBackend DB.MongoContext where
163178
type BaseBackend DB.MongoContext = DB.MongoContext
@@ -430,15 +445,18 @@ toInsertDoc record =
430445
DB.:=
431446
embeddedVal pv
432447
)
433-
$ filter (\(_, pv) -> isNull pv)
448+
$ filter (\(_, pv) -> not $ isNull pv)
434449
$ zip xs ys
435450
where
436451
isNull PersistNull = True
437452
isNull (PersistMap m) = null m
438453
isNull (PersistList l) = null l
439454
isNull _ = False
440455

441-
-- make sure to removed nulls from embedded entities also
456+
-- make sure to removed nulls from embedded entities also.
457+
-- note that persistent no longer supports embedded maps
458+
-- with fields. This means any embedded bson object will
459+
-- insert null. But top level will not.
442460
embeddedVal :: PersistValue -> DB.Value
443461
embeddedVal (PersistMap m) =
444462
DB.Doc $ fmap (\(k, v) -> k DB.:= DB.val v) $ m
@@ -989,25 +1007,24 @@ orderPersistValues entDef castDoc =
9891007
-- another application may use fields we don't care about
9901008
-- our own application may set extra fields with the raw driver
9911009
match [] _ values = values
992-
match ((fieldName, medef) : columns) fields values =
1010+
match ((fName, medef) : columns) fields values =
9931011
let
9941012
((_, pv) , unused) =
9951013
matchOne fields []
9961014
in
9971015
match columns unused $
998-
values ++ [(fieldName, nestedOrder medef pv)]
1016+
values ++ [(fName, nestedOrder medef pv)]
9991017
where
1000-
nestedOrder (Just _) (PersistMap m) =
1001-
PersistMap m
1002-
nestedOrder (Just em) (PersistList l) =
1003-
PersistList $ map (nestedOrder (Just em)) l
1004-
nestedOrder Nothing found =
1005-
found
1018+
-- support for embedding other persistent objects into a schema for
1019+
-- mongodb cannot be currently supported in persistent.
1020+
-- The order will be undetermined but that's ok because there is no
1021+
-- schema migration for mongodb anyways.
1022+
-- nestedOrder (Just _) (PersistMap m) = PersistMap m
1023+
nestedOrder (Just em) (PersistList l) = PersistList $ map (nestedOrder (Just em)) l
1024+
nestedOrder _ found = found
10061025

10071026
matchOne (field:fs) tried =
1008-
if fieldName == fst field
1009-
-- snd drops the name now that it has been used to make the match
1010-
-- persistent will add the field name later
1027+
if fName == fst field
10111028
then (field, tried ++ fs)
10121029
else matchOne fs (field:tried)
10131030
-- if field is not found, assume it was a Nothing
@@ -1016,7 +1033,7 @@ orderPersistValues entDef castDoc =
10161033
-- instead, we want to store no field at all: that takes less space.
10171034
-- Also, another ORM may be doing the same
10181035
-- Also, this adding a Maybe field means no migration required
1019-
matchOne [] tried = ((fieldName, PersistNull), tried)
1036+
matchOne [] tried = ((fName, PersistNull), tried)
10201037

10211038
assocListFromDoc :: DB.Document -> [(Text, PersistValue)]
10221039
assocListFromDoc = Prelude.map (\f -> ( (DB.label f), cast (DB.value f) ) )
@@ -1057,8 +1074,7 @@ instance DB.Val PersistValue where
10571074
val (PersistRational _) = throw $ PersistMongoDBUnsupported "PersistRational not implemented for the MongoDB backend"
10581075
val (PersistArray a) = DB.val $ PersistList a
10591076
val (PersistDbSpecific _) = throw $ PersistMongoDBUnsupported "PersistDbSpecific not implemented for the MongoDB backend"
1060-
val (PersistLiteral _) = throw $ PersistMongoDBUnsupported "PersistLiteral not implemented for the MongoDB backend"
1061-
val (PersistLiteralEscaped _) = throw $ PersistMongoDBUnsupported "PersistLiteralEscaped not implemented for the MongoDB backend"
1077+
val (PersistLiteral_ _ _) = throw $ PersistMongoDBUnsupported "PersistLiteral not implemented for the MongoDB backend"
10621078
cast' (DB.Float x) = Just (PersistDouble x)
10631079
cast' (DB.Int32 x) = Just $ PersistInt64 $ fromIntegral x
10641080
cast' (DB.Int64 x) = Just $ PersistInt64 x

persistent-mongoDB/persistent-mongoDB.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: persistent-mongoDB
2-
version: 2.12.0.0
2+
version: 2.13.0.0
33
license: MIT
44
license-file: LICENSE
55
author: Greg Weber <[email protected]>
6-
maintainer: Greg Weber <[email protected]>
6+
maintainer: Andres Schmois <[email protected]>
77
synopsis: Backend for the persistent library using mongoDB.
88
category: Database
99
stability: Experimental

persistent-mongoDB/test/main.hs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ EmptyEntity
103103
main :: IO ()
104104
main = do
105105
hspec $ afterAll dropDatabase $ do
106-
xdescribe "This test is failing for Mongo by only embedding the first thing." $ do
107-
RenameTest.specsWith (db' RenameTest.cleanDB)
106+
RenameTest.specsWith (db' RenameTest.cleanDB)
108107
DataTypeTest.specsWith
109108
dbNoCleanup
110109
Nothing
@@ -135,13 +134,10 @@ main = do
135134
dbNoCleanup
136135
Nothing
137136
PersistentTest.specsWith (db' PersistentTest.cleanDB)
138-
-- TODO: The upsert tests are currently failing. Find out why and fix
139-
-- them.
140-
xdescribe "UpsertTest is currently failing for Mongo due to differing behavior" $ do
141-
UpsertTest.specsWith
142-
(db' PersistentTest.cleanDB)
143-
UpsertTest.AssumeNullIsZero
144-
UpsertTest.UpsertGenerateNewKey
137+
UpsertTest.specsWith
138+
(db' PersistentTest.cleanDB)
139+
UpsertTest.AssumeNullIsZero
140+
UpsertTest.UpsertGenerateNewKey
145141
EmptyEntityTest.specsWith
146142
(db' EmptyEntityTest.cleanDB)
147143
Nothing

persistent-test/src/PersistentTestModels.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,5 @@ cleanDB = do
239239
deleteWhere ([] :: [Filter (OutdoorPetGeneric backend)])
240240
deleteWhere ([] :: [Filter (UserPTGeneric backend)])
241241
deleteWhere ([] :: [Filter (EmailPTGeneric backend)])
242+
deleteWhere ([] :: [Filter (UpsertGeneric backend)])
243+
deleteWhere ([] :: [Filter (UpsertByGeneric backend)])

stack-nightly.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ packages:
33
- ./persistent
44
- ./persistent-sqlite
55
- ./persistent-test
6-
# - ./persistent-mongoDB
6+
- ./persistent-mongoDB
77
- ./persistent-mysql
88
- ./persistent-postgresql
99
- ./persistent-redis

stack.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ packages:
33
- ./persistent
44
- ./persistent-sqlite
55
- ./persistent-test
6-
# - ./persistent-mongoDB
6+
- ./persistent-mongoDB
77
- ./persistent-mysql
88
- ./persistent-postgresql
99
- ./persistent-redis

0 commit comments

Comments
 (0)