Skip to content

fix: handle duplicates when applying CompontentType constraint#572

Merged
bradenmacdonald merged 1 commit intomainfrom
braden/fix-constraint-migration
Apr 29, 2026
Merged

fix: handle duplicates when applying CompontentType constraint#572
bradenmacdonald merged 1 commit intomainfrom
braden/fix-constraint-migration

Conversation

@bradenmacdonald
Copy link
Copy Markdown
Contributor

Implements #548

This improves content migration 0004 so that installations with invalid duplicates (which exist, see example) will have their data cleaned up automatically.

How to test

You can run this script to test using SQLite with the django test settings:

DJANGO_SETTINGS_MODULE=test_settings python <<'EOF'
import django
django.setup()

from django.core.management import call_command
from django.db.migrations.executor import MigrationExecutor
from django.db import connection
from django.utils import timezone
import uuid

call_command('migrate', 'openedx_content', '0003', verbosity=0)
executor = MigrationExecutor(connection)
state = executor.loader.project_state(("openedx_content", "0003_rename_content_to_media"))
ComponentType = state.apps.get_model('openedx_content', 'ComponentType')
Component = state.apps.get_model('openedx_content', 'Component')
LearningPackage = state.apps.get_model('openedx_content', 'LearningPackage')
PublishableEntity = state.apps.get_model('openedx_content', 'PublishableEntity')

now = timezone.now()
lp = LearningPackage.objects.create(key='lp1', title='Test', created=now, updated=now, uuid=uuid.uuid4())

# Create duplicate ComponentTypes (allowed before migration 0004)
ct1 = ComponentType.objects.create(namespace='xblock.v1', name='problem')
ct2 = ComponentType.objects.create(namespace='xblock.v1', name='problem')
ct3 = ComponentType.objects.create(namespace='xblock.v1', name='problem')
ct_other = ComponentType.objects.create(namespace='xblock.v1', name='video')
ct_html_a = ComponentType.objects.create(namespace='xblock.v1', name='html')
ct_html_b = ComponentType.objects.create(namespace='xblock.v1', name='html')
print(f"Pre-migration ComponentTypes: {sorted(ComponentType.objects.values_list('id', 'namespace', 'name'))}")

# Create some components pointing to duplicates
pe1 = PublishableEntity.objects.create(learning_package=lp, key='c1', uuid=uuid.uuid4(), created=now)
pe2 = PublishableEntity.objects.create(learning_package=lp, key='c2', uuid=uuid.uuid4(), created=now)
pe3 = PublishableEntity.objects.create(learning_package=lp, key='c3', uuid=uuid.uuid4(), created=now)
c1 = Component.objects.create(publishable_entity=pe1, learning_package=lp, component_type=ct2, local_key='one')
c2 = Component.objects.create(publishable_entity=pe2, learning_package=lp, component_type=ct3, local_key='two')
c3 = Component.objects.create(publishable_entity=pe3, learning_package=lp, component_type=ct_other, local_key='three')

call_command('migrate', 'openedx_content', '0004', verbosity=0)

state = executor.loader.project_state(("openedx_content", "0004_componenttype_constraint"))
ComponentType = state.apps.get_model('openedx_content', 'ComponentType')
Component = state.apps.get_model('openedx_content', 'Component')

print(f"Post CTs: {sorted(ComponentType.objects.values_list('id', 'namespace', 'name'))}")
print(f"  c1.ct = {Component.objects.get(pk=c1.pk).component_type_id} (expect {ct1.id})")
print(f"  c2.ct = {Component.objects.get(pk=c2.pk).component_type_id} (expect {ct1.id})")
print(f"  c3.ct = {Component.objects.get(pk=c3.pk).component_type_id} (expect {ct_other.id})")

# Verify constraint is in place
try:
    ComponentType.objects.create(namespace='xblock.v1', name='problem')
    print("❌ should have rejected duplicate")
except Exception as e:
    print(f"✅ constraint rejected duplicate ({type(e).__name__})")
EOF

Co-Authored-By: Claude <noreply@anthropic.com>
@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Apr 29, 2026
@openedx-webhooks
Copy link
Copy Markdown

Thanks for the pull request, @bradenmacdonald!

This repository is currently maintained by @axim-engineering.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@openedx-webhooks openedx-webhooks added the core contributor PR author is a Core Contributor (who may or may not have write access to this repo). label Apr 29, 2026
@github-project-automation github-project-automation Bot moved this to Needs Triage in Contributions Apr 29, 2026
@bradenmacdonald bradenmacdonald requested a review from ormsbee April 29, 2026 17:23
Comment on lines +29 to +31
Component.objects.filter(component_type_id__in=duplicate_ids).update(
component_type_id=keep_id,
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is entirely reasonable, but I also don't think you can actually create new Components in practice if there are duplicates of the ComponentType because it should fail to fetch a unique result. But anyway, this can't hurt.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't think you can actually create new Components in practice if there are duplicates of the ComponentType because it should fail to fetch a unique result

Sorry, I'm not following. Isn't that exactly the bug we're trying to solve, and exactly the problem that was reported on the forum?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was thinking that it was definitely possible to create duplicate ComponentTypes. But when you try to create a Component, I thought it would error out when trying to fetch the type because there are duplicates. But I was forgetting that it's a get_or_create kind of deal, so it would be good for inserting that one Component during the race condition, even if future attempts to create Components of that type would always error out because there are multiple ComponentTypes that match.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in any case, yeah, this code all makes sense. Thank you. 😄

@bradenmacdonald bradenmacdonald merged commit 1fb6630 into main Apr 29, 2026
6 checks passed
@bradenmacdonald bradenmacdonald deleted the braden/fix-constraint-migration branch April 29, 2026 17:59
@github-project-automation github-project-automation Bot moved this from Needs Triage to Done in Contributions Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core contributor PR author is a Core Contributor (who may or may not have write access to this repo). open-source-contribution PR author is not from Axim or 2U

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants