Skip to content

Preserve database alias when deactivating invalid FCM and APNS devices - #827

Open
Fire162 wants to merge 1 commit into
django-commons:masterfrom
Fire162:fix/deactivate-devices-using-db
Open

Fire162 wants to merge 1 commit into
django-commons:masterfrom
Fire162:fix/deactivate-devices-using-db

Conversation

@Fire162

@Fire162 Fire162 commented Nov 6, 2026

Copy link
Copy Markdown

Preserve the originating database alias when deactivating invalid FCM and APNS devices during send operations instead of defaulting to the default database.

Fixes #825

@codecov

codecov Bot commented Nov 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 61.90476% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.64%. Comparing base (1fe48b9) to head (e4e97fc).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
push_notifications/apns.py 0.00% 8 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (1fe48b9) and HEAD (e4e97fc). Click for more details.

HEAD has 3 uploads less than BASE
Flag BASE (1fe48b9) HEAD (e4e97fc)
8 5
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #827      +/-   ##
==========================================
- Coverage   74.65%   68.64%   -6.01%     
==========================================
  Files          32       32              
  Lines        1365     1381      +16     
  Branches      226      206      -20     
==========================================
- Hits         1019      948      -71     
- Misses        300      388      +88     
+ Partials       46       45       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JaeHyuckSa JaeHyuckSa left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Fire162 Thanks! Left a few comments. Also needs a rebase since #826 added the same "other" database.

Comment thread push_notifications/apns.py Resolved
Comment thread push_notifications/models.py Resolved
Comment thread push_notifications/apns_async.py Resolved
@Fire162
Fire162 force-pushed the fix/deactivate-devices-using-db branch from e4e97fc to c91fc1c Compare November 14, 2026 21:19
@Fire162

Fire162 commented Nov 14, 2026

Copy link
Copy Markdown
Author

Rebased onto master to pick up #826, addressed the review comments regarding using handling and router.db_for_write, and added tests for unsaved instances.

# transform legacy data to new message object
message = dict_to_fcm_message(data, **kwargs)

kwargs.setdefault("using", self._state.db or router.db_for_write(type(self), instance=self))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

With a replica router this still writes to the replica. db_for_write() already falls back to _state.db when no router picks one.

Suggested change
kwargs.setdefault("using", self._state.db or router.db_for_write(type(self), instance=self))
kwargs.setdefault("using", router.db_for_write(type(self), instance=self))

except ImportError:
from .apns import apns_send_message

kwargs.setdefault("using", self._state.db or router.db_for_write(type(self), instance=self))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
kwargs.setdefault("using", self._state.db or router.db_for_write(type(self), instance=self))
kwargs.setdefault("using", router.db_for_write(type(self), instance=self))

Comment on lines +173 to +176
manager = models.APNSDevice.objects
if using:
manager = manager.using(using)
manager.filter(registration_id=registration_id).update(active=False)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

.using(None) does the same as no .using(), so the if isn't needed.

Suggested change
manager = models.APNSDevice.objects
if using:
manager = manager.using(using)
manager.filter(registration_id=registration_id).update(active=False)
models.APNSDevice.objects.using(using).filter(
registration_id=registration_id
).update(active=False)

Comment on lines +204 to +207
manager = models.APNSDevice.objects
if using:
manager = manager.using(using)
manager.filter(registration_id__in=inactive_tokens).update(active=False)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
manager = models.APNSDevice.objects
if using:
manager = manager.using(using)
manager.filter(registration_id__in=inactive_tokens).update(active=False)
models.APNSDevice.objects.using(using).filter(
registration_id__in=inactive_tokens
).update(active=False)

Comment on lines +439 to +442
manager = models.APNSDevice.objects
if using:
manager = manager.using(using)
manager.filter(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
manager = models.APNSDevice.objects
if using:
manager = manager.using(using)
manager.filter(
models.APNSDevice.objects.using(using).filter(

Comment thread push_notifications/gcm.py
Comment on lines +149 to +152
manager = GCMDevice.objects
if using:
manager = manager.using(using)
manager.filter(registration_id__in=deactivated_ids).update(active=False)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
manager = GCMDevice.objects
if using:
manager = manager.using(using)
manager.filter(registration_id__in=deactivated_ids).update(active=False)
GCMDevice.objects.using(using).filter(
registration_id__in=deactivated_ids
).update(active=False)

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.

FCM and APNS deactivate invalid tokens in the wrong database

2 participants