Liquibase + JPA Buddy: the problem they solve
A common problem in many projects and frameworks is the management of database structure changes during the life of a project. Reduced to the bare minimum, this problem can be formulated as follows: How can I automate the update of a database based on changes made to project entities?
Let us see how this problem can be solved in a Spring Boot project without having to write SQL scripts and execute them manually. We base this tutorial on JPA Buddy, which integrates with IntelliJ IDEA.
We must then install the IntelliJ IDEA JPA Buddy plugin. In addition, it is necessary to have the Ultimate version of IntelliJ IDEA.
We start with a Spring Boot project. In our case, the basic project is a demo implementation of a Sparkplug primary host: a Spring Boot application implementing the basis of the Sparkplug B v1.0 specification.
The detail of the application here is irrelevant; suffice it to say that it connects to a PostgreSQL database called sparkplugdemo.
We add the Liquibase dependency to the pom.xml:
In the src/main/resources directory we create db/changelog and here a file db.changelog-master.xml. The initial content of the file must be:
Let us now add the reference to Liquibase in the Spring Boot application.properties file:
spring.liquibase.change-log=classpath:/db/changelog/db.changelog-master.xml
spring.liquibase.enabled=true
spring.liquibase.url=jdbc:postgresql://localhost:5431/sparkplugdemo
spring.liquibase.user=[DATABASE_USER]
spring.liquibase.password=[DATABASE_PASSWORD]
In the previous property block, we specify for Liquibase the master changelog file, which in our case is in XML format, and the database connection parameters to be updated by Liquibase.
This way, when the application starts up, it verifies that the database to which it is to connect is correctly updated by a changelog history table. If new changes have been made to the database structure, Liquibase will automatically perform the updates indicated in the various changelogs that have not yet been applied.
Così appare il file con il suo contenuto inizialmente. Afterwards, there will be no need to edit this file manually: JPA Buddy will do it for us.
Let us now open the screen of the JPA Buddy plugin (found under the name JPA Explorer).
From here we can select the +.
We then choose the option Init Changelog.
We use the model as source and configure the database type.
A changelog file will be generated, then a screen similar to the following will appear.
Change the directory and file name. This step is optional; it is important to configure the inclusion of the changelog in the master file created at the beginning, as seen below under Include to.
The directory with the changelog file will then be generated:
The file db.changelog-master.xml will automatically include the new changelog. There is no need to do it by hand.
Our changelog will contain something like the following:
We now apply the changes to the database by returning to the JPA Buddy plugin screen and selecting the icon corresponding to the update.
We will be prompted for the database connection to be updated. Obviously, the database must be present (even if empty). Let us therefore create it before doing so.
After the update, we will see a success message.
If we now modify the entities in our project, it will be sufficient to generate a difference changelog from the JPA plugin, as can be seen in the following screenshot.
Thanks for following this little tutorial, if you still are ‘thirsty’ for tech-pills, don’t miss a single topic on our blog!