将H2数据库用于Flyway的Spring测试配置文件
作者:互联网
我正在尝试设置我的端到端测试,以使用内存数据库,该数据库可以轻松启动,关闭,擦除和播种测试数据.我正在开发一个spring项目,正在使用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试图运行迁移并且数据库不接受查询?
解决方法:
这个答案基于你的问题更新; “如何在两种不同的数据库类型上运行相同的迁移”.从Flyway FAQ:
What is the best strategy for handling database-specific sql?
Assuming you use Derby in TEST and Oracle in PROD.
You can use the
flyway.locations property
. It would look like this:TEST (Derby):
flyway.locations=sql/common,sql/derby
PROD (Oracle):
flyway.locations=sql/common,sql/oracle
You could then have the common statements (V1__Create_table.sql) in
common and different copies of the DB-specific statements
(V2__Alter_table.sql) in the db-specific locations.
从您的配置看起来您已经拥有了不同的测试数据位置,因此您可以顺利完成任务.在命令行上使用-X打开调试,以查看flyway搜索迁移的方式,以帮助管理这三个目录.我不知道如何从春天开始这样做,这个答案可能会有所帮助:logging-flyway-sql-with-spring-boot.
标签:database,spring,h2,flyway,end-to-end 来源: https://codeday.me/bug/20190711/1430948.html