- Flyway Command-line toolを使ってDBをバージョン管理する。
- 使い方は簡単なのですぐに使える。Windowsで実行するとこんな感じになる。
C:\flyway-4.0.3>flyway migrate Flyway 4.0.3 by Boxfuse Database: jdbc:sqlserver://WIN7X64\sqlexpress:1433;authenticationScheme=nativeAuthentication;xopenStates=false;sendTimeAsDatetime=true;trustServerCertificate=false;sendStringParametersAsUnicode=true;selectMethod=direct;responseBuffering=adaptive;packetSize=8000;multiSubnetFailover=false;loginTimeout=15;lockTimeout=-1;lastUpdateCount=true;encrypt=false;disableStatementPooling=true;databaseName=flyway;applicationName=Microsoft JDBC Driver for SQL Server;applicationIntent=readwrite; (Microsoft SQL Server 12.0) Successfully validated 2 migrations (execution time 00:00.010s) SQLServer does not support setting the schema for the current session. Default schema NOT changed to dbo Creating Metadata table: [dbo].[schema_version] Current version of schema [dbo]: << Empty Schema >> Migrating schema [dbo] to version 1 - create table hoge Migrating schema [dbo] to version 2 - create table fuga Successfully applied 2 migrations to schema [dbo] (execution time 00:00.117s).
- この便利なツールをプロダクトのDB用インストーラやアップデータ、パッチに使いたい。
- なので標準出力だけではなく、作業ログとしてファイルにも残しておきたい。
ツールにはログ系のライブラリは特に入っていないし、flyway.confにもそういう設定はなさそうだが、何か良い方法はないものか。
調べてみると対応してそうである。jarと設定ファイル放り込んだらいけるんちゃうん?と思ったらめっちゃハマった。
- まずは結論から。まとめてしまえば、なんともあっさりした手順。
- 後述のハマったところさえクリアすればいいので、適宜変えてください。
手順
ファイルの追加場所
C:\flyway-4.0.3 ├─conf │ logback.xml └─lib logback-classic-1.1.7.jar logback-core-1.1.7.jar slf4j-api-1.7.21.jar
logback.xml
flyway.cmd
の修正
- クラスパスに
%INSTALLDIR%\conf;
を追加した。
%JAVA_CMD% -cp "%INSTALLDIR%\conf;%INSTALLDIR%\lib\*;%INSTALLDIR%\drivers\*" org.flywaydb.commandline.Main %*
実行結果
コンソール
C:\flyway-4.0.3>flyway migrate 2016-10-03 01:52:27.025 INFO : Flyway 4.0.3 by Boxfuse 2016-10-03 01:52:27.031 INFO : 2016-10-03 01:52:27.534 INFO : Database: jdbc:sqlserver://WIN7X64\sqlexpress:1433;authenticationScheme=nativeAuthentication;xopenStates=false;sendTimeAsDatetime=true;trustServerCertificate=false;sendStringParametersAsUnicode=true;selectMethod=direct;responseBuffering=adaptive;packetSize=8000;multiSubnetFailover=false;loginTimeout=15;lockTimeout=-1;lastUpdateCount=true;encrypt=false;disableStatementPooling=true;databaseName=flyway;applicationName=Microsoft JDBC Driver for SQLServer;applicationIntent=readwrite; (Microsoft SQL Server 12.0) 2016-10-03 01:52:27.638 INFO : Successfully validated 2 migrations (execution time 00:00.011s) 2016-10-03 01:52:27.640 INFO : SQLServer does not support setting the schema for the current session. Default schema NOT changed to dbo 2016-10-03 01:52:27.666 INFO : Creating Metadata table: [dbo].[schema_version] 2016-10-03 01:52:27.705 INFO : Current version of schema [dbo]: << Empty Schema>> 2016-10-03 01:52:27.706 INFO : Migrating schema [dbo] to version 1 - create table hoge 2016-10-03 01:52:27.767 INFO : Migrating schema [dbo] to version 2 - create table fuga 2016-10-03 01:52:27.789 INFO : Successfully applied 2 migrations to schema [dbo] (execution time 00:00.135s). C:\flyway-4.0.3>flyway info 2016-10-03 01:53:06.602 INFO : Flyway 4.0.3 by Boxfuse 2016-10-03 01:53:06.604 INFO : 2016-10-03 01:53:07.133 INFO : Database: jdbc:sqlserver://WIN7X64\sqlexpress:1433;authenticationScheme=nativeAuthentication;xopenStates=false;sendTimeAsDatetime=true;trustServerCertificate=false;sendStringParametersAsUnicode=true;selectMethod=direct;responseBuffering=adaptive;packetSize=8000;multiSubnetFailover=false;loginTimeout=15;lockTimeout=-1;lastUpdateCount=true;encrypt=false;disableStatementPooling=true;databaseName=flyway;applicationName=Microsoft JDBC Driver for SQLServer;applicationIntent=readwrite; (Microsoft SQL Server 12.0) 2016-10-03 01:53:07.239 INFO : SQLServer does not support setting the schema for the current session. Default schema NOT changed to dbo 2016-10-03 01:53:07.242 INFO : +---------+-------------------+---------------------+---------+ | Version | Description | Installed on | State | +---------+-------------------+---------------------+---------+ | 1 | create table hoge | 2016-10-03 01:52:27 | Success | | 2 | create table fuga | 2016-10-03 01:52:27 | Success | +---------+-------------------+---------------------+---------+
ログファイル
logs/flyway.log
にちゃんと出てるー!!