Ganesh
Ganesh

Reputation: 13

While adding a new Table column I cannot find any attribute of IF_NOT_EXIST in the Liquibase Script

<changeSet author="John" id="addColumn-example"> 
    <addColumn TableName="person" >
        <column name="address"type="varchar(255)"/> 
    </addColumn>  
</changeSet>

I cannot find any attribute or something for the above change-set tags which work like below Expected Query

ALTER TABLE PERSON 
    ADD COLUMN IF NOT EXISTS ADDRESS VARCHAR(255); 

There is no IF NOT EXISTS attribute in the Liquibase docs. It only shows add column, can anyone suggest how to do it?

Upvotes: 1

Views: 1402

Answers (1)

Jabbasnik
Jabbasnik

Reputation: 92

I'm not completely sure if it's what You are looking for, but I would suggest adding precondition to your changeset. This will prevent the execution of your changeset if requirements from precondition are not met. Take a look at the documentation.

So after that your changeset would look like this:

<changeSet author="John" id="addColumn-example">
    <preConditions onFail="MARK_RAN" onFailMessage="Column address already exists!">
        <not>
            <columnExists tableName="person" columnName="address"/>
        </not>
    </preConditions>
    <addColumn tableName="person">
        <column name="address" type="varchar(255)"/>
    </addColumn>
</changeSet>

What is worth noticing is negation with <not> tag. It is required since we want to check whether a specific column does not exists already. Of course You can pick precondition onFail strategy which fits You the best.

Upvotes: 2

Related Questions