The English version of quarkus.io is the official project site. Translated sites are community supported on a best-effort basis.

Flywayの使用

Flyway は、JVM環境でよく使われているデータベースマイグレーションツールです。

Quarkusでは、このガイドで説明するように、Flywayを使用するためのファーストクラスのサポートを提供しています。

Flywayのサポートのセットアップ

Flywayを使った開発 で示されているように、プロジェクトで Flyway を使い始めるには、以下のことを行う必要があります:

  • Flyway で通常行うように、マイグレーションを src/main/resources/db/migration フォルダーに追加します。

  • migrate-at-start オプションを有効にしてスキーマを自動的に移行するか、 Flyway オブジェクトを注入して通常のように移行を実行します。

以下をビルドファイルに追加してください:

  • Flyway エクステンション

  • お使いの JDBC ドライバーのエクステンション ( quarkus-jdbc-postgresql , quarkus-jdbc-h2 , quarkus-jdbc-mariadb , …​)

  • MariaDB/MySQLのサポートが別の依存関係になったため、MariaDB/MySQLのユーザーは今後、 flyway-mysql の依存関係を追加する必要があります。

  • Microsoft SQL Serverのサポートが別の依存関係になったため、Microsoft SQL Serverのユーザーは今後、 flyway-sqlserver の依存関係を追加する必要があります。

  • オラクルのサポートが別の依存関係になったため、オラクルユーザーは今後、 flyway-database-oracle の依存関係を追加する必要があります。

pom.xml
<!-- Flyway specific dependencies -->
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-flyway</artifactId>
</dependency>

<!-- Flyway SQL Server specific dependencies -->
<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-sqlserver</artifactId>
</dependency>

<!-- Flyway MariaDB/MySQL specific dependencies -->
<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-mysql</artifactId>
</dependency>

<!-- Flyway Oracle specific dependencies -->
<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-database-oracle</artifactId>
</dependency>

<!-- JDBC driver dependencies -->
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
build.gradle
// Flyway specific dependencies
implementation("io.quarkus:quarkus-flyway")
// Flyway SQL Server specific dependencies
implementation("org.flywaydb:flyway-sqlserver")
// Flyway MariaDB/MySQL specific dependencies
implementation("org.flywaydb:flyway-mysql")
// Flyway Oracle specific dependencies
implementation("org.flywaydb:flyway-database-oracle")
// JDBC driver dependencies
implementation("io.quarkus:quarkus-jdbc-postgresql")

Flyway のサポートは、Quarkus のデータソース設定に依存しています。この設定は、デフォルトのデータソースだけでなく、 すべての 名前の付いたデータソース に対してカスタマイズすることができます。まず、Flyway によるスキーマ管理を可能にするために、データソース設定を application.properties ファイルに追加する必要があります。また、以下のプロパティーを使用して、Flyway の動作をカスタマイズすることができます。

ビルド時に固定される構成プロパティ - 他のすべての構成プロパティは実行時にオーバーライド可能

Configuration property

デフォルト

Whether Flyway is enabled during the build.

If Flyway is disabled, the Flyway beans won’t be created and Flyway won’t be usable.

Environment variable: QUARKUS_FLYWAY_ENABLED

Show more

boolean

true

Comma-separated list of locations to scan recursively for migrations. The location type is determined by its prefix.

Unprefixed locations or locations starting with classpath: point to a package on the classpath and may contain both SQL and Java-based migrations.

Locations starting with filesystem: point to a directory on the filesystem, may only contain SQL migrations and are only scanned recursively down non-hidden directories.

Environment variable: QUARKUS_FLYWAY_LOCATIONS

Show more

list of string

db/migration

Comma-separated list of fully qualified class names of Callback implementations to use to hook into the Flyway lifecycle. The org.flywaydb.core.api.callback.Callback subclass must have a no-args constructor and must not be abstract. These classes must also not have any fields that hold state (unless that state is initialized in the constructor).

Environment variable: QUARKUS_FLYWAY_CALLBACKS

Show more

list of string

Flag to activate/deactivate Flyway for a specific datasource at runtime.

Environment variable: QUARKUS_FLYWAY_ACTIVE

Show more

boolean

'true' if the datasource is active; 'false' otherwise

The maximum number of retries when attempting to connect to the database.

After each failed attempt, Flyway will wait up to the configured connect-retries-interval duration before attempting to connect again, up to the maximum number of times specified by connectRetries.

Environment variable: QUARKUS_FLYWAY_CONNECT_RETRIES

