Arpit
Arpit

Reputation: 601

Drop MySQL table using Liquibase

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.

Upvotes: 14

Views: 28526

Answers (3)

Mihkel Selgal
Mihkel Selgal

Reputation: 508

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')
}

Upvotes: 2

Fede Garcia
Fede Garcia

Reputation: 807

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

Upvotes: 4

Lemmy
Lemmy

Reputation: 2575

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

Upvotes: 26

Related Questions