Checks Python API Helper Scripts

Liquibase provides an API that contains helper scripts to enable users to write Python custom policy checks. You can implement any of the helper scripts listed below into your custom policy check to narrow down the content you are searching for. Learn more about how to use these functions by reading our Custom Policy Checks documentation.

Changeset attributes

These attributes are functions available to you after importing the liquibase_changesets file that allow you to pull this content from the given changeset path. The changeset is always the input parameter for these attributes.

Function Return
get_author(changeset) Provides the changeset author
get_contexts(changeset) Provides the changeset contexts
get_file_path(changeset) Provides the changeset file path
get_id(changeset) Provides the changeset ID
get_labels(changeset) Provides the changeset labels
is_always_run(changeset) Provides the changeset alwaysRun value
is_run_on_change(changeset) Provides the changeset runOnChange value

Database attributes

These are functions available to you after importing liquibase_database file and they are functions to access, or return, database attributes. The database is always the input parameter for these attributes.

Function Return
get_database_product_name(database) Provides the database product name
get_database_product_version(database) Provides the database product version
get_default_schema_name(database) Provides the default schema for this database
get_short_name(database) Provides the database short name

JSON utilities

These are functions available to you after importing the liquibase_json file and they are functions to access, or return, JSON attributes. These are functions to parse a JSON snapshot and access, or return, Dict objects.

Function Return Input Parameter
get_column(snapshot, table_name, column_name) Provides the specified Column Dict object for a table

snapshot: the snapshot to parse

table_name: the name of the table to search for

column_name: the name of the column to search for

get_column_size(column) Provides the specified Column's size column: the Column Dict object
get_column_type_name(column) Provides the specified Column's type name column: the Column Dict object
get_columns(snapshot, table_name)

Provides a list of Column Dict objects for a table such as a JSON serialized representation of the Liquibase internal DatabaseObject class.

Example output:

{'column': {'name': 'name', 'nullable': True, 'order': '1!{java.lang.Integer}', 'relation': 'liquibase.structure.core.Table#0eab104', 'snapshotId': '0eab105', 'type': {'characterOctetLength': '20!{java.lang.Integer}', 'columnSize': '20!{java.lang.Integer}', 'columnSizeUnit': 'BYTE!{liquibase.structure.core.DataType$ColumnSizeUnit}', 'dataTypeId': '1!{java.lang.Integer}', 'radix': '10!{java.lang.Integer}', 'typeName': 'bpchar'}}}

snapshot: the snapshot to parse

table_name: the name of the table to search for

get_indexes(snapshot, table_name) Provides a List of Index Dict objects for a table

snapshot: the snapshot to parse

table_name: the name of the table to search for

get_primary_key(snapshot, table_name) Provides a Primary Key Dict object for a table

snapshot: the snapshot to parse

table_name: the name of the table to search for

get_table(snapshot, table_name) Provides a list of Table Dict objects

snapshot: the snapshot to parse

table_name: the name of the table to search for

get_tables(snapshot) Provides a list of Table Dict objects

snapshot: the snapshot to parse

Liquibase utilities

Utilities are general script helpers that retrieve content frequently searched for.

Function Return Input Parameter
generate_sql(change)

Generates a specific SQL Change Type change and database

Example:

changes = liquibase_utilities.get_changes() # get the changes we're looking at
sql = liquibase_utilities.generate_sql(change[0]) # get the first change in the list
if "something bad" in sql:
 status.fired = True
sys.exit(1) # exit from the check


change: the change to generate SQL from

return: the SQL of the change

get_arg(name) Return the value of a script argument name: the argument to find

return:
the value of the argument
get_binding(key) Access the binding object at the given key key: the binding to access

return:
the binding
get_cache(key, default_value)

Returns the stored key/value pairs held by a dict object that are available between script executions. For example, you can count the number of times a check triggers by adding a counter that updates for each feature.
If there is no current value, then put the default value.

key: the look up key
default_value: the value to put for the key if no value present

return:
the current value or the default
get_changes()

Returns the set of changes that the check is actively examining

return: the liquibase change objects

get_changeset()

Returns the changeset object Liquibase is actively examining

return: the liquibase change set object
get_column_type(database_object) Get the data type of the column of a database object.
examples include: varchar, integer, etc.
database_object: the database object

return:
the type of the column, or None if the database object is not a column
get_database() Get the liquibase database object

return: the liquibase database object

get_database_object() Return the DatabaseObject that is being referenced in a database-scoped check return: the current database object being checked
get_database_snapshot() Return the DatabaseSnapshot as a String return: the database snapshot string
get_dbutil() Return the dbutil object used to check for object existence or to snapshot the DatabaseObject and return it return: the db_util object
get_logger() Get the liquibase logger return: the liquibase logger
get_object_type_name(database_object) Get the object type string of a given database object database_object: The database_object to return the type for

