Skip to content

Commit eeb212d

Browse files
committed
change: use createIndexes for MongoDB >= 4.2
Starting in MongoDB 4.2 (Wire version 8), system.indexes has been removed.
1 parent 163e5c6 commit eeb212d

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

Database/MongoDB/Admin.hs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import Control.Applicative ((<$>))
3333
#endif
3434
import Control.Concurrent (forkIO, threadDelay)
3535
import Control.Monad (forever, unless, liftM)
36+
import Control.Monad.Reader (asks)
3637
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
3738
import Data.Maybe (maybeToList)
3839
import Data.Set (Set)
@@ -48,12 +49,12 @@ import Data.Text (Text)
4849
import qualified Data.Text as T
4950

5051
import Database.MongoDB.Connection (Host, showHostPort)
51-
import Database.MongoDB.Internal.Protocol (pwHash, pwKey)
52+
import Database.MongoDB.Internal.Protocol (maxWireVersion, pwHash, pwKey, serverData)
5253
import Database.MongoDB.Internal.Util ((<.>), true1)
5354
import Database.MongoDB.Query (Action, Database, Collection, Username, Password,
5455
Order, Query(..), accessMode, master, runCommand,
5556
useDb, thisDatabase, rest, select, find, findOne,
56-
insert_, save, delete)
57+
insert_, save, delete, mongoPipe)
5758

5859
-- * Admin
5960

@@ -118,7 +119,7 @@ genName :: Order -> IndexName
118119
genName keys = T.intercalate "_" (map f keys) where
119120
f (k := v) = k `T.append` "_" `T.append` T.pack (show v)
120121

121-
ensureIndex :: (MonadIO m) => Index -> Action m ()
122+
ensureIndex :: (MonadFail m, MonadIO m) => Index -> Action m ()
122123
-- ^ Create index if we did not already create one. May be called repeatedly with practically no performance hit, because we remember if we already called this for the same index (although this memory gets wiped out every 15 minutes, in case another client drops the index and we want to create it again).
123124
ensureIndex idx = let k = (iColl idx, iName idx) in do
124125
icache <- fetchIndexCache
@@ -127,9 +128,24 @@ ensureIndex idx = let k = (iColl idx, iName idx) in do
127128
accessMode master (createIndex idx)
128129
liftIO $ writeIORef icache (Set.insert k set)
129130

130-
createIndex :: (MonadIO m) => Index -> Action m ()
131+
createIndex :: (MonadFail m, MonadIO m) => Index -> Action m ()
131132
-- ^ Create index on the server. This call goes to the server every time.
132-
createIndex idx = insert_ "system.indexes" . idxDocument idx =<< thisDatabase
133+
createIndex idx = do
134+
pipe <- asks mongoPipe
135+
if maxWireVersion (serverData pipe) < 8
136+
then
137+
insert_ "system.indexes" . idxDocument idx =<< thisDatabase
138+
else do
139+
-- Starting in MongoDB 4.2 (Wire version 8), system.indexes has been removed
140+
idxDoc <- idxDocument idx <$> thisDatabase
141+
resp <- runCommand
142+
[ "createIndexes" =: iColl idx,
143+
"indexes" =: [idxDoc],
144+
"writeConcern" =: ["w" =: (1 :: Int), "j" =: True]
145+
]
146+
if true1 "ok" resp
147+
then pure ()
148+
else fail $ "createIndex: " <> show resp
133149

134150
dropIndex :: (MonadIO m) => Collection -> IndexName -> Action m Document
135151
-- ^ Remove the index from the given collection.

Database/MongoDB/GridFS.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ md5BlockSizeInBytes = 64
5353
data Bucket = Bucket {files :: Text, chunks :: Text}
5454
-- ^ Files are stored in "buckets". You open a bucket with openDefaultBucket or openBucket
5555

56-
openDefaultBucket :: (Monad m, MonadIO m) => Action m Bucket
56+
openDefaultBucket :: (MonadFail m, MonadIO m) => Action m Bucket
5757
-- ^ Open the default 'Bucket' (named "fs")
5858
openDefaultBucket = openBucket "fs"
5959

60-
openBucket :: (Monad m, MonadIO m) => Text -> Action m Bucket
60+
openBucket :: (MonadFail m, MonadIO m) => Text -> Action m Bucket
6161
-- ^ Open a 'Bucket'
6262
openBucket name = do
6363
let filesCollection = name `append` ".files"

0 commit comments

Comments
 (0)