customChange

The customChange type creates a custom Change Type class.

Uses

Although Liquibase provides a wide range of database Change Types, you may want to create your own custom Change Type class. The customChange type allows you to run the needed code within a changeset.

You can typically use the customChange Change Type to run your script when you want to:

  • Loop through all the tables in the database and convert datatypes
  • Download data from a webserver and insert it into your database
  • Change the lighting color in your data center
  • Perform the other needed custom features

The customChange type is the way to integrate your custom code into the changelog. To create your own custom Change Type by integrating the custom code, follow these steps:

  1. Create a Java class that implements the liquibase.change.custom.CustomSqlChange or liquibase.change.custom.CustomTaskChange interface <customChange> tag in your changeset.
  2. Example of a class

    public class ExampleCustomTaskChange implements CustomTaskChange, CustomTaskRollback {
    private String helloTo;
    @SuppressWarnings({"UnusedDeclaration", "FieldCanBeLocal"})
        private ResourceAccessor resourceAccessor;
        public String getHelloTo() {
            return helloTo;
        }  
        public void setHelloTo(String helloTo) {
            this.helloTo = helloTo;
        }
        @Override
        public void execute(Database database) throws CustomChangeException {
            Scope.getCurrentScope().getLog(getClass()).info("Hello "+getHelloTo());
        } 
        @Override
        public void rollback(Database database) throws CustomChangeException, RollbackImpossibleException {
            Scope.getCurrentScope().getLog(getClass()).info("Goodbye "+getHelloTo());
        }
        @Override
        public String getConfirmationMessage() {
            return "Said Hello";
        }
        @Override
        public void setUp() throws SetupException {
        ;
        }
        @Override
        public void setFileOpener(ResourceAccessor resourceAccessor) {
            this.resourceAccessor = resourceAccessor;
        } 
        @Override
        public ValidationErrors validate(Database database) {
            return new ValidationErrors();
        }
    }

    Note: There is a settable helloTo and the execute(Database database) method in the class that is a part of the interface. You can put any code within the execute() method.

  3. Compile the created class, package it into a JAR file, and then add it to a Liquibase classpath.
  4. Add the class to your changelog as it is shown in the following examples:

Examples

databaseChangeLog:
-  changeSet:
     id:  21
     author: nvoxland
     changes:
     -  customChange:  {
         "class": "liquibase.change.custom.ExampleCustomTaskChange",
         "helloTo": "world" }
{
    "databaseChangeLog": [
        {
            "changeSet": {
                "id": "21",
                "author": "nvoxland",
                "changes": [
                    {
                        "customChange": {
                            "class": "liquibase.change.custom.ExampleCustomTaskChange",
                            "helloTo": "world"
                        }
                    }
                ]
            }
        }
    ]
}
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
    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-latest.xsd
        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">

    <changeSet id="21" author="nvoxland">
        <customChange class="liquibase.change.custom.ExampleCustomTaskChange">
            <param name="helloTo" value="world"/>
        </customChange>
    </changeSet>

</databaseChangeLog>

When that changeset is executed, helloTo will be set to "world" and the execute() method will be called to run what you specified.

Note: If your change can be rolled back, implement the class liquibase.change.custom.CustomSqlRollback as well.

CustomSqlChange and CustomTaskChange

The difference between CustomTaskChange and CustomSqlChange is the following:

  • In CustomTaskChange, you have the execute() method that executes the needed string.
  • In CustomSqlChange, there is a generateStatements(Database database) method that doesn't execute the needed string but returns the SQL that should be run. This makes the custom change work better with the update-sql command, which displays the SQL being run.

Database support

Database Notes Auto Rollback
DB2/LUW Supported No
DB2/z Supported No
Derby Supported No
Firebird Supported No
Google BigQuery Supported No
H2 Supported No
HyperSQL Supported No
INGRES Supported No
Informix Supported No
MariaDB Supported No
MySQL Supported No
Oracle Supported No
PostgreSQL Supported No
Snowflake Supported No
SQL Server Supported No
SQLite Supported No
Sybase Supported No
Sybase Anywhere Supported No