M_F
M_F

Reputation: 456

Liquibase replace <property> tag value?

How to replace tag value for next change set if I'm about to use the same variables but with different values. Example

<!-- TRANSLATION -->
<property name="localization.table"     value="LOCALIZATION"/>
<property name="localization.locale"    value="en_US"/>
<!-- -->
<property name="localization.key"       value="translation.key"/>
<!-- Translation -->
<property name="localization.value"     value="translation"/>
<!-- -->

<changeSet author="me" id="translate">

    <insert tableName="${localization.table}">
        <column name="KEY_">${localization.key}</column>
        <column name="VALUE">${localization.value}</column>
        <column name="LOCALE">${localization.locale}</column>
    </insert>

    <rollback>
        <delete tableName="${localization.table}">
            <!-- Doesnt work with regular '' symbols -->
            <where>KEY_ = &apos;${localization.key}&apos; AND LOCALE = &apos;${localization.locale}&apos;</where>
        </delete>
    </rollback>

</changeSet>

This example works only first time second time I have

Error setting up or running Liquibase: liquibase.exception.SetupException: liquibase.exception.SetupException: Error parsing line 150 column 67 of /patches/translate_me.xml: cvc-complex-type.2.4.a: Invalid content was found starting with element 'property'. One of '{"http://www.liquibase.org/xml/ns/dbchangelog":changeSet, "http://www.liquibase.org/xml/ns/dbchangelog":include, "http://www.liquibase.org/xml/ns/dbchangelog":includeAll}' is expected. -> [Help 1]

How to do this replacement then?

Upvotes: 6

Views: 6277

Answers (1)

geraldhumphries
geraldhumphries

Reputation: 1493

By default, Liquibase properties are applied globally and will keep the same value even if redefined.

As of Liquibase 3.4.0, there's a new global attribute on the property tag. Setting global="false" will limit the property to the databaseChangeLog it's defined in, allowing you to use that property with a new value in a different databaseChangeLog.

If you need to redefine a property, it needs to be done within a new databaseChangeLog. In your specific case, I believe you have a syntax error because you are trying to define a property after a changeSet within the same databaseChangeLog.

Example:

<databaseChangeLog 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-3.5.xsd">
    <property name="myProperty" value="foo" global="false"/>

    <changeSet author="me" id="changeSet-1">
        <!-- will insert `foo` -->
        <insert tableName="my_table">
            <column name="my_column">${myProperty}</column>
        </insert>
    </changeSet>
</databaseChangeLog>


<databaseChangeLog 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-3.5.xsd">
    <property name="myProperty" value="bar" global="false"/>

    <changeSet author="me" id="changeSet-2">
        <!-- will insert `bar` -->
        <insert tableName="my_table">
            <column name="my_column">${myProperty}</column>
        </insert>
    </changeSet>
</databaseChangeLog>

Upvotes: 5

Related Questions