Flyway module for Play 2.4 or later. It aims to be a substitute for play-evolutions.
This module is successor of tototoshi/play-flyway, which is a Play Plugin supporting Play 2.1 ~ 2.3.
- Based on Flyway
- No 'Downs' part.
- Independent of DBPlugin(play.api.db).
build.sbt
libraryDependencies ++= Seq(
"org.flywaydb" %% "flyway-play" % "2.0.1"
)
// or snapshot version
resolvers +=
"Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
libraryDependencies ++= Seq(
"org.flywaydb" %% "flyway-play" % "2.1.0-SNAPSHOT"
)
conf/application.conf
play.modules.enabled += "org.flywaydb.play.PlayModule"
Database settings can be set in manner of Play2.
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:example2;db_CLOSE_DELAY=-1"
db.default.username="sa"
db.default.password="secret"
# optional
db.default.schemas=["public", "other"]
A migration script is just a simple SQL file.
CREATE TABLE FOO (.............
Place your migration scripts in conf/db/migration/${dbName}
.
playapp
├── app
│ ├── controllers
│ ├── models
│ └── views
├── conf
│ ├── application.conf
│ ├── db
│ │ └── migration
│ │ ├── default
│ │ │ ├── V1__Create_person_table.sql
│ │ │ └── V2__Add_people.sql
│ │ └── secondary
│ │ ├── V1__create_job_table.sql
│ │ └── V2__Add_job.sql
│ ├── play.plugins
│ └── routes
Please see flyway's documents about the naming convention for migration scripts.
http://flywaydb.org/documentation/migration/sql.html
Flyway can replace placeholders in Sql migrations. The default pattern is ${placeholder}. This can be configured using the placeholderPrefix and placeholderSuffix properties.
The placeholder prefix, suffix and key-value pairs can be specificed in application.conf, e.g.
db.default.migration.placeholderPrefix="$flyway{{{"
db.default.migration.placeholderSuffix="}}}"
db.default.migration.placeholders.foo="bar"
db.default.migration.placeholders.hoge="pupi"
This would cause
INSERT INTO USERS ($flyway{{{foo}}}) VALUES ('$flyway{{{hoge}}}')
to be rewritten to
INSERT INTO USERS (bar) VALUES ('pupi')
From flyway 3.0, validate
run before migrate
by default.
Set validateOnMigrate
to false if you want to disable this.
db.${dbName}.migration.validateOnMigrate=false // true by default
For existing schema, Flyway has a option called 'initOnMigrate'. This option is enabled when -Ddb.${dbName}.migration.initOnMigrate=true
.
For example,
$ play -Ddb.default.migration.initOnMigrate=true
Of course, You can write this in your application.conf
.
Manual migration is also supported. Click 'Other operations' or open /@flyway/${dbName}
directly.
In Test mode, migration is done automatically.
In production mode, migration is done automatically if db.${dbName}.migration.auto
is set to be true in application.conf.
Otherwise it failed to start when migration is needed.
$ play -Ddb.default.migration.auto=true start
seratch/devteam-app is using play-flyway. Maybe this is a good example.
- Supported new configuration key,
db.default.username
.
- Play 2.4 support
- Apache 2.0 License