代码之家  ›  专栏  ›  技术社区  ›  Meena Chaudhary

如何使用Spring配置文件设置Flyway迁移文件位置

  •  0
  • Meena Chaudhary  · 技术社区  · 5 年前

    我有两个春季简介 dev test 为开发和测试环境配置。在每个环境中,我使用不同的数据库,即 h2 postgresql 在测试中。以下是每个配置文件的属性文件,其中 {vendor} 由spring boot解决 postgresql 根据配置的数据源分别执行。

    spring.flyway.locations=classpath:db/migration/{vendor}
    

    应用测试。属性

    #Data source
    spring.datasource.url=jdbc:postgresql://localhost:5432/test
    spring.datasource.username=postgres
    spring.datasource.password=postgres
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
    
    #Flyway
    spring.flyway.check-location=false
    spring.flyway.locations=classpath:/db/migration/test/{vendor}
    

    的Flyway迁移文件 开发 test/resources 测验 main/resources

    enter image description here

    enter image description here

    当我用 . 然而,当我使用 开发 轮廓我希望它只能在 src/test/resources/db/migration/h2 . 但是Flyway正在从 主要/资源 测试/资源 两者都会导致错误

    0 回复  |  直到 5 年前
        1
  •  11
  •   Meena Chaudhary    5 年前

    我是这样做的。

    1. 使用Spring配置文件为不同的环境配置应用程序,即 dev , test prod .
    2. 使用Spring配置文件根据环境加载flyway迁移文件。

    1. H2 的数据库 开发
    2. postgresql 的数据库 测验
    3. postgresql 的数据库

    配置

    1. 创建弹簧轮廓 , 在里面

      <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>test</id> </profile> <profile> <id>prod</id> </profile> </profiles>

    2. 为每个配置文件创建属性文件

    应用程序开发属性

    spring.flyway.locations=classpath:db/migration/{vendor}
    

    自从 H2 H2 驱动程序在类路径上。我们不需要显式配置它。

    应用测试。属性

    spring.datasource.url=jdbc:postgresql://localhost:5432/db_test
    spring.datasource.username=postgres
    spring.datasource.password=postgres
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
    
    spring.flyway.locations=/db/{vendor}/common,/db/{vendor}/test
    

    spring.datasource.url=jdbc:postgresql://localhost:5432/db_prod
    spring.datasource.username=postgres
    spring.datasource.password=postgres
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
    
    spring.flyway.locations=/db/{vendor}/common,/db/{vendor}/prod
    
    1. Flyway移植文件位置。

    db/migration src/main/resources V2__data_insertion.sql . 自从 H2 迁移文件属于默认配置文件,我将其保留在默认的flyway迁移文件位置。

    enter image description here

    enter image description here

    希望有帮助!!!

        2
  •  0
  •   Mark Bramnik    5 年前

    这不仅仅是关于飞行路线。

    在maven中,它在构建过程中使用两种不同的类路径:

    1. 编译类路径-用于编译(包括 src/main/* )
    2. src/test/* src/主/* 因为在测试中,您应该拥有对实际代码的编译时访问权限。

    另一个观察结果:

    一般来说,生产代码不应该包含任何关于测试的内容。但我看到你补充说: src/main/resources/application-test.properties 该文件将出现在错误的生产工件中。

    src/main/resources/application-prod.properties 它为真正的迁移定义了位置“X” src/test/resources/application-test.properties 为测试迁移定义位置“Y” 使用概要文件测试运行集成测试,您将找不到生产迁移。