Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve java configuration when provided #279

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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 @@

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 @@
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`

Check warning

Code scanning / Codacy-scalameta-pro (reported by Codacy)

FIXME tags are commonly used to mark places where a bug is suspected Warning

Take the required action to fix the issue indicated by this comment.
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
Loading