-
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.
Migration to backfill LMSCourses from grouping
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
lms/migrations/versions/f61cb94edfc8_backfill_lms_course.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,56 @@ | ||
"""LMSCourse backfill.""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
revision = "f61cb94edfc8" | ||
down_revision = "2f05ff66ec2e" | ||
|
||
|
||
def upgrade() -> None: | ||
conn = op.get_bind() | ||
conn.execute( | ||
sa.text( | ||
""" | ||
WITH backfill as ( | ||
-- Deduplicate "grouping" courses on authority_provided_id | ||
SELECT DISTINCT ON (authority_provided_id) | ||
"grouping".created, | ||
"grouping".updated, | ||
tool_consumer_instance_guid, | ||
authority_provided_id, | ||
lms_name, | ||
lms_id | ||
FROM "grouping" | ||
-- join on application_instances to get the GUID | ||
JOIN application_instances on application_instances.id = "grouping".application_instance_id | ||
-- Pick only courses, not sections or groups | ||
WHERE grouping.type ='course' | ||
-- Pick the most recent "grouping" there are duplicates | ||
ORDER BY authority_provided_id, "grouping".updated desc | ||
) | ||
INSERT INTO lms_course ( | ||
created, | ||
updated, | ||
tool_consumer_instance_guid, | ||
h_authority_provided_id, | ||
lti_context_id, | ||
name | ||
) | ||
SELECT | ||
created, | ||
updated, | ||
tool_consumer_instance_guid, | ||
authority_provided_id, | ||
lms_id, | ||
lms_name | ||
FROM backfill | ||
-- We are already inserting rows in lms_course in the python code, leave those alone | ||
ON CONFLICT (h_authority_provided_id) DO NOTHING | ||
""" | ||
) | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
pass |