From 97a847d9432b6d59d5e30a2610961ecef69d7e95 Mon Sep 17 00:00:00 2001 From: Muhammad Soban Javed Date: Fri, 30 May 2025 18:57:14 +0500 Subject: [PATCH] fix!: Update the migration to have one Index on student_module_id This updates the migration to have one index on student_module_id. Previously we had one index on stage and production of edx.org but two indexes on other envs. This change is to make sure we have one index on every env. But this change drops the index and create a new index, so if you have set ENABLE_CSMH_EXTENDED to True and have coursewarehistoryextended_studentmodulehistoryextended in a separate db and you want to avoid rebuilding the index, you can fake this migration. --- ...ended_student_module_student_module_idx.py | 26 ++++++++++++++++--- .../coursewarehistoryextended/models.py | 2 +- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lms/djangoapps/coursewarehistoryextended/migrations/0003_rename_studentmodulehistoryextended_student_module_student_module_idx.py b/lms/djangoapps/coursewarehistoryextended/migrations/0003_rename_studentmodulehistoryextended_student_module_student_module_idx.py index c39e2410b5..9a860e9206 100644 --- a/lms/djangoapps/coursewarehistoryextended/migrations/0003_rename_studentmodulehistoryextended_student_module_student_module_idx.py +++ b/lms/djangoapps/coursewarehistoryextended/migrations/0003_rename_studentmodulehistoryextended_student_module_student_module_idx.py @@ -1,6 +1,7 @@ # Generated by Django 4.2.20 on 2025-05-15 03:30 -from django.db import migrations +from django.db import migrations, models +import django.db.models.deletion class Migration(migrations.Migration): @@ -10,9 +11,26 @@ class Migration(migrations.Migration): ] operations = [ - migrations.RenameIndex( + migrations.AlterField( model_name='studentmodulehistoryextended', - new_name='student_module_idx', - old_fields=('student_module',), + name='student_module', + field=models.ForeignKey(db_constraint=False, db_index=False, on_delete=django.db.models.deletion.DO_NOTHING, to='courseware.studentmodule'), + ), + # This SeparateDatabaseAndState operation updates the state to match the model. + # database_operations is left empty because the indexes were already dropped + # by the previous operation in the database. + migrations.SeparateDatabaseAndState( + database_operations=[], + state_operations=[ + migrations.AlterIndexTogether( + name='studentmodulehistoryextended', + index_together=set(), + ), + ], + ), + # Adds back the index on 'student_module' to ensure there is a single index on this field. + migrations.AddIndex( + model_name='studentmodulehistoryextended', + index=models.Index(fields=['student_module'], name='student_module_idx'), ), ] diff --git a/lms/djangoapps/coursewarehistoryextended/models.py b/lms/djangoapps/coursewarehistoryextended/models.py index d9e2d54cca..0ba1c22e06 100644 --- a/lms/djangoapps/coursewarehistoryextended/models.py +++ b/lms/djangoapps/coursewarehistoryextended/models.py @@ -42,7 +42,7 @@ class StudentModuleHistoryExtended(BaseStudentModuleHistory): id = UnsignedBigIntAutoField(primary_key=True) # pylint: disable=invalid-name - student_module = models.ForeignKey(StudentModule, db_index=True, db_constraint=False, on_delete=models.DO_NOTHING) + student_module = models.ForeignKey(StudentModule, db_index=False, db_constraint=False, on_delete=models.DO_NOTHING) @receiver(post_save, sender=StudentModule) def save_history(sender, instance, **kwargs): # pylint: disable=no-self-argument, unused-argument