将H2数据库用于Flyway的Spring Test Profile

Joe*_*Joe 6 database spring h2 end-to-end flyway

我正在尝试将端到端测试设置为使用内存数据库,该数据库可以轻松启动,关闭,擦除和填充测试数据。我正在做一个春季项目,并且正在使用flyway迁移数据库。当启动没有任何配置文件的spring服务器时,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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

这是使用测试配置文件启动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
Run Code Online (Sandbox Code Playgroud)

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

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [table-name]
Run Code Online (Sandbox Code Playgroud)

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


编辑

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

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

Ham*_*ter 2

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

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

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

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

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

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

然后,您可以将公共语句 (V1__Create_table.sql) 放在公共位置,并将特定于数据库的语句 (V2__Alter_table.sql) 的不同副本放在特定于数据库的位置。

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