Search code examples
mysqlmysql-workbenchliquibaseliquibase-sqlliquibase-cli

How to continue execution of Liquibase script even though there is error thrown?


I'm using MySQL database. I'm giving call to an stored procedure for 3 times to insert 3 records. following is the example for the same.

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:pro="http://www.liquibase.org/xml/ns/pro"
   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.10.xsd
   http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-3.10.xsd">
    <changeSet id="Demo" author="rambo" runOnChange="true">
        <sql splitStatements="true">            
            CALL insert_animal(NULL, 'Cat', 'Cat', NULL, 0, '', '');
            CALL insert_animal(NULL, 'Mouse', 'Mouse', NULL, 0, '', '');
            CALL insert_animal(NULL, 'Dog', 'Dog', NULL, 0, '', '');            
        </sql>
        <rollback/>
    </changeSet>
</databaseChangeLog>

Now when I update this above script for addition of 4th animal lets say, then I get error and basically it says that record exist with the name Cat and script execution is halted.

What I want to do is, I want to continue the execution even if the SP call to insert any animal fails because its duplicate.

I'm new to liquibase and seeking for some advise on this.


Solution

  • You trying to use liquibase wrong way. The changeset aimed be written only once and never changed after you apply it to database. If you need to add something new to existing changeset - just create new one. No exception from this rule, especially if you already commit the changes. Actually, there is only one exception - if you test the changeset on your own database and it is not satisfy the requirements. In this case you should remove corresponding row from databasechangelog table, manually return database schema to previous state, update the changeset and apply it to database again. In addition, you should NEVER use liquibase to add new data to tables. Liquibase is aimed to change schema state, not database data. If you break this rule, you will get a lot of problems all the way, because the data is a subject for frequently changes. Use plain sql for data migration after you apply liquibase changes.