Modules

sqlalchemy_postgresql_audit

sqlalchemy_postgresql_audit.set_session_vars(connectable, **kwargs)[source]

Wrapper to set session settings.

This must be set in a transaction in order for these settings to be present.

Typical use cases would be a username coming from a web request, or a request UUID or a script name.

Parameters:
  • connectable – A connectable that we can execute on.
  • kwargs – key/value pairs of values to set.
Returns:

None

sqlalchemy_postgresql_audit.enable()[source]

Enable the advanced inspector and enables sqlalchemy and alembic event listeners.

sqlalchemy_postgresql_audit.install_audit_triggers(metadata, engine=None)[source]

Installs all audit triggers.

This can be used after calling metadata.create_all() to create all the procedures and triggers.

Parameters:
  • metadata – A sqlalchemy.sql.schema.MetaData
  • engine – A sqlalchemy.engine.Engine or None
Returns:

None or a str for the DDL needed to install all audit triggers.

sqlalchemy_postgresql_audit.uninstall_audit_triggers(metadata, engine=None)[source]

Uninstalls all audit triggers.

This can be used to remove all audit triggers.

Parameters:
  • metadata – A sqlalchemy.sql.schema.MetaData
  • engine – A sqlalchemy.engine.Engine or None
Returns:

None or a str for the DDL needed to uninstall all audit triggers.

sqlalchemy_postgresql_audit.ddl

sqlalchemy_postgresql_audit.ddl.get_audit_spec(table)[source]
sqlalchemy_postgresql_audit.ddl.get_create_trigger_ddl(target_columns, audit_columns, function_name, trigger_name, table_full_name, audit_table_full_name, session_settings=None)[source]
sqlalchemy_postgresql_audit.ddl.get_drop_trigger_ddl(function_name, trigger_name, table_full_name)[source]
sqlalchemy_postgresql_audit.ddl.install_audit_triggers(metadata, engine=None)[source]

Installs all audit triggers.

This can be used after calling metadata.create_all() to create all the procedures and triggers.

Parameters:
  • metadata – A sqlalchemy.sql.schema.MetaData
  • engine – A sqlalchemy.engine.Engine or None
Returns:

None or a str for the DDL needed to install all audit triggers.

sqlalchemy_postgresql_audit.ddl.uninstall_audit_triggers(metadata, engine=None)[source]

Uninstalls all audit triggers.

This can be used to remove all audit triggers.

Parameters:
  • metadata – A sqlalchemy.sql.schema.MetaData
  • engine – A sqlalchemy.engine.Engine or None
Returns:

None or a str for the DDL needed to uninstall all audit triggers.

sqlalchemy_postgresql_audit.dialect

sqlalchemy_postgresql_audit.dialect.DEFAULT_AUDIT_TABLE_FUNCTION_NAMING_CONVENTION = '%(schema)s_%(table_name)s_audit'

The audit table naming convetion. Change this at naming_conventions audit.table key.

sqlalchemy_postgresql_audit.dialect.DEFAULT_AUDIT_TABLE_NAMING_CONVENTION = '%(table_name)s_audit'

The audit table naming convetion. Change this at naming_conventions audit.table key.

sqlalchemy_postgresql_audit.dialect.DEFAULT_AUDIT_TABLE_TRIGGER_CONVENTION = '%(schema)s_%(table_name)s_audit'

The audit table naming convetion. Change this at naming_conventions audit.table key.

class sqlalchemy_postgresql_audit.dialect.PGAdvancedInspector(conn)[source]

A subclass of sqlalchemy.dialects.postgresql.base.PGInspector.

Enables advanced database reflection.

__module__ = 'sqlalchemy_postgresql_audit.dialect'
reflecttable(table, include_columns, *args, **kwargs)[source]

Given a Table object, load its internal constructs based on introspection.

This is the underlying method used by most dialects to produce table reflection. Direct usage is like:

from sqlalchemy import create_engine, MetaData, Table
from sqlalchemy.engine.reflection import Inspector

engine = create_engine('...')
meta = MetaData()
user_table = Table('user', meta)
insp = Inspector.from_engine(engine)
insp.reflecttable(user_table, None)
Parameters:
  • table – a Table instance.
  • include_columns – a list of string column names to include in the reflection process. If None, all columns are reflected.

sqlalchemy_postgresql_audit.install

sqlalchemy_postgresql_audit.plugin

class sqlalchemy_postgresql_audit.plugin.AuditPlugin(url, kwargs)[source]
__init__(url, kwargs)[source]

Construct a new CreateEnginePlugin.

The plugin object is instantiated individually for each call to create_engine(). A single Engine will be passed to the CreateEnginePlugin.engine_created() method corresponding to this URL.