Show more

int

The maximum time between retries when attempting to connect to the database.

This will cap the interval between connect retries to the value provided.

Environment variable: QUARKUS_FLYWAY_CONNECT_RETRIES_INTERVAL

Show more

Duration

120 seconds

Sets the default schema managed by Flyway. This schema name is case-sensitive. If not specified, but schemas is, Flyway uses the first schema in that list. If that is also not specified, Flyway uses the default schema for the database connection.

Consequences:

  • This schema will be the one containing the schema history table.

  • This schema will be the default for the database connection (provided the database supports this concept).

Environment variable: QUARKUS_FLYWAY_DEFAULT_SCHEMA

Show more

string

The JDBC URL that Flyway uses to connect to the database. Falls back to the datasource URL if not specified.

Environment variable: QUARKUS_FLYWAY_JDBC_URL

Show more

string

The username that Flyway uses to connect to the database. If no specific JDBC URL is configured, falls back to the datasource username if not specified.

Environment variable: QUARKUS_FLYWAY_USERNAME

Show more

string

The password that Flyway uses to connect to the database. If no specific JDBC URL is configured, falls back to the datasource password if not specified.

Environment variable: QUARKUS_FLYWAY_PASSWORD

Show more

string

Comma-separated case-sensitive list of schemas managed by Flyway. The first schema in the list will be automatically set as the default one during the migration. It will also be the one containing the schema history table.

Environment variable: QUARKUS_FLYWAY_SCHEMAS

Show more

list of string

The name of Flyway’s schema history table. By default (single-schema mode), the schema history table is placed in the default schema for the connection provided by the datasource. When the flyway.schemas property is set (multi-schema mode), the schema history table is placed in the first schema of the list.

Environment variable: QUARKUS_FLYWAY_TABLE

Show more

string

The file name prefix for versioned SQL migrations. Versioned SQL migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix , which using the defaults translates to V1.1__My_description.sql

Environment variable: QUARKUS_FLYWAY_SQL_MIGRATION_PREFIX

Show more

string

The file name prefix for repeatable SQL migrations. Repeatable SQL migrations have the following file name structure: prefixSeparatorDESCRIPTIONsuffix , which using the defaults translates to R__My_description.sql

Environment variable: QUARKUS_FLYWAY_REPEATABLE_SQL_MIGRATION_PREFIX

Show more

string

true to execute Flyway clean command automatically when the application starts, false otherwise.

Environment variable: QUARKUS_FLYWAY_CLEAN_AT_START

Show more

boolean

false

true to prevent Flyway clean operations, false otherwise.

Environment variable: QUARKUS_FLYWAY_CLEAN_DISABLED

Show more

boolean

false

true to automatically call clean when a validation error occurs, false otherwise.

Environment variable: QUARKUS_FLYWAY_CLEAN_ON_VALIDATION_ERROR

Show more

boolean

false

true to execute Flyway automatically when the application starts, false otherwise.

Environment variable: QUARKUS_FLYWAY_MIGRATE_AT_START

Show more

boolean

false

true to execute a Flyway repair command when the application starts, false otherwise.

Environment variable: QUARKUS_FLYWAY_REPAIR_AT_START

Show more

boolean

false

true to execute a Flyway validate command when the application starts, false otherwise.

Environment variable: QUARKUS_FLYWAY_VALIDATE_AT_START

Show more

boolean

false

true to execute Flyway baseline before migrations This flag is ignored if the flyway_schema_history table exists in the current schema or if the current schema is empty. Note that this will not automatically call migrate, you must either enable baselineAtStart or programmatically call flyway.migrate().

Environment variable: QUARKUS_FLYWAY_BASELINE_ON_MIGRATE

Show more

boolean

false

true to execute Flyway baseline automatically when the application starts. This flag is ignored if the flyway_schema_history table exists in the current schema. This will work even if the current schema is empty.

Environment variable: QUARKUS_FLYWAY_BASELINE_AT_START

Show more

boolean

false

The initial baseline version.

Environment variable: QUARKUS_FLYWAY_BASELINE_VERSION

Show more

string

The description to tag an existing schema with when executing baseline.

Environment variable: QUARKUS_FLYWAY_BASELINE_DESCRIPTION

Show more

string

Whether to automatically call validate when performing a migration.

Environment variable: QUARKUS_FLYWAY_VALIDATE_ON_MIGRATE

Show more

boolean

true

Allows migrations to be run "out of order".

Environment variable: QUARKUS_FLYWAY_OUT_OF_ORDER

