Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
ReactiveRepository#ensureIndex now calls collection.indexesManager.cr…
Browse files Browse the repository at this point in the history
…eate() to ensure all indexes are created

There is an additional code flow to allow for backwards compatibility between 2.6.x and 3.x.x
  • Loading branch information
duncancrawford committed Sep 14, 2015
1 parent 2276949 commit 6c9ffa7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 66 deletions.
18 changes: 0 additions & 18 deletions src/main/scala/uk/gov/hmrc/mongo/EnsureIndexDelete.scala

This file was deleted.

18 changes: 16 additions & 2 deletions src/main/scala/uk/gov/hmrc/mongo/ReactiveRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import reactivemongo.api.commands._
import reactivemongo.api.indexes.Index
import reactivemongo.api.{DB, ReadPreference}
import reactivemongo.core.commands.Count
import reactivemongo.core.errors.{GenericDatabaseException, DetailedDatabaseException, DatabaseException}
import reactivemongo.json.ImplicitBSONHandlers
import reactivemongo.json.collection.JSONCollection
import uk.gov.hmrc.mongo.json.ReactiveMongoFormats
Expand All @@ -30,7 +31,7 @@ abstract class ReactiveRepository[A <: Any, ID <: Any](collectionName: String,
lazy val collection = mc.getOrElse(mongo().collection[JSONCollection](collectionName))

protected val logger = LoggerFactory.getLogger(this.getClass).asInstanceOf[Logger]
val message: String = "ensuring index failed"
val message: String = "Failed to ensure index"

ensureIndexes

Expand Down Expand Up @@ -79,8 +80,21 @@ abstract class ReactiveRepository[A <: Any, ID <: Any](collectionName: String,
}


private val DuplicateKeyError = "E11000"
private def ensureIndex(index: Index)(implicit ec: ExecutionContext): Future[Boolean] = {
collection.indexesManager.ensure(index).recover {
collection.indexesManager.create(index).map(wr => {
if(!wr.ok) {
val maybeMsg = for {
msg <- wr.errmsg
m <- if (msg.contains(DuplicateKeyError)) {
// this is for backwards compatibility to mongodb 2.6.x
throw new GenericDatabaseException(msg, wr.code)
}else Some(msg)
} yield m
logger.error(s"$message : '${maybeMsg.map(_.toString)}'")
}
wr.ok
}).recover {
case t =>
logger.error(message, t)
false
Expand Down
45 changes: 0 additions & 45 deletions src/test/scala/uk/gov/hmrc/mongo/EnsureIndexDeleteSpec.scala

This file was deleted.

10 changes: 9 additions & 1 deletion src/test/scala/uk/gov/hmrc/mongo/ReactiveRepositorySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class ReactiveRepositorySpec extends WordSpec with Matchers with MongoSpecSuppor
}
}

"should log any error that arises when creating an index" in {
"should log any error when index fails to create" in {
withCaptureOfLoggingFrom[FailingIndexesTestRepository] { logList =>
await(uniqueIndexRepository.drop)

Expand All @@ -234,8 +234,16 @@ class ReactiveRepositorySpec extends WordSpec with Matchers with MongoSpecSuppor
await(uniqueIndexRepository.ensureIndexes) shouldBe Seq(false)
logList.size should be(1)
logList.head.getMessage shouldBe (uniqueIndexRepository.message)
}
}

"should ignore already applied index" in {
withCaptureOfLoggingFrom[FailingIndexesTestRepository] { logList =>
await(repository.ensureIndexes) shouldBe Seq(true, true)
logList.size should be(0)
}

await(repository.collection.indexesManager.list()).size shouldBe 3
}
}

Expand Down

0 comments on commit 6c9ffa7

Please sign in to comment.