An article from a series about creating a Java project (links to other materials are at the end). Its goal is to analyze key technologies, the result is to write a telegram bot. In this part, we try to launch SpringBoot and Flyway. The minimum amount of theory, as you like))) We omit the final comparison of Flyway/Liquibase for an indefinite period of time and get to the point. And even so it has already dragged on. In order not to describe Flyway twice, I decided to immediately add it to our future JRTB project.
multi-faceted, multi-modular:
Here you need to fill out and select what you need:
Now let's add the modules. We need to find integration with Flyway. You can also add something related to MySQL and Spring Data. Let's add another lombok (this is a very necessary thing, just trust me for now :D)) To do this, click on ADD DEPENDENCIES ... and select everything you need:
This is how we add Flyway.
Lombok... And so on. As a result, we get:
Huh... we filled out everything)) Now click GENERATE... and that's it - the archive with the generated project is ready :) There is also such a cool thing as SHARE... , which gives you a link to the page that you just filled out. That is, here is the one I generated . And even if something goes wrong, you can always check using the link. Next, we need to link the created project to the Git repository, so we clone the created springboot-flyway-demo project and download it via IDEA. To do this, you need to open the idea and select File -> New -> Project from Existing Sources... :
Now add the URL and click Clone . The next step is to transfer the internals of the generated project inside the one we cloned. Confused? I will show you now. I unzipped it and received the following set of files:
These need to be transferred to the cloned project. As in the previous article, let's add pom.xml as a maven project:
Now we are interested in looking at what was generated for us. If you open all the folders in src and further, you will see the usual hierarchy in Maven projects, which we discussed in the previous article . For those who haven't read it, read it!
It can be seen that we have an Application class, and our SpringBoot application will be launched using it. Thanks to the Maven plugin for SpringBoot, we now have the task we need for Maven, namely spring-boot:run. Where can we find this information? On the right, opening the Maven plate and our project:
There will be an error, and we won’t be able to read it, we’ll see something like this:
To get more information, for speed we can run the main method of the Application class:
And then we’ll see the real reason:
Here already There is more information and you can do something with it. What's wrong? We have dependencies associated with the database, and therefore we need to provide settings for connecting to it. To do this, we google and find that we need to add the following configurations to application.properties:
Now we need to add at least one migration. To correctly create a migration, you need to take the following template: V<VERSION>__<NAME>.sql Using this template, we will create a migration file named V00001__Create_country_table.sql in the appropriate folder: /src/main/resources/db.migration/ . Let's create a country table in it. Let's take the script from the second article about the database .
Before starting, let's go in and create a database for work: flyway_demo_db. Let's do this through MysqlWorkbench:
Now we can run the main method again:
Everything worked out, but since we don’t have anything in the project yet, it finished working. However, it is clear from the logs ( read what logs are ) that:
As I expected, a migration took place, during which the country table was created and the flyway_schema_history table appeared, which stores information on migrations. Let's go further and see what records there are (and whether there are any at all). $SELECT * FROM flyway_schema_history;
Here is the recording, the only one. It contains a lot of interesting data. Version, description of the migration, what type of SQL (and maybe also XML), the name of the script itself, checksum ( this is something like a hashcode, which is used to check whether the migration has changed or not. This is done in case we carried out a migration in the database and then it was corrected: this cannot be done, all changes are made only through a new migration and to prevent this from happening, the check amount monitors this ), user sql name, migration processing date, execution time and result (successful or unsuccessful) . A migration written once should not be changed in the future. Even if there is a defect in it. All changes take place only through a new migration. It is very important. To show what will happen, let's change our script a little and try to run it again. Let's add one entry to the country table in our migration:
and run the main method and this is what we get:
As I expected, flyway recognized that the script had been changed and did not perform the migration. In some cases, it may actually be necessary to run an updated migration, and in order for flyway to skip this, you need to delete the entry in the flyway_schema_history table and then run the updated migration. This is not normal behavior and should not be so, but you also need to know about this method of solving the problem. We dealt with the first migration. Now I would like to add another migration, with data about countries, as was in the article about the database: file V00002__Add_test_data_to_country.sql
And let's run the main method again:
From the logs it is clear that before the migration started, the database was in version 00001, so all migrations after this version. Next, version 00002 was launched and was successful. Let's check, or do you already believe me that three records in country will already be in the database?)) I would check, so proof:
Something like this. If you run the project again, flyway will simply skip rolling out migrations, since the database fully complies with the required version.
What do we need to do as part of this?
- Launch a SpringBoot application based on Maven.
- Add Flyway there: fortunately, they are easily integrated.
- Add a schema for the tables that we have in the example database.
What is flyway
To use something, you first need to figure out what it is and why. Flyway is a database version control tool. The words are well-known, but somehow the understanding has not been added, right? Let's try to describe the problem that flyway solves. Let's say we have a project. Like everything in our world, it is not perfect, so it was not possible to plan and draw up the final version of the project. Every time certain unaccounted nuances appear. The project uses a database in its work. Of course, if the project changes, the database structure may also change. Let's say we add a new field for one of the entities in our project. How to do it?- Add this field to our entity, update everything so that the business logic works.
- Update the database. The only possible way is to do it manually. To do this, you need to go in and register the necessary sql script.
- But if we have more than one place where we deploy our project, then does this need to be done in each of them?
- and if we want to go back, how do we know exactly what state the database structure is in now?
- How will we be sure that the database change was successful?
- How can I get the opportunity to track all the database changes that took place on the project?
Launch SpringBoot + Flyway
What is Spring Boot
What are we launching?... To understand what and why we are doing, you need to decide what SpringBoot is. First, let's quickly (well, very quickly) talk about Spring . At the moment, it is the de facto industry standard in developing server applications in Java. Standard of what? How can I explain this to you? Spring is the skeleton of the application, onto which we then throw the “meat” - our business logic. With the help of Spring (hereinafter I will use this tracing paper so as not to waste time switching languages :D)) Spring gives us a start from which we begin to do everything. It is- Do you want to work with a database? Do you want relational? Do you want non-relational? Here we are with Spring Data.
- Do you want to work with http requests? Here you go, Spring web (Spring MVC).
- Do you need a container for all your objects in one place? Here's Spring Core.
- Do you need to set up security on a project so that there are different roles and chain of command? Spring Security.
- Just when you thought it would be nice to have such a thing, it turns out that Spring already has what you need, and it integrates quickly and easily.
Launching SpringBoot
Since we already understood what Maven is, let's create a new project for our needs. To do this, you just need to go to a website specially created for this matter. It's called Spring Initializr .!["Java project from A to Z": SpringBoot + Flyway - 2](https://cdn.javarush.com/images/article/6c7e470c-dd6d-4f7f-ac1d-03988f1b02d6/1024.jpeg)
- The project build tool is gradle or maven. As you can see, Ant is no longer even mentioned. This is a good hint about which build tools are worth your time.
- The language you can write in is java, kotlin, groovy. Everything is simple here: they are all JVM-like and easily run Java code. By the way, it’s worth looking at Kotlin. Groovy has frankly become uninteresting (there was a time when they switched to grooves, but it quickly passed).
- Spring version... Here you need to understand that the versions of the main part of Spring and its modules are consistent.
- Project data. I have already described these things.
- We choose which archive will be collected - Jar or War.
- Well, the Java version of our favorite. And recently there have been a lot of these versions... They waited for years, and now there are two a year.
- Maven - it’s not for nothing that we talked to you about this earlier.
- Java is our darling :D
- Let's take version 2.2.11. Why not the newest one? Because the newer it is, the greater the chance that there may be some bugs there. For us, it doesn’t matter which version, but the older one will be more reliable. Therefore, we choose 2.2.11.
- Group: com.github.javarushcommunity
- Artifact: springboot-flyway-demo
- Name: SpringBoot + Flyway Demo
- Description: Project demonstrates integration between SpringBoot and Flyway . (Yes, the ability to write documentation is an important part of development :))
- Package name: com.github.javarushcommunity.springbootflywaydemo . Here they will immediately create a basic package for us with a class that will launch our application.
- Packaging: Jar
- Java: 8. Let's not go ahead of the locomotive and take the good old eight. Why not 11? What for? For our example, I don’t see the point.
!["Java project from A to Z": SpringBoot + Flyway - 3](https://cdn.javarush.com/images/article/5246a501-4cd3-4676-b7c1-daa40095db02/800.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 4](https://cdn.javarush.com/images/article/698cd505-dfd6-4f98-82a4-91b30a594ded/1080.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 5](https://cdn.javarush.com/images/article/750b5a4d-6ffd-4884-bc93-f5bb7beaed38/1080.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 6](https://cdn.javarush.com/images/article/c607ca61-d95c-4eb4-9cd7-e60c3419e889/800.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 7](https://cdn.javarush.com/images/article/4d8f6a79-a80e-4511-afb9-a03b4fa6e665/1024.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 8](https://cdn.javarush.com/images/article/ba6a8d83-6598-4351-a088-afd454e11516/800.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 9](https://cdn.javarush.com/images/article/0042e996-a164-4d0c-b5f4-1a7f9d5f0913/512.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 10](https://cdn.javarush.com/images/article/41d9959c-cf9a-4822-89da-f73bc6d8363d/512.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 11](https://cdn.javarush.com/images/article/ae997f33-00de-4c77-92ad-38ce59acc96b/512.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 12](https://cdn.javarush.com/images/article/489e0f24-a8f5-4273-968f-bc50c4030830/1080.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 13](https://cdn.javarush.com/images/article/02a2408a-4eb2-4c2c-8646-d2eb4c18e75c/800.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 14](https://cdn.javarush.com/images/article/39a661fd-61ab-4125-a8b3-67c2b6253ea4/1080.jpeg)
spring.datasource.url=jdbc:mysql://localhost:3306/flyway_demo_db
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
We run the main method again and get: !["Java project from A to Z": SpringBoot + Flyway - 15](https://cdn.javarush.com/images/article/aaaa0ee4-7790-4c60-892c-2d94577adad2/1080.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 16](https://cdn.javarush.com/images/article/800b2f5e-f7dd-49ab-9992-a77d437e3cce/1080.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 17](https://cdn.javarush.com/images/article/035bd6fb-4e11-4cee-9238-0b28da78ebfd/1080.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 18](https://cdn.javarush.com/images/article/0c3f684e-2710-472a-bc02-2ada5b58e7e9/1080.jpeg)
- Successfully connected to the database.
- The migration has been validated and everything is ok.
- Flyway has created a table to manage migrations.
- And what migration 00001 started - the creation of the country was successful.
!["Java project from A to Z": SpringBoot + Flyway - 19](https://cdn.javarush.com/images/article/9e9c928a-fddd-4d7d-b6d2-aca9ee36b2d1/1024.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 20](https://cdn.javarush.com/images/article/f2602d4b-22d4-429b-96e3-43f0de11e710/1024.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 21](https://cdn.javarush.com/images/article/89f91cfe-3c9c-4715-8446-3289acb2a0a8/1080.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 22](https://cdn.javarush.com/images/article/564653d5-9145-4d3b-a12c-dca0b6b3e443/1080.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 23](https://cdn.javarush.com/images/article/86cd9ab4-4c11-4bb5-a113-51db21e678a9/1080.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 24](https://cdn.javarush.com/images/article/7f8d8fa6-fd73-4004-b2d7-160d5e7de3dc/1080.jpeg)
!["Java project from A to Z": SpringBoot + Flyway - 25](https://cdn.javarush.com/images/article/6806330f-aa25-46b8-a184-dfed4a7c59eb/256.jpeg)
Conclusion
This time we learned how to understand and use a database migration tool in conjunction with SpringBoot. This information is necessary to understand what a database version control tool is, using Flyway as an example. Friends, the source code of the project that I showed is published in our organization on Github . If you like the example, give it a star , and I will understand that my work is useful and really worth continuing. Traditionally, I suggest subscribing to my Github account . Through him I conduct all my work on open source and all those demo projects that invariably accompany my articles. Thanks everyone for reading. Next time we will write our application. There will be some necessary theory on Docker in the future, but we will heavily dilute it with practice.useful links
Today there are not many useful links. Pay attention to Evgeniy's video, it's really worth it!- Website for creating projects on Spring
- Evgeniy Borisov — Spring builder
- Documentation in Spring for Flyway
GO TO FULL VERSION