IK.AM

@making's tech note


途中からFlywayでDBマイグレーション導入するときの既存DBへのセットアップ

🗃 {Dev/DBMigration/Flyway}
🏷 Flyway 🏷 Java 
🗓 Updated at 2014-04-19T02:25:58Z  🗓 Created at 2014-04-19T02:25:58Z   🌎 English Page

運用中の既存アプリに途中からFlywayを導入したいときのメモ。

導入するときに用意するSQLが以下のV1~V3、現在運用中のバージョンがV2相当だとする。

  • V1__Create_initial_tables.sql
  • V2__Insert_initial_data.sql
  • V3__Add_schema_for_OAuth2.sql

現在運用中のDBに対して、

$ mvn flyway:info

を実行すると、Flywayはまだ導入されていないので、↓のような結果になる。

+----------------+----------------------------+---------------------+---------+
| Version        | Description                | Installed on        | State   |
+----------------+----------------------------+---------------------+---------+
| 1              | Create initial tables      |                     | Pending |
| 2              | Insert initial data        |                     | Pending |
| 3              | Add schema for OAuth2      |                     | Pending |
+----------------+----------------------------+---------------------+---------+

いまV2相当なので、V1とV2は適用したくなくて、V3から適用したい。そういうときは↓を実行する。

$ mvn flyway:init -Dflyway.initVersion=2.1 -Dflyway.initDescription="Base Version"

そうするとmvn flyway:infoの結果はこうなる。

+----------------+----------------------------+---------------------+---------+
| Version        | Description                | Installed on        | State   |
+----------------+----------------------------+---------------------+---------+
| 1              | Create initial tables      |                     | PreInit |
| 2              | Insert initial data        |                     | PreInit |
| 2.1            | Base Version               | 2014-04-19 20:36:07 | Success |
| 3              | Add schema for OAuth2      |                     | Pending |
+----------------+----------------------------+---------------------+---------+

V2とV3の間にV2.1を割り込ませる格好となる。

$ mvn compile flyway:migrate

を実行すると、mvn flyway:infoの結果は

+----------------+----------------------------+---------------------+---------+
| Version        | Description                | Installed on        | State   |
+----------------+----------------------------+---------------------+---------+
| 1              | Create initial tables      |                     | PreInit |
| 2              | Insert initial data        |                     | PreInit |
| 2.1            | Base Version               | 2014-04-19 20:36:07 | Success |
| 3              | Add schema for OAuth2      | 2014-04-19 20:36:27 | Success |
+----------------+----------------------------+---------------------+---------+

となり、無事途中からFlywayの導入ができた。


✒️️ Edit  ⏰ History  🗑 Delete