Module
Below are the core classes and functions of the pgtrigger
module.
Level clause
pgtrigger.Statement
module-attribute
For specifying statement-level triggers
When clause
pgtrigger.After
module-attribute
For specifying AFTER
in the when clause of a trigger.
pgtrigger.Before
module-attribute
For specifying BEFORE
in the when clause of a trigger.
pgtrigger.InsteadOf
module-attribute
For specifying INSTEAD OF
in the when clause of a trigger.
Operation clause
pgtrigger.Insert
module-attribute
For specifying INSERT
as the trigger operation.
pgtrigger.Update
module-attribute
For specifying UPDATE
as the trigger operation.
pgtrigger.Delete
module-attribute
For specifying DELETE
as the trigger operation.
pgtrigger.Truncate
module-attribute
For specifying TRUNCATE
as the trigger operation.
pgtrigger.UpdateOf
Referencing clause
pgtrigger.Referencing
For specifying the REFERENCING
clause of a statement-level trigger
Source code in pgtrigger/core.py
Timing clause
pgtrigger.Immediate
module-attribute
For deferrable triggers that run immediately by default
pgtrigger.Deferred
module-attribute
For deferrable triggers that run at the end of the transaction by default
Func clause
pgtrigger.Func
Allows for rendering a function with access to the "meta", "fields", and "columns" variables of the current model.
For example, func=Func("SELECT {columns.id} FROM {meta.db_table};")
makes it
possible to do inline SQL in the Meta
of a model and reference its properties.
pgtrigger.Func.render
Render the SQL of the function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model. |
required |
Returns:
Type | Description |
---|---|
str
|
The rendered SQL. |
Source code in pgtrigger/core.py
Conditions
pgtrigger.Condition
pgtrigger.AnyChange
AnyChange(
*fields: str,
exclude: Union[List[str], None] = None,
exclude_auto: Union[bool, None] = None
)
Bases: _Change
If any supplied fields change, trigger the condition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fields |
str
|
If any supplied fields change, trigger the condition. If no fields are supplied, defaults to all fields on the model. |
()
|
exclude |
Union[List[str], None]
|
Fields to exclude. |
None
|
exclude_auto |
Union[bool, None]
|
Exclude all |
None
|
Source code in pgtrigger/core.py
pgtrigger.AnyDontChange
AnyDontChange(
*fields: str,
exclude: Union[List[str], None] = None,
exclude_auto: Union[bool, None] = None
)
Bases: _Change
If any supplied fields don't change, trigger the condition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fields |
str
|
If any supplied fields don't change, trigger the condition. If no fields are supplied, defaults to all fields on the model. |
()
|
exclude |
Union[List[str], None]
|
Fields to exclude. |
None
|
exclude_auto |
Union[bool, None]
|
Exclude all |
None
|
Source code in pgtrigger/core.py
pgtrigger.AllChange
AllChange(
*fields: str,
exclude: Union[List[str], None] = None,
exclude_auto: Union[bool, None] = None
)
Bases: _Change
If all supplied fields change, trigger the condition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fields |
str
|
If all supplied fields change, trigger the condition. If no fields are supplied, defaults to all fields on the model. |
()
|
exclude |
Union[List[str], None]
|
Fields to exclude. |
None
|
exclude_auto |
Union[bool, None]
|
Exclude all |
None
|
Source code in pgtrigger/core.py
pgtrigger.AllDontChange
AllDontChange(
*fields: str,
exclude: Union[List[str], None] = None,
exclude_auto: Union[bool, None] = None
)
Bases: _Change
If all supplied don't fields change, trigger the condition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*fields |
str
|
If all supplied fields don't change, trigger the condition. If no fields are supplied, defaults to all fields on the model. |
()
|
exclude |
Union[List[str], None]
|
Fields to exclude. |
None
|
exclude_auto |
Union[bool, None]
|
Exclude all |
None
|
Source code in pgtrigger/core.py
pgtrigger.Q
Bases: Q
, Condition
Similar to Django's Q
object, allows building filter clauses based
on the old and new rows in a trigger condition.
Source code in pgtrigger/core.py
pgtrigger.F
Bases: F
Similar to Django's F
object, allows referencing the old and new
rows in a trigger condition.
Source code in pgtrigger/core.py
pgtrigger.IsDistinctFrom
Bases: Lookup
A custom IS DISTINCT FROM
field lookup for common trigger conditions.
For example, pgtrigger.Q(old__field__df=pgtrigger.F("new__field"))
.
pgtrigger.IsNotDistinctFrom
Bases: Lookup
A custom IS NOT DISTINCT FROM
field lookup for common trigger conditions.
For example, pgtrigger.Q(old__field__ndf=pgtrigger.F("new__field"))
.
Triggers
pgtrigger.Trigger
Trigger(
*,
name: str = None,
level: Level = None,
when: When = None,
operation: Operation = None,
condition: Union[Condition, None] = None,
referencing: Union[Referencing, None] = None,
func: Union[Func, str] = None,
declare: Union[List[Tuple[str, str]], None] = None,
timing: Union[Timing, None] = None
)
For specifying a free-form PL/pgSQL trigger function or for creating derived trigger classes.
Source code in pgtrigger/core.py
pgtrigger.Trigger.allow_migrate
True if the trigger for this model can be migrated.
Defaults to using the router's allow_migrate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model. |
required |
database |
Union[str, None]
|
The name of the database configuration. |
None
|
Returns:
Type | Description |
---|---|
bool
|
|
Source code in pgtrigger/core.py
pgtrigger.Trigger.compile
Create a compiled representation of the trigger. useful for migrations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model |
required |
Returns:
Type | Description |
---|---|
Trigger
|
The compiled trigger object. |
Source code in pgtrigger/core.py
pgtrigger.Trigger.disable
Disables the trigger for a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model. |
required |
database |
Union[str, None]
|
The name of the database configuration. |
None
|
Source code in pgtrigger/core.py
pgtrigger.Trigger.enable
Enables the trigger for a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model. |
required |
database |
Union[str, None]
|
The name of the database configuration. |
None
|
Source code in pgtrigger/core.py
pgtrigger.Trigger.exec_sql
exec_sql(
sql: str,
model: models.Model,
database: Union[str, None] = None,
fetchall: bool = False,
) -> Any
Conditionally execute SQL if migrations are allowed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sql |
str
|
The SQL string. |
required |
model |
Model
|
The model. |
required |
database |
Union[str, None]
|
The name of the database configuration. |
None
|
fetchall |
bool
|
True if all results should be fetched |
False
|
Returns:
Type | Description |
---|---|
Any
|
A psycopg cursor result |
Source code in pgtrigger/core.py
pgtrigger.Trigger.format_sql
pgtrigger.Trigger.get_condition
Get the condition of the trigger.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model. |
required |
Returns:
Type | Description |
---|---|
Condition
|
The condition. |
pgtrigger.Trigger.get_declare
Gets the DECLARE part of the trigger function if any variables are used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model |
required |
Returns:
Type | Description |
---|---|
List[Tuple[str, str]]
|
A list of variable name / type tuples that will |
List[Tuple[str, str]]
|
be shown in the DECLARE. For example [('row_data', 'JSONB')] |
Source code in pgtrigger/core.py
pgtrigger.Trigger.get_func
Returns the trigger function that comes between the BEGIN and END clause.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model |
required |
Returns:
Type | Description |
---|---|
Union[str, Func]
|
The trigger function as a SQL string or pgtrigger.Func object. |
Source code in pgtrigger/core.py
pgtrigger.Trigger.get_installation_status
get_installation_status(
model: models.Model, database: Union[str, None] = None
) -> Tuple[str, Union[bool, None]]
Returns the installation status of a trigger.
The return type is (status, enabled), where status is one of:
INSTALLED
: If the trigger is installedUNINSTALLED
: If the trigger is not installedOUTDATED
: If the trigger is installed but has been modifiedIGNORED
: If migrations are not allowed
"enabled" is True if the trigger is installed and enabled or false if installed and disabled (or uninstalled).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model. |
required |
database |
Union[str, None]
|
The name of the database configuration. |
None
|
Returns:
Type | Description |
---|---|
Tuple[str, Union[bool, None]]
|
A tuple with the installation and enablement status. |
Source code in pgtrigger/core.py
pgtrigger.Trigger.get_pgid
The ID of the trigger and function object in postgres
All objects are prefixed with "pgtrigger_" in order to be discovered/managed by django-pgtrigger.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model. |
required |
Returns:
Type | Description |
---|---|
str
|
The Postgres ID. |
Source code in pgtrigger/core.py
pgtrigger.Trigger.get_uri
The URI for the trigger.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model |
required |
Returns:
Type | Description |
---|---|
str
|
The URI in the format of the " |
Source code in pgtrigger/core.py
pgtrigger.Trigger.install
Installs the trigger for a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model. |
required |
database |
Union[str, None]
|
The name of the database configuration. |
None
|
Source code in pgtrigger/core.py
pgtrigger.Trigger.register
Register model classes with the trigger
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*models |
Model
|
Models to register to this trigger. |
()
|
Source code in pgtrigger/core.py
pgtrigger.Trigger.render_condition
Renders the condition SQL in the trigger declaration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model. |
required |
Returns:
Type | Description |
---|---|
str
|
The rendered condition SQL |
Source code in pgtrigger/core.py
pgtrigger.Trigger.render_declare
Renders the DECLARE of the trigger function, if any.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model. |
required |
Returns:
Type | Description |
---|---|
str
|
The rendered declare SQL. |
Source code in pgtrigger/core.py
pgtrigger.Trigger.render_execute
Renders what should be executed by the trigger. This defaults to the trigger function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model |
required |
Returns:
Type | Description |
---|---|
str
|
The SQL for the execution of the trigger function. |
Source code in pgtrigger/core.py
pgtrigger.Trigger.render_func
Renders the func.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model |
required |
Returns:
Type | Description |
---|---|
str
|
The rendered SQL of the trigger function |
Source code in pgtrigger/core.py
pgtrigger.Trigger.uninstall
Uninstalls the trigger for a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model. |
required |
database |
Union[str, None]
|
The name of the database configuration. |
None
|
Source code in pgtrigger/core.py
pgtrigger.Trigger.unregister
Unregister model classes with the trigger.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*models |
Model
|
Models to unregister to this trigger. |
()
|
Source code in pgtrigger/core.py
pgtrigger.Trigger.validate_name
Verifies the name is under the maximum length and has valid characters.
Raises:
Type | Description |
---|---|
ValueError
|
If the name is invalid |
Source code in pgtrigger/core.py
pgtrigger.Protect
Protect(
*,
name: str = None,
level: Level = None,
when: When = None,
operation: Operation = None,
condition: Union[Condition, None] = None,
referencing: Union[Referencing, None] = None,
func: Union[Func, str] = None,
declare: Union[List[Tuple[str, str]], None] = None,
timing: Union[Timing, None] = None
)
Bases: Trigger
A trigger that raises an exception.
Source code in pgtrigger/core.py
pgtrigger.ReadOnly
ReadOnly(
*,
fields: Union[List[str], None] = None,
exclude: Union[List[str], None] = None,
**kwargs: Any
)
Bases: Protect
A trigger that prevents edits to fields.
If fields
are provided, will protect edits to only those fields.
If exclude
is provided, will protect all fields except the ones
excluded.
If none of these arguments are provided, all fields cannot be edited.
Source code in pgtrigger/contrib.py
pgtrigger.SoftDelete
SoftDelete(
*,
name: str = None,
condition: Union[core.Condition, None] = None,
field: str = None,
value: Union[bool, str, int, None] = _unset
)
Bases: Trigger
Sets a field to a value when a delete happens.
Supply the trigger with the "field" that will be set
upon deletion and the "value" to which it should be set.
The "value" defaults to False
.
Note
This trigger currently only supports nullable BooleanField
,
CharField
, and IntField
fields.
Source code in pgtrigger/contrib.py
pgtrigger.FSM
FSM(
*,
name: str = None,
condition: Union[core.Condition, None] = None,
field: str = None,
transitions: List[Tuple[str, str]] = None,
separator: str = None
)
Bases: Trigger
Enforces a finite state machine on a field.
Supply the trigger with the field
that transitions and then
a list of tuples of valid transitions to the transitions
argument.
Note
Only non-null CharField
fields without quotes are currently supported.
If your strings have a colon symbol in them, you must override the
"separator" argument to be a value other than a colon.
Source code in pgtrigger/contrib.py
pgtrigger.UpdateSearchVector
UpdateSearchVector(
*,
name: str = None,
vector_field: str = None,
document_fields: List[str] = None,
config_name: str = None
)
Bases: Trigger
Updates a django.contrib.postgres.search.SearchVectorField
from document fields.
Supply the trigger with the vector_field
that will be updated with
changes to the document_fields
. Optionally provide a config_name
, which
defaults to pg_catalog.english
.
This trigger uses tsvector_update_trigger
to update the vector field.
See the Postgres docs
for more information.
Note
UpdateSearchVector
triggers are not compatible with pgtrigger.ignore since
it references a built-in trigger. Trying to ignore this trigger results in a
RuntimeError
.
Source code in pgtrigger/contrib.py
Runtime execution
pgtrigger.constraints
Set deferrable constraint timing for the given triggers, which will persist until overridden or until end of transaction. Must be in a transaction to run this.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timing |
Timing
|
The timing value that overrides the default trigger timing. |
required |
*uris |
str
|
Trigger URIs over which to set constraint timing. If none are provided, all trigger constraint timing will be set. All triggers must be deferrable. |
()
|
databases |
Union[List[str], None]
|
The databases on which to set constraints. If none, all postgres databases will be used. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If the database of any triggers is not in a transaction. |
ValueError
|
If any triggers are not deferrable. |
Source code in pgtrigger/runtime.py
pgtrigger.ignore
Dynamically ignore registered triggers matching URIs from executing in an individual thread. If no URIs are provided, ignore all pgtriggers from executing in an individual thread.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*uris |
str
|
Trigger URIs to ignore. If none are provided, all triggers will be ignored. |
()
|
databases |
Union[List[str], None]
|
The databases to use. If none, all postgres databases will be used. |
None
|
Example
Ingore triggers in a context manager:
with pgtrigger.ignore("my_app.Model:trigger_name"):
# Do stuff while ignoring trigger
Example
Ignore multiple triggers as a decorator:
@pgtrigger.ignore("my_app.Model:trigger_name", "my_app.Model:other_trigger")
def my_func():
# Do stuff while ignoring trigger
Source code in pgtrigger/runtime.py
pgtrigger.schema
Sets the search path to the provided schemas.
If nested, appends the schemas to the search path if not already in it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*schemas |
str
|
Schemas that should be appended to the search path. Schemas already in the search path from nested calls will not be appended. |
()
|
databases |
Union[List[str], None]
|
The databases to set the search path. If none, all postgres databases will be used. |
None
|
Source code in pgtrigger/runtime.py
Registry
pgtrigger.register
Register the given triggers with wrapped Model class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*triggers |
Trigger
|
Trigger classes to register. |
()
|
Example
Register by decorating a model:
@pgtrigger.register(
pgtrigger.Protect(
name="append_only",
operation=(pgtrigger.Update | pgtrigger.Delete)
)
)
class MyModel(models.Model):
pass
Example
Register by calling functionally:
pgtrigger.register(trigger_object)(MyModel)
Source code in pgtrigger/registry.py
pgtrigger.registered
Get registered trigger objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*uris |
str
|
URIs of triggers to get. If none are provided,
all triggers are returned. URIs are in the format of
|
()
|
Returns:
Type | Description |
---|---|
List[Tuple[Model, Trigger]]
|
Matching trigger objects. |
Source code in pgtrigger/registry.py
Installation
pgtrigger.install
Install triggers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*uris |
str
|
URIs of triggers to install. If none are provided, all triggers are installed and orphaned triggers are pruned. |
()
|
database |
Union[str, None]
|
The database. Defaults to the "default" database. |
None
|
Source code in pgtrigger/installation.py
pgtrigger.uninstall
Uninstalls triggers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*uris |
str
|
URIs of triggers to uninstall. If none are provided, all triggers are uninstalled and orphaned triggers are pruned. |
()
|
database |
Union[str, None]
|
The database. Defaults to the "default" database. |
None
|
Source code in pgtrigger/installation.py
pgtrigger.enable
Enables registered triggers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*uris |
str
|
URIs of triggers to enable. If none are provided, all triggers are enabled. |
()
|
database |
Union[str, None]
|
The database. Defaults to the "default" database. |
None
|
Source code in pgtrigger/installation.py
pgtrigger.disable
Disables triggers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*uris |
str
|
URIs of triggers to disable. If none are provided, all triggers are disabled. |
()
|
database |
Union[str, None]
|
The database. Defaults to the "default" database. |
None
|
Source code in pgtrigger/installation.py
pgtrigger.prunable
Return triggers that are candidates for pruning
Parameters:
Name | Type | Description | Default |
---|---|---|---|
database |
Union[str, None]
|
The database. Defaults to the "default" database. |
None
|
Returns:
Type | Description |
---|---|
List[Tuple[str, str, bool, str]]
|
A list of tuples consisting of the table, trigger ID, enablement, and database |
Source code in pgtrigger/installation.py
pgtrigger.prune
Remove any pgtrigger triggers in the database that are not used by models. I.e. if a model or trigger definition is deleted from a model, ensure it is removed from the database
Parameters:
Name | Type | Description | Default |
---|---|---|---|
database |
Union[str, None]
|
The database. Defaults to the "default" database. |
None
|