Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable use of replica database #359

Merged
merged 5 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### Improvements

- feat: enable use of replica database (delegating the choice to `DATABASES_ROUTER`) ([#359](https://github.com/jazzband/django-auditlog/pull/359))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please place in a new section above because this section belongs to the already released version 1.0.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please mention LogEntry no longer save to same database instance is using

- build: add classifiers for Python and Django
- build: replace django-jsonfield with django-jsonfield-backport ([#339](https://github.com/jazzband/django-auditlog/pull/339))
- ci: replace Travis with Github Actions
Expand Down
8 changes: 1 addition & 7 deletions auditlog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,7 @@ def log_create(self, instance, **kwargs):
content_type=kwargs.get("content_type"),
object_pk=kwargs.get("object_pk", ""),
).delete()
# save LogEntry to same database instance is using
db = instance._state.db
return (
self.create(**kwargs)
if db is None or db == ""
else self.using(db).create(**kwargs)
)
return self.create(**kwargs)
return None

def get_for_object(self, instance):
Expand Down
31 changes: 31 additions & 0 deletions auditlog_tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import json

import django
from dateutil.tz import gettz
Expand All @@ -10,7 +11,9 @@
from django.http import HttpResponse
from django.test import RequestFactory, TestCase
from django.utils import dateformat, formats, timezone
from django.utils.connection import ConnectionDoesNotExist

from auditlog.diff import model_instance_diff
from auditlog.middleware import AuditlogMiddleware
from auditlog.models import LogEntry
from auditlog.registry import auditlog
Expand Down Expand Up @@ -920,3 +923,31 @@ def test_no_delete_related(self):
list(entries.values_list("action", flat=True)),
[LogEntry.Action.CREATE, LogEntry.Action.UPDATE, LogEntry.Action.DELETE],
)


class ModelFromDifferentDatabase(TestCase):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need a new class, you can add this test to

class SimpleModelTest(TestCase):

"""
Should use the default database even when object is from other database (read-only, for example)
"""
def setUp(self):
self.obj = SimpleModel.objects.create(text="42 is the answer")

def test_create_log_to_object_from_other_database(self):
msg = "The log should not try to write to the same database as the object"

instance = self.obj
instance._state.db = 'replica' # simulate object obtained from a different database (read only)

changes = model_instance_diff(None, instance)

try:
log_entry = LogEntry.objects.log_create(
instance,
action=LogEntry.Action.CREATE,
changes=json.dumps(changes),
)
self.assertEqual(log_entry._state.db, "default", msg=msg) # must be created in default database
except ConnectionDoesNotExist as e:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this except block?

self.assertTrue(False, msg=msg)