Skip to content

Commit

Permalink
Preserve java configuration when provided (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
geirolz authored Feb 12, 2024
1 parent 8ef4c60 commit 6888a9c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
4 changes: 3 additions & 1 deletion core/src/main/scala-2/fly4s/data/Fly4sConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fly4s.data
import cats.data.NonEmptyList
import com.geirolz.macros.fluent.copy.FluentCopy
import fly4s.data.Fly4sConfigDefaults.*
import org.flywaydb.core.api.configuration.Configuration

import java.nio.charset.Charset

Expand Down Expand Up @@ -51,6 +52,7 @@ case class Fly4sConfig(
skipDefaultCallbacks: Boolean = defaultSkipDefaultCallbacks,
skipDefaultResolvers: Boolean = defaultSkipDefaultResolvers,
// --- mima after 1.0.0 ---
loggers: List[LoggerType] = defaultLoggers
loggers: List[LoggerType] = defaultLoggers,
baseJavaConfig: Option[Configuration] = None
) extends Fly4sConfigContract
object Fly4sConfig extends Fly4sConfigBuilder
5 changes: 3 additions & 2 deletions core/src/main/scala-3/fly4s/data/Fly4sConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package fly4s.data

import cats.data.NonEmptyList
import fly4s.data.Fly4sConfigDefaults.*
import fly4s.data.*
import org.flywaydb.core.api.configuration.Configuration

import java.nio.charset.Charset

Expand Down Expand Up @@ -50,7 +50,8 @@ case class Fly4sConfig(
skipDefaultCallbacks: Boolean = defaultSkipDefaultCallbacks,
skipDefaultResolvers: Boolean = defaultSkipDefaultResolvers,
// --- mima after 1.0.0 ---
loggers: List[LoggerType] = defaultLoggers
loggers: List[LoggerType] = defaultLoggers,
baseJavaConfig: Option[Configuration] = None
) extends Fly4sConfigContract
object Fly4sConfig extends Fly4sConfigBuilder:

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/fly4s/Fly4s.scala
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ object Fly4s extends AllInstances {
)(implicit F: Async[F]): Resource[F, Fly4s[F]] = {

val acquireFly4s = for {
c1 <- Fly4sConfig.toJava(config, classLoader).liftTo[F]
c1 <- Fly4sConfig.toJavaF[F](config, classLoader)
c2 <- F.delay(Flyway.configure(classLoader).configuration(c1))
fly4s <- fromJavaConfig[F](mapFlywayConfig(c2))
} yield fly4s
Expand Down Expand Up @@ -242,7 +242,7 @@ object Fly4s extends AllInstances {
for {
currentJConfig <- F.pure(flyway.getConfiguration)
classLoader = currentJConfig.getClassLoader
c <- Fly4sConfig.toJava(newConfig, classLoader).liftTo[F]
c <- Fly4sConfig.toJavaF[F](newConfig, classLoader)
jConfig <- F.delay {

val newJConfig = new FluentConfiguration(classLoader)
Expand Down
20 changes: 18 additions & 2 deletions core/src/main/scala/fly4s/data/Fly4sConfigBuilder.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fly4s.data

import cats.MonadThrow
import cats.data.NonEmptyList
import org.flywaydb.core.api.configuration.{Configuration, FluentConfiguration}

Expand All @@ -8,6 +9,7 @@ import scala.jdk.CollectionConverters.{MapHasAsJava, MapHasAsScala}
import scala.util.Try

private[fly4s] trait Fly4sConfigContract {
val baseJavaConfig: Option[Configuration]
val connectRetries: Int
val initSql: Option[String]
val defaultSchemaName: Option[String]
Expand Down Expand Up @@ -115,6 +117,7 @@ private[fly4s] trait Fly4sConfigBuilder {

def fromJava(c: Configuration): Fly4sConfig =
new Fly4sConfig(
baseJavaConfig = Some(c),
// ---------- connection ----------
connectRetries = c.getConnectRetries,
initSql = Option(c.getInitSql),
Expand Down Expand Up @@ -161,12 +164,25 @@ private[fly4s] trait Fly4sConfigBuilder {
skipDefaultResolvers = c.isSkipDefaultResolvers
)

@deprecated("Use toJavaF[Try] instead, this will be removed in the future versions.", "1.0.1")
def toJava(
c: Fly4sConfig,
classLoader: ClassLoader = Thread.currentThread.getContextClassLoader
): Try[Configuration] = Try {
): Try[Configuration] = toJavaF[Try](c, classLoader)

// TODO Remove this method in the future to `toJava`
def toJavaF[F[_]: MonadThrow](
c: Fly4sConfig,
classLoader: ClassLoader = Thread.currentThread.getContextClassLoader
): F[Configuration] = MonadThrow[F].catchNonFatal {

val fluentConfiguration: FluentConfiguration = c.baseJavaConfig match {
case Some(jconf) => new FluentConfiguration(classLoader).configuration(jconf)
case None => new FluentConfiguration(classLoader)
}

// ---------- connection ----------
new FluentConfiguration(classLoader)
fluentConfiguration
.connectRetries(c.connectRetries)
.initSql(c.initSql.orNull)
.defaultSchema(c.defaultSchemaName.orNull)
Expand Down

0 comments on commit 6888a9c

Please sign in to comment.