Skip to content

Instantly share code, notes, and snippets.

@achinaou
Last active October 31, 2025 15:08
Show Gist options
  • Select an option

  • Save achinaou/284dd5e16bd1d2d2edf1653647c392c9 to your computer and use it in GitHub Desktop.

Select an option

Save achinaou/284dd5e16bd1d2d2edf1653647c392c9 to your computer and use it in GitHub Desktop.
Flyway ZIO Test Aspect
import javax.sql.DataSource
import org.flywaydb.core.Flyway
import org.flywaydb.core.api.configuration.FluentConfiguration
import org.flywaydb.core.api.output.MigrateResult
import zio.ZIO
import zio.test.TestAspect
object FlywayTestAspect:
type ConfigurationTransformer = FluentConfiguration => FluentConfiguration
val migrateBefore: TestAspect[Nothing, DataSource, Nothing, Any] =
migrateBefore()
def migrateBefore(locations: String*): TestAspect[Nothing, DataSource, Nothing, Any] =
TestAspect.before(migrate(locations))
def migrateBefore(configure: ConfigurationTransformer): TestAspect[Nothing, DataSource, Nothing, Any] =
TestAspect.before(migrate(configure))
val migrateBeforeAll: TestAspect[Nothing, DataSource, Nothing, Any] =
migrateBeforeAll()
def migrateBeforeAll(locations: String*): TestAspect[Nothing, DataSource, Nothing, Any] =
TestAspect.beforeAll(migrate(locations))
def migrateBeforeAll(configure: ConfigurationTransformer): TestAspect[Nothing, DataSource, Nothing, Any] =
TestAspect.beforeAll(migrate(configure))
private def migrate(locations: Seq[String]): ZIO[DataSource, Nothing, MigrateResult] =
migrate: flywayConfiguration =>
if locations.isEmpty
then flywayConfiguration
else flywayConfiguration.locations(locations*)
private def migrate(configure: ConfigurationTransformer): ZIO[DataSource, Nothing, MigrateResult] =
ZIO
.serviceWithZIO[DataSource]: dataSource =>
ZIO.attemptBlockingIO:
val baseConfiguration: FluentConfiguration =
Flyway.configure
.dataSource(dataSource)
.loggers("slf4j")
configure(baseConfiguration).load.migrate
.orDie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment