Spring Boot 使用 flyway

Spring Boot version 2.1.17

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.17.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

pom.xml introduces the dependency of flyway

<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
</dependency>

application-dev.yml configure flyway (database: local_test_db)

spring:
  datasource:
    druid:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/local_test_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&allowPublicKeyRetrieval=true&verifyServerCertificate=false&useSSL=false&autoReconnect=true&failOverReadOnly=false
      username: root
      password: 12345678
  flyway:
    # 如果启动的时候需要flyway管理sql脚本的话,将enabled设置为true
    enabled: true
    # 如果数据库不是空表,需要设置成 true,否则启动报错
    baseline-on-migrate: true
    # 验证错误时 是否自动清除数据库 高危操作!
    clean-on-validation-error: false

Create a new folder migration in the resources directory to store sql scripts.

The directory where the sql file is located: resources/db/migration ( no configuration is required , Springboot will automatically identify the files in the migration directory)

Operation 1: Import V1.0.1__schema.sql  (create table user)

DROP TABLE IF EXISTS user;

CREATE TABLE IF NOT EXISTS user (
	id integer primary key auto_increment,
    name VARCHAR(32),
    address VARCHAR(64)
)
ENGINE = InnoDB
COMMENT = 'Table User';

After the above sql statement is executed successfully, 2 tables will appear in the database

Operation 2: Import V1.0.2__schema.sql (create table selected_track_index)

-- Table selected_track_index
DROP TABLE IF EXISTS selected_track_index;

CREATE TABLE IF NOT EXISTS selected_track_index (
    id INTEGER PRIMARY KEY AUTO_INCREMENT,
    key_name VARCHAR(64),
    key_value VARCHAR(64)
)  ENGINE=INNODB COMMENT='Selected Tracks.';

Run Springboot Application, after success, refresh Tables will show 3 tables, including the newly created table "selected_track_index".

Operation 3: Import V1.0.3__insert_data.sql (insert 1 piece of data in the user table)

3.1 Insertion error

INSERT INTO `user` (`id`, `name`, `address`) VALUES
(1 'Bill', 'Shanghai'); # 1后面少量逗号,sql语句错误。

The insert sql statement has a syntax error, and the execution of V1.0.3__insert_data.sql fails. But the flyway_schema_history table in the database still changes.

The value of success is 0, which means the execution failed. At this time, update V1.0.3__insert_data.sql and correct the sql statement.

INSERT INTO `user` (`id`, `name`, `address`) VALUES
(1, 'Bill', 'Shanghai');

Although the sql statement has been corrected, an error will still be reported when running SpringBoot Application.

Remarks: ``It is necessary, otherwise an error will be reported. ( Flyway will report an error when executing the following sql statement )

INSERT INTO user (id, name, address) VALUES
(1, 'Bill', 'Shanghai');

3.1 Insert correctly

Problem solving steps:

1. Delete the third error record in flyway_schema_history .

delete  FROM flyway_schema_history WHERE installed_rank = 3;

2. Run the Springboot project, V1.0.3__insert_data.sql is successfully executed.

The value of success is 1, which means the execution was successful. User table successfully inserted data

Small summary: When running an error, delete the error record in flyway_schema_history and re-run the project.

reference

1.  Introduction to the use of flyway

2.  Detailed explanation of Flyway and Springboot integration Flyway (transfer)

 

Guess you like

Origin blog.csdn.net/A_bad_horse/article/details/113772268