Gradle Flywayプラグイン

試した環境

java11

gradle6.0

flyway6.3.3

mysql8.0.19

 

build.gradle

plugins {

  id 'java-library'

  id 'org.flywaydb.flyway' version '6.3.3'

}
repositories {

  mavenCentral()

}

dependencies {
  implementation 'mysql:mysql-connector-java:8.0.19'
}
flyway {

  url = 'jdbc:mysql://${host}:3306/${schema}'

  user = '${user}'

  password = '${password}'

  //locations = ['filesystem:src/main/resources/db/migration'] //default

  //sqlMigrationPrefix = 'V' //default

  //sqlMigrationSeparator = '__' //default

  //sqlMigrationSuffixes = '.sql' //default

}

src/main/resources/db/migration/V1.0.0__create_member.sql を用意してタスクを実行した場合

  • flywayMigrate

 Schema version: 1.0.0
 +-----------+---------+---------------+------+---------------------+---------+
| Category | Version | Description | Type | Installed On | State |
+-----------+---------+---------------+------+---------------------+---------+
| Versioned | 1.0.0 | create member | SQL | 2020-04-17 11:43:27 | Success |
+-----------+---------+---------------+------+---------------------+---------+

  • flywayMigrate flywayBaseline

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':flywayBaseline'.
> Error occurred while executing flywayBaseline
Unable to baseline schema history table `work`.`flyway_schema_history` as it already contains migrations

  • flywayBasline

Schema version: 1
+----------+---------+-----------------------+----------+---------------------+----------+
| Category | Version | Description | Type | Installed On | State |
+----------+---------+-----------------------+----------+---------------------+----------+
| | 1 | << Flyway Baseline >> | BASELINE | 2020-04-17 11:44:59 | Baseline |
+----------+---------+-----------------------+----------+---------------------+----------+

  • flywayBaseline flywayMigrate

Schema version: 1
+----------+---------+-----------------------+----------+---------------------+----------+
| Category | Version | Description | Type | Installed On | State |
+----------+---------+-----------------------+----------+---------------------+----------+
| | 1 | << Flyway Baseline >> | BASELINE | 2020-04-17 11:44:59 | Baseline |
+----------+---------+-----------------------+----------+---------------------+----------+

 

src/main/resources/db/migration/V1.0.1__create_member.sql を用意してタスクを実行した場合

  • flywayMigrate

Schema version: 1.0.1
+-----------+---------+---------------+------+---------------------+---------+
| Category | Version | Description | Type | Installed On | State |
+-----------+---------+---------------+------+---------------------+---------+
| Versioned | 1.0.1 | create member | SQL | 2020-04-17 11:48:16 | Success |
+-----------+---------+---------------+------+---------------------+---------+

  • flywayMigrate flywayBaseline

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':flywayBaseline'.
> Error occurred while executing flywayBaseline
Unable to baseline schema history table `work`.`flyway_schema_history` as it already contains migrations

  • flywayBaseline 

Schema version: 1
+-----------+---------+-----------------------+----------+---------------------+----------+
| Category | Version | Description | Type | Installed On | State |
+-----------+---------+-----------------------+----------+---------------------+----------+
| | 1 | << Flyway Baseline >> | BASELINE | 2020-04-17 11:50:10 | Baseline |
| Versioned | 1.0.1 | create member | SQL | | Pending |
+-----------+---------+-----------------------+----------+---------------------+----------+

  • flywayBaseline flywayMigrate

Schema version: 1.0.1
+-----------+---------+-----------------------+----------+---------------------+----------+
| Category | Version | Description | Type | Installed On | State |
+-----------+---------+-----------------------+----------+---------------------+----------+
| | 1 | << Flyway Baseline >> | BASELINE | 2020-04-17 11:50:10 | Baseline |
| Versioned | 1.0.1 | create member | SQL | 2020-04-17 11:51:01 | Success |
+-----------+---------+-----------------------+----------+---------------------+----------+

src/main/resources/db/migration/V1.0.1__create_member.sql を用意して、build.gradleにflyway.baselineVersion=2を追加して、flywayMigrateタスクを実行した場合、SQLは実行されない。

Schema version: 2
+-----------+---------+-----------------------+----------+---------------------+----------------+
| Category | Version | Description | Type | Installed On | State |
+-----------+---------+-----------------------+----------+---------------------+----------------+
| Versioned | 1.0.1 | create member | SQL | | Below Baseline |
| | 2 | << Flyway Baseline >> | BASELINE | 2020-04-17 13:13:22 | Baseline |
+-----------+---------+-----------------------+----------+---------------------+----------------+

 

追記

build.gradleにパスワードとか書くのが嫌なら、gradle.propertiesにflyway.passwordで書く。url, user, baselineVersionなども設定可能。

 

参考

https://flywaydb.org/documentation/gradle/