Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (15) Spring boot uses Flyway to manage database versions

Source: Internet
Author: User

Flyway Introduction

Flyway is a simple open source database version controller (contract greater than configuration), mainly provides migrate, clean, info, validate, baseline, repair and other commands. It supports SQL (PL/SQL) and Java, supports command line clients, and provides a range of plug-in support (Maven, Gradle, SBT, ant, etc.).

Official website: https://flywaydb.org/

In this paper, Flyway's own function is not too much to introduce, readers can read the official documents or use search engines to obtain more information. Let's talk about the application in the spring boot application, how to use Flyway to create a database and check for structural inconsistencies.

Give it a try.

We can do this by using the example in the JdbcTemplate article. Readers can also experiment with any data-access-related engineering to do the following:

  • The first step is to pom.xml increase the dependency of flyway in:
    <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 the Flyway specification.
  • src/main/resourcesCreate a directory under the project directory db
  • dbCreate a versioned SQL script in the directoryV1__Base_version.sql
    DROP TABLE IF EXISTS user; CREATE TABLE ' user ' (  ' id ' bigint) NOT NULL auto_increment COMMENT ' primary key ',  ' name ' varchar ' NOT null COMMENT ' last name Name ',  ' age ' int (5) Default NULL COMMENT ' ages ',  PRIMARY KEY (' id ')) engine=innodb default charset=utf8mb4;
  • The third step is to application.properties configure the location of the SQL script Flyway to load in the file. The results created by the second step are configured as follows:
    flyway.locations=classpath:/db
  • Fourth step, perform unit testApplicationTests, we can see the following information in the log:
    INFO 82441---[main] o.f.core.internal.util.versionprinter:flyway Community Edition 5.0.3 by Boxfuseinfo 82441---[ Main] o.f.c.internal.database.databasefactory:database:jdbc:mysql://localhost:3306/test (MySQL 5.7) INFO 82441---[ Main] o.f.core.internal.command.dbvalidate:successfully validated 1 migration (Execution time 00:00.022s) INFO 82441 ---[main] o.f.c.i.s.jdbctableschemahistory:creating Schema history table: ' Test '. ' Flyway_schema_history ' INFO 82 441---[main] o.f.core.internal.command.dbmigrate:current version of the schema ' test ': << Empty schema >> INFO 82441---[main] o.f.core.internal.command.dbmigrate:migrating schema ' test ' to version 1-base Versionwarn 8 2441---[main] o.f.core.internal.sqlscript.sqlscript:db:unknown table ' Test.user ' (SQL state:42s02-error code:1 051) INFO 82441---[main] o.f.core.internal.command.dbmigrate:successfully Applied 1 migration to Schema ' test ' (ex Ecution time 00:00.128s)

    Flyway monitors the need to run version scripts to initialize the database, so a V1__Base_version.sql script is executed to create the user table, which allows a series of unit tests (CRUD operations on the user table) to pass.

  • In the fifth step, we can proceed with the unit test and we will find that the log output is different from the previous:
    INFO 83150---[main] o.f.core.internal.util.versionprinter    : Flyway Community Edition 5.0.3 by Boxfuseinfo 83150--- [Main] o.f.c.internal.database.databasefactory  : database:jdbc:mysql://localhost:3306/test (MySQL 5.7) INFO 83150---[main] o.f.core.internal.command.dbvalidate     : Successfully validated 1 migration (Execution time 00:00.031s) INFO 83150---[main] o.f.core.internal.command.dbmigrate      : Current version of the schema ' test ': 1INFO 83150- --[main] o.f.core.internal.command.dbmigrate      : Schema ' Test ' is up to date. No migration necessary.

    Since the initialization script was executed at the fourth step, the execution did not execute the V1__Base_version.sql script again to rebuild the user table.

  • Sixth step, we can try to modify V1__Base_version.sql the name field length in the script, and then run the unit test, at this point we can get the following error:
    ERROR 83791---[main] o.s.boot.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 ver Sion 1-> applied to database:466264992-> resolved locally    :-270269434

    Due to the initialization script changes, the Flyway checksum failed, that the current V1__Base_version.sql script and the last execution of the content, prompt for error and terminate the program, so as not to cause more serious data structure damage. SOURCE Source

Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (15) Spring boot uses Flyway to manage database versions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.