Show more

boolean

false

Ignore missing migrations when reading the history table. When set to true migrations from older versions present in the history table but absent in the configured locations will be ignored (and logged as a warning), when false (the default) the validation step will fail.

Environment variable: QUARKUS_FLYWAY_IGNORE_MISSING_MIGRATIONS

Show more

boolean

false

Ignore future migrations when reading the history table. When set to true migrations from newer versions present in the history table but absent in the configured locations will be ignored (and logged as a warning), when false (the default) the validation step will fail.

Environment variable: QUARKUS_FLYWAY_IGNORE_FUTURE_MIGRATIONS

Show more

boolean

false

Whether Flyway should attempt to create the schemas specified in the schemas property

Environment variable: QUARKUS_FLYWAY_CREATE_SCHEMAS

Show more

boolean

true

Prefix of every placeholder (default: ${ )

Environment variable: QUARKUS_FLYWAY_PLACEHOLDER_PREFIX

Show more

string

Suffix of every placeholder (default: } )

Environment variable: QUARKUS_FLYWAY_PLACEHOLDER_SUFFIX

Show more

string

The SQL statements to run to initialize a new database connection immediately after opening it.

Environment variable: QUARKUS_FLYWAY_INIT_SQL

Show more

string

Whether to validate migrations and callbacks whose scripts do not obey the correct naming convention. A failure can be useful to check that errors such as case sensitivity in migration prefixes have been corrected.

Environment variable: QUARKUS_FLYWAY_VALIDATE_MIGRATION_NAMING

Show more

boolean

false

