首页 文章

如何从外部属性文件中填充Liquibase参数值?

提问于
浏览
2

有没有办法根据外部属性文件的内容在Liquibase changelog文件中填充parameters

在,我希望能够说:

<createTable tableName="${table.name}">
     <column name="id" type="int"/>
     <column name="${column1.name}" type="varchar(${column1.length})"/>
     <column name="${column2.name}" type="int"/>
</createTable>

并将 table.name 和其他参数的值保存在外部文件 db.properties 中,并在更改日志或Liquibase命令行中引用此文件,或者作为运行liquibase的Maven插件的选项引用此文件 .

我似乎无法找到任何办法,这可能吗?

2 回答

  • 2

    看看here . 您可以使用命令行参数( -D[arg name]=[arg value] )或环境变量 . 如果不使用任何构建管理器工具(如Maven或Ant),则需要编写脚本以从文件中读取参数并将其传递给命令 . Example

  • 4

    在编译时这样做:听起来像maven filters和/或profiles的工作

    注意:小心liquibase和任何“标记”替换... liquibase存储应用的更改集的CRC

    的pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>test</groupId>
        <artifactId>test</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <build>
            <filters>
                <filter>src/main/filters/liquibase.properties</filter>
            </filters>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                    <includes>
                        <include>liquibase.xml</include>
                    </includes>
                </resource>
            </resources>
        </build>
    </project>
    

    /src/main/filters/liquibase.properties

    table.name=TABLE_NAME
    column1.name=COLUMN1_NAME
    column1.length=10
    column2.name=COLUMN2_NAME
    

    /src/main/resources/liquibase.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <databaseChangeLog logicalFilePath="liquibase.xml" xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">
    
        <changeSet author="me" id="changeSetId1">
            <comment>Test</comment>
    
            <createTable tableName="${table.name}">
                <column name="id" type="int" />
                <column name="${column1.name}" type="varchar(${column1.length})" />
                <column name="${column2.name}" type="int" />
            </createTable>
        </changeSet>
    
    </databaseChangeLog>
    

    编辑:typical调用(使用filtered资源)将如下所示: mvn resources:resources liquibase:update 或更优选使用配置文件... mvn resources:resources liquibase:update -P<profile_name>

    EDIT2:这种定义列的方法有一个很大的优点 . 您可以使用此属性(例如:column1.length)值(例如:10)来验证 every 层:Hibernate,DAO,WEB,faces,JavaScript . 只需在需要验证它的每个地方使用此属性 . 即使在i18n / messages.properties中,如果需要(例如:input1.validation =不超过$ 个字母 . ) .

    唯一的复杂因素是,如果您需要更改此值,则需要提供正确的liquibase更新/回滚脚本 . 有时可以更改值并设置新的liquibase校验和(安全操作,如增加varchar长度),但有时您需要使用新属性/值创建安全更新更改脚本 .

相关问题