Generating SQL to Update Database Schemas

There are two reasons you would want to generate SQL in Liquibase.

  1. You need to know exactly what is being done to your database.
  2. Your company policies prevent you from using Liquibase in certain environments.

Generating SQL can be helpful when you want to update your database schemas, but want to view those database changes before applying them. There are several commands that generate deployable SQL, these are:

update-sql command

Running the update-sql command tells Liquibase to evaluate all the changesets in your changelog, then generates the corresponding SQL for what will be deployed to the database so you can preview the changes.

To use the update-sql command, type the following into your command prompt:

liquibase update-sql --changelog-file=changelog.xml

update-sql command with the labels parameter

The update-sql command allows you to also run a --labels parameter to determine which changesets in the changelog to evaluate based on its label. You can view your labels in your changelog.

To use the update-sql command with the labels parameter, type the following into your command prompt:

liquibase update-sql --changelog-file=changelog.xml --output-file=update-sql.txt --labelFilter=setup

future-rollback-sql command

The future-rollback-sql command generates SQL that reverses changes you applied while using the update-sql command.

To run the future-rollback-sql command, type the following into your command prompt:

liquibase future-rollback-sql --changelog-file=changelog.xml

Note for Liquibase Pro Users

Liquibase Pro allows users to also produce deployable SQL for Stored Logic. If you are a Liquibase Pro user, and you are trying to run the future-rollback-sql command, make sure you add a <rollback> tag to your changesets so you output the correct SQL, as:

<changeSet  author="Liquibase Pro User"  id="1::createFunction-proCatalog"  objectQuotingStrategy="QUOTE_ALL_OBJECTS"  labels="createFunction,rollbackFunction, lbl-func">  
    <pro:createFunction  functionName="emailFunction"  path="sql/lbpro_master_func.sql"  schemaName="dbo"/>
    <rollback>
        <sqlFile  endDelimiter="GO"  path="sql/lbpro_master_func_rollback.sql"  splitStatements="true"  stripComments="true"/>
    </rollback>
</changeSet>