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

fix: course staff mgmt command #310

Merged
merged 1 commit into from
Aug 28, 2024

Conversation

varshamenon4
Copy link
Member

@varshamenon4 varshamenon4 commented Aug 23, 2024

JIRA: COSMO-234

Description: Attempting to fix the course staff role mgmt command by separating the loops and removing the atomic transaction. I am unable to replicate locally, but running this command in stage resulted in an error where the User couldn't be found.

Copy link

github-actions bot commented Aug 23, 2024

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  edx_exams/apps/core/management/commands
  bulk_add_course_staff.py 75-80
  edx_exams/apps/core/management/commands/test
  test_bulk_add_course_staff.py
Project Total  

This report was generated by python-coverage-comment-action

@varshamenon4 varshamenon4 force-pushed the varshamenon4/fix-course-staff-mgmt-command branch 2 times, most recently from 236ed90 to 241fe94 Compare August 26, 2024 15:26
@varshamenon4 varshamenon4 marked this pull request as ready for review August 26, 2024 15:32
Copy link
Member Author

@varshamenon4 varshamenon4 left a comment

Choose a reason for hiding this comment

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

Ready for review. Had one question about error handling included in comments!

) for row in reader[i:i + batch_size]),
ignore_conflicts=True,
)
users_dict = {k: v for k, v in [(u.username, u) for (u, c) in [User.objects.get_or_create(username=row.get('username'), email=row.get('email')) for row in reader[i:i + batch_size]]]}
Copy link
Member Author

Choose a reason for hiding this comment

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

Does this look good? I can now use this to get users in the bulk create function as this does include the pk. Need to handle IntegrityError (specifically ran into the error when trying to add a user with an existing username, but a different email). Guessing this can be done with a try/catch?

Copy link
Member Author

Choose a reason for hiding this comment

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

My question specifically related to error handling: what do we want to happen in this case? Ideally we'd want to skip the failing entry, right? So maybe the move is to break out the list comprehension (for readability sake) and to handle the error at the get_or_create by having a try/catch?

Copy link
Member Author

@varshamenon4 varshamenon4 left a comment

Choose a reason for hiding this comment

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

Took a pass at the handling of the error. But there still seems to be some corner cases that aren't working as I'd expect (see comment for more details). Not sure if this is relevant or needed for this work though.

for row in reader[i:i + batch_size]:
try:
users_list.append(User.objects.get_or_create(username=row.get('username'), email=row.get('email')))
except IntegrityError:
Copy link
Member Author

Choose a reason for hiding this comment

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

Took a pass at handling IntegrityError here. This handles the case where there's a duplicate username with a different email. This does not handle when there is a new username with a duplicate email address. I don't know if this is an issue, but this is also something that may need to be handled in the Model/db itself so might be more cumbersome than needed.

Copy link
Member

Choose a reason for hiding this comment

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

if this is a case we're expecting can we log something or output information about the rows that failed to create when this is run so the failure isn't silent?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I can do that.

@varshamenon4 varshamenon4 force-pushed the varshamenon4/fix-course-staff-mgmt-command branch from ee2fb5c to dafdc1a Compare August 27, 2024 18:28
@varshamenon4 varshamenon4 merged commit 30201a3 into main Aug 28, 2024
5 checks passed
@varshamenon4 varshamenon4 deleted the varshamenon4/fix-course-staff-mgmt-command branch August 28, 2024 15:57
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.

2 participants