-
Notifications
You must be signed in to change notification settings - Fork 256
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
Soft delete assets from deleted facilities/hospitals #1996
base: develop
Are you sure you want to change the base?
Conversation
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.
No changes to the fields in the model were made. But why are there migrations for it?
@rithviknishad I was initially using models.cascade but later I realised I need to perform soft delete so I changed it back to original ones so that's why you can see those redundant migrations.But the functionality is working. Should I remove those redundant migrations? |
@rithviknishad I made a second commit i removed those redundant migration history so effectively i just added the delete method in facility model |
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.
Custom migration needed to delete existing such assets of deleted facility.
https://docs.djangoproject.com/en/5.0/ref/migration-operations/#runpython
Also test cases needs to be added. It'd be great if you could follow the PR template 😄 |
@rithviknishad ya sure I'll make the changes by today |
@rithviknishad I added custom migrations and also added api test case but the tests are not running in my machine because im using windows and im probably getting some connection errors with the test db like some dbus connection errors and no such bucket found. It is giving a failed test for medibase api maybe some db connection issues in windows |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #1996 +/- ##
===========================================
+ Coverage 62.20% 62.38% +0.18%
===========================================
Files 221 221
Lines 12204 12229 +25
Branches 1742 1746 +4
===========================================
+ Hits 7591 7629 +38
+ Misses 4305 4284 -21
- Partials 308 316 +8 ☔ View full report in Codecov by Sentry. |
care/facility/models/facility.py
Outdated
def delete(self, *args, **kwargs): | ||
from care.facility.models.asset import Asset, AssetLocation | ||
|
||
Asset.objects.filter(current_location__facility=self).update(deleted=True) | ||
super().delete(self, *args, **kwargs) | ||
|
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.
this will skip
care/care/facility/models/asset.py
Lines 175 to 179 in ce7073e
def delete(self, *args, **kwargs): | |
from care.facility.models.bed import AssetBed | |
AssetBed.objects.filter(asset=self).update(deleted=True) | |
super().delete(*args, **kwargs) |
a better approach would be to make a cron job that regularly checks for deleted objects and recursively soft deletes related objects
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.
@sainak but if you check recursively it'll slow down and degrade the performance
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.
@sainak and that cron job would run in Linux but not in windows I guess so I can't test it instead what I can do is to delete it in an atomic way maybe
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.
by cron job, I mean celerybeat cron task
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.
@sainak ok how frequent the check should be every minute or every hour or every 24 hrs?
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.
let's run it every 24 hour
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.
@sainak okay I will do it
@sainak plz review the commit... |
@sainak @rithviknishad i added the cron job and should i remove the tests also for soft delete that i created because now that wont work right and maybe the custom migration is also not needed because every midnight the assets will be soft deleted and should i add logging infos and warnings in the task function? plz review it and tell me if any changes required |
@hrit2773 You can remove the custom migration since it'd be taken care by the celery task itself right. |
facilities = Facility.objects.all().values_list("name", flat=True) | ||
Asset.objects.exclude(current_location__facility__name__in=facilities).update( |
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.
you can directly use the id instead of name
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.
@sainak yes yes we can I'll do it
@rithviknishad yes I'll remove it and I'll remove the api test case also because I feel that is not needed |
@rithviknishad @sainak made the required changes plz review these changes |
def soft_delete_assets_schedule(): | ||
facilities = Facility.objects.all().values_list("external_id", flat=True) | ||
Asset.objects.exclude( | ||
current_location__facility__external_id__in=facilities | ||
).update(deleted=True) |
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.
We could simply rely on the PK instead of the UUID right? @sainak
def soft_delete_assets_schedule(): | |
facilities = Facility.objects.all().values_list("external_id", flat=True) | |
Asset.objects.exclude( | |
current_location__facility__external_id__in=facilities | |
).update(deleted=True) | |
def soft_delete_assets_schedule(): | |
facilities = Facility.objects.all().values_list("id", flat=True) | |
Asset.objects.exclude( | |
current_location__facility__id__in=facilities | |
).update(deleted=True) |
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.
@rithviknishad we can do that also but as the filterset class also takes the external_id I considered external id just to avoid any existing wrong entry in the database to spoil the functionality otherwise we can do that
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.
use id as we try to stick with id for internal queries
celerybeat_schedule.py
Outdated
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.
we register tasks in care/facility/tasks/__init__.py
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.
@sainak so should i delete soft_delete_assets.py file and write the same function in the init.py?
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.
no delete the celerybeat_schecule.py and revert changes in celery_app.py
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.
@sainak ok i will do that
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.
@sainak should i change the settings config also because how will it identify that the task should run every 24 hours. I have gone through the init.py file
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.
I should make changes in the init.py also right and in the soft_delete_assets.py i should mark the function using shared task decorator because it should identify that this task should run every 24 hours right?
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.
I will make the changes by today
@sainak view the changes.. and also replaced external_id with id |
@sainak can you review the changes |
@rithviknishad @sainak lets close this issue today |
@sainak @vigneshhari can you review this PR |
Fixes #1982 issue, enabled soft delete of assets by overriding delete method in facility model