diff JSON command

Starting with Liquibase 3.9.0, you can automate drift detection at scale in your database schemas with the Liquibase Pro machine-readable JSON diff output. The diff --format=json command is a Liquibase Pro extension to the existing diff command.

Uses

The diff command in a JSON format is typically used to detect drift between a model schema and a database's actual schema. You can use the output of the diff command in a JSON format as an input to automation processes. For example, the results in a JSON diff can be parsed in your build system to trigger alerts, generate reports, or run a diffChangeLog command.

After running the command, you will receive a JSON-structured object listing the differences between two databases, which values are configured in your liquibase.properties or Maven POM file, or passed as command line arguments under the url and referenceUrl keys.

Running the diff JSON command

Running the diff JSON command requires two URLs:

  • referenceURL – the source for the comparison. The referenceURL attribute represents your source database which is the starting point and the basis for the database you want to compare.
  • url – the target of the comparison. The URL attribute stands for your target database which you want to compare to the source database. You typically perform actions and run the commands against this database

To compare two databases or schemas:

The first option is to run the diff command and pass the attributes needed for your source database and target database. You can do this by running the following:

liquibase
--driver=oracle.jdbc.OracleDriver
--classpath=ojdbc14.jar
--url="jdbc:oracle:thin:@<IP OR HOSTNAME>:<PORT>:<SERVICE NAME OR SID>"
--username=<USERNAME>
--password=<PASSWORD>
--referenceUrl="jdbc:oracle:thin:@<IP OR HOSTNAME>:<PORT>:<SERVICE NAME OR SID>"
--referenceUsername=<USERNAME>
--referencePassword=<PASSWORD>				
diff --format=json

Note: When running diff against two different databases, the classpath property should reference both .jar files. Use the path separator that is correct for your operating system (a semicolon on Windows, a colon on Mac or Linux).

Example: classpath: ojdbc7.jar:postgresql-42.2.8.jar

Alternatively, you can configure the liquibase.properties file to include your driver, classpath, and URL for both databases, and then run the following command:

liquibase diff --format=json

Note: For information, see Creating and configuring a liquibase.properties file.

By default, the result is sent to STDOUT, which provides flexibility to use the result in other tools or in a processing pipeline. You can also have your output in a file using the --outputFile=<filename> attribute.

liquibase --outputFile=myfile.json diff --format=json

The diff JSON command produces a list of categories along with one of the following descriptions:

  • Missing: there are objects on your source database (referenceURL) that are not on your target database (URL).
  • Unexpected: there are objects on your target database (URL) that are not on your source database (referenceURL).
  • Changed: the object as it exists on the source database (referenceURL) is different than as it exists in the target database (URL).

Note: The changed description will not specify the type of change applied to your database. Run the diffChangeLog command to generate a changelog that will apply the changes to the target database.

Liquibase Pro diff JSON categories:

  • Check Constraint
  • Package
  • Package Body
  • Function
  • Trigger
  • Synonyms

Also, Liquibase Pro includes Liquibase Community diff categories when running --format=json:

  • Catalog
  • Column
  • Foreign Key
  • Index
  • Primary Key
  • Schema
  • Sequence
  • Procedure
  • Unique Constraints
  • View

Note: Liquibase does not currently check datatype length.

Filtering diff types

Liquibase allows you to use diffType attribute to filter the types of objects you want to compare. Multiple filters can be added to the attribute as a comma-separated list. If no diffTypes are specified, all objects are considered.

Example: liquibase --diffTypes=tables,indexes,views diff

Related Links