Skip to content

Commit

Permalink
Fixed silent VersionConflicts in doobie driver
Browse files Browse the repository at this point in the history
  • Loading branch information
hnaderi committed Jan 22, 2024
1 parent 588ece5 commit ff48591
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import edomata.backend.eventsourcing.Backend
import edomata.core.CommandMessage
import edomata.core.Edomaton
import edomata.core.ResponseD
import munit.Location
import tests.TestDomain.given_ModelTC_State_Event_Rejection

import java.time.Instant
Expand All @@ -43,25 +44,26 @@ abstract class PersistenceSuite(
private def assertJournal(
s: SUT,
address: String
)(evs: Int*) = s.journal
)(evs: Int*)(using Location) = s.journal
.readStream(address)
.map(_.payload)
.compile
.toList
.assertEquals(evs.toList)

private def assertOutbox(
s: SUT,
address: String
)(evs: Int*) = s.outbox.read
)(evs: Int*)(using Location) = s.outbox.read
.filter(_.streamId == address)
.map(_.data)
.compile
.toList
.assertEquals(evs.toList)

private def assertNotifiedJournal(s: SUT) =
private def assertNotifiedJournal(s: SUT)(using Location) =
s.updates.journal.head.compile.lastOrError.assertEquals(())
private def assertNotifiedOutbox(s: SUT) =
private def assertNotifiedOutbox(s: SUT)(using Location) =
s.updates.outbox.head.compile.lastOrError.assertEquals(())

check("Must append correctly") { s =>
Expand Down
11 changes: 7 additions & 4 deletions modules/doobie/src/main/scala/DoobieRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import cats.data.Chain
import cats.data.NonEmptyChain
import cats.effect.kernel.Clock
import cats.effect.kernel.Sync
import cats.implicits.*
import cats.syntax.all.*
import edomata.backend.*
import edomata.backend.eventsourcing.*
import edomata.core.*
Expand Down Expand Up @@ -89,9 +89,12 @@ private final class DoobieRepository[F[_], S, E, R, N](
_ <- cmds.insert(ctx.command).run.assertInserted
} yield ()

_ <- query.transact(trx).attemptSomeSqlState {
case sqlstate.class23.UNIQUE_VIOLATION => BackendError.VersionConflict
}
_ <- query
.transact(trx)
.attemptSomeSqlState { case sqlstate.class23.UNIQUE_VIOLATION =>
BackendError.VersionConflict
}
.flatMap(F.fromEither)
_ <- updates.notifyJournal
_ <- updates.notifyOutbox
} yield ()
Expand Down

0 comments on commit ff48591

Please sign in to comment.