Parameters:
  • url – the URL object. The plugin should inspect what it needs here as well as remove its custom arguments from the URL.query collection. The URL can be modified in-place in any other way as well.
  • kwargs – The keyword arguments passed to :func`.create_engine`. The plugin can read and modify this dictionary in-place, to affect the ultimate arguments used to create the engine. It should remove its custom arguments from the dictionary as well.
__module__ = 'sqlalchemy_postgresql_audit.plugin'
sqlalchemy_postgresql_audit.plugin.enable()[source]

Enable the advanced inspector and enables sqlalchemy and alembic event listeners.

sqlalchemy_postgresql_audit.session

sqlalchemy_postgresql_audit.session.set_session_var_stmt(**kwargs)[source]

Returns proper sql statements for setting session settings.

Namespaces all settings under audit.* namespace.

e.g.

set_session_var_stmt(foo='bar', baz='foobaz')
# set local "audit.foo" = 'bar'; set local "audit.baz" = 'foobaz';
Parameters:kwargs – key/value pairs of values to set.
Returns:a str, valid to set the relevant settings.
sqlalchemy_postgresql_audit.session.set_session_vars(connectable, **kwargs)[source]

Wrapper to set session settings.

This must be set in a transaction in order for these settings to be present.

Typical use cases would be a username coming from a web request, or a request UUID or a script name.

Parameters:
  • connectable – A connectable that we can execute on.
  • kwargs – key/value pairs of values to set.
Returns:

None

sqlalchemy_postgresql_audit.templates

sqlalchemy_postgresql_audit.templates.make_audit_procedure(procedure_name, trigger_name, check_settings, audit_table_full_name, table_full_name, audit_columns, deletion_elements, updation_elements, insertion_elements)[source]

Return the string

Parameters:
  • procedure_name – The name for the procedure
  • trigger_name – The name for the trigger
  • check_settings – A list of settings checks. Used to validate that the settings are non-empty.
  • audit_table_full_name – The full name (including schema) of the audit table
  • table_full_name – The full name (including schema) of the audited table
  • audit_columns – A list of columns to be audited
  • deletion_elements – A list of column expressions suitable to use in the VALUES block of an insert statement.
  • updation_elements – A list of column expressions suitable to use in the VALUES block of an insert statement.
  • insertion_elements – A list of column expressions suitable to use in the VALUES block of an insert statement.
Returns:

A str of the full DDL needed to be executed to create the procedure and trigger.

sqlalchemy_postgresql_audit.templates.make_drop_audit_procedure(function_name, trigger_name, table_full_name)[source]

sqlalchemy_postgresql_audit.event_listeners

sqlalchemy_postgresql_audit.event_listeners._enable_alembic_event_listeners()[source]
sqlalchemy_postgresql_audit.event_listeners._enable_sqlalchemy_event_listeners()[source]
sqlalchemy_postgresql_audit.event_listeners.enable_event_listeners()[source]

sqlalchemy_postgresql_audit.event_listeners.sqlalchemy

Defines event listeners for:

  • creating table objects
  • generating trigger/procedure DDL for audit tables.
sqlalchemy_postgresql_audit.event_listeners.sqlalchemy.create_audit_table(target, parent)[source]

Create an audit table and generate procedure/trigger DDL.

Naming conventions can be defined for a few of the named elements:

  • audit.table: Controls the name of the table
  • audit.function: Controls the name of the function
  • audit.trigger: Controls the name of the trigger on the table

This function creates a new companion table to store row versions.

Any sqlalchemy.sql.schema.Column`s specified in `table.info['session_settings'] will be copied and included in the audit table.

This function will leave a key in the audited table:

table.info['audit.is_audited']

And a key in the audit table:

table.info['audit.is_audit_table']

Additionally you can find the relevant create/drop ddl at the followng keys:

table.info['audit.create_ddl']
table.info['audit.drop_ddl']
Parameters:
  • target – The sqlalchemy.sql.schema.Table to make an audit table for
  • parent – The sqlalchemy.sql.schema.MetaData to associate the audit table with.
Returns:

None

sqlalchemy_postgresql_audit.event_listeners.alembic

class sqlalchemy_postgresql_audit.event_listeners.alembic.ReversableExecute(sqltext, reverse_ddl, execution_options=None)[source]
__init__(sqltext, reverse_ddl, execution_options=None)[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'sqlalchemy_postgresql_audit.event_listeners.alembic'
reverse()[source]
sqlalchemy_postgresql_audit.event_listeners.alembic.compare_for_table(autogen_context, modify_table_ops, schema, tname, conn_table, metadata_table)[source]