Scala 中的自定义 Flyway 回调

Posted

技术标签:

【中文标题】Scala 中的自定义 Flyway 回调【英文标题】:Custom Flyway callback in Scala 【发布时间】:2014-11-15 20:42:59 【问题描述】:

在运行FlywayClean 之前,我需要删除一个扩展(可能还有其他东西)。看起来使用回调是正确的方法。目前,我只想让 any 简单的方法作为回调运行,然后我可以从那里对其进行按摩。

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.")
 

堆栈跟踪错误:

> 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)

我找不到任何自定义 Scala/Flyway 代码的示例。

【问题讨论】:

【参考方案1】:

通过阅读您的示例,不应该是这样

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

【讨论】:

以上是关于Scala 中的自定义 Flyway 回调的主要内容,如果未能解决你的问题,请参考以下文章

带有回调的自定义插件

编写scala版hive的自定义函数

在 keras 回调中使用带有自定义参数的自定义函数

给提供的 Node JS 回调我自己的自定义回调

401的自定义响应Lambda Authorizer

Android中的自定义控件