使用H2数据库和Flyway为Spring测试配置文件进行设置

9
我尝试设置端到端测试使用的内存数据库,可以轻松地启动、关闭、擦除和种植测试数据。我正在处理一个Spring项目,并使用Flyway迁移数据库。在没有任何配置文件的情况下启动Spring服务器时,Flyway会正确运行迁移,一切都很顺利。然而,在“test”配置文件中运行时,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]

有没有想过为什么迁移在“test”配置文件下不能运行?
编辑 刚意识到我的迁移是用PostgresQL编写的,而我希望它们能与H2一起使用...我认为这显然是一个问题。所以如果可能的话,请扩展此问题,了解如何使相同的迁移在两种不同的数据库类型上运行...
但是为什么我没有收到错误消息,说明Flyway尝试运行迁移,但数据库不接受查询?

1
我曾经使用过Liquibase,它提供了一个数据库无关的迁移语法(例如XML)。你在Flyway中找到了什么好的解决方案吗? - Ko-Chih Wu
1个回答

2
这个答案基于你的问题更新;“如何在两种不同的数据库类型上运行相同的迁移”。来自Flyway FAQ
引用:

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

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

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

TEST(Derby):flyway.locations = sql / common,sql / derby

PROD(Oracle):flyway.locations = sql / common,sql / oracle

然后,您可以将通用语句(V1__Create_table.sql)放在common中,并将特定于不同数据库的语句(V2__Alter_table.sql)放在特定于数据库的位置。

从您的配置中,看起来您已经为测试数据拥有了不同的位置,因此您已经走在了正确的路上。使用命令行上的-X打开调试,以查看Flyway搜索迁移的日志,以帮助管理三个目录。我不确定如何从spring实现这一点,这个答案可能会有所帮助:logging-flyway-sql-with-spring-boot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接