fkNames

fkNames is a custom policy check that ensures all foreign key names have the pattern FK_<child table>_<parent table>.

Learn how to create and customize the fkNames Liquibase Custom Policy Check using a Python script.

This example works for relational databases. You can use this check as it is or customize it further to fit your needs in your SQL database.

For a conceptual overview of this feature, see Liquibase Pro Custom Policy Checks.

Scope Database
Database Relational

Step-by-step

These steps describe how to create the Custom Policy Check. It does not exist by default in Liquibase Pro.

  1. Create a Check Settings file: Use the Checks Settings Configuration File
  2. Add this code to your Checks Settings file:

    Copy

    fkNames Python Script

    ###
    ### This script ensures all foreign key names have the pattern FK_<child table>_<parent table>
    ###
    ### Notes:
    ### 1. Only basic CREATE or ALTER statements are supported
    ### 2. Constraint names must be provided (not auto-generated)
    ### 3. Only first foreign key name is checked in CREATE statement
    ###

    ###
    ### Helpers come from Liquibase
    ###
    import sys
    import liquibase_utilities

    ###
    ### Functions
    ###

    ###
    ### Retrieve log handler
    ### Ex. liquibase_logger.info(message)
    ###
    liquibase_logger = liquibase_utilities.get_logger()

    ###
    ### Retrieve status handler
    ###
    liquibase_status = liquibase_utilities.get_status()

    ###
    ### Retrieve all changes in changeset
    ###
    changes = liquibase_utilities.get_changeset().getChanges()

    ###
    ### Loop through all changes
    ###
    for change in changes:
        ###
        ### LoadData change types are not currently supported
        ###
        if "loaddatachange" in change.getClass().getSimpleName().lower():
            liquibase_logger.info("LoadData change type not supported. Statement skipped.")
            continue
        ###
        ### Retrieve sql as string, remove extra whitespace
        ###
        raw_sql = liquibase_utilities.strip_comments(liquibase_utilities.generate_sql(change)).casefold()
        raw_sql = " ".join(raw_sql.split())
        ###
        ### Split sql into statements
        ###
        raw_statements = liquibase_utilities.split_statements(raw_sql)
        for raw_statement in raw_statements:
            ###
            ### Split raw_statement into list, look for create/alter table, remove schema if provided
            ###
            sql_list = raw_statement.split()
            ###
            ### CREATE TABLE NAME (column1 type1, column2 type2, ...) CONSTRAINT NAME FOREIGN KEY (column) REFERENCES TABLE (column)
            ### ALTER TABLE NAME ADD CONSTRAINT NAME FOREIGN KEY (column) REFERENCES TABLE (column)
            ###
            try:
                if (sql_list[0] == "create" or sql_list[0] == "alter") and (sql_list[1] == "table"):
                    ### Child (current) table name
                    table_name_child = sql_list[2].split(".")[-1]
                    start = table_name_child.rfind("(")
                    if start != -1:
                        table_name_child = table_name_child[0:start]
                    ### FK name
                    start = sql_list.index("foreign")
                    fk_name_current = sql_list[start - 1].split(".")[-1]
                    ### Parent table name
                    start = sql_list.index("references", start)
                    table_name_parent = sql_list[start + 1].split(".")[-1]
                    start = table_name_parent.rfind("(")
                    if start != -1:
                        table_name_parent = table_name_parent[0:start]
                else:
                    raise UserWarning
            except (IndexError, ValueError):
                liquibase_logger.warning(f"Unsupported Create/Alter statement skipped: {raw_statement}")
                continue
            except UserWarning:
                liquibase_logger.info(f"Non Create/Alter statement skipped: {raw_statement}")
                continue
            ###
            ### Compare constraint name to pattern
            ###
            fk_name_standard = f"fk_{table_name_child}_{table_name_parent}"
            if fk_name_standard not in fk_name_current:
                liquibase_status.fired = True
                status_message = str(liquibase_utilities.get_script_message()).replace("__NAME_CURRENT__", f"\"{fk_name_current}\"")
                status_message = status_message.replace("__NAME_STANDARD__", f"\"{fk_name_standard}\"")
                liquibase_status.message = status_message
                sys.exit(1)


    ###
    ### Default return code
    ###
    False
  3. Initiate the customization process. In the CLI, run this command:
    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 CustomCheckTemplate and initiate the customization workflow.

  4. Give your check a short name so you can easily identify what Python script it is associated with (up to 64 alpha-numeric characters only).
    In this example we will name the check:
    fkNames

  5. Set the Severity to return a code of 0-4 when triggered. These severity codes allow you to determine if the job moves forward or stops when this check triggers.
    Learn more here: Use Policy Checks in Automation: Severity and Exit Code
    options: 'INFO'=0, 'MINOR'=1, 'MAJOR'=2, 'CRITICAL'=3, 'BLOCKER'=4

  6. Set SCRIPT_DESCRIPTION. In this example, we will set the description to:
    This script triggers when any foreign key names have the pattern FK_<child table>_<parent table>.
  7. Set SCRIPT_SCOPE. In this example, we will set the scope to changelog.
    • changelog: for example, if your check looks for syntax patterns or attributes in your Liquibase Changelog (the changes you author in your repository). With this value, the check runs once per changeset.
  8. Set the SCRIPT_MESSAGE. This message will display when the check is triggered. In this example we will use:
    Foreign key name __NAME_CURRENT__ must include parent and child table names (__NAME_STANDARD__).
  9. Set the SCRIPT_PATH. This is the relative path where your script is stored in relation to the changelog specified in --changelog-file, whether it is stored locally or in a repository.
    In this example, we will set the path to: scripts/fk-names.py.
  10. This policy check does not require a SCRIPT_ARGUMENT, so leave this field blank.
  11. Set the REQUIRES_SNAPSHOT. If your script scope is changelog, set whether the check requires a database snapshot. Specify true if your check needs to inspect database objects. (If your script scope is database, Liquibase always takes a snapshot, so this prompt does not appear.)
  12. Note: The larger your database, the more performance impact a snapshot causes. If you cannot run a snapshot due to memory limitations, see Memory Limits of Inspecting Large Schemas.

You have now successfully created and customized a policy check!

Next steps

Related Topics

Sample Custom Policy Check Scripts