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.

  1. Run this command in the CLI to create a copy of the check. The copy of the check will become the customized policy check:
  2. liquibase checks copy --check-name=SqlUserDefinedPatternCheck
  1. 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 provide SqlTruncateCheck as the short name because we will be searching for any instances of Truncate.
    The CLI will indicate that the new check was created from SqlUserDefinedPattern check successfully.:
    New check 'SqlTruncateCheck' created from 'SqlUserDefinedPatternCheck'
  2. When you see the below instructions in the CLI, provide a search string.
    In this example, we will use TRUNCATE because this check will search the database or changelog for the TRUNCATE label.
    
    Set 'SEARCH_STRING' (current: 'null'):
    (?i)^(\s*)(TRUNCATE)
    
  3. 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:
  4. 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>."):
    
  5. In this example, we will use this as our message:
  6. Table truncation detected.  Review this changeset to ensure data is not unintentionally deleted.
    
  7. The CLI will prompt you to review the table to confirm the customization:
  8. Customization complete. Review the table below to confirm your changes.

    After you complete these tasks, the checks configuration displays for your review.

  9. Add the following to the changelog.sql file and save your changes:
  10. --changeset other.dev:4 labels:v0 context:all
    truncate person;
  11. Execute the run subcommand again:
  12. 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.