FlywayのmigrateでcleanOnValidationErrorを試す
概要
flyway
のコマンドmigrate
にあるcleanOnValidationError
というオプションを使うとどうなるかのメモです。
今回はPostgresで試してます。
環境
どうやる?
今回はmavenを使って実行したいのでpom.xmlを準備します。
... <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.5</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.flywaydb</groupId> <artifactId>flyway-maven-plugin</artifactId> <version>5.2.1</version> </plugin> </plugins> </build> ...
migration用のSQLを準備します。 今回は2つのテーブルを作ってみます。
--V1__create_a.sql CREATE TABLE AAA ( id NUMERIC PRIMARY KEY );
--V2__create_b.sql CREATE TABLE BBB ( id NUMERIC PRIMARY KEY );
postgresはdockerで作ります。
docker run -d \ --name postgres10 \ -e POSTGRES_USER=postgres \ -e POSTGRES_PASSWORD=postgres \ -p 5432:5432 \ postgres:10
どうなる?
1回目
mvnからflywayのmigrateコマンドを実行します。
./mvnw flyway:migrate \ -Dflyway.url=jdbc:postgresql://127.0.0.1:5432/postgres \ -Dflyway.user=postgres \ -Dflyway.password=postgres \ -Dflyway.driver=org.postgresql.Driver \ -Dflyway.locations=classpath:db/migration
[INFO] Scanning for projects... [INFO] [INFO] ------------------< com.example.panage:sample-flyway >------------------ [INFO] Building sample-flyway 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- flyway-maven-plugin:5.2.3:migrate (default-cli) @ sample-flyway --- [INFO] Flyway Community Edition 5.2.3 by Boxfuse [INFO] Database: jdbc:postgresql://127.0.0.1:5432/postgres (PostgreSQL 10.4) [INFO] Successfully validated 2 migrations (execution time 00:00.026s) [INFO] Current version of schema "public": << Empty Schema >> [INFO] Migrating schema "public" to version 1 - create a [INFO] Migrating schema "public" to version 2 - create b [INFO] Successfully applied 2 migrations to schema "public" (execution time 00:00.147s) [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.394 s [INFO] Finished at: 2018-12-01T12:27:09+09:00 [INFO] ------------------------------------------------------------------------
migrateが成功して、2つのSQLが実行されたことがわかります。
SQLを編集
ここでmigrateしたSQLの1つを編集してしまいます。
--V1__create_a.sql CREATE TABLE AAA ( id NUMERIC PRIMARY KEY, value VARCHAR(100) NOT NULL );
2回目
今度はcleanOnValidationError
のオプションを付けて実行します。
./mvnw flyway:migrate \ -Dflyway.url=jdbc:postgresql://127.0.0.1:5432/postgres \ -Dflyway.user=postgres \ -Dflyway.password=postgres \ -Dflyway.driver=org.postgresql.Driver \ -Dflyway.locations=classpath:db/migration \ -Dflyway.cleanOnValidationError=true ←ココ
[INFO] Scanning for projects... [INFO] [INFO] ------------------< com.example.panage:sample-flyway >------------------ [INFO] Building sample-flyway 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- flyway-maven-plugin:5.2.3:migrate (default-cli) @ sample-flyway --- [INFO] Flyway Community Edition 5.2.3 by Boxfuse [INFO] Database: jdbc:postgresql://127.0.0.1:5432/postgres (PostgreSQL 10.4) [INFO] Successfully cleaned schema "public" (execution time 00:00.044s) [INFO] Creating Schema History table: "public"."flyway_schema_history" [INFO] Current version of schema "public": << Empty Schema >> [INFO] Migrating schema "public" to version 1 - create a [INFO] Migrating schema "public" to version 2 - create b [INFO] Successfully applied 2 migrations to schema "public" (execution time 00:00.175s) [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.858 s [INFO] Finished at: 2018-12-01T12:29:06+09:00 [INFO] ------------------------------------------------------------------------
成功しました。
Successfully cleaned schema "public"
と出て、schemaが一度cleanされていることがわかります。
注意
このオプションをつけると問答無用でschemaがキレイになってしまうので、すでにレコードが登録されているときは、そのレコードが消えていいのか確認してから実行しましょう。
また、公式にもWarning ! Do not enable in production !
とあるように、限られた状況以外で本番の環境で使うのは避けるのが良さそうです。
参考
Command-line: migrate - Command-line: migrate - Flyway by Boxfuse • Database Migrations Made Easy.