Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions api/audit/migrations/0016_add_organisation_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 5.2.17 on 2026-11-06 21:08

from django.db import migrations, models
from django.db.backends import utils as backend_utils

# Matches the name Django generates for a `db_index=True` index on this column
ORGANISATION_ID_INDEX_NAME = "audit_auditlog_organisation_id_" + (
backend_utils.names_digest( # type: ignore[attr-defined] # Missing from django-stubs
"audit_auditlog", "organisation_id", length=8
)
)


class Migration(migrations.Migration):
atomic = False

dependencies = [
("audit", "0015_add_log_trigram_index"),
]

operations = [
migrations.SeparateDatabaseAndState(
database_operations=[
migrations.AddField(
model_name="auditlog",
name="organisation_id",
field=models.IntegerField(null=True),
),
migrations.RunSQL(
f"CREATE INDEX CONCURRENTLY IF NOT EXISTS {ORGANISATION_ID_INDEX_NAME} "
"ON audit_auditlog (organisation_id)",
reverse_sql=f"DROP INDEX CONCURRENTLY IF EXISTS {ORGANISATION_ID_INDEX_NAME}",
),
],
state_operations=[
migrations.AddField(
model_name="auditlog",
name="organisation_id",
field=models.IntegerField(db_index=True, null=True),
),
],
),
]
9 changes: 7 additions & 2 deletions api/audit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from api_keys.models import MasterAPIKey
from audit.related_object_type import RelatedObjectType
from organisations.models import Organisation
from projects.models import Project

if typing.TYPE_CHECKING:
Expand All @@ -28,6 +29,9 @@
class AuditLog(LifecycleModel): # type: ignore[misc]
created_date = models.DateTimeField("DateCreated") # type: ignore[var-annotated]

organisation_id = models.IntegerField[int | None, int | None](
null=True, db_index=True
)
project = models.ForeignKey( # type: ignore[var-annotated]
Project, related_name="audit_logs", null=True, on_delete=models.DO_NOTHING
)
Expand Down Expand Up @@ -85,8 +89,9 @@ class Meta:

@property
def organisation(self) -> "Organisation | None":
# TODO properly implement organisation relation
# maybe the relation list should not be _that_ exhaustive...
if self.organisation_id is not None:
return Organisation.objects.get(id=self.organisation_id) # type: ignore[no-any-return]

for relation in (
"project",
"environment",
Expand Down
Loading
Loading