【问题标题】:How to divide Liquibase package structure for dev and prod environment in Spring Boot?如何在 Spring Boot 中为 dev 和 prod 环境划分 Liquibase 包结构?
【发布时间】:2019-03-09 18:11:31
【问题描述】:

我的包结构如下:

/db.changelog/db.changelod-master.xml 我包括 /db.changelog/v1/db.changelog-1.0.xml 我还包括/db.changelog/v1/changeset 包中的所有变更日志。

在我的应用程序中,我有两个配置文件:devprod,我需要根据 Liquibase 的“最佳实践”划分包的结构。一些变更日志可以在 proddev 环境中。

另外,我可以在 changeset 标记中使用 context 属性并显式设置 devprod 值,但是这种解决方法不可取。

简单用法如下:我切换到 prod 配置文件,某些表将不会创建,或者某些对数据库的插入将被跳过。

你能帮我根据 Liquibase“最佳实践”重构包的结构吗?

【问题讨论】:

  • 1- 您是如何在应用程序中定义 dev 和 prod 配置文件的? 2-您使用了哪些构建工具? (Maven,Gradle,...) 3- dev 和 prod 配置文件的更改日志有什么区别?有没有分开的路径?
  • @M-Razavi, 1. 我们在 标签内的 maven 中定义配置文件,并且在资源模块中我们将包命名为:dev、prod、test。在每个包中,我们都有不同的 application.properties 文件。 2. Maven 3. 对于 prod,我们有参考值,例如城市列表等。对于 dev,我们也有参考值,还有一些开发环境所需的预定义数据,这些数据在我们将 profile 切换到 prod 后不能加载行家。 3.我在截图中显示的包中的所有变更日志,我需要根据环境分离变更日志。

标签: java spring spring-boot liquibase liquibase-hibernate


【解决方案1】:

至于我,我将 dev、prod 放入具有不同上下文的不同变更集中。

有留在同一个文件中。

<changeSet author="test" id="API-111" context="dev">
    <sql>
        insert into api_key(id, value) values (1, 'dev_value');                   
    </sql>
</changeSet>

<changeSet author="test" id="API-111" context="uat">
    <sql>
        insert into api_key(id, value) values (1, 'uat_value');                   
    </sql>
</changeSet>    

<changeSet author="test" id="API-111" context="prod">
    <sql>
        insert into api_key(id, value) values (1, 'prod_value');                   
    </sql>
</changeSet>  

【讨论】:

    【解决方案2】:

    解决方案 1:
    您需要在 yaml 文件中定义“liquibase.contexts”属性。如下所示。

    spring:
      profiles: dev
      datasource:
        url: jdbc:postgresql://localhost:5432/dev
        username: postgres
        password: password
        driver-class-name: org.postgresql.Driver
    liquibase:
       contexts: dev
    

    添加后,以下更改集将仅在您的本地配置文件为“dev”时执行(即 spring-boot:run -Dspring.profiles.active=dev)

    <changeSet id="20161016_my_first_change2" author="krudland" context="dev">
        <sql>
            insert into customer (firstname, lastname) values ('Franklin','Ike');
        </sql>
        <rollback>
            delete from customer where firstname = 'Franklin' and lastname = 'Ike';
        </rollback>
    </changeSet>
    

    解决方案 2:
    如果您不想使用 liquibase.context,您可以使用 maven 过滤资源: 关键是将 maven filter 元素与 resource 元素结合使用,如 Liquibase Documentation 中所述。

    在 maven 命令中包含 resources 目标也很重要:

    mvn resources:resources liquibase:update -Plocal
    

    这是我使用的文件层次结构:

    |-- pom.xml
    `-- src
        `-- main
           |-- resources
           |   `-- liquibase.properties
           |   |-- changelog
           |       `-- db-changelog-master.xml
           |       `-- db-changelog-1.0.xml
           |-- filters
               |-- local
               |   `-- db.properties
               |-- dev
               |   `-- db.properties
    

    db.properties 文件如下所示:

    database.driver = oracle.jdbc.driver.OracleDriver
    database.url = jdbc:oracle:thin:@<host_name>:<port_number>/instance
    database.username = user
    database.password = password123
    database.changelogfile = db.changelog-master.xml
    

    liquibase.properties 文件如下所示:

    changeLogFile: changelog/${database.changelogfile}
    driver: ${database.driver}
    url: ${database.url}
    username: ${database.username}
    password: ${database.password}
    verbose: true
    

    POM 文件如下所示:

    <build>
          <pluginManagement>
             <plugins>
                <plugin>
                   <groupId>org.liquibase</groupId>
                   <artifactId>liquibase-maven-plugin</artifactId>
                   <version>3.1.0</version>
                   <configuration>
                      <propertyFile>target/classes/liquibase.properties</propertyFile>
                   </configuration>
                </plugin>
             </plugins>
          </pluginManagement>
       </build>
    
    
    <profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <filters>
                    <filter>src/main/filters/local/db.properties</filter>
                </filters>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </build>
        </profile>
        <profile>
            <id>dev</id>
            <build>
                <filters>
                    <filter>src/main/filters/dev/db.properties</filter>
                </filters>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </build>
        </profile>
    </profiles>
    

    【讨论】:

    • 问题是:如何划分开发环境和生产环境的Liquibase包结构。在我的问题中,我指定我可以使用上下文。我需要知道如何为 prod 和 dev 环境将变更集划分为单独的 xml 文件。
    • @MisterMagic 使用资源过滤。我已经更新了答案
    • spring: profiles: dev datasource: url: jdbc:postgresql://localhost:5432/dev username: postgres password: password driver-class-name: org.postgresql.Driver liquibase: contexts: dev 如果我想定义多个配置文件/上下文怎么办?
    • @SoT 哪个解决方案?
    • 除此之外,当我应用解决方案 2 时,出现错误:设置或运行 Liquibase 时出错:liquibase.exception.DatabaseException: liquibase.exception.DatabaseException: Connection could not be created to ${database. url} 与驱动程序 org.postgresql.Driver。似乎它不能将值从 dev/db.properties 转换为 liquibase.properties
    猜你喜欢
    • 2021-09-22
    • 2021-06-18
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    相关资源
    最近更新 更多