-
Notifications
You must be signed in to change notification settings - Fork 172
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
feat: skip the history object creation #4071
Conversation
if not self.has_changed: | ||
setattr(self, 'skip_history_when_saving', True) | ||
|
||
# Course runs calculate enterprise subscription inclusion based off of their parent's status, so we need to | ||
# force a recalculation | ||
course_runs = self.course_runs.all() | ||
for course_run in course_runs: | ||
course_run.save() | ||
try: | ||
self.update_data_modified_timestamp() | ||
super().save(*args, **kwargs) | ||
self.enterprise_subscription_inclusion = self._check_enterprise_subscription_inclusion() | ||
kwargs['force_insert'] = False | ||
kwargs['force_update'] = True | ||
super().save(*args, **kwargs) | ||
|
||
# Course runs calculate enterprise subscription inclusion based off of their parent's status, so we need to | ||
# force a recalculation | ||
course_runs = self.course_runs.all() | ||
for course_run in course_runs: | ||
course_run.save() | ||
finally: | ||
if hasattr(self, 'skip_history_when_saving'): | ||
delattr(self, 'skip_history_when_saving') |
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.
Rather than doing this, add a new method that is called in place of super. save. Inside that method, we can decide that if self.has_changed is true, call super.save as it is. Otherwise, set skip history attribute to true. This way, the history logic will be contained in a single place. Also, since the new method is being called inside the overridden save, we wont need any changes across the codebase.
889c701
to
ec4b6b3
Compare
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.
Just a suggestion: can we create a Mixin which will have save() method in it. And we will add
logic of should_history_be_skipped_on_save
in there. By doing this we don't have to change logic in model level.
We just have to inherit that mixin for that model and rest will be handled in overridden save
method.
WDYT?
by and large, it is the same thing that you are suggesting, right now |
But we have to update all |
this change is aimed to target |
obj: Any Model instance | ||
parent_obj: parent object of the instance obj | ||
""" | ||
if not obj.has_changed: |
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.
Aren't we planning to use a field tracker here instead of simple has_changed
method to check for changes in attributes and associated models?
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.
FieldTracker()
is already implemented there! has_changed
is working on top of it.
https://github.com/openedx/course-discovery/blob/master/course_discovery/apps/course_metadata/models.py#L1407
3d05017
to
7216ccf
Compare
# - 2 updates for courserun1 | ||
# - 0 updates for courserun2 | ||
# - 3 updates for courserun3 | ||
# In PROD-3525, history is only created if the instance is changed rather than saving it multiple times |
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.
There is no need to mention an internal ticket in the comments.
assert courserun2_count_final == 1 | ||
assert courserun3_count_final == 1 | ||
assert courserun3_count_final == 3 |
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.
Please ensure that inline comments are updated to reflect the changes.
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.
yes, a comment was added just above the assert statement(s).
""" | ||
Sets the parameter 'skip_history_on_save' if the object is not changed | ||
Args: | ||
mro: method resolution order of the 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.
nit: Why is the first argument named mro? It is evaluated to super(). Wouldn't super() take care of MRO based on the inheritance structure?
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 code is refactored and now it is much more readable!
course = factories.CourseFactory() | ||
course_run = factories.CourseRunFactory() | ||
program = factories.ProgramFactory() |
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.
Missing Seat & CourseEntitlement
32e98c9
to
20bed96
Compare
@@ -122,7 +123,7 @@ class ProductValueAdmin(admin.ModelAdmin): | |||
|
|||
|
|||
@admin.register(Course) | |||
class CourseAdmin(DjangoObjectActions, admin.ModelAdmin): | |||
class CourseAdmin(DjangoObjectActions, SimpleHistoryAdmin): |
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.
Can this increase loading time on admin? There are million of rows on prod, just be careful here.
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 have to add this change because of the difference in working nature of django model admin and simple history admin. Changes in this PR can better be observed via SimpleHistoryAdmin
.
Your concern is valid.After its deployment, i ll monitor it and will take any followup action accordingly.
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.
While I have not tested it out on the local, CI being green gives a confidence boost on the changes here. Love the mixin approach, much cleaner now.
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.
LGTM! thanks for considering the suggestion
This change will add the modifications in which the history object is created only if the model instance is changed not the otherwise. PROD-3525
20bed96
to
c9ff964
Compare
This change will add the modifications in which the history object is created only if the model instance is changed not the otherwise.
PROD-3525