audit_trail.tables¶
Core table definitions for the audit log.
Both tables are partitioned in PostgreSQL, so their primary keys include the partition keys and nothing has a foreign key to them:
audit_transaction:PARTITION BY RANGE (issued_at), one partition per month.audit_activity:PARTITION BY LIST (severity), and each severity partition is itselfPARTITION BY RANGE (created_at), one partition per month.
The partitions are not part of the metadata; audit_trail.migrations
creates the parents and severity partitions, audit_trail.maintenance the
monthly ones.
MAX_IDENTIFIER_LENGTH
module-attribute
¶
PostgreSQL's limit on identifier length in bytes; longer names are truncated.
IndexKey
module-attribute
¶
IndexKey = Literal[
"severity",
"actor",
"object",
"target",
"scope",
"transaction",
"correlation",
"changes_gin",
]
Key of one audit_activity index.
DEFAULT_INDEXES
module-attribute
¶
DEFAULT_INDEXES: frozenset[IndexKey] = frozenset(
{
"severity",
"actor",
"object",
"target",
"scope",
"transaction",
"correlation",
}
)
Keys of the audit_activity indexes created unless indexes says otherwise.
OPTIONAL_INDEXES
module-attribute
¶
Keys of the indexes that exist but are off by default.
ALL_INDEXES
module-attribute
¶
Every valid key for build_tables(indexes=...).
AuditTables
dataclass
¶
The audit tables and the metadata they are bound to.
Attributes:
| Name | Type | Description |
|---|---|---|
metadata |
MetaData
|
Metadata holding both tables and the activity indexes. |
transaction |
Table
|
The |
activity |
Table
|
The |
audit_connection ¶
The session's connection for the audit tables.
Resolved by Session.get_bind with the audit_activity table as the
clause: a bind of that table in Session(binds=...), else the default
bind. A get_bind override is honoured.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
The session. |
required |
tables
|
AuditTables
|
The audit tables. |
required |
hint
|
str
|
Appended to the error message. |
''
|
Raises:
| Type | Description |
|---|---|
UnboundExecutionError
|
Neither resolves, chained to SQLAlchemy's error. |
Source code in audit_trail/tables.py
build_tables ¶
build_tables(
*,
schema: str = "audit",
transaction_table: str = "audit_transaction",
activity_table: str = "audit_activity",
indexes: Collection[str] | None = None,
) -> AuditTables
Build the audit tables in a new MetaData.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
str
|
Schema holding the tables. |
'audit'
|
transaction_table
|
str
|
Name of the transaction (context) table. |
'audit_transaction'
|
activity_table
|
str
|
Name of the activity (event) table. |
'audit_activity'
|
indexes
|
Collection[str] | None
|
Keys of the |
None
|
Returns:
| Type | Description |
|---|---|
AuditTables
|
The tables and their metadata. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a name is empty or too long for PostgreSQL once a partition suffix is added, the table names are equal, or an index key is unknown. |
Source code in audit_trail/tables.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |