【问题标题】:SpringBoot - Testing with Flyway and H2 databaseSpring Boot - 使用 Flyway 和 H2 数据库进行测试
【发布时间】:2019-02-02 14:22:07
【问题描述】:

我正在使用 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

在目录 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)
);

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

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

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

谢谢!

【问题讨论】:

    标签: spring-boot testing h2 flyway acceptance


    【解决方案1】:

    Flyway 没有找到任何迁移,然后 Hibernate 已从您的实体创建表。

    位置应该是:

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

    这是默认设置,可以省略。

    【讨论】:

      【解决方案2】:

      1- 确保禁用休眠 DDL 生成:

      spring.jpa.hibernate.ddl-auto=none
      

      2- 确保您的 SQL 迁移脚本的名称符合 flyway 的约定。即

      V1__create_user_table_for_test.sql
      

      【讨论】:

        猜你喜欢
        • 2019-05-17
        • 2019-04-01
        • 2020-12-25
        • 2018-09-13
        • 2023-03-26
        • 2016-09-07
        • 1970-01-01
        • 1970-01-01
        • 2019-09-06
        相关资源
        最近更新 更多