This article introduces the use of FlyWay for SpringBoot to enable database migration. It is shared with you. The details are as follows:
First, let me first understand how FlyWay works.
The easiest solution is to point Flyway to an empty database.
It will try to find its metadata table. When the database is empty, Flyway will not find it, but create it. You now have a database with a single empty table named SCHEMA_VERSION:
This table will be used to track the state of the database. After that, Flyway will start scanning the application's file system or classpath for migration. They can be written in Sql or Java.
Then sort the migrations by their version number and apply in order:
As each migration is applied, the metadata table will be updated accordingly:
schema_version
With the metadata and initial state in place, we can now talk about migration to newer versions.
Flyway will scan the application's file system or classpath again for migration. Check migrations against metadata tables. If the version numbers are lower than or equal to the version number marked as the current version, they are ignored.
The remaining migrations are pending migrations: available but not applied.
Then, they sort by version number and execute in turn:
This metadata table is updated, so:
schema_version
That's it! Whenever you need to develop a database, whether it is structure (DDL) or reference data (DML), just create a new migration with a version number higher than the current version. The next time Flyway starts, it discovers and upgrades the database accordingly.
2. The use of FlyWay in SpringBoot
One way is to set the hibernate.hbm2ddl.auto property to create, create-drop, or update via the Spring Boot's spring.jpa.hibernate.ddl-auto property. For example, to set hibernate.hbm2ddl.auto to create-drop, we can add the following content in application.yml:
spring: jpa: hibernate: ddl-auto: create-drop
However, this is not ideal for production environments, because every time the application restarts the database, Schema will be emptied and rebuilt from scratch. It can be set to update, but even so, we don't recommend using it for production environments.
There is another way. We can define Schema in schema.sql. On the first run, there is no problem doing this, but then every time the application is started, this initialization script fails because the data table already exists. This requires extra care when writing initialization scripts and not repeating the work that has been done.
A better option is to use the database migration library. It uses a series of database scripts and records which have been used and does not use the same script multiple times. Each deployment package of an application contains these scripts, and the database can be consistent with the application. Spring Boot provides automatic configuration support for two popular database migration libraries.
When you want to use one of these libraries in Spring Boot, you just need to add the corresponding dependencies to the project and write a script. Let's start with Flyway.
1. Use Flyway to define the database migration process
Flyway is a very simple open source database migration library that uses SQL to define migration scripts. The idea is that each script has a version number, and Flyway will execute these scripts in sequence to get the database to the desired state. It also records the executed script status and will not be repeated. Here in the Read List application, we start with an empty database without data tables and data. Therefore, this script needs to create Reader and Book tables first, including foreign key constraints and initialization data. Listing 8-2 of the code is a Flyway script from an empty database to an available state.
Flyway database initial script
create table Reader (id serial primary key,username varchar(25) unique not null,password varchar(25) not null,fullname varchar(50) not null); create table Book (id serial primary key,author varchar(50) not null,description varchar(1000) not null,isbn varchar(10) not null,title varchar(250) not null,reader_username varchar(25) not null,foreign key (reader_username) references Reader(username)); create sequence hibernate_sequence;insert into Reader (username, password, fullname)values ('craig', 'password', 'Craig Walls'); As you can see, the Flyway script is SQL. What makes it work is its location and file name in the Classpath. Flyway scripts all follow a naming specification, containing version numbers, as shown in Figure 8-1.
All Flyway scripts have names that start with capital letter V, followed by the script's version number. Followed by two underscores and a description of the script. Since this is the first script in the entire migration process, its version is 1. The description can be very flexible and is mainly used to help understand the purpose of the script. Later we need to add a new table to the database, or add a new field to the existing data table. You can create another script with the version number 2. The Flyway script needs to be placed under the /db/migration path relative to the application Classpath root path. Therefore, in the project, the script needs to be placed in src/main/resources/db/migration. You also need to set spring.jpa.hibernate.ddl-auto to none, so that Hibernate does not create data tables. This is related to the following content in application.yml:
spring:jpa:hibernate:ddl-auto: none
All that's left is to add Flyway as a project dependency. In Gradle, this dependency looks like this:
compile("org.flywaydb:flyway-core")In the Maven project, it looks like this:
<dependency><groupId>org.flywayfb</groupId><artifactId>flyway-core</artifactId></dependency>
After the application is deployed and run, Spring Boot will detect the Flyway in the Classpath and automatically configure the required beans. Flyway will view the scripts in /db/migration in turn, and run these scripts if they have not been executed. After each script is executed, write a record to the schema_version table. The next time the application is started, Flyway will first look at the records in schema_version and skip those scripts.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.