代码之家  ›  专栏  ›  技术社区  ›  Abzelhan

如何在springboot中为dev和prod环境划分Liquibase包结构?

  •  2
  • Abzelhan  · 技术社区  · 6 年前

    我的包结构如下所示:

    enter image description here

    /db.changelog/v1/db.changelog-1.0.xml 其中我还包括了 /db.changelog/v1/changeset更改集 包裹。

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

    还有,我可以用 上下文 中的属性 生产 但这种变通方法并不可取。

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

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

    1 回复  |  直到 6 年前
        1
  •  7
  •   M-Razavi    6 年前

    解决方案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过滤资源: 滤波器 资源 元素,如中所述 Liquibase Documentation .

    资源 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>
    
        2
  •  1
  •   sendon1982    4 年前

    <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>  
    
    推荐文章