Quarkus - Using Flyway

Flyway is a popular database migration tool that is commonly used in JVM environments.

Quarkus provides first class support for using Flyway as will be explained in this guide.

Setting up support for Flyway

To start using Flyway with your project, you just need to:

  • add your migrations to the src/main/resources/db/migration folder as you usually do with Flyway

  • activate the migrate-at-start option to migrate the schema automatically or inject the Flyway object and run your migration as you normally do

In your pom.xml, add the following dependencies:

  • the Flyway extension

  • your JDBC driver extension (quarkus-jdbc-postgresql, quarkus-jdbc-h2, quarkus-jdbc-mariadb, …​)

  1. <dependencies>
  2. <!-- Flyway specific dependencies -->
  3. <dependency>
  4. <groupId>io.quarkus</groupId>
  5. <artifactId>quarkus-flyway</artifactId>
  6. </dependency>
  7. <!-- JDBC driver dependencies -->
  8. <dependency>
  9. <groupId>io.quarkus</groupId>
  10. <artifactId>quarkus-jdbc-postgresql</artifactId>
  11. </dependency>
  12. </dependencies>

Flyway support relies on the Quarkus datasource config. It can be customized for the default datasource as well as for every named datasource. First, you need to add the datasource config to the application.properties file in order to allow Flyway to manage the schema. Also, you can customize the Flyway behaviour by using the following properties:

The following is an example for the application.properties file:

  1. # configure your datasource
  2. quarkus.datasource.db-kind=postgresql
  3. quarkus.datasource.username=sarah
  4. quarkus.datasource.password=connor
  5. quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/mydatabase
  6. # Flyway minimal config properties
  7. quarkus.flyway.migrate-at-start=true
  8. # Flyway optional config properties
  9. # quarkus.flyway.baseline-on-migrate=true
  10. # quarkus.flyway.baseline-version=1.0.0
  11. # quarkus.flyway.baseline-description=Initial version
  12. # quarkus.flyway.connect-retries=10
  13. # quarkus.flyway.schemas=TEST_SCHEMA
  14. # quarkus.flyway.table=flyway_quarkus_history
  15. # quarkus.flyway.locations=db/location1,db/location2
  16. # quarkus.flyway.sql-migration-prefix=X
  17. # quarkus.flyway.repeatable-sql-migration-prefix=K

Add a SQL migration to the default folder following the Flyway naming conventions: src/main/resources/db/migration/V1.0.0__Quarkus.sql

  1. CREATE TABLE quarkus
  2. (
  3. id INT,
  4. name VARCHAR(20)
  5. );
  6. INSERT INTO quarkus(id, name)
  7. VALUES (1, 'QUARKED');

Now you can start your application and Quarkus will run the Flyway’s migrate method according to your config:

  1. @ApplicationScoped
  2. public class MigrationService {
  3. // You can Inject the object if you want to use it manually
  4. @Inject
  5. Flyway flyway; (1)
  6. public void checkMigration() {
  7. // This will print 1.0.0
  8. System.out.println(flyway.info().current().getVersion().toString());
  9. }
  10. }
1Inject the Flyway object if you want to use it directly

Multiple datasources

Flyway can be configured for multiple datasources. The Flyway properties are prefixed exactly the same way as the named datasources, for example:

  1. quarkus.datasource.db-kind=h2
  2. quarkus.datasource.username=username-default
  3. quarkus.datasource.jdbc.url=jdbc:h2:tcp://localhost/mem:default
  4. quarkus.datasource.jdbc.min-size=3
  5. quarkus.datasource.jdbc.max-size=13
  6. quarkus.datasource.users.db-kind=h2
  7. quarkus.datasource.users.username=username1
  8. quarkus.datasource.users.jdbc.url=jdbc:h2:tcp://localhost/mem:users
  9. quarkus.datasource.users.jdbc.min-size=1
  10. quarkus.datasource.users.jdbc.max-size=11
  11. quarkus.datasource.inventory.db-kind=h2
  12. quarkus.datasource.inventory.username=username2
  13. quarkus.datasource.inventory.jdbc.url=jdbc:h2:tcp://localhost/mem:inventory
  14. quarkus.datasource.inventory.jdbc.min-size=2
  15. quarkus.datasource.inventory.jdbc.max-size=12
  16. # Flyway configuration for the default datasource
  17. quarkus.flyway.schemas=DEFAULT_TEST_SCHEMA
  18. quarkus.flyway.locations=db/default/location1,db/default/location2
  19. quarkus.flyway.migrate-at-start=true
  20. # Flyway configuration for the "users" datasource
  21. quarkus.flyway.users.schemas=USERS_TEST_SCHEMA
  22. quarkus.flyway.users.locations=db/users/location1,db/users/location2
  23. quarkus.flyway.users.migrate-at-start=true
  24. # Flyway configuration for the "inventory" datasource
  25. quarkus.flyway.inventory.schemas=INVENTORY_TEST_SCHEMA
  26. quarkus.flyway.inventory.locations=db/inventory/location1,db/inventory/location2
  27. quarkus.flyway.inventory.migrate-at-start=true

Notice there’s an extra bit in the key. The syntax is as follows: quarkus.flyway.[optional name.][datasource property].

Without configuration, Flyway is set up for every datasource using the default settings.

Using the Flyway object

In case you are interested in using the Flyway object directly, you can inject it as follows:

If you enabled the quarkus.flyway.migrate-at-start property, by the time you use the Flyway instance, Quarkus will already have run the migrate operation
  1. @ApplicationScoped
  2. public class MigrationService {
  3. // You can Inject the object if you want to use it manually
  4. @Inject
  5. Flyway flyway; (1)
  6. @Inject
  7. @FlywayDataSource("inventory") (2)
  8. Flyway flywayForInventory;
  9. @Inject
  10. @Named("flyway_users") (3)
  11. Flyway flywayForUsers;
  12. public void checkMigration() {
  13. // Use the flyway instance manually
  14. flyway.clean(); (4)
  15. flyway.migrate();
  16. // This will print 1.0.0
  17. System.out.println(flyway.info().current().getVersion().toString());
  18. }
  19. }
1Inject the Flyway object if you want to use it directly
2Inject Flyway for named datasources using the Quarkus FlywayDataSource qualifier
3Inject Flyway for named datasources
4Use the Flyway instance directly