R2DBC and liquibase
Asked Answered
V

4

9

So starting a new project and i want to use r2dbc and webflux, been looking into what support there is to handle database migration. The last answer i could find here was from july 2019 that liquibase does not support R2DBC and after googling, this seems to still be the case.

The dream would be to use r2dbc-h2 while developing locally, and then use something like postgres during production. Liquibase would manage the table structure both locally and in production.

Been trying to google a bit about how such a setup would look like and there is very little information out there.

I have been thinking about setting up the tables using the liquibase-maven-plugin, but i don't know if that will work with r2dbc-h2.

So several questions:

  • How to setup so that liquibase uses a regular driver during migration, while the rest of the application uses the reactive driver?
  • if using the maven plugin can this be used with H2 or do i need postgres as a docker?

This is a very black hole for me, does any have any information?

Vertebral answered 24/6, 2020 at 12:38 Comment(3)
FWIW, there's a ticket open at Liquibase for R2DBC support: liquibase.jira.com/browse/CORE-3419. You might want to leave a comment there.Vallee
yes i have seen it and it has not been updated since April 26, 2019, 10:05 AM and there is no way to leave comments there.Vertebral
Meh, you're right. Sorry for bothering you.Vallee
P
29

I think there should be no problem using 2 drivers in application. As liquibase uses standard jdbc driver you can configure it to use that one for migrations and configure r2dbc to run the application. Maybe some tweeks needs to be done but I would start with something like:

spring:
  liquibase:
    url: jdbc:postgresql://localhost:5432/mydb
    user: postgres
  r2dbc:
    url: r2dbc:postgresql://localhost:5432/mydb
    username: postgres

and include both libraries:

io.r2dbc:r2dbc-postgresql
org.postgresql:postgresql

If there is error keep us posted.

note: for testing you can use testcontainers or embedded postgresql also

Predispose answered 25/6, 2020 at 15:28 Comment(2)
I’ll look into it, for now im using the liquibase maven plugin to execute it during runtime and it works okey. Would be better though to be anle to verify database integrity during application startup than during build time.Vertebral
I can confirm that the suggested approach works. I tried with h2 and managed to run migration on startup using flyway.Behlau
R
4

Complementing @bilak's answer, you must add the following dependency to your pom.xml:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>

I was facing the same problem and I had added the properties user and URL to Liquibase configuration on my application.yml, and then Spring started to claim about one class not found, adding this dependency solved the issue

Roguery answered 21/8, 2020 at 15:38 Comment(2)
After adding the above dependency still, I was getting the following error msg: Caused by: java.lang.IllegalArgumentException: URL jdbc:postgresql://localhost:5433/dbname does not start with the r2dbc scheme To resolve that I need to explicitly specify the DB configuration in the Configuration class and add @EnableR2dbcRepositoriesBoling
for R2DBC you need to use it for example ** url: r2dbc:postgresql://localhost:5432/localdatabase?currentSchema=public username: mypostgres password: admin**Roentgenograph
M
1

As of Spring Boot 2.6.3 with Spring Framework 5.3.15, the following configuration works for R2DBC with Liquibase

build.gradle snippet

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'

    //database
    implementation "io.r2dbc:r2dbc-postgresql"
    runtimeOnly 'org.postgresql:postgresql'

    //liquibase
    implementation "org.liquibase:liquibase-core"
    runtimeOnly 'org.springframework:spring-jdbc'

    testImplementation 'io.projectreactor:reactor-test'
}

application.yml

spring:
  main:
    web-application-type: REACTIVE
  r2dbc:
    url: r2dbc:postgresql://localhost/mydb
    username: postgres
  liquibase:
    url: jdbc:postgresql://localhost/mydb
Modeling answered 30/6, 2022 at 15:1 Comment(1)
web-application-type: REACTIVE is not neededVertebral
H
0

Maven dependencies required for integration

 <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-core</artifactId>
        <version>4.18.0</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-r2dbc</artifactId>
    </dependency>
    <dependency>
        <groupId>io.r2dbc</groupId>
        <artifactId>r2dbc-postgresql</artifactId>
        <version>${r2dbc-postgresql.version}</version>
    </dependency>
        

Applcation.yml configuration

spring:
  r2dbc:
   username: postgres
   password: password1
   url: r2dbc:postgresql://localhost:5433
  liquibase:
   url: jdbc:postgresql://localhost:5433/postgres
   user: postgres
   password: password1
   change-log: liquibase-changeLog.xml

Nothing else is required no manual beans or any other configuration is required

Heteroousian answered 14/2, 2023 at 4:45 Comment(2)
Isn't jdbc driver needed?Zel
JDBC driver is not required and it worked for me too with the above config. Liqibase have its own dependecies which pull the jars (transitive dependecies) for the liquibase to work. Thank you so much @Ishant choukseDisconnect

© 2022 - 2024 — McMap. All rights reserved.