公開:2023.01.30 10:00 | 更新: 2023.01.30 12:21
データベースのバージョンを管理してくれるツールです。
対応しているデータベースは幅広く、ファイル名がFlywayのお約束に従っていればDDLやDMLでのマイグレーション(DBスキーマのバージョンアップ)を行ってくれます。
既存DBへの導入方法と基本のマイグレーションについては過去記事「既存DBにスキーマ管理ツール(Flyway)を導入する 」を参照ください。
公式サイト https://flywaydb.org/
下記のようにrepairコマンドを実行します。
./flyway repair
flywayはスキーマ管理用にflyway_schema_historyというテーブルを保持しています。
test_db=# \dt
List of relations
Schema | Name | Type | Owner
--------+-----------------------+-------+----------
public | flyway_schema_history | table | postgres
(1 row)
test_db=# select * from flyway_schema_history;
installed_rank | version | description | type | script | checksum | installed_by | installed_on | execution_time | success
----------------+----------------+------------------------+----------+---------------------------------------------+----------+--------------+----------------------------+----------------+---------
1 | 2.0.0 | baseline_migration | BASELINE | baseline_migration | | postgres | 2023-01-23 16:52:49.694519 | 0 | t
2 | 2.0.0.20230123 | Seconde test migration | SQL | V2.0.0.20230123__Seconde_test_migration.sql | 0 | postgres | 2023-01-23 16:54:27.402267 | 10 | t
(2 rows)
flywayはマイグレーションしたsqlファイルのチェックサムを保持しています。
例えば、一度マイグレーションしたsqlファイルが何らかの都合で書き換えます。
(私の所属するチームではコードレビューを行っていますので、レビューのフィードバック対応を行うタイミングなどで発生します。)
書き換えてマイグレーションを実行すると、flywayがチェックサムを確認し一致しないと「ERROR: Validate failed: Migrations have failed validation」と怒られます。
# ./flyway migrate
Flyway Community Edition 9.12.0 by Redgate
See what's new here: https://flywaydb.org/documentation/learnmore/releaseNotes#9.12.0
Database: jdbc:postgresql://localhost:5432/test_db (PostgreSQL 12.13)
ERROR: Validate failed: Migrations have failed validation
Migration checksum mismatch for migration version 2.0.0.20230123
-> Applied to database : 0
-> Resolved locally : 1531363961
Either revert the changes to the migration, or run repair to update the schema history.
Need more flexibility with validation rules? Learn more: https://rd.gt/3AbJUZE
repairを実行すると下記のようにflyway_schema_historyのレコードが書き換えられます。
test_db=# select * from flyway_schema_history;
installed_rank | version | description | type | script | checksum | installed_by | installed_on | execution_time | success
----------------+----------------+------------------------+----------+---------------------------------------------+------------+--------------+----------------------------+----------------+---------
1 | 2.0.0 | baseline_migration | BASELINE | baseline_migration | | postgres | 2023-01-23 16:52:49.694519 | 0 | t
2 | 2.0.0.20230123 | Seconde test migration | SQL | V2.0.0.20230123__Seconde_test_migration.sql | 1531363961 | postgres | 2023-01-23 16:54:27.402267 | 10 | t
(2 rows)
checksum列がmigrate時に怒られた「0」から「1531363961」になっているのが確認できます。
下記はrepair実行時のログです。該当のsqlファイルのチェックサムについて修正されているのがわかります。
# ./flyway repair
Flyway Community Edition 9.12.0 by Redgate
See what's new here: https://flywaydb.org/documentation/learnmore/releaseNotes#9.12.0
Database: jdbc:postgresql://localhost:5432/test_db (PostgreSQL 12.13)
Repair of failed migration in Schema History table "public"."flyway_schema_history" not necessary. No failed migration detected.
Repairing Schema History table for version 2.0.0.20230123 (Description: Seconde test migration, Type: SQL, Checksum: 1531363961) ...
Successfully repaired schema history table "public"."flyway_schema_history" (execution time 00:00.069s).
repair後、再度migrateすると上手くいきます。
# ./flyway migrate
Flyway Community Edition 9.12.0 by Redgate
See what's new here: https://flywaydb.org/documentation/learnmore/releaseNotes#9.12.0
Database: jdbc:postgresql://localhost:5432/test_db (PostgreSQL 12.13)
Successfully validated 2 migrations (execution time 00:00.051s)
Current version of schema "public": 2.0.0.20230123
Schema "public" is up to date. No migration necessary.
LOADING...