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:
1 2 3 4 |
CREATE TABLE student ( ID INT PRIMARY KEY, name VARCHAR(250) NOT NULL ); |
- Configure the datasource in the application.properties file:
1 2 3 |
spring.datasource.url=jdbc:postgresql://localhost:5432/flyway_example spring.datasource.username=khanh spring.datasource.password=1 |
- And configure the database for Spring Data R2DBC too:
1 2 3 |
spring.r2dbc.url=r2dbc:postgresql://localhost:5432/flyway_example spring.r2dbc.username=khanh spring.r2dbc.password=1 |
Now, if you run the application, you will see that Flyway is not working.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=53801:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/khanh/Documents/code/huongdanjava.com/spring-boot-flyway-spring-data-r2dbc-example/target/classes:/Users/khanh/.m2/repository/org/springframework/boot/spring-boot-starter-data-r2dbc/3.1.1/spring-boot-starter-data-r2dbc-3.1.1.jar:/Users/khanh/.m2/repository/org/springframework/boot/spring-boot-starter/3.1.1/spring-boot-starter-3.1.1.jar:/Users/khanh/.m2/repository/org/springframework/boot/spring-boot/3.1.1/spring-boot-3.1.1.jar:/Users/khanh/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/3.1.1/spring-boot-autoconfigure-3.1.1.jar:/Users/khanh/.m2/repository/org/springframework/boot/spring-boot-starter-logging/3.1.1/spring-boot-starter-logging-3.1.1.jar:/Users/khanh/.m2/repository/ch/qos/logback/logback-classic/1.4.8/logback-classic-1.4.8.jar:/Users/khanh/.m2/repository/ch/qos/logback/logback-core/1.4.8/logback-core-1.4.8.jar:/Users/khanh/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.20.0/log4j-to-slf4j-2.20.0.jar:/Users/khanh/.m2/repository/org/apache/logging/log4j/log4j-api/2.20.0/log4j-api-2.20.0.jar:/Users/khanh/.m2/repository/org/slf4j/jul-to-slf4j/2.0.7/jul-to-slf4j-2.0.7.jar:/Users/khanh/.m2/repository/jakarta/annotation/jakarta.annotation-api/2.1.1/jakarta.annotation-api-2.1.1.jar:/Users/khanh/.m2/repository/org/yaml/snakeyaml/1.33/snakeyaml-1.33.jar:/Users/khanh/.m2/repository/org/springframework/data/spring-data-r2dbc/3.1.1/spring-data-r2dbc-3.1.1.jar:/Users/khanh/.m2/repository/org/springframework/data/spring-data-relational/3.1.1/spring-data-relational-3.1.1.jar:/Users/khanh/.m2/repository/org/springframework/data/spring-data-commons/3.1.1/spring-data-commons-3.1.1.jar:/Users/khanh/.m2/repository/org/springframework/spring-context/6.0.10/spring-context-6.0.10.jar:/Users/khanh/.m2/repository/org/springframework/spring-aop/6.0.10/spring-aop-6.0.10.jar:/Users/khanh/.m2/repository/org/springframework/spring-expression/6.0.10/spring-expression-6.0.10.jar:/Users/khanh/.m2/repository/org/springframework/spring-r2dbc/6.0.10/spring-r2dbc-6.0.10.jar:/Users/khanh/.m2/repository/org/slf4j/slf4j-api/2.0.7/slf4j-api-2.0.7.jar:/Users/khanh/.m2/repository/io/r2dbc/r2dbc-spi/1.0.0.RELEASE/r2dbc-spi-1.0.0.RELEASE.jar:/Users/khanh/.m2/repository/org/reactivestreams/reactive-streams/1.0.4/reactive-streams-1.0.4.jar:/Users/khanh/.m2/repository/io/r2dbc/r2dbc-pool/1.0.0.RELEASE/r2dbc-pool-1.0.0.RELEASE.jar:/Users/khanh/.m2/repository/io/projectreactor/addons/reactor-pool/1.0.0/reactor-pool-1.0.0.jar:/Users/khanh/.m2/repository/org/flywaydb/flyway-core/9.16.3/flyway-core-9.16.3.jar:/Users/khanh/.m2/repository/com/fasterxml/jackson/dataformat/jackson-dataformat-toml/2.15.2/jackson-dataformat-toml-2.15.2.jar:/Users/khanh/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.15.2/jackson-databind-2.15.2.jar:/Users/khanh/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.15.2/jackson-annotations-2.15.2.jar:/Users/khanh/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.15.2/jackson-core-2.15.2.jar:/Users/khanh/.m2/repository/org/apache/commons/commons-text/1.10.0/commons-text-1.10.0.jar:/Users/khanh/.m2/repository/org/apache/commons/commons-lang3/3.12.0/commons-lang3-3.12.0.jar:/Users/khanh/.m2/repository/org/springframework/spring-jdbc/6.0.10/spring-jdbc-6.0.10.jar:/Users/khanh/.m2/repository/org/springframework/spring-beans/6.0.10/spring-beans-6.0.10.jar:/Users/khanh/.m2/repository/org/springframework/spring-core/6.0.10/spring-core-6.0.10.jar:/Users/khanh/.m2/repository/org/springframework/spring-jcl/6.0.10/spring-jcl-6.0.10.jar:/Users/khanh/.m2/repository/org/springframework/spring-tx/6.0.10/spring-tx-6.0.10.jar:/Users/khanh/.m2/repository/org/postgresql/postgresql/42.6.0/postgresql-42.6.0.jar:/Users/khanh/.m2/repository/org/checkerframework/checker-qual/3.31.0/checker-qual-3.31.0.jar:/Users/khanh/.m2/repository/org/postgresql/r2dbc-postgresql/1.0.1.RELEASE/r2dbc-postgresql-1.0.1.RELEASE.jar:/Users/khanh/.m2/repository/com/ongres/scram/client/2.1/client-2.1.jar:/Users/khanh/.m2/repository/com/ongres/scram/common/2.1/common-2.1.jar:/Users/khanh/.m2/repository/com/ongres/stringprep/saslprep/1.1/saslprep-1.1.jar:/Users/khanh/.m2/repository/com/ongres/stringprep/stringprep/1.1/stringprep-1.1.jar:/Users/khanh/.m2/repository/io/projectreactor/reactor-core/3.5.7/reactor-core-3.5.7.jar:/Users/khanh/.m2/repository/io/projectreactor/netty/reactor-netty-core/1.1.8/reactor-netty-core-1.1.8.jar:/Users/khanh/.m2/repository/io/netty/netty-handler/4.1.94.Final/netty-handler-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-common/4.1.94.Final/netty-common-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-resolver/4.1.94.Final/netty-resolver-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-buffer/4.1.94.Final/netty-buffer-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-transport/4.1.94.Final/netty-transport-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-transport-native-unix-common/4.1.94.Final/netty-transport-native-unix-common-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-codec/4.1.94.Final/netty-codec-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-handler-proxy/4.1.94.Final/netty-handler-proxy-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-codec-socks/4.1.94.Final/netty-codec-socks-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-codec-http/4.1.94.Final/netty-codec-http-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-resolver-dns/4.1.94.Final/netty-resolver-dns-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-codec-dns/4.1.94.Final/netty-codec-dns-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-resolver-dns-native-macos/4.1.94.Final/netty-resolver-dns-native-macos-4.1.94.Final-osx-x86_64.jar:/Users/khanh/.m2/repository/io/netty/netty-resolver-dns-classes-macos/4.1.94.Final/netty-resolver-dns-classes-macos-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-transport-native-epoll/4.1.94.Final/netty-transport-native-epoll-4.1.94.Final-linux-x86_64.jar:/Users/khanh/.m2/repository/io/netty/netty-transport-classes-epoll/4.1.94.Final/netty-transport-classes-epoll-4.1.94.Final.jar com.huongdanjava.springboot.SpringBootFlywaySpringDataR2dbcExampleApplication . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.1.1) 2023-07-09T22:17:55.684-04:00 INFO 5852 --- [ main] tFlywaySpringDataR2dbcExampleApplication : Starting SpringBootFlywaySpringDataR2dbcExampleApplication using Java 17 with PID 5852 (/Users/khanh/Documents/code/huongdanjava.com/spring-boot-flyway-spring-data-r2dbc-example/target/classes started by khanh in /Users/khanh/Documents/code/huongdanjava.com/spring-boot-flyway-spring-data-r2dbc-example) 2023-07-09T22:17:55.689-04:00 INFO 5852 --- [ main] tFlywaySpringDataR2dbcExampleApplication : No active profile set, falling back to 1 default profile: "default" 2023-07-09T22:17:56.272-04:00 INFO 5852 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data R2DBC repositories in DEFAULT mode. 2023-07-09T22:17:56.290-04:00 INFO 5852 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 12 ms. Found 0 R2DBC repository interfaces. 2023-07-09T22:17:57.196-04:00 INFO 5852 --- [ main] tFlywaySpringDataR2dbcExampleApplication : Started SpringBootFlywaySpringDataR2dbcExampleApplication in 2.159 seconds (process running for 3.052) Process finished with exit code 0 |
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:
1 2 3 |
spring.flyway.url=jdbc:postgresql://localhost:5432/flyway_example spring.flyway.user=khanh spring.flyway.password=1 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=53839:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/khanh/Documents/code/huongdanjava.com/spring-boot-flyway-spring-data-r2dbc-example/target/classes:/Users/khanh/.m2/repository/org/springframework/boot/spring-boot-starter-data-r2dbc/3.1.1/spring-boot-starter-data-r2dbc-3.1.1.jar:/Users/khanh/.m2/repository/org/springframework/boot/spring-boot-starter/3.1.1/spring-boot-starter-3.1.1.jar:/Users/khanh/.m2/repository/org/springframework/boot/spring-boot/3.1.1/spring-boot-3.1.1.jar:/Users/khanh/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/3.1.1/spring-boot-autoconfigure-3.1.1.jar:/Users/khanh/.m2/repository/org/springframework/boot/spring-boot-starter-logging/3.1.1/spring-boot-starter-logging-3.1.1.jar:/Users/khanh/.m2/repository/ch/qos/logback/logback-classic/1.4.8/logback-classic-1.4.8.jar:/Users/khanh/.m2/repository/ch/qos/logback/logback-core/1.4.8/logback-core-1.4.8.jar:/Users/khanh/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.20.0/log4j-to-slf4j-2.20.0.jar:/Users/khanh/.m2/repository/org/apache/logging/log4j/log4j-api/2.20.0/log4j-api-2.20.0.jar:/Users/khanh/.m2/repository/org/slf4j/jul-to-slf4j/2.0.7/jul-to-slf4j-2.0.7.jar:/Users/khanh/.m2/repository/jakarta/annotation/jakarta.annotation-api/2.1.1/jakarta.annotation-api-2.1.1.jar:/Users/khanh/.m2/repository/org/yaml/snakeyaml/1.33/snakeyaml-1.33.jar:/Users/khanh/.m2/repository/org/springframework/data/spring-data-r2dbc/3.1.1/spring-data-r2dbc-3.1.1.jar:/Users/khanh/.m2/repository/org/springframework/data/spring-data-relational/3.1.1/spring-data-relational-3.1.1.jar:/Users/khanh/.m2/repository/org/springframework/data/spring-data-commons/3.1.1/spring-data-commons-3.1.1.jar:/Users/khanh/.m2/repository/org/springframework/spring-context/6.0.10/spring-context-6.0.10.jar:/Users/khanh/.m2/repository/org/springframework/spring-aop/6.0.10/spring-aop-6.0.10.jar:/Users/khanh/.m2/repository/org/springframework/spring-expression/6.0.10/spring-expression-6.0.10.jar:/Users/khanh/.m2/repository/org/springframework/spring-r2dbc/6.0.10/spring-r2dbc-6.0.10.jar:/Users/khanh/.m2/repository/org/slf4j/slf4j-api/2.0.7/slf4j-api-2.0.7.jar:/Users/khanh/.m2/repository/io/r2dbc/r2dbc-spi/1.0.0.RELEASE/r2dbc-spi-1.0.0.RELEASE.jar:/Users/khanh/.m2/repository/org/reactivestreams/reactive-streams/1.0.4/reactive-streams-1.0.4.jar:/Users/khanh/.m2/repository/io/r2dbc/r2dbc-pool/1.0.0.RELEASE/r2dbc-pool-1.0.0.RELEASE.jar:/Users/khanh/.m2/repository/io/projectreactor/addons/reactor-pool/1.0.0/reactor-pool-1.0.0.jar:/Users/khanh/.m2/repository/org/flywaydb/flyway-core/9.16.3/flyway-core-9.16.3.jar:/Users/khanh/.m2/repository/com/fasterxml/jackson/dataformat/jackson-dataformat-toml/2.15.2/jackson-dataformat-toml-2.15.2.jar:/Users/khanh/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.15.2/jackson-databind-2.15.2.jar:/Users/khanh/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.15.2/jackson-annotations-2.15.2.jar:/Users/khanh/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.15.2/jackson-core-2.15.2.jar:/Users/khanh/.m2/repository/org/apache/commons/commons-text/1.10.0/commons-text-1.10.0.jar:/Users/khanh/.m2/repository/org/apache/commons/commons-lang3/3.12.0/commons-lang3-3.12.0.jar:/Users/khanh/.m2/repository/org/springframework/spring-jdbc/6.0.10/spring-jdbc-6.0.10.jar:/Users/khanh/.m2/repository/org/springframework/spring-beans/6.0.10/spring-beans-6.0.10.jar:/Users/khanh/.m2/repository/org/springframework/spring-core/6.0.10/spring-core-6.0.10.jar:/Users/khanh/.m2/repository/org/springframework/spring-jcl/6.0.10/spring-jcl-6.0.10.jar:/Users/khanh/.m2/repository/org/springframework/spring-tx/6.0.10/spring-tx-6.0.10.jar:/Users/khanh/.m2/repository/org/postgresql/postgresql/42.6.0/postgresql-42.6.0.jar:/Users/khanh/.m2/repository/org/checkerframework/checker-qual/3.31.0/checker-qual-3.31.0.jar:/Users/khanh/.m2/repository/org/postgresql/r2dbc-postgresql/1.0.1.RELEASE/r2dbc-postgresql-1.0.1.RELEASE.jar:/Users/khanh/.m2/repository/com/ongres/scram/client/2.1/client-2.1.jar:/Users/khanh/.m2/repository/com/ongres/scram/common/2.1/common-2.1.jar:/Users/khanh/.m2/repository/com/ongres/stringprep/saslprep/1.1/saslprep-1.1.jar:/Users/khanh/.m2/repository/com/ongres/stringprep/stringprep/1.1/stringprep-1.1.jar:/Users/khanh/.m2/repository/io/projectreactor/reactor-core/3.5.7/reactor-core-3.5.7.jar:/Users/khanh/.m2/repository/io/projectreactor/netty/reactor-netty-core/1.1.8/reactor-netty-core-1.1.8.jar:/Users/khanh/.m2/repository/io/netty/netty-handler/4.1.94.Final/netty-handler-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-common/4.1.94.Final/netty-common-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-resolver/4.1.94.Final/netty-resolver-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-buffer/4.1.94.Final/netty-buffer-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-transport/4.1.94.Final/netty-transport-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-transport-native-unix-common/4.1.94.Final/netty-transport-native-unix-common-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-codec/4.1.94.Final/netty-codec-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-handler-proxy/4.1.94.Final/netty-handler-proxy-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-codec-socks/4.1.94.Final/netty-codec-socks-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-codec-http/4.1.94.Final/netty-codec-http-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-resolver-dns/4.1.94.Final/netty-resolver-dns-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-codec-dns/4.1.94.Final/netty-codec-dns-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-resolver-dns-native-macos/4.1.94.Final/netty-resolver-dns-native-macos-4.1.94.Final-osx-x86_64.jar:/Users/khanh/.m2/repository/io/netty/netty-resolver-dns-classes-macos/4.1.94.Final/netty-resolver-dns-classes-macos-4.1.94.Final.jar:/Users/khanh/.m2/repository/io/netty/netty-transport-native-epoll/4.1.94.Final/netty-transport-native-epoll-4.1.94.Final-linux-x86_64.jar:/Users/khanh/.m2/repository/io/netty/netty-transport-classes-epoll/4.1.94.Final/netty-transport-classes-epoll-4.1.94.Final.jar com.huongdanjava.springboot.SpringBootFlywaySpringDataR2dbcExampleApplication . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.1.1) 2023-07-09T22:20:06.613-04:00 INFO 5918 --- [ main] tFlywaySpringDataR2dbcExampleApplication : Starting SpringBootFlywaySpringDataR2dbcExampleApplication using Java 17 with PID 5918 (/Users/khanh/Documents/code/huongdanjava.com/spring-boot-flyway-spring-data-r2dbc-example/target/classes started by khanh in /Users/khanh/Documents/code/huongdanjava.com/spring-boot-flyway-spring-data-r2dbc-example) 2023-07-09T22:20:06.617-04:00 INFO 5918 --- [ main] tFlywaySpringDataR2dbcExampleApplication : No active profile set, falling back to 1 default profile: "default" 2023-07-09T22:20:07.127-04:00 INFO 5918 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data R2DBC repositories in DEFAULT mode. 2023-07-09T22:20:07.142-04:00 INFO 5918 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 10 ms. Found 0 R2DBC repository interfaces. 2023-07-09T22:20:08.083-04:00 INFO 5918 --- [ main] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 9.16.3 by Redgate 2023-07-09T22:20:08.083-04:00 INFO 5918 --- [ main] o.f.c.internal.license.VersionPrinter : See release notes here: https://rd.gt/416ObMi 2023-07-09T22:20:08.084-04:00 INFO 5918 --- [ main] o.f.c.internal.license.VersionPrinter : 2023-07-09T22:20:08.233-04:00 INFO 5918 --- [ main] o.f.c.i.database.base.BaseDatabaseType : Database: jdbc:postgresql://localhost:5432/flyway_example (PostgreSQL 12.1) 2023-07-09T22:20:08.275-04:00 INFO 5918 --- [ main] o.f.core.internal.command.DbValidate : Successfully validated 1 migration (execution time 00:00.022s) 2023-07-09T22:20:08.296-04:00 INFO 5918 --- [ main] o.f.core.internal.command.DbMigrate : Current version of schema "public": 2023.07.09.00000 2023-07-09T22:20:08.297-04:00 INFO 5918 --- [ main] o.f.core.internal.command.DbMigrate : Schema "public" is up to date. No migration necessary. 2023-07-09T22:20:08.368-04:00 INFO 5918 --- [ main] tFlywaySpringDataR2dbcExampleApplication : Started SpringBootFlywaySpringDataR2dbcExampleApplication in 2.367 seconds (process running for 3.237) Process finished with exit code 0 |