Liquibase Attributes failOnError

Liquibase runs the update command to execute the ChangeSet sequentially and if encounters any error then aborts the execution and logs the message. but sometimes, the requirement is to keep on executing the ChangeSet irrespective of errors. for this, Liquibase provides failOnError an attribute, by default failOnError has value true, hence execution fails if an error encounters, but if required, it can be set as false, and Liquibase will keep on executing the ChangeSet irrespective of errors.

the below changelog file contains 2 ChangeSet, 1 for inserting the data in the apple table, and the other for the creation of the vendor table. but the apple table doesn't exist in the database, hence Liquibase will be about the execution at apple table insertion ChangeSet and will not execute the vendor table creation ChangeSet.

changelog.mysql.sql

-- liquibase formatted sql

-- changeset wesome:1692446446077-1
INSERT INTO appledb.apple (apple_id, apple_name) VALUES (1, 'Macintosh');

-- changeset wesome:1692446446077-2
CREATE TABLE appledb.vendor (vendor_id BIGINT NOT NULL, vendor_name VARCHAR(20) NULL, active CHAR(1) NULL, apple_id INT NULL, CONSTRAINT PK_VENDOR PRIMARY KEY (vendor_id));

changelog.mysql.xml

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
    <changeSet author="shriksha (generated)" id="1692446190573-1">
        <insert catalogName="appledb" tableName="apple">
            <column name="apple_id" valueNumeric="1"/>
            <column name="apple_name" value="Macintosh"/>
        </insert>
    </changeSet>
    <changeSet author="shriksha (generated)" id="1692446190573-2">
        <createTable catalogName="appledb" tableName="vendor">
            <column name="vendor_id" type="BIGINT">
                <constraints nullable="false" primaryKey="true"/>
            </column>
            <column name="vendor_name" type="VARCHAR(20)"/>
            <column name="active" type="CHAR(1)"/>
            <column name="apple_id" type="INT"/>
        </createTable>
    </changeSet>
</databaseChangeLog>

changelog.mysql.yaml

databaseChangeLog:
  - changeSet:
      id: 1692446695953-1
      author: wesome (generated)
      changes:
        - insert:
            catalogName: appledb
            columns:
              - column:
                  name: apple_id
                  valueNumeric: 1
              - column:
                  name: apple_name
                  value: Macintosh
            tableName: apple
  - changeSet:
      id: 1692446695953-2
      author: wesome (generated)
      changes:
        - createTable:
            catalogName: appledb
            columns:
              - column:
                  constraints:
                    nullable: false
                    primaryKey: true
                  name: vendor_id
                  type: BIGINT
              - column:
                  name: vendor_name
                  type: VARCHAR(20)
              - column:
                  name: active
                  type: CHAR(1)
              - column:
                  name: apple_id
                  type: INT
            tableName: vendor

changelog.mysql.json

{
  "databaseChangeLog": [
    {
      "changeSet": {
        "id": "1692446435796-1",
        "author": "wesome (generated)",
        "changes": [
          {
            "insert": {
              "catalogName": "appledb",
              "columns": [
                {
                  "column": {
                    "name": "apple_id",
                    "valueNumeric": 1
                  }
                },
                {
                  "column": {
                    "name": "apple_name",
                    "value": "Macintosh"
                  }
                }
              ],
              "tableName": "apple"
            }
          }
        ]
      }
    },
    {
      "changeSet": {
        "id": "1692446435796-2",
        "author": "wesome (generated)",
        "changes": [
          {
            "createTable": {
              "catalogName": "appledb",
              "columns": [
                {
                  "column": {
                    "constraints": {
                      "nullable": false,
                      "primaryKey": true
                    },
                    "name": "vendor_id",
                    "type": "BIGINT"
                  }
                },
                {
                  "column": {
                    "name": "vendor_name",
                    "type": "VARCHAR(20)"
                  }
                },
                {
                  "column": {
                    "name": "active",
                    "type": "CHAR(1)"
                  }
                },
                {
                  "column": {
                    "name": "apple_id",
                    "type": "INT"
                  }
                }
              ],
              "tableName": "vendor"
            }
          }
        ]
      }
    }
  ]
}

liquibase.properties (update changeLogFile file format with SQL, XML, YAML, JSON as per changelog.mysql.<format>)

changeLogFile=changelog.mysql.xml
liquibase.command.url:jdbc:mysql://localhost:3306/AppleDb?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
liquibase.command.username: root
liquibase.command.password: rootroot

command liquibase update will result into

Running Changeset: changelog.mysql.xml::1692446190573-1::wesome (generated)

Unexpected error running Liquibase: Migration failed for changeset changelog.mysql.xml::1692446190573-1::wesome (generated):
     Reason: liquibase.exception.DatabaseException: Table 'appledb.apple' doesn't exist [Failed SQL: (1146) INSERT INTO appledb.apple (apple_id, apple_name) VALUES (1, 'Macintosh')]

