SpringBoot - 使用 Flyway 和 H2 数据库进行测试

Ale*_*her 5 testing h2 acceptance flyway spring-boot

我正在使用 Cucumber 编写验收测试,并且我想使用 H2 数据库进行测试。

application-test.properties 如下所示:

server.port:8090

spring.jpa.database=H2
spring.database.driverClassName=org.h2.Driver
spring.datasource.url:jdbc:h2:mem:database_user;DB_CLOSE_ON_EXIT=FALSE
flyway.locations=classpath:resources/db/migration
flyway.enabled=true

spring.datasource.username:SA
spring.datasource.password:

spring.h2.console.enabled=true
spring.jpa.show-sql=true

security.basic.enabled:false
spring.application.name=userService
Run Code Online (Sandbox Code Playgroud)

在目录 resources/db/migration 中,我有一个包含以下脚本的 sql 文件:

create table user_image(
 id int unsigned not null AUTO_INCREMENT,
 url varchar(1000) not null,
 s3_key varchar(200) not null,
 PRIMARY KEY (id)
);


create table user (
    id int unsigned not null AUTO_INCREMENT,
    email varchar(50) not null,
    password varchar(100) not null,
    first_name varchar(50) not null,
    last_name varchar(50) not null,
    description varchar(50),
    phone_number varchar(50),
    user_image_id int unsigned,
    need_refresh_pass boolean not null,
    PRIMARY KEY (id),
    CONSTRAINT fk_user_image FOREIGN KEY (user_image_id)
    REFERENCES user_image(id)
);
Run Code Online (Sandbox Code Playgroud)

但是当我运行测试时,H2 使用默认格式创建模式,而不是使用脚本:

在此输入图像描述

正如您所看到的,所有 VARCHAR 都是以 255 大小创建的,而不是实际值。

您能帮我将 Flyway 与 H2 集成吗?

谢谢!

cod*_*key 1

Flyway 尚未发现任何迁移,Hibernate 随后从您的实体创建了表。

位置应该是:

# spring-boot 2.x
spring.flyway.locations=classpath:db/migration

# spring-boot 1.5.x
flyway.locations=classpath:db/migration
Run Code Online (Sandbox Code Playgroud)

这是默认值,因此可以省略。