Use Flyway to manage database versions in Spring boot

Source: Internet
Author: User

Flyway is an open-source database migration Tool. A version control tool similar to a database. The Flyway database modification file is placed by default in the Db.migration folder under Resource and is named in the format V{version_number}__{description}. The execution information for each script is recorded in detail in the Schema_version table of the database. Each time you compile or run, scripts that are not yet recorded are automatically executed according to the records in the Schema_version table, ensuring that the database is updated to the latest. Using Flyway is an effective way to ensure consistency in the database (including the local database) for projects that are in continuous development or in parallel development of multiple people. So how does the Springboot project integrate Flyway?

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. Create the db/migration directory in the project's src/main/resources directory, and in db/ migration Create a versioned SQL script under directory v1__base_version.sql

DROP TABLE IF EXISTS user; CREATE TABLE ' user ' (  ' id ' bigint (' primary key ',  ' name ' varchar (  - ' name ' ,   int (5' age ',  PRIMARY KEY (' id ')) ENGINE=innodb DEFAULT CHARSET=UTF8MB4;

One thing to emphasize here is that version_number must be added in order of increment. Flyway each execution examines all migration scripts, and throws an exception if there is a script that is not executed but Version_number is older than the one that has already been executed.

Finally, in the Spring boot configuration file application.properties, join the database link basic information. You need to create the migration database in advance and give the user root crud permissions.

Spring.datasource.url=jdbc:mysql://127.0.0.1:3306/migration?useunicode=true&characterencoding =utf8&autoreconnect=true&rewritebatchedstatements=true&usessl=falseSpring.datasource.username =Rootspring.datasource.password=rootspring.datasource.driver-class-name= Com.mysql.jdbc.Driver

When you are ready, go to the console to execute MVN clean install-dskiptests, the project will be automatically recompiled, the compilation will check migration footsteps and execute the corresponding script.

2018-Geneva- to Ten: -: -,867INFO databasefactory: --Database:jdbc:mysql://127.0.0.1:3306/migration (MySQL 5.7)2018-Geneva- to Ten: -: -,975INFO dbvalidate: --Successfully validated1Migration (Execution timexx:xx. 016s)2018-Geneva- to Ten: -: the,089INFO jdbctableschemahistory: --Creating Schema History table: ' Migration '. ' Flyway_schema_history '2018-Geneva- to Ten: -: *,081INFO dbmigrate: --current version of the schema ' migration ': << Empty schema >>2018-Geneva- to Ten: -: *,090INFO dbmigrate: --Migrating schema ' migration ' to version1-DemoBase

Enter the database to see if the execution succeeds.

mysql>for thisget a quicker startup with-adatabase changedmysql > show tables; +-----------------------+| Tables_in_migration   |+-----------------------+| flyway_schema_history | | user                  |+-----------------------+ 2 inch Set (0.00 sec)

Later involved in database changes, directly in the db.migration according to the naming rules to join the corresponding SQL or Java script, at compile time will automatically perform the database migration, is not very convenient AH.

  

Use Flyway to manage database versions in Spring boot

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.