11

I am looking to drop a table in MySQL using Liquibase only if the table exists.

I am not able to figure out how to check if a table exists in Liquibase.

Gama11
  • 31,714
  • 9
  • 78
  • 100
Arpit
  • 521
  • 2
  • 8
  • 22

3 Answers3

22

You should use

<changeSet author="liquibase-docs" id="dropTable-example">
    <preConditions onFail="MARK_RAN"><tableExists schemaName="schemaName" tableName="tableName"/></preConditions>
    <dropTable cascadeConstraints="true"
            catalogName="cat"
            schemaName="public"
            tableName="person"/>
</changeSet>

Also, you can check this link for more <preConditions> options: http://www.liquibase.org/documentation/preconditions.html

Lemmy
  • 2,437
  • 1
  • 22
  • 30
  • So I'm doing a yaml equivilant to this (I think). It checks the precondition and fails, but still continues – Stealth Rabbi Aug 20 '19 at 11:23
  • I'm sorry, I had a typo. on-fail vs onFail. What you have is valid. Nonetheless, here is the changeset I used (asking a similar question): https://stackoverflow.com/a/57572634/680268 – Stealth Rabbi Aug 21 '19 at 12:30
2

Below is the Groovy version of dropping the table with Liquibase using preConditions chack that the table exists.

changeSet(author: 'author', id: 'some_id_value') {
    preConditions(onFail: 'MARK_RAN'){
        tableExists(tableName: 'table_name')
    }

    dropTable(tableName: 'table_name')
}
Sergey
  • 3,253
  • 2
  • 33
  • 55
Mihkel Selgal
  • 508
  • 6
  • 10
0

Here is a YAML snippet for dropping a table if exists

- changeSet:
    id: drop-my-table-if-exists
    author: the-author
    comment: drop my_table if exists
    preConditions:
    - onFail: MARK_RAN
    - tableExists:
        tableName: my_table
    changes:
    - dropTable:
        tableName: my_table
Fede Garcia
  • 677
  • 11
  • 18