Spring Boot: Using Liquibase to load test and demo data

Marc Enschede
2 min readMay 18, 2021

--

In one of my previous project we had to preload some test/demo data into our database on the DEV and TST environments, but of course not in the PROD environment. As we deployed the system over and over again on DEV, TST and our local machines we wanted to have that test data loader automatically. In our Spring Boot environment we used Liquibase to load the database schema and we wanted to load the test data using Liquibase as well. So we used Liquibase contexts.

In the first step a pair of Liquibase changesets is defined; the common changeset (without a context) and the dev changeset (annotated with the dev context). If the dev context is defined both changesets are executed, if another context is given (like prod) only the first changeset is executed.

A small warning; if no context is defined at all, the context system will be switched off. So if no context is defined, both contexts are executed. To avoid that you have to define some arbitrary context.

Now, how do we make sure that the right context is deployed to the right stage. We can use spring profiles.

In the application.properties the contexts like this

For the deployment to the DEV environment, we will override the context in the application-dev.profile like this

Testing data migrations

Now imagine our product owner comes in and asks us the add a nickname column and to generate a nickname for existing users as “<name> from nowhere’. Of course we want to test the data migration. Therefor we define a next pair of changesets.

If the prod context is select, the existing data is converted. More interesting is to see what is happening in the dev context. First, the 18s01 and 18s01dev changesets are executed. Then the 18s02 context is executed and as a result, the testdata from the 18s01dev changeset is converted. As a result, we are able to verify if the testdata is converted as expected.

As expected, it is possible to create a more complex situation. For example to have 3 context; prod without preloaded data, tst with test data preloaded and demo to add both test and demo data.

Happy coding!

--

--

Marc Enschede

Java, Spring Boot and Kotlin developer. The backend guy!