Customizing a Policy Check
Some default Policy Checks have customization options that allow you to adjust the parameters used to determine whether a changeset meets the criteria of the check. You can adjust these parameters using the copy or customize command.
Step-by-step
In this example we will create a check that finds table truncation statements in a sample changelog by generating a copy of the SqlUserDefinedPatternCheck
check.
- Run this command in the CLI to create a copy of the check. The copy of the check will become the customized policy check:
liquibase checks copy --check-name=SqlUserDefinedPatternCheck
- When you see the below instructions in the CLI, provide a short name. A short name is a descriptive name that indicates what you want the check to search for in the database or changelog.
In this example we will provideSqlTruncateCheck
as the short name because we will be searching for any instances ofTruncate
.
The CLI will indicate that the new check was created from SqlUserDefinedPattern check successfully.:New check 'SqlTruncateCheck' created from 'SqlUserDefinedPatternCheck'
- When you see the below instructions in the CLI, provide a search string.
In this example, we will useTRUNCATE
because this check will search the database or changelog for theTRUNCATE
label.Set 'SEARCH_STRING' (current: 'null'): (?i)^(\s*)(TRUNCATE)
- When you see the below instructions in the CLI, provide a message for your check. This message should indicate that the check you are creating is triggered. You can type custom values, or press the Enter/Return key to use the default values:
- In this example, we will use this as our message:
- The CLI will prompt you to review the table to confirm the customization:
- Add the following to the
changelog.sql
file and save your changes: - Execute the
run
subcommand again:
Set 'MESSAGE' (current: 'A match for regular expression <SEARCH_STRING> was detected in Changeset <CHANGESET>.') (default "A match for regular expression <SEARCH_STRING> was detected in Changeset <CHANGESET>."):
Table truncation detected. Review this changeset to ensure data is not unintentionally deleted.
Customization complete. Review the table below to confirm your changes.
After you complete these tasks, the checks configuration displays for your review.
--changeset other.dev:4 labels:v0 context:all
truncate person;
liquibase checks run
The new check indicates that there is an issue with your new changeset:
Checks completed validation of the changelog and found the following issues:
Changeset ID: 5
Changeset Filepath: changelog.sql
Check Name: Check for specific patterns in sql (SqlUserDefinedPatternCheck)
Message: Table truncation detected. Review this changeset to ensure
data is not unintentionally deleted.
With this knowledge provided by the customized policy check, you can investigate the table truncation and resolve it before proceeding with any deployments to the database.