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

Methods to return Liquibasechangeset attributes

Function Definition Input Parameter
get_author(changeset) Returns the changeset author changeset
get_contexts(changeset) Returns the changeset contexts changeset
get_file_path(changeset) Returns the changeset file path changeset
get_id(changeset) Returns the changeset ID changeset
get_labels(changeset) Returns the changeset labels changeset
is_always_run(changeset) Returns the changeset alwaysRun value changeset
is_run_on_change(changeset) Returns the changeset runOnChange value changeset

Database attributes

Methods to return database attributes

Function Definition Input Parameter
get_database_product_name(database) Returns the database product name database
get_database_product_version(database) Returns the database product version database
get_default_schema_name(database) Returns the default schema for this database database
get_short_name(database) Returns the database short name database

JSON snapshot and return Dict objects

Methods to parse a JSON snapshot and return Dict objects

Function Definition Input Parameter
get_column(snapshot, table_name, column_name) Return 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) Return the specified Column's size column: the Column Dict object
get_column_type_name(column) Return the specified Column's type name column: the Column Dict object
get_columns(snapshot, table_name) Return a list of Column Dict objects for a table

snapshot: the snapshot to parse

table_name: the name of the table to search for

get_indexes(snapshot, table_name) Return 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) Return 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) Return a list of Table Dict objects

snapshot: the snapshot to parse

table_name: the name of the table to search for

get_tables(snapshot) Return 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 Definition Input Parameter
generate_sql(change) Generate the SQL for this change and database

change: the change to generate sql from

return: the sql of the change

get_arg(name) Return the value of 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) Return the results cache Dict object from the bindings 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() Return a list of Liquibase Change objects

return: the liquibase change objects

get_changeset() Return the Liquibase Change Set object return: the liquibase change set object
get_column_type(database_object) Get the type of the column of a database object 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 a 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) Put the value in the cache 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) Return 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) Returns 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 create by sqlparse and return a list statement: the statement to tokenize

return:
the tokenized statement as a list