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:
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 . Here you need to fill out and select what you need:- 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.
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: 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:
- 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.
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