Bill Frasure Bill Frasure - 1 month ago 22
Scala Question

Custom Flyway callback in Scala

I need to drop an extension (and possibly other things) before running

FlywayClean
and, after looking through numerous StackOverflow and Github posts, it looks like using a callback is the correct approach. For the moment, I just want to get ANY simple method running as a callback, and I can massage it from there.

build.sbt
:

name := "FlywayMigrations"

version := "1.0"

scalaVersion := "2.11.1"

libraryDependencies ++= Seq(
"org.postgresql" % "postgresql" % "9.3-1100-jdbc4",
"org.flywaydb" % "flyway-sbt" % "3.0"
)

seq(flywaySettings: _*)

flywayUrl := "jdbc:postgresql://localhost:5432/flyway"

flywayLocations := Seq( "filesystem:../dbSmileReminder/")

flywayCallbacks := Seq("com.communitect.FlywayCleaner")

flywayUser := "***"


project/plugins.sbt
:

addSbtPlugin("org.flywaydb" % "flyway-sbt" % "3.0")

resolvers += "Flyway" at "http://flywaydb.org/repo"


src/main/scala/FlywayCleaner.scala
:

package com.communitect.callback

import java.sql.Connection

import org.flywaydb.core.api.callback.FlywayCallback

class FlywayCleaner extends FlywayCallback {

override def beforeClean(connection:Connection) = {
println("Before clean.")
}

override def afterClean(connection:Connection) = {
println("After clean.")
}

override def afterEachMigrate(connection:Connection, migrationInfo: org.flywaydb.core.api.MigrationInfo) = {
println("Callback.")
}
override def afterInfo(connection:Connection) = {
println("Callback.")
}
override def afterInit(connection:Connection) = {
println("Callback.")
}
override def afterMigrate(connection:Connection) = {
println("Callback.")
}
override def afterRepair(connection:Connection) = {
println("Callback.")
}
override def afterValidate(connection:Connection) = {
println("Callback.")
}
override def beforeEachMigrate(connection:Connection, migrationInfo: org.flywaydb.core.api.MigrationInfo) = {
println("Callback.")
}

override def beforeInfo(connection:Connection) = {
println("Callback.")
}
override def beforeInit(connection:Connection) = {
println("Callback.")
}
override def beforeMigrate(connection:Connection) = {
println("Callback.") }
override def beforeRepair(connection:Connection) = {
println("Callback.")
}
override def beforeValidate(connection:Connection) = {
println("Callback.")
}

def main(args: Array[String]) =
{
println("FlywayCleaner running.")
}
}


Error with the stacktrace:

> last *:flywayInit
org.flywaydb.core.api.FlywayException: Unable to instantiate class: com.communitect.FlywayCleaner
at org.flywaydb.core.internal.util.ClassUtils.instantiateAll(ClassUtils.java:67)
at org.flywaydb.core.Flyway.setCallbacks(Flyway.java:780)
at org.flywaydb.sbt.FlywayPlugin$FlywayOps$.configure$extension2(FlywayPlugin.scala:246)
at org.flywaydb.sbt.FlywayPlugin$FlywayOps$.configure$extension0(FlywayPlugin.scala:226)
at org.flywaydb.sbt.FlywayPlugin$Flyway$.apply(FlywayPlugin.scala:210)
at org.flywaydb.sbt.FlywayPlugin$$anonfun$flywaySettings$35$$anonfun$apply$6.apply$mcV$sp(FlywayPlugin.scala:177)
at org.flywaydb.sbt.FlywayPlugin$$anonfun$flywaySettings$35$$anonfun$apply$6.apply(FlywayPlugin.scala:177)
at org.flywaydb.sbt.FlywayPlugin$$anonfun$flywaySettings$35$$anonfun$apply$6.apply(FlywayPlugin.scala:177)
at org.flywaydb.sbt.FlywayPlugin$.withContextClassLoader(FlywayPlugin.scala:201)
at org.flywaydb.sbt.FlywayPlugin$.org$flywaydb$sbt$FlywayPlugin$$withPrepared(FlywayPlugin.scala:184)
at org.flywaydb.sbt.FlywayPlugin$$anonfun$flywaySettings$35.apply(FlywayPlugin.scala:177)
at org.flywaydb.sbt.FlywayPlugin$$anonfun$flywaySettings$35.apply(FlywayPlugin.scala:177)
at scala.Function3$$anonfun$tupled$1.apply(Function3.scala:35)
at scala.Function3$$anonfun$tupled$1.apply(Function3.scala:34)
at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:42)
at sbt.std.Transform$$anon$4.work(System.scala:64)
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)
at sbt.Execute.work(Execute.scala:244)
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:160)
at sbt.CompletionService$$anon$2.call(CompletionService.scala:30)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)


I'm still new to Scala, so it's likely something fundamental to the language that I'm messing up. But, I could find exactly zero examples of any custom Scala/Flyway code.

Answer

By reading your example, shouldn't it be

flywayCallbacks := Seq("com.communitect.callback.FlywayCleaner")