Search code examples
spring-bootliquibase

how to call liquibase from java integration test?


I have springboot app with oracle testcontainer. It works, I can connect to db and ran statements against it. Next step is to init db using liquibase.

There is documentation how you can use springboot to init db. If you follow it, it does literally nothing.

Then there is liquibase api which we can use, and if we use source code from link, there will be unimplemented methods I have no idea what they should do( ?? getDatabase() ??), reference to inexisting classes (DbUrlConnectionCommandStep), and there is no mention about which liquibase version is this sample for, so I really don't know what is wrong ...

Can anyone show me how to call liquibase update command from java(or give reference to good article/mwe)? It can be generic api, but springboot focused would be better.


Solution

  • How to init db with liquibase scripts:

    Add gradle/maven dependency:

    Gradle:

    implementation 'org.liquibase:liquibase-core:4.24.0'
    

    Maven:

    <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-core</artifactId>
        <version>4.24.0</version>
    </dependency>
    

    On application.yml configuration set next one parameter:

    spring:
      liquibase:
        enabled: true
        change-log: classpath:db/changelog/db.changelog-master.yaml
    

    Where change-log is the path to change-log file into resources folder of project.

    Change-Log file: Create a db.changelog-master.yaml file into resources->db->changelog folders path

    db.changelog-master.yaml file will contains next one information:

    databaseChangeLog:
      - includeAll:
          path: liquibase/init
    

    Here you have to declare path to sql files also it will be located to folders paths resources->liquibase->init and here into init folder you have to add .sql files.

    And that is all.

    P.S. If you are writing spring-boot integration tests it will automatically up the context and read/reread the liquibase sql files