From 1c4cd93a640a8f713d0246fbe94d168b4e2882a9 Mon Sep 17 00:00:00 2001 From: Marcos Prieto Date: Wed, 8 Jan 2025 14:59:00 +0100 Subject: [PATCH] Migration to fill LMSCourse.lms_api_course_id from existing grouping 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 --- ...7d_backfill_lmscourse_api_lms_course_id.py | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lms/migrations/versions/9be518500f7d_backfill_lmscourse_api_lms_course_id.py diff --git a/lms/migrations/versions/9be518500f7d_backfill_lmscourse_api_lms_course_id.py b/lms/migrations/versions/9be518500f7d_backfill_lmscourse_api_lms_course_id.py new file mode 100644 index 0000000000..9382f18e21 --- /dev/null +++ b/lms/migrations/versions/9be518500f7d_backfill_lmscourse_api_lms_course_id.py @@ -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