Ignore migrations during validate and repair according to a given list of patterns (see https://flywaydb.org/documentation/configuration/parameters/ignoreMigrationPatterns for more information). When this configuration is set, the ignoreFutureMigrations and ignoreMissingMigrations settings are ignored. Patterns are comma separated.

Environment variable: QUARKUS_FLYWAY_IGNORE_MIGRATION_PATTERNS

Show more

list of string

Comma-separated list of locations to scan recursively for migrations. The location type is determined by its prefix.

Unprefixed locations or locations starting with classpath: point to a package on the classpath and may contain both SQL and Java-based migrations.

Locations starting with filesystem: point to a directory on the filesystem, may only contain SQL migrations and are only scanned recursively down non-hidden directories.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__LOCATIONS

Show more

list of string

db/migration

Comma-separated list of fully qualified class names of Callback implementations to use to hook into the Flyway lifecycle. The org.flywaydb.core.api.callback.Callback subclass must have a no-args constructor and must not be abstract. These classes must also not have any fields that hold state (unless that state is initialized in the constructor).

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__CALLBACKS

Show more

list of string

Sets the placeholders to replace in SQL migration scripts.

Environment variable: QUARKUS_FLYWAY_PLACEHOLDERS

Show more

Map<String,String>

Flag to activate/deactivate Flyway for a specific datasource at runtime.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__ACTIVE

Show more

boolean

'true' if the datasource is active; 'false' otherwise

The maximum number of retries when attempting to connect to the database.

After each failed attempt, Flyway will wait up to the configured connect-retries-interval duration before attempting to connect again, up to the maximum number of times specified by connectRetries.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__CONNECT_RETRIES

Show more

int

The maximum time between retries when attempting to connect to the database.

This will cap the interval between connect retries to the value provided.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__CONNECT_RETRIES_INTERVAL

Show more

Duration

120 seconds

Sets the default schema managed by Flyway. This schema name is case-sensitive. If not specified, but schemas is, Flyway uses the first schema in that list. If that is also not specified, Flyway uses the default schema for the database connection.

Consequences:

  • This schema will be the one containing the schema history table.

  • This schema will be the default for the database connection (provided the database supports this concept).

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__DEFAULT_SCHEMA

Show more

string

The JDBC URL that Flyway uses to connect to the database. Falls back to the datasource URL if not specified.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__JDBC_URL

Show more

string

The username that Flyway uses to connect to the database. If no specific JDBC URL is configured, falls back to the datasource username if not specified.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__USERNAME

Show more

string

The password that Flyway uses to connect to the database. If no specific JDBC URL is configured, falls back to the datasource password if not specified.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__PASSWORD

Show more

string

Comma-separated case-sensitive list of schemas managed by Flyway. The first schema in the list will be automatically set as the default one during the migration. It will also be the one containing the schema history table.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__SCHEMAS

Show more

list of string

The name of Flyway’s schema history table. By default (single-schema mode), the schema history table is placed in the default schema for the connection provided by the datasource. When the flyway.schemas property is set (multi-schema mode), the schema history table is placed in the first schema of the list.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__TABLE

Show more

string

The file name prefix for versioned SQL migrations. Versioned SQL migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix , which using the defaults translates to V1.1__My_description.sql

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__SQL_MIGRATION_PREFIX

Show more

string

The file name prefix for repeatable SQL migrations. Repeatable SQL migrations have the following file name structure: prefixSeparatorDESCRIPTIONsuffix , which using the defaults translates to R__My_description.sql

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__REPEATABLE_SQL_MIGRATION_PREFIX

Show more

string

true to execute Flyway clean command automatically when the application starts, false otherwise.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__CLEAN_AT_START

Show more

boolean

false

true to prevent Flyway clean operations, false otherwise.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__CLEAN_DISABLED

Show more

boolean

false

true to automatically call clean when a validation error occurs, false otherwise.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__CLEAN_ON_VALIDATION_ERROR

Show more

boolean

false

true to execute Flyway automatically when the application starts, false otherwise.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__MIGRATE_AT_START

Show more

boolean

false

true to execute a Flyway repair command when the application starts, false otherwise.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__REPAIR_AT_START

Show more

boolean

false

true to execute a Flyway validate command when the application starts, false otherwise.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__VALIDATE_AT_START

Show more

boolean

false

true to execute Flyway baseline before migrations This flag is ignored if the flyway_schema_history table exists in the current schema or if the current schema is empty. Note that this will not automatically call migrate, you must either enable baselineAtStart or programmatically call flyway.migrate().

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__BASELINE_ON_MIGRATE

Show more

boolean

false

true to execute Flyway baseline automatically when the application starts. This flag is ignored if the flyway_schema_history table exists in the current schema. This will work even if the current schema is empty.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__BASELINE_AT_START

Show more

boolean

false

The initial baseline version.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__BASELINE_VERSION

Show more

string

The description to tag an existing schema with when executing baseline.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__BASELINE_DESCRIPTION

Show more

string

Whether to automatically call validate when performing a migration.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__VALIDATE_ON_MIGRATE

Show more

boolean

true

Allows migrations to be run "out of order".

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__OUT_OF_ORDER

Show more

boolean

false

Ignore missing migrations when reading the history table. When set to true migrations from older versions present in the history table but absent in the configured locations will be ignored (and logged as a warning), when false (the default) the validation step will fail.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__IGNORE_MISSING_MIGRATIONS

Show more

boolean

false

Ignore future migrations when reading the history table. When set to true migrations from newer versions present in the history table but absent in the configured locations will be ignored (and logged as a warning), when false (the default) the validation step will fail.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__IGNORE_FUTURE_MIGRATIONS

Show more

boolean

false

Sets the placeholders to replace in SQL migration scripts.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__PLACEHOLDERS

Show more

Map<String,String>

Whether Flyway should attempt to create the schemas specified in the schemas property

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__CREATE_SCHEMAS

Show more

boolean

true

Prefix of every placeholder (default: ${ )

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__PLACEHOLDER_PREFIX

Show more

string

Suffix of every placeholder (default: } )

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__PLACEHOLDER_SUFFIX

Show more

string

The SQL statements to run to initialize a new database connection immediately after opening it.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__INIT_SQL

Show more

string

Whether to validate migrations and callbacks whose scripts do not obey the correct naming convention. A failure can be useful to check that errors such as case sensitivity in migration prefixes have been corrected.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__VALIDATE_MIGRATION_NAMING

Show more

boolean

false

Ignore migrations during validate and repair according to a given list of patterns (see https://flywaydb.org/documentation/configuration/parameters/ignoreMigrationPatterns for more information). When this configuration is set, the ignoreFutureMigrations and ignoreMissingMigrations settings are ignored. Patterns are comma separated.

Environment variable: QUARKUS_FLYWAY__NAMED_DATA_SOURCES__IGNORE_MIGRATION_PATTERNS

Show more

list of string

期間フォーマットについて

To write duration values, use the standard java.time.Duration format. See the Duration#parse() Java API documentation for more information.

数字で始まる簡略化した書式を使うこともできます:

  • 数値のみの場合は、秒単位の時間を表します。

  • 数値の後に ms が続く場合は、ミリ秒単位の時間を表します。

その他の場合は、簡略化されたフォーマットが解析のために java.time.Duration フォーマットに変換されます:

  • 数値の後に hms が続く場合は、その前に PT が付けられます。

  • 数値の後に d が続く場合は、その前に P が付けられます。

Flywayを使った開発

application.properties ファイルの例は以下の通りです。

# configure your datasource
quarkus.datasource.db-kind=postgresql
quarkus.datasource.username=sarah
quarkus.datasource.password=connor
quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/mydatabase

# Run Flyway migrations automatically
quarkus.flyway.migrate-at-start=true

# More Flyway configuration options
# quarkus.flyway.baseline-on-migrate=true
# quarkus.flyway.baseline-version=1.0.0
# quarkus.flyway.baseline-description=Initial version
# quarkus.flyway.connect-retries=10
# quarkus.flyway.schemas=TEST_SCHEMA
# quarkus.flyway.table=flyway_quarkus_history
# quarkus.flyway.locations=db/location1,db/location2
# quarkus.flyway.sql-migration-prefix=X
# quarkus.flyway.repeatable-sql-migration-prefix=K

src/main/resources/db/migration/V1.0.0__Quarkus.sql のように Flyway の命名規則に従って、デフォルトフォルダーに SQL マイグレーションを追加します。

CREATE TABLE quarkus
(
  id   INT,
  name VARCHAR(20)
);
INSERT INTO quarkus(id, name)
VALUES (1, 'QUARKED');

これでアプリケーションを起動でき、Quarkus は設定に従って Flyway のマイグレーションメソッドを実行します。

上記の例のように quarkus.flyway.migrate-at-start=true とすることで Quarkusは アプリケーションの起動 の一部としてFlywayのマイグレーションを実行します。
@ApplicationScoped
public class MigrationService {
    // You can Inject the object if you want to use it manually
    @Inject
    Flyway flyway; (1)

    public void checkMigration() {
        // This will print 1.0.0
        System.out.println(flyway.info().current().getVersion().toString());
    }
}
1 Flyway オブジェクトを直接使用する場合はインジェクトします。
開発モードでは、既存のマイグレーションスクリプトが変更された場合、Quarkusは自動的にアプリケーションを再起動します。新しいマイグレーションスクリプトの開発やテスト中にこの機能を利用したい場合は、 %dev.quarkus.flyway.clean-at-start=true を設定し、変更したマイグレーションをFlywayが実際に実行するようにする必要があります。

Flywayスキーマ履歴テーブルの修復

Flyway スキーマ履歴テーブルの 修復 が必要となるシナリオはさまざまです。そのようなシナリオの1つは、トランザクションDDL文をサポートしていないデータベースで移行が失敗した場合です。

このような状況では、 Flyway repairコマンド が便利です。Quarkusでは、マイグレーション前に quarkus.flyway.repair-at-start=true を設定して自動的に実行するか、 Flyway オブジェクトをインジェクションして Flyway#repair() を呼び出すことで手動で実行することができます。

複数のデータソース

Flyway は、複数のデータソースに対して設定可能です。Flyway のプロパティーには、例えば、名前の付いたデータソースと全く同じように接頭辞が付けられます。

quarkus.datasource.db-kind=h2
quarkus.datasource.username=username-default
quarkus.datasource.jdbc.url=jdbc:h2:tcp://localhost/mem:default
quarkus.datasource.jdbc.max-size=13

quarkus.datasource.users.db-kind=h2
quarkus.datasource.users.username=username1
quarkus.datasource.users.jdbc.url=jdbc:h2:tcp://localhost/mem:users
quarkus.datasource.users.jdbc.max-size=11

quarkus.datasource.inventory.db-kind=h2
quarkus.datasource.inventory.username=username2
quarkus.datasource.inventory.jdbc.url=jdbc:h2:tcp://localhost/mem:inventory
quarkus.datasource.inventory.jdbc.max-size=12

# Flyway configuration for the default datasource
quarkus.flyway.schemas=DEFAULT_TEST_SCHEMA
quarkus.flyway.locations=db/default/location1,db/default/location2
quarkus.flyway.migrate-at-start=true

# Flyway configuration for the "users" datasource
quarkus.flyway.users.schemas=USERS_TEST_SCHEMA
quarkus.flyway.users.locations=db/users/location1,db/users/location2
quarkus.flyway.users.migrate-at-start=true

# Flyway configuration for the "inventory" datasource
quarkus.flyway.inventory.schemas=INVENTORY_TEST_SCHEMA
quarkus.flyway.inventory.locations=db/inventory/location1,db/inventory/location2
quarkus.flyway.inventory.migrate-at-start=true

キーに余分なビットがあることに注意してください。構文は次の通りです。quarkus.flyway.[optional name.][datasource property]

設定なしの場合、Flyway はデフォルト設定を使用して各データソースに対して設定されます。

Flywayのカスタマイズ

Quarkusが提供する設定オプションに加えてFlywayを設定する必要がある場合、 io.quarkus.flyway.FlywayConfigurationCustomizer クラスが便利です。

Flywayをデフォルトのデータソース用にカスタマイズするには、次のようなBeanを追加するだけです:

@Singleton
public static class MyCustomizer implements FlywayConfigurationCustomizer {

    @Override
    public void customize(FluentConfiguration configuration) {
        // do something with configuration
    }
}

名前付きデータソースを使用する場合、 @FlywayDataSource アノテーションを使用して、カスタマイザーが適用されるデータソースを指定することができます。例えば、複数のデータソースがあり、そのうちの1つが users という名前で、そのデータソースに対してのみ Flyway のカスタマイズが必要な場合、以下のコードを使用することができます:

@Singleton
@FlywayDataSource("users")
public static class UsersCustomizer implements FlywayConfigurationCustomizer {

    @Override
    public void customize(FluentConfiguration configuration) {
        // do something with configuration
    }
}

Flyway オブジェクトの使用

Flyway オブジェクトを直接使いたい場合は、以下のように注入できます。

@ApplicationScoped
public class MigrationService {
    // You can Inject the object if you want to use it manually
    @Inject
    Flyway flyway; (1)

    @Inject
    @FlywayDataSource("inventory") (2)
    Flyway flywayForInventory;

    @Inject
    @Named("flyway_users") (3)
    Flyway flywayForUsers;

    public void checkMigration() {
        // Use the flyway instance manually
        flyway.clean(); (4)
        flyway.migrate();
        // This will print 1.0.0
        System.out.println(flyway.info().current().getVersion().toString());
    }
}
1 Flyway オブジェクトを直接使用する場合はインジェクトします。
2 Quarkus FlywayDataSource 修飾子を使用して、指定されたデータソースに Flyway を注入します。
3 名前の付いたデータソースへの Flyway の注入
4 Flyway インスタンスの直接使用

FlywayとHibernate ORM

FlywayをHibernate ORMと一緒に使用する場合、Dev UIを使用して初期スキーマ作成スクリプトを生成できます。

この機能の詳細については、 Hibernate ORM ガイドを参照してください。

Flyway and Reactive datasources

Flyway internally relies on a JDBC datasource, whereas reactive use cases will rely on reactive SQL clients, either directly or through Hibernate Reactive. This is not a problem in Quarkus, because a single configured datasource can be made available both through reactive clients and JDBC.

To use Flyway on a datasource you otherwise access reactively, simply make sure to configure that datasource both as JDBC and reactive. This involves in particular adding dependencies to Quarkus extensions for both the JDBC driver and the reactive client, for instance quarkus-jdbc-postgresql and quarkus-reactive-pg-client.

KubernetesでFlyway

アプリケーションの起動ごとにFlywayの初期化を実行しない方が便利な場合があります。そのような例の一つがデプロイ時です。

Kubernetes上では、すべてのレプリカでFlywayを実行することは意味がありません。 代わりに、Flywayを一度実行してから、Flywayなしで実際のアプリケーションを起動することが望ましいです。 このユースケースをサポートするために、Kubernetes用のマニフェストを生成するとき、生成されたマニフェストにはFlyway用のKubernetes初期化 Job が含まれます。 Job は初期化を実行し、 Job が正常に完了すると、実際の Pod が開始されます。

無効化

この機能はデフォルトで有効になっており、以下を使用してグローバルに無効にすることができます:

quarkus.kubernetes.init-task-defaults.enabled=false

あるいはOpenShift上では次を使用することができます:

quarkus.openshift.init-task-defaults.enabled=false

ジョブ待機をコントロールするカスタム画像の使用

デフォルトでは groundnuty/k8s-wait-for:no-root-v1.7 である wait-for イメージを変更するには、次を使用できます。

quarkus.kubernetes.init-task-defaults.wait-for-container.image=my/wait-for-image:1.0

あるいはOpenShift上では次を使用することができます:

quarkus.openshift.init-task-defaults.wait-for-container.image=my/wait-for-image:1.0

: この文脈では、グローバルとは、 初期化タスクの外部化をサポートする全てのエクステンションに対して を意味します。

関連コンテンツ