Mark
Mark

Reputation: 5622

How do I conditionally add a primary key to a database table using Liquibase?

I need to craft a Liquibase migration script that adds a primary key to a database table only if that primary key hasn't been added already. What is the best way to go about doing this? It's going to be something like this:

<changeSet id="...">
  <preConditions>
    (What goes here? Should I use <sqlCheck>, or is there a better alternative?)
  </preConditions>
  <addPrimaryKey tableName="foo" columnNames="bar" constraintName="foo_pk" />
</changeSet>

Upvotes: 5

Views: 2304

Answers (1)

Mark
Mark

Reputation: 5622

And the answer is...

<preConditions onFail="MARK_RAN">
  <not>
    <primaryKeyExists tableName="foo" />
  </not>
</preConditions>

Upvotes: 9

Related Questions