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

Add callbacks support #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ Otherwise it failed to start when migration is needed.
$ play -Ddb.default.migration.auto=true start
```

### Callbacks

You can register callbacks during the migration process.
For that use `callbacks`, and give an array with all the callbacks to run.

```
db.default.migration.callbacks=["db.MigrationLogCallback"]
```

## <a class="anchor" name="example"></a>Example application

[seratch/devteam-app](https://github.com/seratch/devteam-app "seratch/devteam-app") is using play-flyway. Maybe this is a good example.
Expand Down
41 changes: 41 additions & 0 deletions playapp/app/db/MigrationLogCallback.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package db

import java.sql.Connection

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

case class MigrationLogCallback() extends FlywayCallback {

override def beforeClean(connection: Connection) = ()

override def afterInfo(connection: Connection) = ()

override def beforeInit(connection: Connection) = ()

override def beforeRepair(connection: Connection) = ()

override def afterRepair(connection: Connection) = ()

override def afterInit(connection: Connection) = ()

override def afterValidate(connection: Connection) = ()

override def beforeEachMigrate(connection: Connection, info: MigrationInfo) = println(s"Applying migration to version:${info.getVersion} description: ${info.getDescription}")

override def afterEachMigrate(connection: Connection, info: MigrationInfo) = println(s"Applied migration to version:${info.getVersion} description: ${info.getDescription}")

override def afterMigrate(connection: Connection) = ()

override def beforeValidate(connection: Connection) = ()

override def beforeInfo(connection: Connection) = ()

override def afterClean(connection: Connection) = ()

override def beforeMigrate(connection: Connection) = ()

override def afterBaseline(c: Connection) = ()

override def beforeBaseline(c: Connection) = ()
}
1 change: 1 addition & 0 deletions playapp/conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ application.langs="en"
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:example;DB_CLOSE_DELAY=-1"
db.default.user=sa
db.default.migration.callbacks=["db.MigrationLogCallback"]

db.secondary.driver=org.h2.Driver
db.secondary.url="jdbc:h2:mem:example2;db_CLOSE_DELAY=-1"
Expand Down
6 changes: 5 additions & 1 deletion plugin/src/main/scala/org/flywaydb/play/ConfigReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.flywaydb.play

import play.api._
import scala.collection.JavaConverters._

class ConfigReader(app: Application) extends UrlParser {

Expand Down Expand Up @@ -65,6 +66,8 @@ class ConfigReader(app: Application) extends UrlParser {
app.configuration.getBoolean(s"db.${dbName}.migration.outOfOrder").getOrElse(false)
val auto =
app.configuration.getBoolean(s"db.${dbName}.migration.auto").getOrElse(false)
val callbacks =
app.configuration.getStringList(s"db.${dbName}.migration.callbacks").map(_.asScala.toSeq)

val database = DatabaseConfiguration(
driver,
Expand All @@ -81,7 +84,8 @@ class ConfigReader(app: Application) extends UrlParser {
placeholderPrefix,
placeholderSuffix,
placeholders,
outOfOrder
outOfOrder,
callbacks
)
}).toMap

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ case class FlywayConfiguration(
placeholderPrefix: Option[String],
placeholderSuffix: Option[String],
placeholders: Map[String, String],
outOfOrder: Boolean)
outOfOrder: Boolean,
callbacks: Option[Seq[String]])

case class DatabaseConfiguration(
driver: String,
Expand Down
3 changes: 3 additions & 0 deletions plugin/src/main/scala/org/flywaydb/play/PlayInitializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class PlayInitializer @Inject() (implicit app: Application, webCommands: WebComm
flyway.setPlaceholderSuffix(suffix)
}
flyway.setPlaceholders(configuration.placeholders.asJava)
for (callbacks <- configuration.callbacks) {
flyway.setCallbacks(callbacks: _*)
}

dbName -> flyway
}
Expand Down
15 changes: 14 additions & 1 deletion plugin/src/test/scala/org/flywaydb/play/ConfigReaderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ConfigReaderSpec extends FunSpec with ShouldMatchers {
"db.third.pass" -> "secret3"
)

def withDefaultDB[A](additionalConfiguration: Map[String, String])(assertion: FlywayConfiguration => A): A =
def withDefaultDB[A](additionalConfiguration: Map[String, _])(assertion: FlywayConfiguration => A): A =
running(FakeApplication(
additionalConfiguration = defaultDB ++ additionalConfiguration
)) {
Expand Down Expand Up @@ -176,5 +176,18 @@ class ConfigReaderSpec extends FunSpec with ShouldMatchers {
}
}

describe("callbacks") {
it("should be parsed") {
withDefaultDB(Map("db.default.migration.callbacks" -> Seq("org.flyway.AnyCallback"))) { config =>
config.callbacks should be(Some("org.flyway.AnyCallback" :: Nil))
}
}
it("should be none by default") {
withDefaultDB(Map.empty) { config =>
config.callbacks should be(None)
}
}
}

}
}