If you have not read the above content, you can read it if you are interested. In the above article using JdbcTemplate, the user table is mainly added, deleted, modified and checked through JdbcTemplate provided by spring. When implementing this example, we created the user table in MySQL in advance. We often use the process of creating tables when actually developing the system, but there has always been a problem. Since the program version of a system is well controlled through git, and the database structure does not. Even if we version statements through Git, how to manage the version in the databases of each environment? Next, we will learn how to use Flyway to manage database versions in Spring Boot through this article.
Introduction to Flyway
Flyway is a simple open source database version controller (convention is greater than configuration), which mainly provides migrate, clean, info, validate, baseline, repair and other commands. It supports SQL (PL/SQL, T-SQL) and Java methods, supports command line clients, etc., and also provides a series of plug-in support (Maven, Gradle, SBT, ANT, etc.).
Official website: https://flywaydb.org/
This article does not introduce Flyway's own functions too much. Readers can obtain more information by reading official documents or using search engines. Below we will talk about the application in Spring Boot application in detail, how to use Flyway to create databases and check structure inconsistently.
Try it out
Below we can complete the processing by using the example in the article using JdbcTemplate. Readers can also use any project related to data access to experiment with the following content:
The first step is to add flyway's dependency in pom.xml:
<dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>5.0.3</version></dependency>
The second step is to create a versioned SQL script according to Flyway's specifications.
Create a db directory under the src/main/resources directory of the project
Create a versioned SQL script V1__Base_version.sql in the db directory
DROP TABLE IF EXISTS user ;CREATE TABLE `user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'primary key', `name` varchar(20) NOT NULL COMMENT 'name', `age` int(5) DEFAULT NULL COMMENT 'Age', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
The third step is to configure the location of the SQL script to be loaded in the application.properties file. The result created in the second step is as follows:
flyway.locations=classpath:/db
The fourth step is to execute unit test ApplicationTests. At this time, we can see the following information in the log:
INFO 82441 --- [main] ofcore.internal.util.VersionPrinter : Flyway Community Edition 5.0.3 by BoxfuseINFO 82441 --- [main] ofcinternal.database.DatabaseFactory : Database: jdbc:mysql://localhost:3306/test (MySQL 5.7)INFO 82441 --- [main] ofcore.internal.command.DbValidate : Successfully validated 1 migration (execution time 00:00.022s)INFO 82441 --- [main] ofcore.internal.command.DbValidate : Successfully validated 1 migration (execution time 00:00.022s)INFO 82441 --- [main] ofcisJdbcTableSchemaHistory : Creating Schema History table: `test`.`flyway_schema_history`INFO 82441 --- [main] ofcore.internal.command.DbMigrate : Current version of schema `test`: << Empty Schema >>INFO 82441 --- [main] ofcore.internal.command.DbMigrate : Migrating schema `test` to version 1 - Base versionWARN 82441 --- [main] ofcore.internal.sqlscript.SqlScript : DB: Unknown table 'test.user' (SQL State: 42S02 - Error Code: 1051)INFO 82441 --- [main] ofcore.internal.command.DbMigrate : Successfully applied 1 migration to schema `test` (execution time 00:00.128s)
Flyway monitors that it is necessary to run a version script to initialize the database, so it executes the V1__Base_version.sql script, thus creating the user table, which allows a series of unit tests (CRUD operations on the user table) to pass.
In the fifth step, we can continue to execute the unit test, and at this time we will find that the log output is different from before:
INFO 83150 --- [main] ofcore.internal.util.VersionPrinter : Flyway Community Edition 5.0.3 by BoxfuseINFO 83150 --- [main] ofcinternal.database.DatabaseFactory : Database: jdbc:mysql://localhost:3306/test (MySQL 5.7)INFO 83150 --- [main] ofcore.internal.command.DbValidate : Successfully validated 1 migration (execution time 00:00.031s)INFO 83150 --- [main] ofcore.internal.command.DbValidate : Successfully validated 1 migration (execution time 00:00.031s)INFO 83150 --- [main] ofcore.internal.command.DbMigrate : Current version of schema `test`: 1INFO 83150 --- [main] ofcore.internal.command.DbMigrate : Schema `test` is up to date. No migration necessary.
Since the initialization script has been executed in the fourth step, this time the execution did not execute the V1__Base_version.sql script to rebuild the user table.
In the sixth step, we can try to modify the length of the name field in the V1__Base_version.sql script, and then run the unit test, we can get the following error:
ERROR 83791 --- [main] osboot.SpringApplication : Application startup failedorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Validate failed: Migration checksum mismatch for migration version 1-> Applied to database : 466264992-> Resolved locally : -270269434
Due to changes in the initialization script, Flyway verification failed. It is believed that the current V1__Base_version.sql script is different from the last executed content, prompting an error and terminating the program to avoid causing more serious data structure damage.
Summarize
So far, the content of this article has come to an end. Due to the length of the blog post, I didn’t say much about the use of Flyway in more detail. This article is mainly used as a stepping stone to help and guide individuals or teams who are using Spring Boot to do better in database version control. As for more in-depth applications, please read the official documents for reference and study.
Code of this article: Github: https://github.com/dyc87112/SpringBoot-Learning/
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.