Skip to content

Commit

Permalink
skunk + cats effect munit
Browse files Browse the repository at this point in the history
  • Loading branch information
djnzx committed Jul 19, 2024
1 parent 771407e commit cc5d164
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions munitx/src/main/scala/skunkx/LearnSkunkAndMunit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -253,20 +253,22 @@ class LearnSkunkAndMunit extends CatsEffectSuite
def selectAll(s: Session[IO]): IO[List[Pet]] =
s.execute(selectAllQ)

def tryInsertAll(s: Session[IO], xs: List[Pet]) =
case class RxWithContext(a: Pet, t: Throwable) extends RuntimeException

def tryInsertAll(s: Session[IO], xs: List[Pet]): IO[Either[(Throwable, String, Pet), Unit]] =
s.prepare(insertOneQ)
.flatMap { pc =>
s.transaction.use { xa =>
xa.savepoint
.flatMap { sp =>
xs.traverse_(pc.execute)
xs.traverse_(x => pc.execute(x).adaptError(t => RxWithContext(x, t)))
.map(_.asRight)
.recoverWith {
case ss @ SqlState.UniqueViolation(ex) =>
case RxWithContext(x, ss @ SqlState.UniqueViolation(ex)) =>
val msg = ex.constraintName.getOrElse("<unknown>")
logF(s"Unique violation: $msg, rolling back...") >>
logF(s"Unique violation: $msg, for id=${x.age}, rolling back...") >>
xa.rollback(sp) >>
ex.code.asLeft.pure[IO]
(ss, msg, x).asLeft.pure[IO]
}
}
}
Expand Down

0 comments on commit cc5d164

Please sign in to comment.