ChangeSet with failOnError as false

update the ChangeSet with failOnError="false" will make sure the ChangeSet execution will continue irrespective of error encounterd, and will create the vendor table in database.

changelog.mysql.sql

-- liquibase formatted sql

-- changeset wesome:1692446446077-1 failOnError="false"
INSERT INTO appledb.apple (apple_id, apple_name) VALUES (1, 'Macintosh');

-- changeset wesome:1692446446077-2
CREATE TABLE appledb.vendor (vendor_id BIGINT NOT NULL, vendor_name VARCHAR(20) NULL, active CHAR(1) NULL, apple_id INT NULL, CONSTRAINT PK_VENDOR PRIMARY KEY (vendor_id));

changelog.mysql.xml

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
     <changeSet author="wesome (generated)" id="1692446190573-1" failOnError="false">
        <insert catalogName="appledb" tableName="apple">
            <column name="apple_id" valueNumeric="1"/>
            <column name="apple_name" value="Macintosh"/>
        </insert>
    </changeSet>
    <changeSet author="wesome (generated)" id="1692446190573-2">
        <createTable catalogName="appledb" tableName="vendor">
            <column name="vendor_id" type="BIGINT">
                <constraints nullable="false" primaryKey="true"/>
            </column>
            <column name="vendor_name" type="VARCHAR(20)"/>
            <column name="active" type="CHAR(1)"/>
            <column name="apple_id" type="INT"/>
        </createTable>
    </changeSet>    
</databaseChangeLog>

changelog.mysql.yaml

databaseChangeLog:
  - changeSet:
      id: 1692446695953-1
      author: wesome (generated)
      failOnError:  false
      changes:
        - insert:
            catalogName: appledb
            columns:
              - column:
                  name: apple_id
                  valueNumeric: 1
              - column:
                  name: apple_name
                  value: Macintosh
            tableName: apple
  - changeSet:
      id: 1692446695953-2
      author: wesome (generated)
      changes:
        - createTable:
            catalogName: appledb
            columns:
              - column:
                  constraints:
                    nullable: false
                    primaryKey: true
                  name: vendor_id
                  type: BIGINT
              - column:
                  name: vendor_name
                  type: VARCHAR(20)
              - column:
                  name: active
                  type: CHAR(1)
              - column:
                  name: apple_id
                  type: INT
            tableName: vendor

changelog.mysql.json

{
  "databaseChangeLog": [
    {
      "changeSet": {
        "id": "1692446435796-1",
        "author": "wesome (generated)",
        "failOnError": "false",
        "changes": [
          {
            "insert": {
              "catalogName": "appledb",
              "columns": [
                {
                  "column": {
                    "name": "apple_id",
                    "valueNumeric": 1
                  }
                },
                {
                  "column": {
                    "name": "apple_name",
                    "value": "Macintosh"
                  }
                }
              ],
              "tableName": "apple"
            }
          }
        ]
      }
    },
    {
      "changeSet": {
        "id": "1692446435796-2",
        "author": "wesome (generated)",
        "changes": [
          {
            "createTable": {
              "catalogName": "appledb",
              "columns": [
                {
                  "column": {
                    "constraints": {
                      "nullable": false,
                      "primaryKey": true
                    },
                    "name": "vendor_id",
                    "type": "BIGINT"
                  }
                },
                {
                  "column": {
                    "name": "vendor_name",
                    "type": "VARCHAR(20)"
                  }
                },
                {
                  "column": {
                    "name": "active",
                    "type": "CHAR(1)"
                  }
                },
                {
                  "column": {
                    "name": "apple_id",
                    "type": "INT"
                  }
                }
              ],
              "tableName": "vendor"
            }
          }
        ]
      }
    }
  ]
}

liquibase.properties (update changeLogFile file format with SQL, XML, YAML, JSON as per changelog.mysql.<format>)

changeLogFile=changelog.mysql.xml
liquibase.command.url:jdbc:mysql://localhost:3306/AppleDb?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
liquibase.command.username: root
liquibase.command.password: rootroot
loglevel=INFO

command liquibase update will result into

INFO [liquibase.ui] Running Changeset: changelog.mysql.xml::1692446190573-1::wesome (generated)
SEVERE [liquibase.changelog] ChangeSet changelog.mysql.xml::1692446190573-1::wesome (generated) encountered an exception.
INFO [liquibase.changelog] Changeset changelog.mysql.xml::1692446190573-1::wesome (generated) failed, but failOnError was false.  Error: Table 'appledb.apple' doesn't exist [Failed SQL: (1146) INSERT INTO appledb.apple (apple_id, apple_name) VALUES (1, 'Macintosh')]
Running Changeset: changelog.mysql.xml::1692446190573-2::wesome (generated)
INFO [liquibase.ui] Running Changeset: changelog.mysql.xml::1692446190573-2::wesome (generated)
INFO [liquibase.changelog] Table vendor created

follow us on