9

我的包结构如下:

在此处输入图像描述

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

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

此外,我可以在变更集标记中使用上下文属性并显式设置devprod值,但这种解决方法并不可取。

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

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

4

2 回答 2

7

解决方案 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过滤器元素与资源元素结合使用,如Liquibase 文档中所述。

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

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>
于 2018-10-04T12:57:31.010 回答
2

至于我,我将 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>  
于 2020-06-15T04:19:12.743 回答