Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way how to truncate all tables using liquibase?

I would like to truncate all data from an application before it goes to production I was looking into the documentation and didn't find anything about truncating tables using liquibase. so I was wondering if anybody else solved something similar

like image 950
Rubenex Avatar asked Mar 01 '18 12:03

Rubenex


People also ask

How do I TRUNCATE all records in SQL?

To remove all data from an existing table, use the SQL TRUNCATE TABLE order. You can also use the DROP TABLE command to delete an entire table. But Truncate will remove the entire table structure from the database, and you will need to recreate the table if you want to store any data.

Is it better to TRUNCATE or DROP TABLE?

To remove all rows from a large table and leave the table structure, use TRUNCATE TABLE . It's faster than DELETE . To remove an entire table, including its structure and data, use DROP TABLE .

Can we TRUNCATE table?

TRUNCATE TABLE removes all rows from a table, but the table structure and its columns, constraints, indexes, and so on remain. To remove the table definition in addition to its data, use the DROP TABLE statement.


1 Answers

You can use native SQL. It isn't liquibase constructs as such, and not DB agnostic, but I suspect truncate commands should be supported by any DB platform that liquibase supports. Of course, be careful of Foreign Key references when truncating any table, and make sure you truncate in the right order.

For example:

<changeSet author="eric.b" id="10288-201-5">
    <comment>Clear any existing legacy data in the tables</comment>
    <sql splitStatements="true">
        TRUNCATE TABLE ADDRESS;
        TRUNCATE TABLE PHONE;
        TRUNCATE TABLE USERS;
    </sql>
</changeSet>
like image 71
Eric B. Avatar answered Oct 02 '22 17:10

Eric B.