diff --git a/Content/liquibase-pro/policy-checks/custom-policy-checks/home.htm b/Content/liquibase-pro/policy-checks/custom-policy-checks/home.htm index 6f8f4d644..a4f6d6846 100644 --- a/Content/liquibase-pro/policy-checks/custom-policy-checks/home.htm +++ b/Content/liquibase-pro/policy-checks/custom-policy-checks/home.htm @@ -8,134 +8,8 @@
Custom Policy Checks allow you to enforce compliance for a wide array of security, code standards, data quality, and other policies using Python scripts. In addition to
This is a
liquibase-checks-<version>.jar
and put it in the liquibase/lib
directory.pom.xml
file:Before creating a custom policy check with Python, we recommend being familiar with:
-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.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
Tool | -Version | -
---|---|
Python | -3.10.14 | -
GraalPy | -24.0.0 | -
liquibase checks show
- liquibase.checks-settings.conf
.custom-check-no-tables.py
.custom-check-no-tables.py
file and add the following custom policy check to it:The purpose of this sample check is to ensure that there are no tables in the database.
-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.
- CustomCheckNoTables
.CustomCheckNoTables
from CustomCheckTemplate
.The new check short name CustomCheckNoTables
and all of its associated information comes from the Python script you created. Your company may have their own coding standards that these scripts must adhere to.
1
. Options:This script looks to see if any tables exist and notifies you if one is detected.
- database
. The Python sample provided in this tutorial requires it.In general, you should set the scope to changelog
or database
depending on what your custom script does:
changelog
: for example, if your check looks for syntax patterns or attributes in your 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.It is a best practice for your custom checks to have only one scope, not both scopes.
-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.
-Option for advanced users: You can create
In this example, we will set the path to Scripts/custom-check-no-tables.py
.
REQUIRES_SNAPSHOT (options: true, false) [false]:
- 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!
-To run your custom check, you must use the checks run
command.
checks run
command, you must set --checks-scripts-enabled=true
in the CLI or set LIQUIBASE_COMMAND_CHECKS_RUN_CHECKS_SCRIPTS_ENABLED=TRUE
via environment variable.checks run
command, you can set the --checks-scripts-path
parameter, LIQUIBASE_COMMAND_CHECKS_RUN_CHECKS_SCRIPTS_PATH
environment variable, and other standard methods.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
- 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
.
Custom Policy Checks are Python scripts that allow you run advanced policies using the
While it's possible to configure the behavior of many built-in
This is a