Spring Boot & Liquibase by Example

15,203

The db/changelog/db.changelog-master.yaml file is the one executed on application startup when using default configuration. In that file you can have the sequential SQL changes as well as inclusions to other files. For example the file could contain inclusions like this (xml syntax)

<include file="migrations/001-schema.sql"/> 
<include file="migrations/002-init.sql"/> 
<include file="migrations/003-changing-account-types.sql"/> 

and you would have the configuration you wanted.

About your second question - yes, they are applied at startup. If it runs on a cluster of nodes, they will each check the status and apply the changes to database if they aren't already applied(the databasechangelog and databasechangelock tables are used for that and they make sure the changes are only applied once)

example for yaml syntax

databaseChangeLog:
- include:
    file: migrations/001-schema.sql
- include:
    file: migrations/002-init.sql
- include:
    file: migrations/003-changing-account-types.sql
Share:
15,203

Related videos on Youtube

smeeb
Author by

smeeb

Updated on July 23, 2022

Comments

  • smeeb
    smeeb almost 2 years

    Spring Boot and MySQL here. Trying to get my Spring Boot app to use Liquibase for my DB migrations and see in the docs that Spring Boot has built-in support for Liquibase.

    However after reading those docs, I'm left with several related concerns:

    • What is the fundamental purpose of the db/changelog/db.changelog-master.yaml file? Is it to store Liquibase configurations (that dictate how Liquibase behaves), or is that where I'm supposed to put the actual, sequential SQL changes (the "migrations") themselves?
      • Ideally I'd like to have a src/main/resources/migrations directory and store my migration changes as individual SQL files, like so:
      • src/main/resources/migrations/001-schema.sql
      • src/main/resources/migrations/002-init.sql
      • src/main/resources/migrations/003-changing-account-types.sql
      • ...etc. Is it possible to configure Liquibase to do this via Spring Boot?
    • When will Spring Boot run these Liquibase migrations? At app startup? What if the Spring Boot app actually runs on a cluster of nodes (say 5 nodes behind a load-balanced URL)? Will Spring Boot run Liquibase run 5 times, once on each node? Or does it somehow sense that one node is the "master migrator", etc.?
  • smeeb
    smeeb over 6 years
    Thanks @Janar (+1) just one quick question about those <include file .../> operations. Are you saying I can add them to thedb.changelog-master.yaml file? I found evidence here on SO that you can't include files inside of YAML files. Any ideas? Thanks again!
  • Janar
    Janar over 6 years
    yes, you can add them to db.changelog-master.yaml file. That answer is either outdated or does not apply to liquibase. I'll change the example of the syntax to .yaml file one (the previous one was for .xml file)