2016-05-12 87 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

相關問題