2016-05-12 52 views
4

我正在尝试设置我的端到端测试以使用内存数据库,该数据库可以很容易地调出,关闭,擦除并播种测试数据。我正在开发一个Spring项目,并使用flyway迁移数据库。在没有任何配置文件的情况下启动我的弹簧服务器时,flyway会正确运行迁移,并且都很好。但是,在我的“测试”配置文件中运行时,飞路迁移不会运行。使用H2数据库进行弹道测试配置文件

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 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试图运行迁移,数据库不接受查询?

+0

我曾经使用liquibase,它为迁移提供了一个DB-agnostic语法(例如,在XML中)。你有没有找到与飞路有什么好的解决方案? –

回答

0

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

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

假设您在PROD中的TEST和Oracle中使用Derby。可以使用flyway.locations property。它应该是这样的:

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

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

然后,您可以在的 共同和不同的副本共同声明(V1__Create_table.sql)数据库特定的语句 (V2__Alter_table.sql)在数据库特定的位置。

从你的配置看起来你已经有了不同的测试数据位置,所以你很好。在命令行上使用-X打开调试,查看日志记录,了解flyway如何搜索迁移以帮助管理这三个目录。我不知道如何从春季做到这一点,这个答案可能有帮助:logging-flyway-sql-with-spring-boot

相关问题