Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop foreign key if exists using liquibase in MYSQL

I'd like to ask, is there a way how to drop foreign key constraint only when it exists? My current changeset looks like this:

 <changeSet author="blabla" id="10_1_1">

    <comment>Drop FK DummyFK1</comment>

    <dropForeignKeyConstraint constraintName="DummyFK1" baseTableName="DummyToken" />

</changeSet>

Thanks!

like image 593
Sofi Avatar asked Apr 30 '18 07:04

Sofi


1 Answers

You need to add preConditions. There's foreignKeyConstraintExists built-in precondition in liquibase.

<changeSet id="changeSetId" author="John Doe">
    <preConditions onFail="MARK_RAN">
        <foreignKeyConstraintExists foreignKeyName="DummyFK1"/>
    </preConditions>
    <comment>Drop FK DummyFK1</comment>
    <dropForeignKeyConstraint baseTableName="DummyToken" constraintName="DummyFK1"/>
</changeSet>
like image 116
htshame Avatar answered Oct 19 '22 03:10

htshame