9

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
  • 27,777
  • 57
  • 250
  • 447

1 Answers1

24

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
Janar
  • 2,623
  • 1
  • 22
  • 32
  • Thanks @Janar (+1) just one quick question about those `` operations. Are you saying I can add them to the`db.changelog-master.yaml` file? I found [evidence here on SO](https://stackoverflow.com/a/15437697/4009451) that you can't include files inside of YAML files. Any ideas? Thanks again! – smeeb Feb 15 '18 at 03:06
  • 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) – Janar Feb 15 '18 at 08:31