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

Conversation

fgsamuel
Copy link
Contributor

@fgsamuel fgsamuel commented Feb 24, 2022

When you use replica database the django-auditlog try to write in the same database where object was read (replica). But this is a read only database and crash the application.

With this changes it saves always in the default database.

If you want to save in multiple databases or in a special one use DATABASE_ROUTERS to configure it.

To continue using multiple databases, you must configure it in your DATABASE ROUTERS.

When you use replica database the django-auditlog try to write in the same database where object was read (replica). But this is a read only database and crash the application.

This changes saves always in the default database.

If you want to save in multiple databases or in a special one use `DATABASE_ROUTERS` to configure it.
isaquealves
isaquealves previously approved these changes Mar 2, 2022
Copy link

@isaquealves isaquealves left a comment

Choose a reason for hiding this comment

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

Good catch.

CharlesTenorio
CharlesTenorio previously approved these changes Mar 2, 2022
Copy link

@CharlesTenorio CharlesTenorio left a comment

Choose a reason for hiding this comment

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

important feature for reading bank

kneipp
kneipp previously approved these changes Mar 2, 2022
Copy link

@kneipp kneipp left a comment

Choose a reason for hiding this comment

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

Very usefull I can't imagine why we don't use this yet.

@kneipp
Copy link

kneipp commented Mar 2, 2022

Very usefull I can't imagine why we don't use this yet.

Usefull for amazon rds writer/reader databases.

@codecov
Copy link

codecov bot commented Mar 2, 2022

Codecov Report

Merging #359 (240364e) into master (665217d) will increase coverage by 0.16%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #359      +/-   ##
==========================================
+ Coverage   82.41%   82.57%   +0.16%     
==========================================
  Files          20       20              
  Lines         506      505       -1     
==========================================
  Hits          417      417              
+ Misses         89       88       -1     
Impacted Files Coverage Δ
auditlog/models.py 82.68% <100.00%> (+0.45%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 665217d...240364e. Read the comment docs.

@kneipp
Copy link

kneipp commented Mar 2, 2022

PSC @Linkid any thoughts?

@hramezani
Copy link
Member

hramezani commented Mar 3, 2022

@fgsamuel Thanks for this patch 👍
Please add a changelog entry for this change. because it's a change in previous behavior.
Also, please add a test to confirm the object is saved in the default database.

@fgsamuel fgsamuel dismissed stale reviews from kneipp, CharlesTenorio, and isaquealves via be4467f March 3, 2022 22:19
@fgsamuel
Copy link
Contributor Author

fgsamuel commented Mar 3, 2022

@hramezani Thanks a lot for the review.

I just made the changes you suggested.

Please check everything is ok to merge with master.

CHANGELOG.md Outdated
@@ -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

@@ -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):

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?

@hramezani
Copy link
Member

Please take a look at failing tests as well.

@fgsamuel
Copy link
Contributor Author

fgsamuel commented Mar 4, 2022

@hramezani thank you again.

I have updated all the points you suggested

All tests are working fine.

@hramezani
Copy link
Member

Thanks @fgsamuel I will take a look again next week

CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
auditlog_tests/tests.py Outdated Show resolved Hide resolved
Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>
auditlog_tests/tests.py Outdated Show resolved Hide resolved
Copy link
Member

@hramezani hramezani left a comment

Choose a reason for hiding this comment

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

LGTM!

@hramezani hramezani merged commit 77ef852 into jazzband:master Mar 7, 2022
barrachri added a commit to barrachri/django-auditlog that referenced this pull request May 8, 2022
* master:
  Remove default_app_config configuration
  Fix lint entry in tox.ini
  Drop Django 2.2 support.
  [pre-commit.ci] pre-commit autoupdate
  Fix small typo
  Add mask_fields argument in register (jazzband#310)
  Use pre-commit as lint command
  Add black and isort to .pre-commit-config.yaml
  Replace assertTrue with assertEqual
  enable use of replica database (jazzband#359)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants