Configure Flyway with Spring Data R2DBC in Spring Boot

In the previous tutorial, I showed you how to configure Flyway with the Spring Boot application using Spring Data JDBC. If your Spring Boot application is using Spring Data R2DBC, then Flyway will not work.

For example, I have a Spring Boot project with Flyway, Spring Data R2DBC Starter, and R2DBC PostgreSQL dependencies as follows:

I will re-declare the configuration as I did in the tutorial Using Flyway in Spring Boot, including:

The SQL file V2023.07.09.000__Init.sql is located in the src/main/resources/db/migration directory with the following content:

  • Configure the datasource in the application.properties file:

  • And configure the database for Spring Data R2DBC too:

Now, if you run the application, you will see that Flyway is not working.

It does not automatically initialize and pick up the SQL file that we have defined in the src/main/resources/db/migration directory as we would like.

In order for Flyway to work properly in a Spring Boot application that uses Spring Data R2DBC, you can use some properties starting with “spring.flyway”. As follows:

It’s as simple as that, guys.

You can also use the above properties for the example application in the tutorial Using Flyway in Spring Boot, but because Flyway can reuse the datasource that we have declared for Spring Data JDBC, we do not need to declare any more of these properties.

Run the application again, you will see Flyway work as before:

Add Comment