【问题标题】:Use H2 Database for Spring Test Profile with Flyway将 H2 数据库用于 Spring 测试配置文件和 Flyway
【发布时间】:2016-09-07 19:18:51
【问题描述】:

我正在尝试设置我的端到端测试以使用内存数据库,该数据库可以轻松启动、关闭、擦除和植入测试数据。我正在从事一个春季项目,并正在使用 flyway 迁移数据库。在没有任何配置文件的情况下启动我的 spring 服务器时,flyway 正确运行迁移并且一切都很好。但是,当在我的“测试”配置文件中运行时,flyway 迁移不会运行。

application.properties

# Database Properties
spring.jpa.database=POSTGRESQL
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=validate
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb

# Data Rest Properties
spring.data.rest.basePath=/api

# Logging Properties
logging.level.root=WARN
logging.level.org.flywaydb=INFO
logging.level.com.myproj=INFO

application-test.properties

# Server Properties
server.port=8081

# Database Properties
spring.jpa.database=H2
spring.database.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:mydb-test

# Dev Tools Properties
spring.devtools.restart.enabled=false

# Flyway Properties
flyway.locations=classpath:db/migration,classpath:db/test_seed_data

这是我使用测试配置文件启动 spring 服务器时得到的输出:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::             (v1.4.0.M2)

2016-05-11 23:01:16.052  INFO 86897 --- [  restartedMain] com.myproj.myprojApplicationKt           : Starting myprojApplicationKt on me.local with PID 86897 (/Users/me/Workspace/myproj/target/classes started by me in /Users/me/Workspace/myproj)
2016-05-11 23:01:16.054  INFO 86897 --- [  restartedMain] com.me.myprojApplicationKt               : The following profiles are active: test
2016-05-11 23:01:20.312 ERROR 86897 --- [ost-startStop-1] o.s.b.c.embedded.tomcat.TomcatStarter    : Error starting Tomcat context: org.springframework.beans.factory.UnsatisfiedDependencyException
2016-05-11 23:01:20.379  WARN 86897 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
2016-05-11 23:01:20.404 ERROR 86897 --- [  restartedMain] o.s.boot.SpringApplication               : Application startup failed

最终的错误是验证失败(关闭验证时仍然无法创建表):

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [table-name]

知道为什么迁移不针对“测试”配置文件运行吗?


编辑

刚刚意识到我的迁移是用 PostgresQL 编写的,我希望它们能够与 H2 一起使用......我认为这显然是一个问题。因此,将此问题扩展为如何在两种不同的数据库类型上运行相同的迁移(如果可能的话)...

但是为什么我没有收到一条错误消息,指出 Flyway 尝试运行迁移并且数据库不接受查询?

【问题讨论】:

  • 我曾经使用 liquibase,它为迁移提供了一种与 DB 无关的语法(例如,在 XML 中)。你找到什么好的flyway解决方案了吗?

标签: database spring h2 flyway end-to-end


【解决方案1】:

此答案基于您的问题更新; “如何在两种不同的数据库类型上运行相同的迁移”。来自Flyway FAQ

处理特定于数据库的 sql 的最佳策略是什么?

假设您在 TEST 中使用 Derby,在 PROD 中使用 Oracle。

您可以使用flyway.locations property。它看起来像这样:

测试(德比):flyway.locations=sql/common,sql/derby

生产(甲骨文):flyway.locations=sql/common,sql/oracle

然后您可以在 特定于 DB 的语句的常见和不同副本 (V2__Alter_table.sql) 在特定于 db 的位置。

从您的配置看来,您已经为测试数据设置了不同的位置,因此您一切顺利。在命令行上使用 -X 打开调试以查看 flyway 如何搜索迁移以帮助管理三个目录的日志记录。我不知道从春天开始怎么做,这个答案可能会有所帮助:logging-flyway-sql-with-spring-boot

【讨论】:

    猜你喜欢
    • 2019-02-02
    • 2011-01-02
    • 2020-12-25
    • 2018-09-13
    • 2019-01-26
    • 2016-10-20
    • 2016-05-15
    • 2017-12-30
    • 1970-01-01
    相关资源
    最近更新 更多