Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot & Liquibase by Example

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.?
like image 356
smeeb Avatar asked Feb 14 '18 20:02

smeeb


1 Answers

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
like image 177
Janar Avatar answered Sep 18 '22 20:09

Janar