前提
手順
まずは、src/main/groovy内に、以下のGroovyクラスを作成する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package db.migration; | |
import com.googlecode.flyway.core.api.migration.jdbc.JdbcMigration; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
/** | |
* Example of a Groovy-based migration. | |
*/ | |
public class V1_0__Create_person_table implements JdbcMigration { | |
public void migrate(Connection connection) throws Exception { | |
def sql = """\ | |
create table PERSON ( | |
ID int not null, | |
NAME varchar(100) not null | |
);""" | |
PreparedStatement statement = | |
connection.prepareStatement(sql) | |
try { | |
statement.execute() | |
} finally { | |
statement.close() | |
} | |
} | |
} |
Java Migrationと同様、FlywayのJdbcMigrationインターフェースを実装したクラスを作成する(9行目)。このクラスの命名規約は以下の通り。
- パッケージ名
- 必ず
db.migration
にする - クラス名
- V(バージョン)__(説明)
- 先頭は必ず大文字のV
- バージョンは数字、バージョンの区切り文字はアンダーバー
- バージョンと説明はアンダーバー2文字で区切る
- 説明は英数字で単語をアンダーバーで区切る
クラス名の命名規則の詳細は、下記を参照。
- Java Migrations - Documentation - Flyway: the agile database migration framework for Java
- Namingの項を参照
上記の移行スクリプトをビルド、実行するためのgradleスクリプトは、以下の通り。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'groovy' | |
apply plugin: 'eclipse' | |
import java.util.Properties; | |
import com.googlecode.flyway.commandline.Main | |
import com.googlecode.flyway.core.Flyway | |
import com.googlecode.flyway.core.util.PropertiesUtils | |
repositories{ mavenCentral() } | |
dependencies{ | |
groovy localGroovy() | |
compile 'com.googlecode.flyway:flyway-core:2.1.1' | |
} | |
buildscript { | |
repositories { mavenCentral() } | |
dependencies { | |
classpath fileTree(dir: 'build/libs', include: '*.jar') | |
classpath 'com.googlecode.flyway:flyway-core:2.1' | |
classpath 'com.googlecode.flyway:flyway-commandline:2.1.1' | |
classpath 'com.h2database:h2:1.3.171' | |
} | |
} | |
def flyway = new Flyway() | |
flyway.setDataSource("jdbc:h2:file:sampledb", 'SA', '') | |
Properties properties = new Properties(); | |
int consoleWidth = PropertiesUtils.getIntProperty(properties, "flyway.consoleWidth", 80); | |
Main.initLogging(false) | |
task flywayInit << { | |
Main.executeOperation(flyway, "init", consoleWidth); | |
} | |
task flywayClean << { | |
Main.executeOperation(flyway, "clean", consoleWidth); | |
} | |
task flywayMigrate << { | |
Main.executeOperation(flyway, "migrate", consoleWidth); | |
} | |
task flywayRepair << { | |
Main.executeOperation(flyway, "repqir", consoleWidth); | |
} | |
task flywayInfo << { | |
Main.executeOperation(flyway, "info", consoleWidth); | |
} |
gradleのbuildタスクを実行してGroovy migrationの移行スクリプトをビルドした後、flywayInfoタスクを実行する。
上記出力から、移行スクリプトは存在するが、まだ実行されていない(StateがPending)ことが確認できる。なお、DBが作成されていない場合は、このタイミングで自動的に作成され、上記の表などの情報を含むメタデータを格納するためのテーブル(テーブル名はschema_version)が作成される。$ gradle flywayInfo :flywayInfo +----------------+----------------------------+---------------------+---------+ | Version | Description | Installed on | State | +----------------+----------------------------+---------------------+---------+ | 1.0 | Create person table | | Pending | +----------------+----------------------------+---------------------+---------+
続いて、flywayMigrateタスクを実行し、移行スクリプトを適用する。
再度、flywayInfoタスクを実行する。$ gradle flywayMigrate :flywayMigrate Creating Metadata table: "PUBLIC"."schema_version" Current version of schema "PUBLIC": << Empty Schema >> Migrating schema "PUBLIC" to version 1.0 Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.094s).
移行スクリプトが適用されている(StateがSuccess)ことを確認できる。$ gradle flywayInfo :flywayInfo +----------------+----------------------------+---------------------+---------+ | Version | Description | Installed on | State | +----------------+----------------------------+---------------------+---------+ | 1.0 | Create person table | 2013-05-24 20:20:17 | Success | +----------------+----------------------------+---------------------+---------+
感想
Groovy migrationは、Java migrationと比べて記述の簡略化が期待できる。特に、データの変換やファイルからの読み込みなど、SQL文では記述が難しい、あるいは不可能な処理を移行時に行いたい場合に威力を発揮しそう。
関連リンク
- 2013-03-13 - splash of waters
- SQL MigrationをGradleから行う方法
- ryu22eBlog : Javaのデータベースマイグレーションツール「Flyway」 #JJUG
- Maven PluginからFlywayを使う方法
- dev-xconnecting: データベース移行ツールFlyway
0 件のコメント:
コメントを投稿