May 4, 2021
Database Migrations for Java Applications
See Liquibase in Action
Accelerate database changes, reduce failures, and enforce governance across your pipelines.
Liquibase offers a powerful open source database migration tool for Java apps. It brings structure and confidence to developers and DBAs that need to easily create and track database schema changes. Liquibase supports most common databases including PostgreSQL, MariaDB, Oracle, SQL Server, DB2, MySQL, H2, and many more. Migrations can be written in plain old SQL or you can use the abstracting power of XML, YAML, or JSON. Changes can be executed through an API, Maven plugin, GitHub Action, Hibernate extension, Spring Bean, or the command-line tool.
And hey, Liquibase is written in Java. It’s a no-brainer to use and you have the option to use open source or get expert support and advanced features down the road.
Why database migrations are a good idea
Agile and DevOps processes are key in making application development faster and easier. You wouldn’t develop app code without version control — the same should be true for database changes. (If you need more convincing, I suggest reading Martin Fowler’s in-depth Evolutionary Database.)
If you’ve ever worked on a project where all database changes were manually created, tracked, reviewed, and deployed, you probably already understand the value of using a tool to help you keep track of these changes. It quickly gets painful. Over time, your team will frequently be asking many questions:
- What is the state of the database in the Staging environment?
- Was this script successfully applied?
- Who made this change?
- Has this hotfix in production been deployed to the lower environments?
- How can I set up a new database instance with the latest state?
Using Liquibase helps answer all of these questions. So let’s jump into how to start using Liquibase with your Java application. One of the most common ways is via Spring Boot.
Using Liquibase with Spring Boot
Spring Boot includes Liquibase by default. To execute Liquibase to update your database when your application starts, simply update your application properties file with a single line:
spring.liquibase.change-log=classpath:db/changelog/db.changelog-main.xml
This property, spring.liquibase.change-log, tells Spring Boot to execute Liquibase on startup to update the database. The value of the property tells Spring Boot where the Liquibase Changelog is located. In this case, the file db.changelog-main.xml is located in src/main/resources/db/changelog.
Of course, there’s more than one way to execute Liquibase with your Java application. Here are some resources to learn more about using Liquibase with Spring Boot: