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

2024-03-23

有什么办法可以让我填充参数 http://www.liquibase.org/documentation/changelog_parameters.html在基于外部属性文件内容的 Liquibase 变更日志文件中?

就像,我想说:

<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 插件的选项。

我似乎找不到任何方法来做到这一点,可能吗?


在编译时执行此操作: 听起来像是 Maven 的工作filters http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html and/or profiles http://maven.apache.org/guides/introduction/introduction-to-profiles.html

注意:小心 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>

EDIT: A typical http://www.liquibase.org/documentation/maven/调用(使用filtered http://maven.apache.org/guides/getting-started/index.html#How_do_I_filter_resource_files资源)看起来像这样: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=不超过 ${column1.length} 个字母。)。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从外部属性文件填充 Liquibase 参数值? 的相关文章

随机推荐