Create a Custom Policy Check
This tutorial shows you how to create a Liquibase custom policy check. For a conceptual overview of this feature, see Custom Policy Checks.
Note: This is a Liquibase Pro feature, so you need a Liquibase Pro License Key to use it.
Prerequisites
Liquibase prerequisites
- Install Liquibase 4.29.0+
- Install the Liquibase Checks extension
- Download the
liquibase-checks-<version>.jar
and put it in theliquibase/lib
directory. - If you use Maven, add this dependency to your
pom.xml
file:
Copy<dependency>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-checks</artifactId>
<version>1.0.0</version>
</dependency> - Download the
- Java Development Kit 17+ (available for Open JDK and Oracle JDK)
- Linux, macOS, or Windows operating system
- Familiarity with Liquibase concepts: Changelog, Changeset, Policy Checks Commands, checks-scope, and snapshot
Python prerequisites
Before creating a custom policy check with Python, we recommend being familiar with:
- Python 3.10+
- Optional: General coding and Python best practices which will improve your check performance:
- Efficiently handling of structured data objects
- Effective and targeted parsing of text, objects, and SQL
- Using Regular Expressions and other pattern matching tools within Python
- Optional: Import and use external utility and helper files
- Using an IDE and
pip install liquibase-checks-python
to develop Python checks is optional, but will allow you to utilize auto-completion and auto-documentation of helper methods. - Checks Python API Helper Scripts
Tip: If you're new to Python, it is a best practice to read the official Python tutorials before making custom checks.
Downloading Python itself is not required to create custom checks in Liquibase, but it may be useful to test them. It is a best practice to test custom checks with Python 3.10.14+.
Tool | Version |
---|---|
Python | 3.10.14 |
GraalPy | 24.0.0 |
Create a checks settings file
- Create a checks settings file if you don't already have one. In the CLI, run this command:
- If you don't have a checks settings file, a prompt appears that allows you to create the configuration file. Confirm the file creation in the prompt. By default, the settings file is named
liquibase.checks-settings.conf
.
liquibase checks show
Create a new custom policy check
- Create a new file in your Liquibase working directory. This file will contain the Python script that is your custom policy check. In this example, we title our new file
custom_check_no_tables.py
. - Open the new
custom_check_no_tables.py
file and add the following custom policy check to it:
# import Liquibase helper scripts containing useful functions
import liquibase_utilities as lb
import sys
# define reusable variables
obj = lb.get_database_object() # database object to examine
status = lb.get_status() # Status object of the check
# write check logic
if lb.is_table(obj): # checks if the current object is a table
status.fired = True # indicates that the custom check has been triggered
status.message = "No tables allowed!" # message for Liquibase to return when check is triggered
sys.exit(1) # halt execution of the script
The purpose of this sample check is to ensure that there are no tables in the database.
Note: Liquibase will run the check against every object in the database, so this script doesn't need a Python looping mechanism to iterate through database objects.
Configure and customize your new check in the CLI
- Initiate the customization process. In the CLI, run this command:
- Give your check a short name for easier identification. In this example, we will name the check
CustomCheckNoTables
.
Now you have successfully created the checkCustomCheckNoTables
fromCustomCheckTemplate
. - Set the severity to return a code of 0-4 when triggered. In this example, we will set the severity to
1
. Options: 'INFO' | 0
'MINOR' | 1
'MAJOR' | 2
'CRITICAL' | 3
'BLOCKER' | 4
- Set the script description for the custom check. In this example we will set the description to:
- Set the script scope for the custom check to
database
. The Python sample provided in this tutorial requires it. changelog
: for example, if your check looks for syntax patterns or attributes in your Liquibase changelog. With this value, the check runs once per changeset.database
: for example, if your check looks for the presence of keys, indexes, or table name patterns in your database schema. With this value, the check runs once for each database object.-
Set the script message. This message will display when the check is triggered. In this example we will leave this blank, as we are handling the message in the script.
Tip: Option for advanced users: You can create Status Message Variables for Custom Policy Checks which are identified in your Python script.
- Set the script type. In Liquibase Pro 4.29.0, Python is the only available script type.
- Set the script path. This is the relative path where your script is stored, whether it is stored locally or in a repository.
- Set the script argument. This allows you to pass dynamic information into the custom policy check without modifying the Python code. The script arguments ensure that your Python scripts remain reusable with different variables.
- Set whether the check requires a snapshot:
liquibase checks customize --check-name=CustomCheckTemplate
The CLI prompts you to finish configuring your file. A message displays:
This check cannot be customized directly because one or more fields does not have a default value.
Liquibase will then create a copy of this check and initiate the customization workflow.
Tip: For ease of use, it is a best practice to name your custom check after your Python script.
This script looks to see if any tables exist and notifies you if one is detected.
In general, you should set the scope to changelog
or database
depending on what your custom script does:
Tip: It is a best practice for your custom checks to have only one scope, not both scopes.
In this example, we will set the path to Scripts/custom-check-no-tables.py
.
REQUIRES_SNAPSHOT (options: true, false) [false]:
Note: If your check requires a snapshot, it may need to query the database, which can impact performance. The larger your database, the more performance impact this causes.
You have now successfully created and customized a policy check!
Run your new check
To run your custom check, you must use the checks run
command. Liquibase provides additional security configuration parameters for this command to ensure you do not accidentally execute Python code on your database:
- Custom policy checks are disabled by default. Using the
checks run
command, you must set--checks-scripts-enabled=true
in the CLI or setLIQUIBASE_COMMAND_CHECKS_RUN_CHECKS_SCRIPTS_ENABLED=TRUE
via environment variable. - Custom policy check Python scripts can filter to specific directory paths. Using the
checks run
command, you can set the--checks-scripts-path
parameter,LIQUIBASE_COMMAND_CHECKS_RUN_CHECKS_SCRIPTS_PATH
environment variable, and other standard methods.- When set, the check's Python script must be in the specified path(s) to execute successfully.
- If you don't set a script path, Liquibase accepts any script path.
Warning: Custom policy checks are not isolated and can interact with both local file systems and network utilities like the targeted database. We recommend reviewing these checks prior to execution to ensure they only affect the intended object(s).
For example, if you enable custom checks via the CLI and want to run all policy checks, including your new check:
liquibase checks run --checks-scripts-enabled=true
If you instead only want to run policy checks with the scope database
(such as this check), you must set the --checks-scope
parameter to database
:
liquibase checks run --checks-scope=database --checks-scripts-enabled=true
If you instead only want to run this specific check, you must specify the check name with --check-name
parameter:
liquibase checks run --check-name=CustomCheckNoTables --checks-scripts-enabled=true
Troubleshooting
If the scope you specify in the CLI while creating your check is mismatched with what your Python code actually does, you may receive an error like this:
Error while executing script 'custom-check-no-tables.py': AttributeError: 'NoneType' object has no attribute 'getObjectTypeName' line: 7
The Python code provided in this tutorial calls on database objects. This means you necessarily have to set the scope to database
while you create the check. Conversely, if you are creating a check that calls on changelog objects, you necessarily have to set the scope to changelog
.