Skip to content

Commit

Permalink
Merge pull request #424 from laserdisc-io/scala3
Browse files Browse the repository at this point in the history
Issue #423 - Scala 3 Support
  • Loading branch information
barryoneill authored May 16, 2024
2 parents e692696 + 6128c6a commit 130fec6
Show file tree
Hide file tree
Showing 45 changed files with 315 additions and 238 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
target/
.idea/
.bsp/
.DS_Store

58 changes: 34 additions & 24 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
version = 3.4.0

runner.dialect = scala213
version=3.8.1

maxColumn = 140
style = default
align.preset = more
maxColumn = 140

rewrite.sortModifiers.order = [
"override"
"private"
"protected"
"implicit"
"final"
"sealed"
"abstract"
"lazy"
]
runner.dialect = scala213source3
fileOverride {
"glob:**/scala-3/**" { runner.dialect = scala3 }
}

danglingParentheses.preset = true
docstrings.wrap = "no"

rewrite.rules = [
SortModifiers
AvoidInfix
RedundantBraces
RedundantParens
AsciiSortImports
PreferCurlyFors
SortModifiers
Imports
]

spaces.inByNameTypes = false

rewrite.redundantBraces.stringInterpolation = true
rewrite.redundantBraces.includeUnitMethods = true
rewrite.neverInfix.excludeFilters = [until
to
by
eq
ne
"should.*"
"contain.*"
"must.*"
in
be
taggedAs
thrownBy
synchronized
have
when
size
theSameElementsAs
at
can
should]

newlines.afterCurlyLambda = preserve
newlines.neverInResultType = true
newlines.beforeMultilineDef = keep
newlines.sometimesBeforeColonInMethodReturnType = false
newlines.penalizeSingleSelectMultiArgList = false
rewrite.imports.sort = original
newlines.implicitParamListModifierPrefer = before
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.laserdisc.mysql.binlog.models

import cats.implicits._
import doobie._
import doobie.implicits._
import cats.implicits.*
import doobie.*
import doobie.implicits.*
import cats.effect.MonadCancel

case class BinaryLogs(fileName: String, size: Long)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package io.laserdisc.mysql.binlog.models

import java.io.Serializable
import com.github.shyiko.mysql.binlog.event.DeleteRowsEventData as JDeleteRowsEventData

import com.github.shyiko.mysql.binlog.event.{DeleteRowsEventData => JDeleteRowsEventData}

