-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add audit log feature for IDIR administrators
- Loading branch information
1 parent
2fd89f8
commit 9ec7770
Showing
26 changed files
with
1,368 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,64 @@ | ||
from lcfs.db.base import Auditable, BaseModel | ||
from sqlalchemy import ( | ||
BigInteger, | ||
Integer, | ||
Column, | ||
Text, | ||
) | ||
from sqlalchemy.dialects.postgresql import JSONB | ||
|
||
|
||
class AuditLog(BaseModel, Auditable): | ||
__tablename__ = "audit_log" | ||
__table_args__ = {"comment": "Track changes in defined tables."} | ||
""" | ||
Audit log capturing changes to database tables. | ||
As the table grows, consider implementing automatic archiving (e.g., moving older logs to an archive table) | ||
and purging (e.g., deleting logs after a retention period) using tools like `pg_cron` or external schedulers. | ||
id = Column(BigInteger, primary_key=True, autoincrement=True) | ||
Archiving: | ||
- Create an `audit_log_archive` table with the same structure as `audit_log`. | ||
- Use a scheduled job (e.g., with `pg_cron`) to move records older than a certain threshold (e.g., 1 month) from `audit_log` to `audit_log_archive`. | ||
- Alternatively, consider creating date-based archive tables (e.g., audit_log_archive_2025_01) to organize logs by time periods. | ||
table_name = Column(Text, nullable=False) | ||
operation = Column(Text, nullable=False) | ||
Purging: | ||
- Use a scheduled job (e.g., with `pg_cron`) to delete records older than a defined retention period (e.g., 1 year) from `audit_log_archive`. | ||
""" | ||
|
||
__tablename__ = "audit_log" | ||
__table_args__ = {"comment": "Track changes in defined tables."} | ||
|
||
# JSONB fields for row ID, old values, new values, and delta | ||
row_id = Column(JSONB, nullable=False) | ||
old_values = Column(JSONB, nullable=True) | ||
new_values = Column(JSONB, nullable=True) | ||
delta = Column(JSONB, nullable=True) | ||
audit_log_id = Column( | ||
Integer, | ||
primary_key=True, | ||
autoincrement=True, | ||
comment="Unique identifier for each audit log entry.", | ||
) | ||
table_name = Column( | ||
Text, | ||
nullable=False, | ||
comment="Name of the table where the action occurred.", | ||
) | ||
operation = Column( | ||
Text, | ||
nullable=False, | ||
comment="Type of operation: 'INSERT', 'UPDATE', or 'DELETE'.", | ||
) | ||
row_id = Column( | ||
JSONB, | ||
nullable=False, | ||
comment="Primary key of the affected row, stored as JSONB to support composite keys.", | ||
) | ||
old_values = Column( | ||
JSONB, | ||
nullable=True, | ||
comment="Previous values before the operation.", | ||
) | ||
new_values = Column( | ||
JSONB, | ||
nullable=True, | ||
comment="New values after the operation.", | ||
) | ||
delta = Column( | ||
JSONB, | ||
nullable=True, | ||
comment="JSONB delta of the changes.", | ||
) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.