Generating Data Model Changes with Liquibase

Upgrading major/minor versions of Broadleaf core or installing/upgrading new modules usually requires the addition of new columns or tables to your database. This guide will walk you through generating these schema migrations against your database platform in order to confidently deploy the upgrade to your QA/Production environments.

If you are not already using Liquibase to automatically run data migrations for you, see Setting up Liquibase.

Create a copy of your existing database into a new schema. This new schema will be referred to as broadleaf-original and will serve as the base for the database diff. The schema that your project is pointed to will be referred to as broadleaf-updated

Go through any code modifications necessary for the project to fully start up. If you are adding a new module, this means add the module to your dependencies. If you are upgrading Broadleaf or dependent modules, this means completing the upgrade from a code perspective. After you have finished starting up the application with Hibernate set to update, you now have a version of the fully updated database (the broadleaf-updated schema your application was pointing to before).

Now it is time to compare broadleaf-original and broadleaf-updated to generate the changelog necessary to move to different environments. Take the liquibase jar from step 1 and execute the diffChangeLog command, passing in the JDBC URLs for the broadleaf-updated as the "reference" connection. The original, unchanged database (broadleaf-original) is the other, non-reference connection properties. The following example is for MySQL. For other databases, you will need to change the --classpath and --driver to your database driver.

This command generates a file called broadleaf-update.xml. This is a Liquibase changelog file that you can give to Liquibase to perform the migration. This file can be converted to SQL by executing the following command, if you prefer your changelogs as sql:

Now that the changelog file has been generated, you can review the the diff of the schema changes and make any adjustments. Most likely, the only tables that should be changed are Broadleaf tables relevant to what you're upgrading, either a specific module or the entire framework.

Apply the changelog to all environments. We recommend having Liquibase run as a Spring bean on startup to ensure all databases are kept in sync, documented here