Liquibase on Maven with SQL files | Spring Boot Backend #6
HTML-код
- Опубликовано: 6 фев 2025
- In this video I show the usage of Liquibase to manage the database migrations with SQL files. I also show how to configure Liquibase on Maven.
This is the sixth video of a playlist where I show how to create a Spring Boot Backend, how to create a website: • Spring Boot
In this video, I will handle:
how to handle the database migration automatically;
what is Liquibase;
configuration of Liquibase on Maven;
configuration of the Liquibase Maven Plugin;
creation of the Liquibase SQL changesets;
execution of the Liquibase Maven Plugin;
rollbacks with Liquibase.
Repository: github.com/ser...
My NEW eBook: sergiolema.dev...
Blog: bit.ly/47ornJL
LinkedIn: bit.ly/41Nn61q
Facebook: bit.ly/47rc9nh
Track the performance of your APIs: bit.ly/4d7C0ny
Great explanation! I was finding it difficult to follow the documentation on the website of liquibase but this tutorial cleared everything up. You gained a subscriber!
Thanks to you for those words.
Good work Lema family we will support you from Barcelona, Ánimo 😊 !
Muchas gracias!!
Bro, bueno, sencillo y bien explicado. Muchas gracias. Saludos desde Venezuela.
Gracias!!
Thank you very much for your explanation. you're awesome
Thanks to you to take your time to watch and comment my video
Thank you Sergio for the quality of this content. Appreciated!!!
Allow me to ask two question :
1. In a CI environment (different stages = different DB hosts), what is the best approach to go with Liquibase properties file?
2. What are the bests practices to work with multipe files for changesets in case of rollback required?
1. Generally, I have a named property file per environment : db.master.production.xml or db.master.release.xml or db.master.ci.xml which will be selected depending on some environment variable or hardcoded when running liquibase
2. The rollback commands are associated per changeset. A changeset can't be separated in several files. When writing a changeset, ensure you also write the rollback command, this way, they will always be together. And finally, when indicating to liquibase you want to rollback something, you also have to tell about changesets, no matter how many files you have.
And thank you for watching!
Thank you for this tutorial! You purpose me a good formation on Spring Boot
Thank you for your interest!
it's useful for me,thanks
Glad it helped you. Share it on your network, I'm sure some more will find it useful.
Hi Sergio, Your videos are awesome so helpful I here by request to come up with videos on microservice project using spring cloud and deploy to aws using kubernetes
I already have a video about microservices on my pipeline. Thanks for your feedback, i'll try to do the best i can
Hi, I've just started the playlist about Microservices with Kubernetes and Spring Cloud. You can check it here: ruclips.net/p/PLab_if3UBk9-BjDP7Yh4uiy8z0pEd14Tp
Thank you!
Gracias a ti por seguirme!
Hi Sergio,
First of all, thank you for your video tutorial, it was really helpful. However, I have a doubt or maybe I didn't understand something correctly. As I understood, the project executes database procedures during the project compilation phase. I would like these operations to be executed during the deployment of the application instead. How can I achieve that?
Thank you very much.
Thank to you for watching!
In fact, the changesets are applied when running the Maven goal liquibase:update (not at the complication step).
You can run this goal just after deploying the application and before starting the application.
And by using the liquibase-core dependency instead of the Liquibase Maven plugin, the changesets are run when starting the application with mvn spring-boot:run (no need to run a separate goal).
Muy interesante, no entiendo pero quando tengo que crear un nuovo db.changelog-2.0. Es suficiente un solo changelog y poner nuevos changeset para modificar el database? Muchas gracias
Tienes que crear un nuevo db.changelog-X.X.sql a cada vez que quieras añadir modificaciones a la base de datos.
Digamos que empiezas creando la aplicación con la table "peliculas", entonces añades el comando SQL en db.changelog-1.0.sql.
Dias mas tarde, necesitas crear la table "actores", entonces es cuando tienes que crear db.changelog-1.1.sql (o el 2.0, como quieras). Pero en ningún caso puedes modificar el existente db.changelog-1.0.sql, puesto que ese ya pasó, ya se ejecutó.
@@TheDevWorldbySergioLema Muchas gracias Sergio, todo muy claro. Lo que estoy provando es automatizar la creacion del nuovo changelog utilizando liquibase con maven. Por ejempo, añado un nuevo field en mi entity "pelicualas", y utilizando "mvn liquibase:diff" le estoy diciendo de crear un nuovo changelog si encuentra diferencias con l'entity y la tabla ya esistiente. Es buena pratica o mejor crearlo manualmente? Por supesto...me escribido en tu canal, tan bueno para aprender! Gracias!!!
Personalmente, prefiero crear manualmente los ficheros changelog. Pero si liquibase:diff te da buenos resultados, adelante!
Hi, I have 2 questions. (1) Supposed in changelog-master.xml I have 2 sql files (task1.sql, task2.sql) with rollbacks and I just wanted to rollback (be it first or 2nd changeset) a specific changeset to task1.sql, what's the correct command for that? (2) Once the maven project (not spring boot) was compiled into jar, how can the liquibase gets executed?
1/ If you don't have the Pro version of liquibase, you can only rollback sequentially the changesets (not target changesets). You can do it by date, tag or count (docs.liquibase.com/workflows/liquibase-community/using-rollback.html)
2/ I've never done something like that. I'm affraid I can't help you much.
Great to the point explanation.
Now Sergio im looking for how to use Maven cucumber plugin for unit testing database. I used this on a client some years ago:
jenkins pipeline > maven + maven plugin liquidbase + maven plugin cucumber.
I can't find simple examples on internet on oracle cucumber tests, neither details of whats needed to have cucumber working.
--insert 3 records on empty table (maybe pre-dev clean database with all tables but empty of data).
--table dw
id date somecolumn
100 2022--01-01 xpto
101 2022-02-02 abcd
--table staging
id date somecolumn
100 2022-04-18 newvalue
101 2022-02-02 oops
--execute some oracle procedure MERGE staging table into table dw - pk is (id,date)
--describe how table should be after calling procedures
id date somecolumn
100 2022--01-01 xpto
100 2022-04-18 newvalue
Thank you. But i'm not sure about your question. Long time ago I don't use Jenkins but Gitlab. Nevertheless, the concepts are the same.
When I need to prepare the database for some integration tests, I setup the database in the stage/pipeline, then, as a before script, I run the Liquibase Maven goal to create all the necessary schemas. About the data, I prefer to use a setup directly in the unit/integration tests (checkout DBUnit, ruclips.net/video/GDRS6XXs0Oc/видео.html).
I don't know if this answers to your question.
@@TheDevWorldbySergioLema not really ;) Im database guy, our unit testing is different from java or any other "fancy buttons" language. In database theres a non-intrusive way (no need to insert test code or tags inside code), where we just define like i explained a simple text file we define records to be inserted on a database table --> then execution of database procedure --> that we define how all rows and columns in the table are expected as per execution of procedure.
I used couple years ago something called "cucumber" which i think was attached as a maven plugin into a maven project.
Problem today is im trying to setup myself same environment which couple years ao i used as a developer and was great but the ci/cd environment was already fully installed.
I keep googling for maven + cucumber tests and 99.99999999 % of the all searches till now only show - sorry the french - the damm examples using java, python and whatever languages out there.. Im struggling to find examples to test calling a procedure in a database by syupplying some 4 records to be inserted in a table before and then a snapshot of the expected rows on that table.
Hope this clarifies.. i see its not related to your experience, which is based on inserting test tags in middle of unit testing which also written on java etc..
But the method i explained is to test data before and after is correct, as we change database objects.
Ok, I see your problem. Nevertheless, I've never used Cucumber. if you're not fixed to a language, you can run Liquibase (and maybe Cucumber too) without Maven, just as a standalone library. But you will need Java to be installed, as it's based on Java (almost for Liquibase).