ConstraintMustExist

The ConstraintMustExist check allows you to specify an individual table or column by name to check that it has one or more specific constraints.

  • Checks require specific parameters noted in the table.
  • Customizations to checks configuration are stored in the liquibase.checks-settings file.

Uses

You can use the check to ensure a specific table contains specific elements, such as a primary key, default values, or other constraints. This upholds the critical data architecture decisions and ensures that non-compliance problems are identified as early as possible.

Run the ConstraintMustExist check

In this example, we have a table.column name already and one or more required constraints exist. First, ensure you have a Liquibase Labs license, a valid URL property, and the checks-scope property includes database. The checks-scope property is set in the default properties file, environment variable, or any standard method. Then, check the table for constraints. Enable the ConstraintMustExist check by running the following command in the CLI:

liquibase checks enable --check-name=ConstraintMustExist

Liquibase then prompts you to establish a copy of this check and to initiate the customization work flow. To do this, the user must select options for each of the attributes below in the CLI.

Note: This check can not be enabled directly because one or more fields does not have a default value. Creating a copy of this check initiates the customization work flow.

Available attributes

Attribute Name Type Description Customization Default Value
SEVERITY String/enum

Set the severity to return a code of 0–4 when the check is triggered.

  • 'INFO' | 0
  • 'MINOR' | 1
  • 'MAJOR' | 2
  • 'CRITICAL' | 3
  • 'BLOCKER' | 4

'INFO' | 0

CONSTRAINT_OPERATOR String/enum

The location to look for the provided SEARCH_STRING value

 

  • STARTS_WITH – Value of SEARCH_STRING should be found at the beginning of the table.
  • ENDS_WITH – Value of SEARCH_STRING should be found at the end of the table.
  • CONTAINS – Value of SEARCH_STRING can be anywhere in the table.
  • REGEXP – Value of SEARCH_STRING is a regexp that should be matched.
  • ALL – Check all tables for specified constraint.

STARTS_WITH

TABLE_NAME String/enum Optional: The table name string or regex to match. (Note: leave blank to check all tables for column name.)    
COLUMN_NAME String/enum The column name string or regex to match.    
CONSTRAINT String/enum Enter the required constraint(s), separate NOT NULL, UNIQUE, PRIMARYKEY, FOREIGNKEY PRIMARYKEY
CASE_SENSITIVE Boolean

Set case sensitivity (options: true, false)

User entry of true or false is accepted case-insensitively

When operator=REGEXP, CASE_SENSITIVE prompt is not shown

true

MESSAGE String/enum

The output printed when the check detects a pattern match

The specified table 'TABLE_NAME' does not contain the required 'CONSTRAINT' constraint.

Note: Liquibase uses the java.util.regex engine to match regular expressions.

After you select your options, liquibase checks enable --check-name=ConstraintMustExist will run successfully.