-
-
Notifications
You must be signed in to change notification settings - Fork 62
Fix issue #383: Optimize SQL queries for notification storm prevention #399
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
Open
Eeshu-Yadav
wants to merge
1
commit into
openwisp:master
Choose a base branch
from
Eeshu-Yadav:issues/383-optimize-notification-sql-queries
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 389
🏁 Script executed:
git ls-files | head -30Repository: openwisp/openwisp-notifications
Length of output: 890
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 113
🏁 Script executed:
sed -n '160,185p' openwisp_notifications/handlers.pyRepository: openwisp/openwisp-notifications
Length of output: 1188
🏁 Script executed:
sed -n '1,50p' openwisp_notifications/handlers.pyRepository: openwisp/openwisp-notifications
Length of output: 1999
🏁 Script executed:
rg -n "send_email_notification" openwisp_notifications/handlers.py -B 2 -A 10Repository: openwisp/openwisp-notifications
Length of output: 1232
🏁 Script executed:
sed -n '183,210p' openwisp_notifications/handlers.pyRepository: openwisp/openwisp-notifications
Length of output: 1205
🏁 Script executed:
sed -n '210,240p' openwisp_notifications/handlers.pyRepository: openwisp/openwisp-notifications
Length of output: 1255
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 6626
🏁 Script executed:
rg -n "bulk_create" openwisp_notifications/ -B 2 -A 2Repository: openwisp/openwisp-notifications
Length of output: 2108
🏁 Script executed:
grep -r "DATABASE" tests/openwisp2/settings.py openwisp_notifications/settings.pyRepository: openwisp/openwisp-notifications
Length of output: 116
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 331
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 126
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 1160
🏁 Script executed:
rg -n "bulk_create.*populate_pk|return_pk" openwisp_notifications/ tests/Repository: openwisp/openwisp-notifications
Length of output: 57
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 290
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 124
🏁 Script executed:
rg -n "notification.id" openwisp_notifications/handlers.py -B 2 -A 2Repository: openwisp/openwisp-notifications
Length of output: 57
🏁 Script executed:
sed -n '215,230p' openwisp_notifications/handlers.pyRepository: openwisp/openwisp-notifications
Length of output: 689
🏁 Script executed:
rg -n "instance\.id\|notification\.id" openwisp_notifications/handlers.pyRepository: openwisp/openwisp-notifications
Length of output: 57
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 384
🏁 Script executed:
cat requirements.txt | grep -i djangoRepository: openwisp/openwisp-notifications
Length of output: 57
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 57
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 57
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 57
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 57
🏁 Script executed:
fd "test_.*\.py" openwisp_notifications/tests/ --type fRepository: openwisp/openwisp-notifications
Length of output: 725
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 160
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 57
🏁 Script executed:
rg -n "class.*Test.*Notif" openwisp_notifications/tests/test_notifications.pyRepository: openwisp/openwisp-notifications
Length of output: 231
🏁 Script executed:
sed -n '1,100p' openwisp_notifications/tests/test_notifications.pyRepository: openwisp/openwisp-notifications
Length of output: 3707
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 57
🏁 Script executed:
rg -n "def test.*batch" openwisp_notifications/tests/test_notifications.pyRepository: openwisp/openwisp-notifications
Length of output: 689
🏁 Script executed:
sed -n '1500,1550p' openwisp_notifications/tests/test_notifications.pyRepository: openwisp/openwisp-notifications
Length of output: 2196
🏁 Script executed:
sed -n '1098,1135p' openwisp_notifications/tests/test_notifications.pyRepository: openwisp/openwisp-notifications
Length of output: 1867
🏁 Script executed:
sed -n '1136,1203p' openwisp_notifications/tests/test_notifications.pyRepository: openwisp/openwisp-notifications
Length of output: 3107
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 170
🌐 Web query:
Django bulk_create PK population SQLite backend💡 Result:
With Django’s
bulk_create(), primary keys (AutoField/BigAutoField) are only populated on the in-memory instances if the DB backend supports returning rows from a bulk insert (i.e., Django can useINSERT … RETURNING …). Django’s docs explicitly list SQLite 3.35+ as supported (along with PostgreSQL and MariaDB 10.5+); otherwise, the objects are inserted but their.pkstaysNone. [1]For SQLite, PK population after
bulk_create()therefore requires:sqlite3module is linked against), andCommon reasons you still see
pk=Noneon SQLite:ignore_conflicts=True, which disables PK-setting even on DBs that otherwise support it. [1]can_return_rows_from_bulk_insertis false, in which case Django won’t set autoincrement PKs duringbulk_create(). [2]Workarounds if you can’t upgrade SQLite:
Sources: Django QuerySet
bulk_create()docs (PK support + SQLite 3.35+ note, Django 4.0 change) [1]; Django ORM implementation notes on PK setting depending oncan_return_rows_from_bulk_insert[2].[1] (docs.djangoproject.com)
[2] (docs.djangoproject.com)
Citations:
🏁 Script executed:
rg -n "\.id.*None\|None.*\.id" openwisp_notifications/handlers.pyRepository: openwisp/openwisp-notifications
Length of output: 57
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 4677
🏁 Script executed:
sed -n '183,250p' openwisp_notifications/handlers.pyRepository: openwisp/openwisp-notifications
Length of output: 2709
🏁 Script executed:
grep -n "populate_pk\|return_pk\|can_return_rows" openwisp_notifications/ -rRepository: openwisp/openwisp-notifications
Length of output: 57
🏁 Script executed:
rg -n "ignore_conflicts" openwisp_notifications/ --type pyRepository: openwisp/openwisp-notifications
Length of output: 181
🏁 Script executed:
rg -n "post_save.*Notification\|receiver.*send_email" openwisp_notifications/handlers.py -B 2 -A 2Repository: openwisp/openwisp-notifications
Length of output: 57
🏁 Script executed:
sed -n '165,181p' openwisp_notifications/handlers.pyRepository: openwisp/openwisp-notifications
Length of output: 758
🏁 Script executed:
rg -n "dispatch_uid.*send_email" openwisp_notifications/ --type pyRepository: openwisp/openwisp-notifications
Length of output: 195
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 57
🏁 Script executed:
Repository: openwisp/openwisp-notifications
Length of output: 57
🏁 Script executed:
sed -n '100,150p' openwisp_notifications/handlers.pyRepository: openwisp/openwisp-notifications
Length of output: 1814
Ensure
bulk_create()returns populated PKs before using notifications for email batching and WebSocket updatesLine 169's
bulk_create()may not populate PKs depending on the database backend and Django version. SQLite requires version 3.35+ and Django 4.0+; older environments will return objects withid=None. Line 220 then appendsinstance.idtobatched_notifications, and ifidisNone, this silently corrupts the batch tracking list. The WebSocket serialization at line 135 also depends on valid notification IDs.Explicitly handle the case where PKs are unpopulated, either by:
values_list('id', flat=True)or similar, or🧰 Tools
🪛 Ruff (0.14.14)
[warning] 174-174:
zip()without an explicitstrict=parameterAdd explicit value for parameter
strict=(B905)
🤖 Prompt for AI Agents