return:
the type as a string
get_script_message() Get the message for the script return: the message
get_script_path() Return the path of the script return: the path of the script
get_snapshot() Return the snapshot as a JSON object. return: The snapshot JSON object or None if no snapshot is available
get_status() Return the Status object which will be used to set the check fired status and return a message return: the status object
has(object_type, schema_name, object_name) Returns true if there is an object of this type and name in the schema object_type: the object type
schema_name: the schema name
object_name: the object name

return:
true if there is an object that matches this description, false otherwise
has_relation(object_type, object_name, relation_type, relation_schema_name, relation_name) Returns true if there is an object of this type that has a relation object that matches the relation type, schema, and name object_type: the object type
object_name: the object name
relation_type: the type of relation (Table or View)
relation_schema_name: the schema of the relation object
relation_name: the name of the relation

return:

Returns true if there is an object of this type that has a relation object that matches the relation type, schema, and name.
Returns false if no items match the provided criteria.
is_catalog(database_object) Check if the database object is a catalog database_object: the database object to check

return:
true if the object is a catalog, false otherwise
is_check_constraint(database_object) Check if the database object is a check constraint database_object: the database object to check

return:
true if the object is a check constraint, false otherwise
is_column(database_object) Check if the database object is a column database_object: the database object to check

return:
true if the object is a column, false otherwise
is_database_package(database_object) Check if the database object is a database package database_object: the database object to check

return:
true if the object is a database package, false otherwise
is_database_package_body(database_object) Check if the database object is a database package body database_object: the database object to check

return:
true if the object is a database package body, false otherwise
is_foreign_key(database_object) Check if the database object is a foreign key database_object: the database object to check

return:
true if the object is a foreign key, false otherwise
is_function(database_object) Check if the database object is a function database_object: the database object to check

return:
true if the object is a function, false otherwise
is_index(database_object) Check if the database object is an index database_object: the database object to check

return:
true if the object is a index, false otherwise
is_primary_key(database_object) Check if the database object is a primary key database_object: the database object to check

return:
true if the object is a primary key, false otherwise
is_schema(database_object) Check if the database object is a schema database_object: the database object to check

return:
true if the object is a schema, false otherwise
is_sequence(database_object) Check if the database object is a sequence database_object: the database object to check

return:
true if the object is a sequence, false otherwise
is_stored_database_logic(database_object) Check if the database object is stored database logic database_object: the database object to check

return:
true if the object is stored database logic, false otherwise
is_stored_procedure(database_object) Check if the database object is a stored procedure database_object: the database object to check

return:
true if the object is a stored procedure, false otherwise
is_synonym(database_object) Check if the database object is a synonym database_object: the database object to check

return:
true if the object is a synonym, false otherwise
is_table(database_object) Check if the database object is a table database_object: the database object to check

return:
true if the object is a table, false otherwise
is_trigger(database_object) Check if the database object is a trigger database_object: the database object to check

return:
true if the object is a trigger, false otherwise
is_unique_constraint(database_object) Check if the database object is a unique constraint database_object: the database object to check

return:
true if the object is a unique constraint, false otherwise
is_view(database_object) Check if the database object is a view database_object: the database object to check

return:
true if the object is a view, false otherwise
put_cache(key, value) Places the stored key/value pairs held by a dict object that are available between script executions. key The key to use
value The value to put in the cache
query_for_list(SQL, SQL_file, end_delimiter) Execute a SQL statement or script SQL: the SQL to execute
SQL_file: the SQL file to execute
end_delimiter: the end delimiter to use

return:
the results of the SQL as a list
snapshot_object(object_type, object_name, relation_type, schema_name, relation_name) Provides a Liquibase model object that represents a database object object_type: the type of the object, like Table
object_name: the objects name
relation_type: a type of relation that we need to snapshot to access the object
schema_name: the schema for the relation
relation_name: the name of the relation

return:
the liquibase model for the database object
split_SQL(SQL_string, strip_comments_flag, end_delimiter, changeset) Provides an array of SQL lines SQL_string: the SQL to process
strip_comments_flag: true to strip out comments
end_delimiter: the end delimiter to use while processing the SQL
changeset: the change set associated with the SQL

return:
An array of SQL lines
split_statements(SQL_string) Split a string of SQL into individual statements SQL_string: the SQL string to split

return:
the list of SQL strings
strip_comments(SQL_string) Strip comments from a SQL string SQL_string: the SQL to strip

return:
the SQL string with comments removed
tokenize(statement) Tokenize a statement which was created by SQLparse and returns a list statement: the statement to tokenize

return:
the tokenized statement as a list