From fd0e2614156c3e4bf0a966ce8c8cea329d73a11c Mon Sep 17 00:00:00 2001 From: balanka Date: Thu, 27 Jul 2023 14:50:55 +0200 Subject: [PATCH 1/5] Implemented https://github.com/zio/zio-sql/issues/899 and refactored changing the return typ of the bach delete and update from IO[Exception, List[Int]] to IO[Exception, Int] --- .../scala/zio/sql/SqlDriverLiveModule.scala | 22 +++-- .../zio/sql/TransactionSyntaxModule.scala | 10 +++ jdbc/src/main/scala/zio/sql/jdbc.scala | 12 ++- .../zio/sql/postgresql/DeleteBatchSpec.scala | 4 +- .../zio/sql/postgresql/TransactionSpec.scala | 90 +++++++++++++++++-- .../zio/sql/postgresql/UpdateBatchSpec.scala | 2 +- 6 files changed, 117 insertions(+), 23 deletions(-) diff --git a/jdbc/src/main/scala/zio/sql/SqlDriverLiveModule.scala b/jdbc/src/main/scala/zio/sql/SqlDriverLiveModule.scala index af6563645..4d363a583 100644 --- a/jdbc/src/main/scala/zio/sql/SqlDriverLiveModule.scala +++ b/jdbc/src/main/scala/zio/sql/SqlDriverLiveModule.scala @@ -14,9 +14,9 @@ import zio.sql.delete._ trait SqlDriverLiveModule { self: Jdbc => private[sql] trait SqlDriverCore { - def deleteOnBatch(delete: List[Delete[_]], conn: Connection): IO[Exception, List[Int]] + def deleteOnBatch(delete: List[Delete[_]], conn: Connection): IO[Exception, Int] - def updateOnBatch(update: List[Update[_]], conn: Connection): IO[Exception, List[Int]] + def updateOnBatch(update: List[Update[_]], conn: Connection): IO[Exception, Int] def deleteOn(delete: Delete[_], conn: Connection): IO[Exception, Int] @@ -31,7 +31,7 @@ trait SqlDriverLiveModule { self: Jdbc => def delete(delete: Delete[_]): IO[Exception, Int] = ZIO.scoped(pool.connection.flatMap(deleteOn(delete, _))) - def delete(delete: List[Delete[_]]): IO[Exception, List[Int]] = + def delete(delete: List[Delete[_]]): IO[Exception, Int] = ZIO.scoped(pool.connection.flatMap(deleteOnBatch(delete, _))) def deleteOn(delete: Delete[_], conn: Connection): IO[Exception, Int] = @@ -41,11 +41,11 @@ trait SqlDriverLiveModule { self: Jdbc => statement.executeUpdate(query) }.refineToOrDie[Exception] - def deleteOnBatch(delete: List[Delete[_]], conn: Connection): IO[Exception, List[Int]] = + def deleteOnBatch(delete: List[Delete[_]], conn: Connection): IO[Exception, Int] = ZIO.attemptBlocking { val statement = conn.createStatement() delete.map(delete_ => statement.addBatch(renderDelete(delete_))) - statement.executeBatch().toList + statement.executeBatch().foldLeft(0) { case (acc, el) => acc + el } }.refineToOrDie[Exception] def update(update: Update[_]): IO[Exception, Int] = @@ -58,14 +58,14 @@ trait SqlDriverLiveModule { self: Jdbc => statement.executeUpdate(query) }.refineToOrDie[Exception] - def update(update: List[Update[_]]): IO[Exception, List[Int]] = + def update(update: List[Update[_]]): IO[Exception, Int] = ZIO.scoped(pool.connection.flatMap(updateOnBatch(update, _))) - def updateOnBatch(update: List[Update[_]], conn: Connection): IO[Exception, List[Int]] = + def updateOnBatch(update: List[Update[_]], conn: Connection): IO[Exception, Int] = ZIO.attemptBlocking { val statement = conn.createStatement() update.map(update_ => statement.addBatch(renderUpdate(update_))) - statement.executeBatch().toList + statement.executeBatch().foldLeft(0) { case (acc, el) => acc + el } }.refineToOrDie[Exception] def read[A](read: Read[A]): Stream[Exception, A] = @@ -136,9 +136,15 @@ trait SqlDriverLiveModule { self: Jdbc => def delete(delete: Delete[_]): IO[Exception, Int] = deleteOn(delete, connection) + def delete(delete: List[Delete[_]]): IO[Exception, Int] = + deleteOnBatch(delete, connection) + def update(update: Update[_]): IO[Exception, Int] = updateOn(update, connection) + def update(update: List[Update[_]]): IO[Exception, Int] = + updateOnBatch(update, connection) + def read[A](read: Read[A]): Stream[Exception, A] = readOn(read, connection) diff --git a/jdbc/src/main/scala/zio/sql/TransactionSyntaxModule.scala b/jdbc/src/main/scala/zio/sql/TransactionSyntaxModule.scala index 3548c6846..e0e79db33 100644 --- a/jdbc/src/main/scala/zio/sql/TransactionSyntaxModule.scala +++ b/jdbc/src/main/scala/zio/sql/TransactionSyntaxModule.scala @@ -19,6 +19,11 @@ trait TransactionSyntaxModule { self: Jdbc => ZIO.serviceWithZIO(_.delete(self)) } + implicit final class BatchDeleteSyntax[A: Schema](self: List[Delete[_]]) { + def run: ZIO[SqlTransaction, Exception, Int] = + ZIO.serviceWithZIO(_.delete(self)) + } + implicit final class InsertSyntax[A: Schema](self: Insert[_, A]) { def run: ZIO[SqlTransaction, Exception, Int] = ZIO.serviceWithZIO(_.insert(self)) @@ -28,4 +33,9 @@ trait TransactionSyntaxModule { self: Jdbc => def run: ZIO[SqlTransaction, Exception, Int] = ZIO.serviceWithZIO(_.update(self)) } + + implicit final class BatchUpdatedSyntax(self: List[Update[_]]) { + def run: ZIO[SqlTransaction, Exception, Int] = + ZIO.serviceWithZIO(_.update(self)) + } } diff --git a/jdbc/src/main/scala/zio/sql/jdbc.scala b/jdbc/src/main/scala/zio/sql/jdbc.scala index eb1674a93..d620fd615 100644 --- a/jdbc/src/main/scala/zio/sql/jdbc.scala +++ b/jdbc/src/main/scala/zio/sql/jdbc.scala @@ -13,11 +13,11 @@ trait Jdbc extends Sql with JdbcInternalModule with SqlDriverLiveModule with Tra trait SqlDriver { def delete(delete: Delete[_]): IO[Exception, Int] - def delete(delete: List[Delete[_]]): IO[Exception, List[Int]] + def delete(delete: List[Delete[_]]): IO[Exception, Int] def update(update: Update[_]): IO[Exception, Int] - def update(update: List[Update[_]]): IO[Exception, List[Int]] + def update(update: List[Update[_]]): IO[Exception, Int] def read[A](read: Read[A]): Stream[Exception, A] @@ -35,8 +35,12 @@ trait Jdbc extends Sql with JdbcInternalModule with SqlDriverLiveModule with Tra def delete(delete: Delete[_]): IO[Exception, Int] + def delete(delete: List[Delete[_]]): IO[Exception, Int] + def update(update: Update[_]): IO[Exception, Int] + def update(update: List[Update[_]]): IO[Exception, Int] + def read[A](read: Read[A]): Stream[Exception, A] def insert[A: Schema](insert: Insert[_, A]): IO[Exception, Int] @@ -65,7 +69,7 @@ trait Jdbc extends Sql with JdbcInternalModule with SqlDriverLiveModule with Tra def execute(delete: Delete[_]): ZIO[SqlDriver, Exception, Int] = ZIO.serviceWithZIO(_.delete(delete)) - def executeBatchDelete(delete: List[Delete[_]]): ZIO[SqlDriver, Exception, List[Int]] = + def executeBatchDelete(delete: List[Delete[_]]): ZIO[SqlDriver, Exception, Int] = ZIO.serviceWithZIO(_.delete(delete)) def execute[A: Schema](insert: Insert[_, A]): ZIO[SqlDriver, Exception, Int] = @@ -74,7 +78,7 @@ trait Jdbc extends Sql with JdbcInternalModule with SqlDriverLiveModule with Tra def execute(update: Update[_]): ZIO[SqlDriver, Exception, Int] = ZIO.serviceWithZIO(_.update(update)) - def executeBatchUpdate(update: List[Update[_]]): ZIO[SqlDriver, Exception, List[Int]] = + def executeBatchUpdate(update: List[Update[_]]): ZIO[SqlDriver, Exception, Int] = ZIO.serviceWithZIO(_.update(update)) val transact: ZLayer[SqlDriver, Exception, SqlTransaction] = diff --git a/postgres/src/test/scala/zio/sql/postgresql/DeleteBatchSpec.scala b/postgres/src/test/scala/zio/sql/postgresql/DeleteBatchSpec.scala index 8e7efb1a5..6257c05b6 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/DeleteBatchSpec.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/DeleteBatchSpec.scala @@ -23,7 +23,7 @@ object DeleteBatchSpec extends PostgresRunnableSpec with DbSchema { val assertion = for { r <- result - } yield assert(r)(equalTo(List(1))) + } yield assert(r)(equalTo(1)) assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) }, @@ -83,7 +83,7 @@ object DeleteBatchSpec extends PostgresRunnableSpec with DbSchema { _ <- insertResult customers <- selectResult deletes = customers.toList.map(delete_) - result <- executeBatchDelete(deletes).map(l => l.fold(0)((a, b) => a + b)) + result <- executeBatchDelete(deletes) } yield assert(result)(equalTo(expected)) assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) diff --git a/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala b/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala index d34dda408..a8a627f43 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala @@ -1,16 +1,25 @@ package zio.sql.postgresql import zio._ +import zio.sql.update.Update import zio.test.Assertion._ import zio.test._ import zio.test.TestAspect.sequential +import java.time.{LocalDate, ZonedDateTime} +import java.util.UUID + object TransactionSpec extends PostgresRunnableSpec with DbSchema { override val autoCommit = false import CustomerSchema._ + private def update_(c: Customer): Update[customers.TableType] = + update(customers) + .set(verified, !c.verified) + .where(customerId === c.id) + override def specLayered = suite("Postgres module")( test("Transaction is returning the last value") { val query = select(customerId) from customers @@ -38,18 +47,83 @@ object TransactionSpec extends PostgresRunnableSpec with DbSchema { assertZIO(result)(equalTo((5L, 5L))).mapErrorCause(cause => Cause.stackless(cause.untraced)) }, test("Transaction succeeded and deleted rows") { - val query = select(customerId) from customers val deleteQuery = deleteFrom(customers).where(verified === false) + val id1 = UUID.randomUUID() + val id2 = UUID.randomUUID() - val tx = transact(deleteQuery.run) + val c1 = Customer( + id1, + LocalDate.now(), + "fnameCustomer1", + "lnameCustomer1", + true, + LocalDate.now().toString, + ZonedDateTime.now() + ) + val c2 = Customer( + id2, + LocalDate.now(), + "fnameCustomer2", + "lnameCustomer2", + true, + LocalDate.now().toString, + ZonedDateTime.now() + ) + val allCustomer = List(c1, c2) + val data = allCustomer.map(Customer.unapply(_).get) + val insertStmt = insertInto(customers)(ALL).values(data) + val updateStmt = allCustomer.map(update_) - val result = (for { - allCustomersCount <- execute(query).runCount - _ <- tx - remainingCustomersCount <- execute(query).runCount - } yield (allCustomersCount, remainingCustomersCount)) + val batchResult = for { + deleted<-deleteQuery.run + inserted<-insertStmt.run + updated<-updateStmt.run + }yield deleted+inserted+updated + + val result = for { + tx <- transact(batchResult) + } yield tx + + assertZIO(result)(equalTo(5)).mapErrorCause(cause => Cause.stackless(cause.untraced)) + }, + test ("Transaction failed and no row was inserted updated or deleted") { + val deleteQuery = deleteFrom(customers).where(verified === false) + val id1 = UUID.randomUUID() + //val id2 = UUID.randomUUID() + + val c1 = Customer( + id1, + LocalDate.now(), + "fnameCustomer1", + "lnameCustomer1", + true, + LocalDate.now().toString, + ZonedDateTime.now() + ) + val c2 = Customer( + id1, + LocalDate.now(), + "fnameCustomer2", + "lnameCustomer2", + true, + LocalDate.now().toString, + ZonedDateTime.now() + ) + val allCustomer = List(c1, c2) + val data = allCustomer.map(Customer.unapply(_).get) + val insertStmt = insertInto(customers)(ALL).values(data) + val updateStmt = allCustomer.map(update_) + + val batchResult = for { + deleted <- deleteQuery.run + inserted <- insertStmt.run + updated <- updateStmt.run + } yield deleted + inserted + updated - assertZIO(result)(equalTo((5L, 4L))).mapErrorCause(cause => Cause.stackless(cause.untraced)) + val result = for { + tx <- transact(batchResult) + } yield tx + assertZIO(result)(equalTo(0)).mapErrorCause(cause => Cause.stackless(cause.untraced)) } ) @@ sequential } diff --git a/postgres/src/test/scala/zio/sql/postgresql/UpdateBatchSpec.scala b/postgres/src/test/scala/zio/sql/postgresql/UpdateBatchSpec.scala index cd1b1cae2..04dad2230 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/UpdateBatchSpec.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/UpdateBatchSpec.scala @@ -76,7 +76,7 @@ object UpdateBatchSpec extends PostgresRunnableSpec with DbSchema { val assertion_ = for { x <- result_ updated = x.toList.map(update_) - result <- executeBatchUpdate(updated).map(l => l.reduce((a, b) => a + b)) + result <- executeBatchUpdate(updated) } yield assert(result)(equalTo(5)) assertion_.mapErrorCause(cause => Cause.stackless(cause.untraced)) } From 142d782ba45b0a7f00ed2f1e9f1f3be77cfdd325 Mon Sep 17 00:00:00 2001 From: balanka Date: Fri, 28 Jul 2023 09:14:45 +0200 Subject: [PATCH 2/5] Enhanced unittest by adding transactional processing --- .../zio/sql/postgresql/TransactionSpec.scala | 41 +------------------ 1 file changed, 1 insertion(+), 40 deletions(-) diff --git a/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala b/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala index a8a627f43..24f290217 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala @@ -6,6 +6,7 @@ import zio.test.Assertion._ import zio.test._ import zio.test.TestAspect.sequential + import java.time.{LocalDate, ZonedDateTime} import java.util.UUID @@ -83,47 +84,7 @@ object TransactionSpec extends PostgresRunnableSpec with DbSchema { val result = for { tx <- transact(batchResult) } yield tx - assertZIO(result)(equalTo(5)).mapErrorCause(cause => Cause.stackless(cause.untraced)) - }, - test ("Transaction failed and no row was inserted updated or deleted") { - val deleteQuery = deleteFrom(customers).where(verified === false) - val id1 = UUID.randomUUID() - //val id2 = UUID.randomUUID() - - val c1 = Customer( - id1, - LocalDate.now(), - "fnameCustomer1", - "lnameCustomer1", - true, - LocalDate.now().toString, - ZonedDateTime.now() - ) - val c2 = Customer( - id1, - LocalDate.now(), - "fnameCustomer2", - "lnameCustomer2", - true, - LocalDate.now().toString, - ZonedDateTime.now() - ) - val allCustomer = List(c1, c2) - val data = allCustomer.map(Customer.unapply(_).get) - val insertStmt = insertInto(customers)(ALL).values(data) - val updateStmt = allCustomer.map(update_) - - val batchResult = for { - deleted <- deleteQuery.run - inserted <- insertStmt.run - updated <- updateStmt.run - } yield deleted + inserted + updated - - val result = for { - tx <- transact(batchResult) - } yield tx - assertZIO(result)(equalTo(0)).mapErrorCause(cause => Cause.stackless(cause.untraced)) } ) @@ sequential } From 69ac917aa62e0d81470c038c006029983911226a Mon Sep 17 00:00:00 2001 From: balanka Date: Fri, 28 Jul 2023 10:56:28 +0200 Subject: [PATCH 3/5] Bug fix --- .../zio/sql/postgresql/TransactionSpec.scala | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala b/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala index 24f290217..022b624a0 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala @@ -6,7 +6,7 @@ import zio.test.Assertion._ import zio.test._ import zio.test.TestAspect.sequential - +//import java.sql.BatchUpdateException import java.time.{LocalDate, ZonedDateTime} import java.util.UUID @@ -85,6 +85,48 @@ object TransactionSpec extends PostgresRunnableSpec with DbSchema { tx <- transact(batchResult) } yield tx assertZIO(result)(equalTo(5)).mapErrorCause(cause => Cause.stackless(cause.untraced)) + }, + test ("Transaction failed and no row was inserted updated or deleted") { + val deleteQuery = deleteFrom(customers).where(verified === false) + val id1 = UUID.randomUUID() + //val id2 = UUID.randomUUID() + + val c1 = Customer( + id1, + LocalDate.now(), + "fnameCustomer1", + "lnameCustomer1", + true, + LocalDate.now().toString, + ZonedDateTime.now() + ) + val c2 = Customer( + id1, + LocalDate.now(), + "fnameCustomer2", + "lnameCustomer2", + true, + LocalDate.now().toString, + ZonedDateTime.now() + ) + val allCustomer = List(c1, c2) + val data = allCustomer.map(Customer.unapply(_).get) + val insertStmt = insertInto(customers)(ALL).values(data) + val updateStmt = allCustomer.map(update_) + + val batchResult = for { + deleted <- deleteQuery.run + //inserted <- insertStmt.run + _ <- ZIO.fail(insertStmt.run).exit + updated <- updateStmt.run + + } yield deleted+updated //+ inserted + + val result = (for { + tx <- transact(batchResult) + } yield tx).flip.exit + assertZIO(result) (fails((anything))) + } ) @@ sequential } From 7e04aaaf4310ff16b484fca07607b2cdc7410143 Mon Sep 17 00:00:00 2001 From: balanka Date: Fri, 28 Jul 2023 11:00:44 +0200 Subject: [PATCH 4/5] Reformated the code --- .../zio/sql/postgresql/TransactionSpec.scala | 63 +++++++++---------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala b/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala index 022b624a0..1956fc1df 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala @@ -6,8 +6,7 @@ import zio.test.Assertion._ import zio.test._ import zio.test.TestAspect.sequential -//import java.sql.BatchUpdateException -import java.time.{LocalDate, ZonedDateTime} +import java.time.{ LocalDate, ZonedDateTime } import java.util.UUID object TransactionSpec extends PostgresRunnableSpec with DbSchema { @@ -16,11 +15,6 @@ object TransactionSpec extends PostgresRunnableSpec with DbSchema { import CustomerSchema._ - private def update_(c: Customer): Update[customers.TableType] = - update(customers) - .set(verified, !c.verified) - .where(customerId === c.id) - override def specLayered = suite("Postgres module")( test("Transaction is returning the last value") { val query = select(customerId) from customers @@ -49,10 +43,10 @@ object TransactionSpec extends PostgresRunnableSpec with DbSchema { }, test("Transaction succeeded and deleted rows") { val deleteQuery = deleteFrom(customers).where(verified === false) - val id1 = UUID.randomUUID() - val id2 = UUID.randomUUID() + val id1 = UUID.randomUUID() + val id2 = UUID.randomUUID() - val c1 = Customer( + val c1 = Customer( id1, LocalDate.now(), "fnameCustomer1", @@ -61,7 +55,7 @@ object TransactionSpec extends PostgresRunnableSpec with DbSchema { LocalDate.now().toString, ZonedDateTime.now() ) - val c2 = Customer( + val c2 = Customer( id2, LocalDate.now(), "fnameCustomer2", @@ -71,27 +65,27 @@ object TransactionSpec extends PostgresRunnableSpec with DbSchema { ZonedDateTime.now() ) val allCustomer = List(c1, c2) - val data = allCustomer.map(Customer.unapply(_).get) - val insertStmt = insertInto(customers)(ALL).values(data) - val updateStmt = allCustomer.map(update_) + val data = allCustomer.map(Customer.unapply(_).get) + val insertStmt = insertInto(customers)(ALL).values(data) + val updateStmt = allCustomer.map(update_) val batchResult = for { - deleted<-deleteQuery.run - inserted<-insertStmt.run - updated<-updateStmt.run - }yield deleted+inserted+updated + deleted <- deleteQuery.run + inserted <- insertStmt.run + updated <- updateStmt.run + } yield deleted + inserted + updated val result = for { - tx <- transact(batchResult) - } yield tx + tx <- transact(batchResult) + } yield tx assertZIO(result)(equalTo(5)).mapErrorCause(cause => Cause.stackless(cause.untraced)) }, - test ("Transaction failed and no row was inserted updated or deleted") { + test("Transaction failed and no row was inserted updated or deleted") { val deleteQuery = deleteFrom(customers).where(verified === false) - val id1 = UUID.randomUUID() - //val id2 = UUID.randomUUID() + val id1 = UUID.randomUUID() + // val id2 = UUID.randomUUID() - val c1 = Customer( + val c1 = Customer( id1, LocalDate.now(), "fnameCustomer1", @@ -100,7 +94,7 @@ object TransactionSpec extends PostgresRunnableSpec with DbSchema { LocalDate.now().toString, ZonedDateTime.now() ) - val c2 = Customer( + val c2 = Customer( id1, LocalDate.now(), "fnameCustomer2", @@ -110,23 +104,28 @@ object TransactionSpec extends PostgresRunnableSpec with DbSchema { ZonedDateTime.now() ) val allCustomer = List(c1, c2) - val data = allCustomer.map(Customer.unapply(_).get) - val insertStmt = insertInto(customers)(ALL).values(data) - val updateStmt = allCustomer.map(update_) + val data = allCustomer.map(Customer.unapply(_).get) + val insertStmt = insertInto(customers)(ALL).values(data) + val updateStmt = allCustomer.map(update_) val batchResult = for { deleted <- deleteQuery.run - //inserted <- insertStmt.run - _ <- ZIO.fail(insertStmt.run).exit + // inserted <- insertStmt.run + _ <- ZIO.fail(insertStmt.run).exit updated <- updateStmt.run - } yield deleted+updated //+ inserted + } yield deleted + updated // + inserted val result = (for { tx <- transact(batchResult) } yield tx).flip.exit - assertZIO(result) (fails((anything))) + assertZIO(result)(fails((anything))) } ) @@ sequential + + private def update_(c: Customer): Update[customers.TableType] = + update(customers) + .set(verified, !c.verified) + .where(customerId === c.id) } From 8b437bfc0d8e2b1f6129d7e4d48689da3e5ac0f0 Mon Sep 17 00:00:00 2001 From: balanka Date: Fri, 28 Jul 2023 12:00:07 +0200 Subject: [PATCH 5/5] Refactored --- jdbc/src/main/scala/zio/sql/SqlDriverLiveModule.scala | 4 ++-- .../src/test/scala/zio/sql/postgresql/TransactionSpec.scala | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/jdbc/src/main/scala/zio/sql/SqlDriverLiveModule.scala b/jdbc/src/main/scala/zio/sql/SqlDriverLiveModule.scala index 4d363a583..690d479c7 100644 --- a/jdbc/src/main/scala/zio/sql/SqlDriverLiveModule.scala +++ b/jdbc/src/main/scala/zio/sql/SqlDriverLiveModule.scala @@ -45,7 +45,7 @@ trait SqlDriverLiveModule { self: Jdbc => ZIO.attemptBlocking { val statement = conn.createStatement() delete.map(delete_ => statement.addBatch(renderDelete(delete_))) - statement.executeBatch().foldLeft(0) { case (acc, el) => acc + el } + statement.executeBatch().sum }.refineToOrDie[Exception] def update(update: Update[_]): IO[Exception, Int] = @@ -65,7 +65,7 @@ trait SqlDriverLiveModule { self: Jdbc => ZIO.attemptBlocking { val statement = conn.createStatement() update.map(update_ => statement.addBatch(renderUpdate(update_))) - statement.executeBatch().foldLeft(0) { case (acc, el) => acc + el } + statement.executeBatch().sum }.refineToOrDie[Exception] def read[A](read: Read[A]): Stream[Exception, A] = diff --git a/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala b/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala index 1956fc1df..ffdadc64b 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/TransactionSpec.scala @@ -83,7 +83,6 @@ object TransactionSpec extends PostgresRunnableSpec with DbSchema { test("Transaction failed and no row was inserted updated or deleted") { val deleteQuery = deleteFrom(customers).where(verified === false) val id1 = UUID.randomUUID() - // val id2 = UUID.randomUUID() val c1 = Customer( id1, @@ -110,11 +109,10 @@ object TransactionSpec extends PostgresRunnableSpec with DbSchema { val batchResult = for { deleted <- deleteQuery.run - // inserted <- insertStmt.run _ <- ZIO.fail(insertStmt.run).exit updated <- updateStmt.run - } yield deleted + updated // + inserted + } yield deleted + updated val result = (for { tx <- transact(batchResult)