import scala.jdk.CollectionConverters._
import scala.jdk.CollectionConverters.*
object DeleteRowsEventData {
def unapply(arg: JDeleteRowsEventData): Option[(Long, List[Array[Serializable]], Array[Int])] =
Some((arg.getTableId, arg.getRows.asScala.toList, arg.getIncludedColumns.stream().toArray))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.laserdisc.mysql.binlog.models

import com.github.shyiko.mysql.binlog.event.{EventHeaderV4 => JEventHeaderV4, EventType}
import com.github.shyiko.mysql.binlog.event.{EventHeaderV4 as JEventHeaderV4, EventType}

object EventHeaderV4 {
def unapply(arg: JEventHeaderV4): Option[(EventType, Long, Long)] =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.laserdisc.mysql.binlog.models

import com.github.shyiko.mysql.binlog.event.{QueryEventData => JQueryEventData}
import com.github.shyiko.mysql.binlog.event.QueryEventData as JQueryEventData

object QueryEventData {
def truncateTable(sql: String) = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.laserdisc.mysql.binlog.models

import com.github.shyiko.mysql.binlog.event.{RotateEventData => JRotateEventData}
import com.github.shyiko.mysql.binlog.event.RotateEventData as JRotateEventData

object RotateEventData {
def unapply(arg: JRotateEventData): Option[(String, Long)] =
Some((arg.getBinlogFilename, arg.getBinlogPosition))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.laserdisc.mysql.binlog.models

import doobie._
import doobie.implicits._
import doobie.*
import doobie.implicits.*

import scala.collection.mutable
import cats.effect.MonadCancel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.laserdisc.mysql.binlog.models

import com.github.shyiko.mysql.binlog.event.{TableMapEventData => JTableMapEventData}
import com.github.shyiko.mysql.binlog.event.TableMapEventData as JTableMapEventData

object TableMapEventData {
def unapply(arg: JTableMapEventData): Option[(Long, String, String)] =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package io.laserdisc.mysql.binlog.models

import java.io
import com.github.shyiko.mysql.binlog.event.UpdateRowsEventData as JUpdateRowsEventData

import com.github.shyiko.mysql.binlog.event.{UpdateRowsEventData => JUpdateRowsEventData}

import scala.jdk.CollectionConverters._
import scala.jdk.CollectionConverters.*

object UpdateRowsEventData {
def unapply(
arg: JUpdateRowsEventData
): Some[(Long, List[(Array[io.Serializable], Array[io.Serializable])], Array[Int])] = {
): Some[(Long, List[(Array[Serializable], Array[Serializable])], Array[Int])] = {
val beforeAfter =
arg.getRows.asScala.toList.map(entry => entry.getKey -> entry.getValue)
Some((arg.getTableId, beforeAfter, arg.getIncludedColumns.stream().toArray))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package io.laserdisc.mysql.binlog.models

import java.io.Serializable
import com.github.shyiko.mysql.binlog.event.WriteRowsEventData as JWriteRowsEventData

import com.github.shyiko.mysql.binlog.event.{WriteRowsEventData => JWriteRowsEventData}

import scala.jdk.CollectionConverters._
import scala.jdk.CollectionConverters.*

object WriteRowsEventData {
def unapply(arg: JWriteRowsEventData): Option[(Long, List[Array[Serializable]], Array[Int])] =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.laserdisc.mysql.binlog.models

import com.github.shyiko.mysql.binlog.event.{XidEventData => JXidEventData}
import com.github.shyiko.mysql.binlog.event.XidEventData as JXidEventData

object XidEventData {
def unapply(arg: JXidEventData): Option[Long] = Some(arg.getXid)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
package io.laserdisc.mysql.binlog.models

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import com.dimafeng.testcontainers.ForAllTestContainer
import db.MySqlContainerTest
import doobie.util.transactor.Transactor
import doobie.util.transactor.Transactor.Aux
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import cats.effect.unsafe.implicits.global

class SchemaMetadataTest extends AnyWordSpec with ForAllTestContainer with MySqlContainerTest with Matchers {

"Schema Metadata" should {

"restore schema state from DB" in {

val props = new java.util.Properties()
props.put("user", mySqlContainer.getUsername)
props.put("password", mySqlContainer.getPassword)

implicit val testTransactor: Aux[IO, Unit] =
Transactor.fromDriverManager[IO](
mySqlContainer.getDriverClassName,
s"${mySqlContainer.getJdbcUrl}?useSSL=false",
props,
mySQLContainer.driverClassName,
s"${mySQLContainer.jdbcUrl}?useSSL=false",
mysqlDBCreds,
None
)
val schemaState =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.laserdisc.mysql.binlog

import cats.effect.Sync
import cats.implicits._
import cats.implicits.*
import com.github.shyiko.mysql.binlog.BinaryLogClient
import org.typelevel.log4cats.Logger
import io.laserdisc.mysql.binlog.checkpoint.BinlogOffset
import io.laserdisc.mysql.binlog.config.BinLogConfig
import io.laserdisc.mysql.binlog.config.{BinLogConfig, BinLogConfigOps}
import org.typelevel.log4cats.Logger

package object client {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.circe.Json
import io.laserdisc.mysql.binlog.event.EventMessage

import scala.collection.mutable
import cats.implicits._
import cats.implicits.*
import io.laserdisc.mysql.json

package object compaction {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.laserdisc.mysql.binlog

import cats.effect._
import doobie._
import cats.effect.*
import doobie.*
import doobie.hikari.HikariTransactor
import io.laserdisc.mysql.binlog.config.BinLogConfig

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.laserdisc.mysql.binlog.stream

import cats.effect.std.{Dispatcher, Queue}
import cats.effect.{Async, IO, LiftIO}
import cats.implicits._
import cats.implicits.*
import com.github.shyiko.mysql.binlog.BinaryLogClient
import com.github.shyiko.mysql.binlog.event.Event
import fs2.Stream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package io.laserdisc.mysql.binlog.stream

import cats.data.State
import cats.effect.{Ref, Sync}
import cats.implicits._
import cats.implicits.*
import com.github.shyiko.mysql.binlog.BinaryLogClient
import com.github.shyiko.mysql.binlog.event.EventType.{EXT_UPDATE_ROWS, UPDATE_ROWS}
import com.github.shyiko.mysql.binlog.event.deserialization.json.JsonBinary
import com.github.shyiko.mysql.binlog.event.{Event, EventData, EventHeaderV4 => JEventHeaderV4, EventType}
import com.github.shyiko.mysql.binlog.event.{Event, EventData, EventHeaderV4 as JEventHeaderV4, EventType}
import io.circe.Json
import io.laserdisc.mysql.binlog.event.EventMessage
import io.laserdisc.mysql.binlog.models._
import io.laserdisc.mysql.binlog.models.*
import org.typelevel.log4cats.Logger

import java.math.BigDecimal
Expand Down Expand Up @@ -351,6 +351,13 @@ object TransactionState {

def mapRawToMeta(table: TableMetadata): ((ColumnMetadata, Option[Serializable])) => (String, Json) = {
case (column, Some(value)) =>
/** At some point, Scala will drop ClassTag, so we should be prepared to either drop cross compilation for 2.13,
* else add an alternative implementation that uses TypeTest. At the current time, I don't want to introduce
* any potential bugs into _this_ at the same time that I'm introducing Scala 3..
*/
@annotation.nowarn(
"msg=Use of `scala.reflect.ClassTag` for type testing may be unsound. Consider using `scala.reflect.TypeTest` instead."
)
def unsafeCast[T](implicit m: ClassTag[T]): T =
try
value match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.laserdisc.mysql.binlog

import cats.effect.kernel.Ref
import cats.effect.{Concurrent, Sync}
import cats.implicits._
import cats.implicits.*
import com.github.shyiko.mysql.binlog.event.Event
import org.typelevel.log4cats.Logger
import io.laserdisc.mysql.binlog.event.EventMessage
Expand All @@ -13,14 +13,14 @@ package object stream {
schema: String
): fs2.Pipe[F, Event, EventMessage] =
_.through(streamTransactionPackages[F](transactionState, schema)).flatMap(pkg =>
fs2.Stream.eval(warnBigTransactionPackage(pkg)) >> fs2.Stream(pkg.events: _*)
fs2.Stream.eval(warnBigTransactionPackage(pkg)) >> fs2.Stream(pkg.events*)
)

def streamCompactedEvents[F[_]: Concurrent: Logger](
transactionState: Ref[F, TransactionState],
schema: String
): fs2.Pipe[F, Event, EventMessage] =
_.through(streamTransactionPackages[F](transactionState, schema)).flatMap(pkg => fs2.Stream(compaction.compact(pkg.events): _*))
_.through(streamTransactionPackages[F](transactionState, schema)).flatMap(pkg => fs2.Stream(compaction.compact(pkg.events)*))

def streamTransactionPackages[F[_]: Concurrent: Logger](
transactionState: Ref[F, TransactionState],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import cats.effect.unsafe.implicits.global
import com.dimafeng.testcontainers.ForAllTestContainer
import com.github.shyiko.mysql.binlog.BinaryLogClient
import db.MySqlContainerTest
import doobie.implicits._
import doobie.implicits.*
import io.circe.optics.JsonPath.root
import io.laserdisc.mysql.binlog.database
import io.laserdisc.mysql.binlog.config.BinLogConfigOps
import io.laserdisc.mysql.binlog.event.EventMessage
import io.laserdisc.mysql.binlog.models.SchemaMetadata
import org.scalatest.matchers.should.Matchers
Expand All @@ -28,8 +29,8 @@ class MultiSchemaTest extends AnyWordSpec with ForAllTestContainer with MySqlCon

def runScenarioForSchema(schema: String): List[EventMessage] = {

val client = containerBinlogConfig.mkBinaryLogClient()
val xaResource = database.transactor[IO](containerBinlogConfig)
val client = binlogConfig.mkBinaryLogClient()
val xaResource = database.transactor[IO](binlogConfig)

client.registerLifecycleListener(new BinaryLogClient.AbstractLifecycleListener {
override def onConnect(client: BinaryLogClient): Unit =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import com.github.shyiko.mysql.binlog.BinaryLogClient
import com.github.shyiko.mysql.binlog.event.{EventHeaderV4, EventType}
import db.MySqlContainerTest
import doobie.hikari.HikariTransactor
import doobie.implicits._
import doobie.implicits.*
import io.laserdisc.mysql.binlog.config.BinLogConfigOps
import io.laserdisc.mysql.binlog.database
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
Expand All @@ -18,26 +19,19 @@ import scala.language.reflectiveCalls

class MysqlBinlogStreamTest extends AnyWordSpec with ForAllTestContainer with MySqlContainerTest with Matchers {

def fixture =
new {

val testTransactor: Resource[IO, HikariTransactor[IO]] =
database.transactor[IO](containerBinlogConfig)

val client: BinaryLogClient = containerBinlogConfig.mkBinaryLogClient()

}

implicit val logger: SelfAwareStructuredLogger[IO] = Slf4jLogger.getLogger[IO]

"Binlog stream" should {

"emit events from mysql" in {

val (client, xaResource) = (fixture.client, fixture.testTransactor)
val testTransactor: Resource[IO, HikariTransactor[IO]] = database.transactor[IO](binlogConfig)

val client: BinaryLogClient = binlogConfig.mkBinaryLogClient()

client.registerLifecycleListener(new BinaryLogClient.AbstractLifecycleListener {
override def onConnect(client: BinaryLogClient): Unit =
xaResource
testTransactor
.use(xa =>
Sku
.insertMany(
Expand Down
Loading

0 comments on commit 130fec6

Please sign in to comment.