-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
lms/migrations/versions/50fb51c51314_backfill_lms_course_membership.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Backfill lms_course_membership.""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
revision = "50fb51c51314" | ||
down_revision = "a7e9dc7c4c0f" | ||
|
||
|
||
def upgrade() -> None: | ||
conn = op.get_bind() | ||
conn.execute( | ||
sa.text(""" | ||
WITH backfill as ( | ||
select lms_course.id lms_course_id, lms_user.id as lms_user_id, lti_role_id, min(assignment_membership.created) created, max(assignment_membership.updated) updated | ||
from assignment_membership | ||
join assignment on assignment_id = assignment.id join grouping on grouping.id = course_id | ||
join lms_course on lms_course.h_authority_provided_id = grouping.authority_provided_id | ||
join "user" on "user".id = assignment_membership.user_id | ||
join lms_user on lms_user.h_userid = "user".h_userid | ||
group by lms_course.id, lms_user.id, lti_role_id | ||
) | ||
INSERT INTO lms_course_membership ( | ||
created, | ||
updated, | ||
lms_course_id, | ||
lms_user_id, | ||
lti_role_id | ||
) | ||
SELECT | ||
created, | ||
updated, | ||
lms_course_id, | ||
lms_user_id, | ||
lti_role_id | ||
FROM backfill | ||
-- We are already inserting rows in the membership table in the python code, leave those alone | ||
ON CONFLICT (lms_course_id, lms_user_id, lti_role_id) DO NOTHING | ||
""") | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
pass |