Skip to content
Mitsuru Ogawa edited this page Sep 27, 2015 · 5 revisions

Flyway

初期設定

本家を参照。

Gradle - First Steps - Flyway • Database Migrations Made Easy.

Gradleに依存関係を追加して、Grable用のプラグインを追加すると、既存のbuild.gradleファイルにflyway用のGradleタスクが追加される。

Flyway tasks
------------
flywayBaseline - Baselines an existing database, excluding all migrations up to and including baselineVersion.
flywayClean - Drops all objects in the configured schemas.
flywayInfo - Prints the details and status information about all the migrations.
flywayInit - Baselines an existing database, excluding all migrations up to and including baselineVersion.
flywayMigrate - Migrates the schema to the latest version.
flywayRepair - Repairs the Flyway metadata table.
flywayValidate - Validates the applied migrations against the ones available on the classpath.

起動コマンド

基本的にはこれを常に使うイメージ。

gradle flywayMigrate

トラブル

初回セットアップなどトラブルがあったものを追記。

デバックなどでDBが利用中

Execution failed for task ':flywayMigrate'.
> Error occurred while executing flywayMigrate
  Unable to obtain Jdbc connection from DataSource (jdbc:h2:file:./.data/sample) for user 'sa': データベースが       使用中です: "/Users/mitsuruog/workspace/spring-boot-sample/.data/sample.mv.db". 可能な解決策: 他の接続を全て閉じる; サーバモードを使う
  Database may be already in use: "/Users/mitsuruog/workspace/spring-boot-sample/.data/sample.mv.db". Possible solutions: close all other connection(s); use the server mode [90020-187]
  データベースが使用中です: "/Users/mitsuruog/workspace/spring-boot-sample/.data/sample.mv.db". 可能な解決策: 他の接続を全て閉じる; サーバモードを使う
  Database may be already in use: "/Users/mitsuruog/workspace/spring-boot-sample/.data/sample.mv.db". Possible solutions: close all other connection(s); use the server mode [90020-187]
  The file is locked: nio:/Users/mitsuruog/workspace/spring-boot-sample/.data/sample.mv.db [1.4.187/7]

DBを使っているプロセスを止める

SQL格納用のLocation間違い

Execution failed for task ':flywayMigrate'.
> Error occurred while executing flywayMigrate
  Unable to scan for SQL migrations in location: classpath:db/migration
  Unable to determine URL for classpath location: db/migration (ClassLoader: java.net.URLClassLoader@2ab5afc7)

Spring boot の場合、resources/db/migrationなので、SQLが格納されているパスを確認すること

Schemaのバージョン差異

Execution failed for task ':flywayMigrate'.
> Error occurred while executing flywayMigrate
  Found non-empty schema "PUBLIC" without metadata table! Use baseline() or set baselineOnMigrate to true to initialize the metadata table.

flywayは内部でSchemaのバージョンを管理しているため、差異があると発生する。 ひとまず、全履歴をクリアしてやり直す場合は、flywayCleanをすればOK。

gradle flywayClean flywayMigrate

H2のJDCB URLサンプル

こんなのでいいのかな??

H2 JDBC Driver and URL Information

  • H2 Server JDBC Driver
    • DRIVER CLASS: org.h2.Driver
    • DRIVER LOCATION: Enter the location of the jar file containing the H2 JDBC Driver
    • JDBC URL FORMAT: jdbc:h2:tcp://<host>/<database>
  • H2 Embedded JDBC Driver
    • DRIVER CLASS: org.h2.Driver
    • DRIVER LOCATION: Enter the location of the jar file containing the H2 JDBC Driver
    • JDBC URL FORMAT: jdbc:h2:file:<filepath>

flywayMigrateしたDBが「.mv.db」なんだけど・・・

java - Why is my embedded h2 program writing to a .mv.db file - Stack Overflow

次のオプションを接続URLに付けなければ、デフォルトでMVStore storageに格納されるっぽい。

;MV_STORE=FALSE;MVCC=FALSE