Skip to content

Commit

Permalink
Migration to fill LMSCourse.lms_api_course_id from existing grouping …
Browse files Browse the repository at this point in the history
…rows

While this data was in the database as part of Grouping.extra we "promoted"
it to it's own column on LMSCourse to make it easier to query and index.

While we only have this now for canvas now the new column names is a generic one now
  • Loading branch information
marcospri committed Jan 8, 2025
1 parent 9f14e41 commit 1c4cd93
Showing 1 changed file with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Backfill LMSCourse.api_lms_course_id."""

import sqlalchemy as sa
from alembic import op

revision = "9be518500f7d"
down_revision = "cf20e70211f9"


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)
authority_provided_id,
extra->'canvas'->>'custom_canvas_course_id' as api_id
FROM "grouping"
-- Pick only courses, not sections or groups
WHERE grouping.type ='course'
-- Pick only courses with an API ID
AND extra->'canvas'->>'custom_canvas_course_id' IS NOT NULL
-- Pick the most recent "grouping" there are duplicates
ORDER BY authority_provided_id, "grouping".updated desc
)
UPDATE lms_course
SET
lms_api_course_id = backfill.api_id
FROM backfill
WHERE
lms_course.h_authority_provided_id = backfill.authority_provided_id
-- We are already inserting rows in lms_course in the python code, leave those alone
AND lms_course.lms_api_course_id IS NULL
"""
)
)


def downgrade() -> None:
pass

0 comments on commit 1c4cd93

Please sign in to comment.