-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow choosing python versions for graders.
Custom migrations were written to safely transfer data, and handle deprecated languages.
- Loading branch information
1 parent
8b15637
commit 0b538be
Showing
17 changed files
with
347 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Generated by Django 4.2.16 on 2024-11-11 01:40 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
def migrate_to_foreignkey(apps, schema_editor): | ||
"""Creates a default :class:`.Language` model for each assignment. | ||
This converts the old language field to a foreign key to the new language model. | ||
""" | ||
|
||
Language = apps.get_model("assignments", "Language") | ||
db_alias = schema_editor.connection.alias | ||
python_310, py_created = Language.objects.using(db_alias).get_or_create( | ||
name="Python 3.10", | ||
executable="/usr/bin/python3.10", | ||
language="P", | ||
version=310, | ||
) | ||
|
||
# Keep backwards compatibility with Java assignments by making them | ||
# use python. Note that we cannot deprecate it yet until we have | ||
# Java support, and figure out how to migrate the media. | ||
java, java_created = Language.objects.using(db_alias).get_or_create( | ||
name="Java/Python 3.10", | ||
executable="/usr/bin/python3.10", | ||
language="J", | ||
version=310, | ||
) | ||
|
||
Assignment = apps.get_model("assignments", "Assignment") | ||
for assignment in Assignment.objects.using(db_alias).all(): | ||
if assignment.language == "P": | ||
assignment.language_details = python_310 | ||
elif assignment.language == "J": | ||
assignment.language_details = java | ||
assignment.save() | ||
|
||
# avoid creating empty models | ||
if py_created and not python_310.assignment_set.exists(): | ||
python_310.delete() | ||
if java_created and not java.assignment_set.exists(): | ||
java.delete() | ||
|
||
|
||
def revert_default_language(apps, schema_editor): | ||
"""Converts the foreign key :class:`.Language` back to the old ``language`` field.""" | ||
|
||
Assignment = apps.get_model("assignments", "Assignment") | ||
Language = apps.get_model("assignments", "Language") | ||
db_alias = schema_editor.connection.alias | ||
java = ( | ||
Language.objects.using(db_alias) | ||
.filter( | ||
language="J", | ||
name="Java/Python 3.10", | ||
) | ||
.first() | ||
) | ||
for assignment in Assignment.objects.using(db_alias).all(): | ||
if java and assignment.language_details == java: | ||
assignment.language = "J" | ||
else: | ||
assignment.language = "P" | ||
assignment.save() | ||
|
||
|
||
# fmt: off | ||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('assignments', '0032_assignment_quiz_description_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Language', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('is_deprecated', models.BooleanField(default=False)), | ||
('language', models.CharField(choices=[('P', 'Python 3'), ('J', 'Java')], max_length=1)), | ||
('name', models.CharField(help_text='The name of the language', max_length=50)), | ||
('executable', models.CharField(help_text='The path to the language executable', max_length=100)), | ||
('version', models.PositiveSmallIntegerField(help_text="The version of the executable.")), | ||
], | ||
options={'ordering': ['-language', '-version']}, | ||
), | ||
migrations.AddField( | ||
model_name="assignment", | ||
name="language_details", | ||
field=models.ForeignKey( | ||
null=True, | ||
on_delete=models.deletion.CASCADE, | ||
related_name="assignment_set", | ||
to="assignments.language", | ||
), | ||
), | ||
migrations.RunPython(migrate_to_foreignkey, revert_default_language), | ||
migrations.AlterField( | ||
model_name="assignment", | ||
name="language_details", | ||
field=models.ForeignKey( | ||
null=False, | ||
on_delete=models.deletion.CASCADE, | ||
related_name="assignment_set", | ||
to="assignments.language", | ||
), | ||
), | ||
migrations.RemoveField( | ||
model_name='assignment', | ||
name='language', | ||
), | ||
|
||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.