Skip to content

Commit

Permalink
Rely on Django DB routing when storing log entries (#5)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergio Ocaña Gálvez <[email protected]>
  • Loading branch information
okiwan and okiwan committed Jun 2, 2022
1 parent 11996b6 commit 8e02237
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,26 @@ jobs:
id: pip-cache
run: |
echo "::set-output name=dir::$(pip cache dir)"
- name: Cache
uses: actions/cache@v2
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: release-${{ hashFiles('**/setup.py') }}
restore-keys: |
release-
- name: Install dependencies
run: |
python -m pip install -U pip
python -m pip install -U setuptools twine wheel
- name: Build package
run: |
python setup.py --version
python setup.py sdist --format=gztar bdist_wheel
twine check dist/*
- name: Upload packages to Jazzband
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@master
with:
user: jazzband
password: ${{ secrets.JAZZBAND_RELEASE_KEY }}
repository_url: https://jazzband.co/projects/django-auditlog/upload
repository_url: https://jazzband.co/projects/django-auditlog/upload
13 changes: 7 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ jobs:
fail-fast: false
max-parallel: 5
matrix:
python-version: ['3.6', '3.7', '3.8', '3.9']
python-version: ['3.7', '3.8', '3.9', '3.10']

services:
postgres:
image: postgres:10
image: postgres:12
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
Expand All @@ -25,7 +25,6 @@ jobs:
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v2

Expand All @@ -38,7 +37,6 @@ jobs:
id: pip-cache
run: |
echo "::set-output name=dir::$(pip cache dir)"
- name: Cache
uses: actions/cache@v2
with:
Expand All @@ -47,12 +45,10 @@ jobs:
-${{ matrix.python-version }}-v1-${{ hashFiles('**/setup.py') }}
restore-keys: |
-${{ matrix.python-version }}-v1-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade tox tox-gh-actions
- name: Tox tests
run: |
tox -v
Expand All @@ -62,3 +58,8 @@ jobs:
TEST_DB_PASS: postgres
TEST_DB_NAME: postgres
TEST_DB_PORT: ${{ job.services.postgres.ports[5432] }}

- name: Upload coverage
uses: codecov/codecov-action@v1
with:
name: Python ${{ matrix.python-version }}
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
17 changes: 16 additions & 1 deletion auditlog_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.test import RequestFactory, TestCase
from django.utils import dateformat, formats, timezone

from auditlog.diff import model_instance_diff
from auditlog.middleware import AuditlogMiddleware
from auditlog.models import LogEntry
from auditlog.registry import auditlog
Expand All @@ -33,11 +34,25 @@
UUIDPrimaryKeyModel,
)


class SimpleModelTest(TestCase):
def setUp(self):
self.obj = SimpleModel.objects.create(text="I am not difficult.")

def test_log_create_updates_on_original_database(self):
"""Log creation should use object's default database"""
obj = self.obj
obj._state.db = "readonly"
changes = model_instance_diff(None, obj)
log_entry = LogEntry.objects.log_create(
obj, action=LogEntry.Action.CREATE, changes=json.dumps(changes)
)

self.assertEqual(
log_entry._state.db,
"default",
msg="Object should have used the default database",
)

def test_create(self):
"""Creation is logged correctly."""
# Get the object to work with
Expand Down

0 comments on commit 8e02237

Please sign in to comment.