公開:2022.07.11 10:00 | 更新: 2022.08.08 08:59
データベースのバージョンを管理してくれるツールです。
対応しているデータベースは幅広く、ファイル名がFlywayのお約束に従っていればDDLやDMLでのマイグレーション(DBスキーマのバージョンアップ)を行ってくれます。
公式サイト:https://flywaydb.org/
方法としては2通りあります。
今回は、既に独自のdump、DMLにてDBが構築されている想定のため、後者のbaselineを使用したやり方で導入します。
baselineとは、端的に言えば既にテーブルなどが定義され構築されているDBに対して指定のバージョンを付与することです。
今回はFlywayのCLIツールのLinux版を取得します。
2022年6月16日 時点での最新版は8.5.12でした。
cd <任意の場所>
wget -q https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/8.5.12/flyway-commandline-8.5.12-linux-x64.tar.gz
tar -zxvf flyway-commandline-8.5.12-linux-x64.tar.gz
設定ファイルconf/flyway.confを編集していきます。
/optに展開した場合
/opt/flyway-8.5.12/conf/flyway.conf
となります。
接続情報だけ設定すれば他はデフォルトで動きます。
flyway.url=
flyway.user=
flyway.password=
flyway.locations=
flyway.url=jdbc:postgresql://localhost:5432/mydb
flyway.user=postgres
flyway.password=postgres
flyway.locations=filesystem:sql
導入前にDBの状態を確認します。
./flyway info
Flyway is up to date
Flyway Community Edition 8.5.12 by RedgateSee what's new here: https://flywaydb.org/documentation/learnmore/releaseNotes#8.5.12
Database: jdbc:postgresql://localhost:5432/postgres (PostgreSQL 11.10)
Schema version: << Empty Schema >>
+----------+---------+-------------+------+--------------+-------+
| Category | Version | Description | Type | Installed On | State |
+----------+---------+-------------+------+--------------+-------+
| No migrations found |
+----------+---------+-------------+------+--------------+-------+
導入していきます。
./flyway baseline -baselineVersion=1.0.1 -baselineDescription="baseline_migration"
A new version of Flyway is available
Upgrade to Flyway 8.5.13: https://rd.gt/2X0gakb
Flyway Community Edition 8.5.12 by Redgate
See what's new here: https://flywaydb.org/documentation/learnmore/releaseNotes#8.5.12
Database: jdbc:postgresql://localhost:5432/postgres (PostgreSQL 11.10)
Creating Schema History table "postgres"."flyway_schema_history" with baseline …
Successfully baselined schema with version: 1.0.1
上記設定例の場合は、flyway/sql ディレクトリにDDLを配置します。
例:sql/V1.0.1.20220616__First_test_migration.sql
./flyway migrate
A new version of Flyway is available
Upgrade to Flyway 8.5.13: https://rd.gt/2X0gakbFlyway Community Edition 8.5.12 by Redgate
See what's new here: https://flywaydb.org/documentation/learnmore/releaseNotes#8.5.12
Database: jdbc:postgresql://localhost:5432/postgres (PostgreSQL 11.10)
Schema version: 1.0.1.20220616
+-----------+----------------+----------------------+----------+---------------------+----------+
| Category | Version | Description | Type | Installed On | State |
+-----------+----------------+----------------------+----------+---------------------+----------+
| | 1.0.1 | baseline_migration | BASELINE | 2022-06-16 17:51:47 | Baseline |
| Versioned | 1.0.1.20220616 | First test migration | SQL | 2022-06-16 18:08:53 | Success |
+-----------+----------------+----------------------+----------+---------------------+----------+
以上で既存DBへのFlywayの導入を終わります。
公式のドキュメントが手描きで良い味出してます。
公式サイト:https://flywaydb.org/
Flyway by Redgate • Database Migrations Made Easy.
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
Flywayの使い方
LOADING...