Preconditions can be attached to change logs or changesets to control the execution of an update based on the state of the database.
There are several reasons to use preconditions, including:
If desired, a precondition can be the only tag in a <changeSet>
.
Preconditions at the changelog level apply to all changeSets, not just those listed in the current changelog or its child changelogs.
The above changelog will only run if the database executed against is Oracle and the database user executing the script is “SYSTEM”. It will also only run the drop_Table command if there are no values in the “oldtable”.
Liquibase distinguishes between precondition “failures” (check failed) and “errors” (exception thrown in execution of check) and the reaction to both can be controlled via the “onFail” and “onError” attributes on the <preConditions>
tag. Since 1.8
Attribute | Description |
---|---|
onFail | What to do when preconditions fail (see below). |
onError | What to do when preconditions error (see below). |
onSqlOutput | What to do in updateSQL mode (see below). Since 1.9.5 |
onFailMessage | Custom message to output when preconditions fail. Since 2.0 |
onErrorMessage | Custom message to output when preconditions fail. Since 2.0 |
Value | Description |
---|---|
HALT | Immediately halt the execution of the entire change log. [DEFAULT] |
CONTINUE | Skip over the *changeSet*. Execution of the change set will be attempted again on the next update. Continue with the *changelog*. |
MARK_RAN | Skip over the change set, but mark it as executed. Continue with the change log. |
WARN | Output a warning and continue executing the *changeSet*/*changelog* as normal. |
Outside a changeSet (e.g. at the beginning of the change log), only HALT and WARN are possible values.
Value | Description |
---|---|
TEST | Run the changeSet in updateSQL mode. |
FAIL | Fail the preCondition in updateSQL mode. |
IGNORE | Ignore the preCondition in updateSQL mode (default). |
Conditional logic can be applied to preconditions using nestable <and>
, <or>
and <not>
tags. If no conditional tags are specified, it defaults to AND.
Examples:
Will check that the update is running on Oracle AND with the SYSTEM user, but will only generate a warning if the precondition fails.
Will require running on Oracle AND MySQL, which will always be false, unless a huge and unexpected merger takes place.
Will require running on Oracle OR MySQL which makes more sense than the above example.
Will require running as SYSTEM if executing against an Oracle database or running as SA if running against a MS-SQL database.
Passes if the database executed against matches the type specified.
Attribute | Description |
---|---|
type | Type of database expected. Multiple dbms values can be specified using comma separated values. required |
Passes if the database user executed under matches the username specified.
Attribute | Description |
---|---|
username | Database user script is expected to run as. required |
Passes if the specified change set has already been executed. Since 1.8
Attribute | Description |
---|---|
id | Change set "id". required |
author | Change set "author". required |
changeLogFile | File name (including classpath relative path) of change set. required |
Passes if the specified column exists in the database. Since 1.8
Attribute | Description |
---|---|
schemaName | Name of the table's schema. required |
tableName | Name of the column's table. required |
columnName | Name of column. required |
Passes if the specified table exists in the database. Since 1.8
Attribute | Description |
---|---|
schemaName | Name of the table's schema. required* |
tableName | Name of the table. required |
Passes if the specified view exists in the database. Since 1.8
Attribute | Description |
---|---|
schemaName | Name of the view's schema. required |
viewName | Name of the view. required |
Passes if the specified foreign key exists in the database. Since 1.8
Attribute | Description |
---|---|
schemaName | Name of the foreign key's schema. required |
foreignKeyName | Name of the foreign key. required |
Passes if the specified index exists in the database. Since 1.8
Attribute | Description |
---|---|
schemaName | Name of the index's schema. required |
indexName | Name of the index. required |
Passes if the specified sequence exists in the database. Since 1.8
Attribute | Description |
---|---|
schemaName | Name of the sequences's schema. required |
sequenceName | Name of the sequence. required |
Passes if the specified primary key exists in the database. Since 1.8
Attribute | Description |
---|---|
schemaName | Name of the primary key's schema. |
primaryKeyName | Name of the primary key. tableName OR primaryKeyName required |
tableName | Name of the table containing primary key. tableName OR primaryKeyName required Since 1.9 |
Executes an SQL string and checks the returned value. The SQL must return a single row with a single value. To check numbers of rows, use the “count” SQL function. To check for ranges of values, perform the check in the SQL and return a value that can be easily compared against.
Attribute | Description |
---|---|
expectedResult | Value to compare the SQL result to. required |
Checks whether given changelog parameter is present. If a value is also given, it only fails, if the value is not the same as given. Since 2.0
Attribute | Description |
---|---|
property | Name of the property to check for. required |
value | Required value for given property. |
Custom preconditions can be created by creating a class that implements the liquibase.precondition.CustomPrecondition interface. Parameters on custom classes are set through reflection based on the <param> sub-tags. Parameters are passed as strings to the custom precondition.
Attribute | Description |
---|---|
className | Name of custom precondition class. required |
Attribute | Description |
---|---|
param | Parameter to pass to the custom precondition. |
Attribute | Description |
---|---|
name | Name of the parameter to set. required |
value | String value to set parameter to. required |
Preconditions are checked at the beginning of the execution of a particular changelog. If you use the “include” tag and only have preconditions on the child changelog, those preconditions will not be checked until the migrator reaches that file. This behavior may change in future releases, so don’t rely on this behavior.