Skip to content

Commit

Permalink
Merge branch 'master' into 639_hikaricp
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeda authored Jun 20, 2022
2 parents 112b7a5 + 8cc7f98 commit b8d1ab8
Show file tree
Hide file tree
Showing 22 changed files with 491 additions and 215 deletions.
15 changes: 10 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ addCommandAlias("check", "all scalafmtSbtCheck scalafmtCheck test:scalafmtCheck"
val zioVersion = "2.0.0-RC6"
val zioSchemaVersion = "0.1.9"
val testcontainersVersion = "1.17.2"
val testcontainersScalaVersion = "0.40.7"
val testcontainersScalaVersion = "0.40.8"
val logbackVersion = "1.2.11"

lazy val root = project
.in(file("."))
Expand Down Expand Up @@ -161,7 +162,8 @@ lazy val mysql = project
"org.testcontainers" % "jdbc" % testcontainersVersion % Test,
"org.testcontainers" % "mysql" % testcontainersVersion % Test,
"mysql" % "mysql-connector-java" % "8.0.29" % Test,
"com.dimafeng" %% "testcontainers-scala-mysql" % testcontainersScalaVersion % Test
"com.dimafeng" %% "testcontainers-scala-mysql" % testcontainersScalaVersion % Test,
"ch.qos.logback" % "logback-classic" % logbackVersion % Test
)
)
.settings(testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"))
Expand All @@ -179,7 +181,8 @@ lazy val oracle = project
"org.testcontainers" % "oracle-xe" % testcontainersVersion % Test,
"org.testcontainers" % "jdbc" % testcontainersVersion % Test,
"com.oracle.database.jdbc" % "ojdbc8" % "21.5.0.0" % Test,
"com.dimafeng" %% "testcontainers-scala-oracle-xe" % testcontainersScalaVersion % Test
"com.dimafeng" %% "testcontainers-scala-oracle-xe" % testcontainersScalaVersion % Test,
"ch.qos.logback" % "logback-classic" % logbackVersion % Test
)
)
.settings(testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"))
Expand All @@ -197,7 +200,8 @@ lazy val postgres = project
"org.testcontainers" % "postgresql" % testcontainersVersion % Test,
"org.testcontainers" % "jdbc" % testcontainersVersion % Test,
"org.postgresql" % "postgresql" % "42.3.6" % Compile,
"com.dimafeng" %% "testcontainers-scala-postgresql" % testcontainersScalaVersion % Test
"com.dimafeng" %% "testcontainers-scala-postgresql" % testcontainersScalaVersion % Test,
"ch.qos.logback" % "logback-classic" % logbackVersion % Test
)
)
.settings(testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"))
Expand All @@ -215,7 +219,8 @@ lazy val sqlserver = project
"org.testcontainers" % "mssqlserver" % testcontainersVersion % Test,
"org.testcontainers" % "jdbc" % testcontainersVersion % Test,
"com.microsoft.sqlserver" % "mssql-jdbc" % "9.4.0.jre8" % Test,
"com.dimafeng" %% "testcontainers-scala-mssqlserver" % testcontainersScalaVersion % Test
"com.dimafeng" %% "testcontainers-scala-mssqlserver" % testcontainersScalaVersion % Test,
"ch.qos.logback" % "logback-classic" % logbackVersion % Test
)
)
.settings(testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"))
40 changes: 36 additions & 4 deletions jdbc/src/main/scala/zio/sql/SqlDriverLiveModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import zio.schema.Schema
trait SqlDriverLiveModule { self: Jdbc =>
private[sql] trait SqlDriverCore {

def deleteOnBatch(delete: List[Delete[_]], conn: Connection): IO[Exception, List[Int]]

def updateOnBatch(update: List[Update[_]], conn: Connection): IO[Exception, List[Int]]

def insertOnBatch[A: Schema](insert: List[Insert[_, A]], conn: Connection): IO[Exception, List[Int]]

def deleteOn(delete: Delete[_], conn: Connection): IO[Exception, Int]

def updateOn(update: Update[_], conn: Connection): IO[Exception, Int]
Expand All @@ -22,25 +28,41 @@ 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]] =
ZIO.scoped(pool.connection.flatMap(deleteOnBatch(delete, _)))

def deleteOn(delete: Delete[_], conn: Connection): IO[Exception, Int] =
ZIO.attemptBlocking {
val query = renderDelete(delete)
val statement = conn.createStatement()
statement.executeUpdate(query)
}.refineToOrDie[Exception]

def deleteOnBatch(delete: List[Delete[_]], conn: Connection): IO[Exception, List[Int]] =
ZIO.attemptBlocking {
val statement = conn.createStatement()
delete.map(delete_ => statement.addBatch(renderDelete(delete_)))
statement.executeBatch().toList
}.refineToOrDie[Exception]

def update(update: Update[_]): IO[Exception, Int] =
ZIO.scoped(pool.connection.flatMap(updateOn(update, _)))

def updateOn(update: Update[_], conn: Connection): IO[Exception, Int] =
ZIO.attemptBlocking {

val query = renderUpdate(update)

val query = renderUpdate(update)
val statement = conn.createStatement()

statement.executeUpdate(query)
}.refineToOrDie[Exception]

def update(update: List[Update[_]]): IO[Exception, List[Int]] =
ZIO.scoped(pool.connection.flatMap(updateOnBatch(update, _)))

def updateOnBatch(update: List[Update[_]], conn: Connection): IO[Exception, List[Int]] =
ZIO.attemptBlocking {
val statement = conn.createStatement()
update.map(update_ => statement.addBatch(renderUpdate(update_)))
statement.executeBatch().toList
}.refineToOrDie[Exception]

def read[A](read: Read[A]): Stream[Exception, A] =
Expand Down Expand Up @@ -93,9 +115,19 @@ trait SqlDriverLiveModule { self: Jdbc =>
statement.executeUpdate(query)
}.refineToOrDie[Exception]

override def insertOnBatch[A: Schema](insert: List[Insert[_, A]], conn: Connection): IO[Exception, List[Int]] =
ZIO.attemptBlocking {
val statement = conn.createStatement()
insert.map(insert_ => statement.addBatch(renderInsert(insert_)))
statement.executeBatch().toList
}.refineToOrDie[Exception]

override def insert[A: Schema](insert: Insert[_, A]): IO[Exception, Int] =
ZIO.scoped(pool.connection.flatMap(insertOn(insert, _)))

def insert[A: Schema](insert: List[Insert[_, A]]): IO[Exception, List[Int]] =
ZIO.scoped(pool.connection.flatMap(insertOnBatch(insert, _)))

override def transact[R, A](tx: ZTransaction[R, Exception, A]): ZIO[R, Throwable, A] =
ZIO.scoped[R] {
for {
Expand Down
15 changes: 15 additions & 0 deletions jdbc/src/main/scala/zio/sql/TransactionModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,31 @@ trait TransactionModule { self: Jdbc =>
ZTransaction.fromEffect(coreDriver.updateOn(update, connection))
}

def batchUpdate(update: List[self.Update[_]]): ZTransaction[Any, Exception, List[Int]] =
txn.flatMap { case Txn(connection, coreDriver) =>
ZTransaction.fromEffect(coreDriver.updateOnBatch(update, connection))
}

def apply[Z: Schema](insert: self.Insert[_, Z]): ZTransaction[Any, Exception, Int] =
txn.flatMap { case Txn(connection, coreDriver) =>
ZTransaction.fromEffect(coreDriver.insertOn(insert, connection))
}

def batchInsert[Z: Schema](insert: List[self.Insert[_, Z]]): ZTransaction[Any, Exception, List[Int]] =
txn.flatMap { case Txn(connection, coreDriver) =>
ZTransaction.fromEffect(coreDriver.insertOnBatch(insert, connection))
}

def apply(delete: self.Delete[_]): ZTransaction[Any, Exception, Int] =
txn.flatMap { case Txn(connection, coreDriver) =>
ZTransaction.fromEffect(coreDriver.deleteOn(delete, connection))
}

def batchDelete(delete: List[self.Delete[_]]): ZTransaction[Any, Exception, List[Int]] =
txn.flatMap { case Txn(connection, coreDriver) =>
ZTransaction.fromEffect(coreDriver.deleteOnBatch(delete, connection))
}

def succeed[A](a: => A): ZTransaction[Any, Nothing, A] = fromEffect(ZIO.succeed(a))

def fail[E](e: => E): ZTransaction[Any, E, Nothing] = fromEffect(ZIO.fail(e))
Expand Down
16 changes: 16 additions & 0 deletions jdbc/src/main/scala/zio/sql/jdbc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ trait Jdbc extends zio.sql.Sql with TransactionModule with JdbcInternalModule wi
trait SqlDriver {
def delete(delete: Delete[_]): IO[Exception, Int]

def delete(delete: List[Delete[_]]): IO[Exception, List[Int]]

def update(update: Update[_]): IO[Exception, Int]

def update(update: List[Update[_]]): IO[Exception, List[Int]]

def read[A](read: Read[A]): Stream[Exception, A]

def transact[R, A](tx: ZTransaction[R, Exception, A]): ZIO[R, Throwable, A]

def insert[A: Schema](insert: Insert[_, A]): IO[Exception, Int]

def insert[A: Schema](insert: List[Insert[_, A]]): IO[Exception, List[Int]]
}
object SqlDriver {

val live: ZLayer[ConnectionPool, Nothing, SqlDriver] =
ZLayer(ZIO.serviceWith[ConnectionPool](new SqlDriverLive(_)))
}
Expand All @@ -32,9 +39,18 @@ trait Jdbc extends zio.sql.Sql with TransactionModule with JdbcInternalModule wi
def execute(delete: Delete[_]): ZIO[SqlDriver, Exception, Int] =
ZIO.serviceWithZIO(_.delete(delete))

def executeBatchDelete(delete: List[Delete[_]]): ZIO[SqlDriver, Exception, List[Int]] =
ZIO.serviceWithZIO(_.delete(delete))

def execute[A: Schema](insert: Insert[_, A]): ZIO[SqlDriver, Exception, Int] =
ZIO.serviceWithZIO(_.insert(insert))

def executeBatchInsert[A: Schema](insert: List[Insert[_, A]]): ZIO[SqlDriver, Exception, List[Int]] =
ZIO.serviceWithZIO(_.insert(insert))

def execute(update: Update[_]): ZIO[SqlDriver, Exception, Int] =
ZIO.serviceWithZIO(_.update(update))

def executeBatchUpdate(update: List[Update[_]]): ZIO[SqlDriver, Exception, List[Int]] =
ZIO.serviceWithZIO(_.update(update))
}
55 changes: 52 additions & 3 deletions jdbc/src/test/scala/zio/sql/JdbcRunnableSpec.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,68 @@
package zio.sql

import com.dimafeng.testcontainers.JdbcDatabaseContainer
import zio.test.TestEnvironment
import zio.ZLayer
import zio.{ Scope, ZIO, ZLayer }
import zio.test.ZIOSpecDefault
import com.dimafeng.testcontainers.SingleContainer
import java.util.Properties
import zio.test.Spec

/**
* Base trait for integration-style tests running on Testcontainers.
* Extending classes are expected to provide the container implementation
* this test suite will work on by implementing {@link getContainer}.
*
* Test suite should be implemented in {@link specLayered} and
* particular tests can depend on {@link SQLDriver} in the environment.
*/
trait JdbcRunnableSpec extends ZIOSpecDefault with Jdbc {

type JdbcEnvironment = TestEnvironment with SqlDriver

val poolConfigLayer: ZLayer[Any, Throwable, ConnectionPoolConfig]
def specLayered: Spec[JdbcEnvironment, Object]

final lazy val jdbcLayer: ZLayer[Any, Any, SqlDriver] =
protected def getContainer: SingleContainer[_] with JdbcDatabaseContainer

protected val autoCommit = false

override def spec: Spec[TestEnvironment, Any] =
specLayered.provideCustomShared(jdbcLayer)

private[this] def connProperties(user: String, password: String): Properties = {
val props = new Properties
props.setProperty("user", user)
props.setProperty("password", password)
props
}

private[this] val poolConfigLayer: ZLayer[Any, Throwable, ConnectionPoolConfig] =
ZLayer.scoped {
testContainer
.map(a =>
ConnectionPoolConfig(
url = a.jdbcUrl,
properties = connProperties(a.username, a.password),
autoCommit = autoCommit
)
)
}

private[this] final lazy val jdbcLayer: ZLayer[Any, Any, SqlDriver] =
ZLayer.make[SqlDriver](
poolConfigLayer.orDie,
ConnectionPool.live.orDie,
SqlDriver.live
)

private[this] def testContainer: ZIO[Scope, Throwable, SingleContainer[_] with JdbcDatabaseContainer] =
ZIO.acquireRelease {
ZIO.attemptBlocking {
val c = getContainer
c.start()
c
}
} { container =>
ZIO.attemptBlocking(container.stop()).orDie
}
}
14 changes: 14 additions & 0 deletions mysql/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT"/>
</root>

<logger name="org.testcontainers" level="INFO"/>
<logger name="com.github.dockerjava" level="WARN"/>
</configuration>
30 changes: 9 additions & 21 deletions mysql/src/test/scala/zio/sql/mysql/MysqlRunnableSpec.scala
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
package zio.sql.mysql

import java.util.Properties
import zio.sql.{ ConnectionPoolConfig, JdbcRunnableSpec }
import zio.test._
import zio.ZLayer
import com.dimafeng.testcontainers.{ JdbcDatabaseContainer, MySQLContainer, SingleContainer }
import org.testcontainers.utility.DockerImageName
import zio.sql.JdbcRunnableSpec

trait MysqlRunnableSpec extends JdbcRunnableSpec with MysqlJdbcModule {

private def connProperties(user: String, password: String): Properties = {
val props = new Properties
props.setProperty("user", user)
props.setProperty("password", password)
props
}

val poolConfigLayer: ZLayer[Any, Throwable, ConnectionPoolConfig] =
ZLayer.scoped {
TestContainer
.mysql()
.map(a => ConnectionPoolConfig(a.jdbcUrl, connProperties(a.username, a.password)))
override protected def getContainer: SingleContainer[_] with JdbcDatabaseContainer =
new MySQLContainer(
mysqlImageVersion = Option("mysql").map(DockerImageName.parse)
).configure { a =>
a.withInitScript("shop_schema.sql")
()
}

override def spec: Spec[TestEnvironment, Any] =
specLayered.provideCustomLayerShared(jdbcLayer)

def specLayered: Spec[JdbcEnvironment, Object]

}
22 changes: 0 additions & 22 deletions mysql/src/test/scala/zio/sql/mysql/TestContainer.scala

This file was deleted.

14 changes: 14 additions & 0 deletions oracle/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT"/>
</root>

<logger name="org.testcontainers" level="INFO"/>
<logger name="com.github.dockerjava" level="WARN"/>
</configuration>
Loading

0 comments on commit b8d1ab8

Please sign in to comment.