Skip to content

Commit

Permalink
Migration to backfill LMSCourses from grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospri committed Aug 29, 2024
1 parent 54ca00e commit 2adc0e4
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions lms/migrations/versions/f61cb94edfc8_backfill_lms_course.py
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

0 comments on commit 2adc0e4

Please sign in to comment.