Postgresql – ny way to clear all tables using Liquibase

postgresqlunit test

I'm currently using Liquibase for creating test data in my database before each test run.

I came across a problem where some of the test cases need special data, while others need the tables to be empty.

So I decided to give Liquibase contexts a try and only insert the data I really need for the current testcase plus – in an abstract superclass – all the data every test case needs (user accounts and stuff)…

I would like to clear the tables before each run, however I can't seem to find anything for this case except dropAll(), which seems to be overkill…

I'm not quite sure if it is even possible – because of foreign keys and other constraints – but I wonder if there is a way to delete the content of ALL the tables without dropping them completely?

Best Answer

I guess the fastest way would be to use a custom sql tag that uses truncate ... cascade on all "top-level" tables.

Using the cascade keyword for truncate will also (recursively) delete all rows from all dependent tables (all tables referencing the one that is being truncated).

Something like:

<sql>
   truncate table parent_1, parent_2, foobar cascade;
</sql>

Don't know if that is feasible with the number of tables you have or not.