How to start using Liquibase
01.05.2024
15 min
How to create a simple program that will have liquibase migrations
To begin, let’s create a new project via start.spring.io and add the Lombok, Spring Web, Spring Data JPA, Liquibase Migration, and MySQL driver dependencies:
Let’s add a new plugin to pom.xml:
Let’s change our application.properties file so that it contains all the necessary fields for connecting to the database, as well as information about the main liquibase file in the project, in our case it is changelog.xml:
Next, we will create changelog.xml in resources/db/changelog and insert the ready-made template there, which can be found at the link: https://docs.liquibase.com/concepts/changelogs/home.html
And after a little editing, we got the following changelog.xml:
Now let’s create this changelog-v1.0.0.xml file and then we’ll add a changeSet to it, but for now, we’ll just leave it empty:
Let’s create UserEntity, UserRepository, and UserController to interact with users in runtime:
Now let’s create a table in the database, in our case, in MySQL:
Let’s run our program and check how everything works with Postman:
Now let’s add a changeSet to our changelog-v1.0.0.xml file, in which we will indicate that Chloe is a very good person:
After that, restart the program and get a list of all users:
As we can see, our migration worked and thus we changed the description of a certain field.
Using liquibase you can change anything in the database, in this simple example we have shown that you can change the value of a field according to certain characteristics, but liquibase is not limited to this and with its help, you can do many things such as creating and deleting tables, changing data, adding, changing or removing columns and so on.